Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 Copyright 2011 Google Inc | 2 Copyright 2011 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 16 matching lines...) Expand all Loading... | |
27 /// File data store that implements <seealso cref="IDataStore"/>. This store creates a different file for each· | 27 /// File data store that implements <seealso cref="IDataStore"/>. This store creates a different file for each· |
28 /// combination of type and key. | 28 /// combination of type and key. |
29 /// </summary> | 29 /// </summary> |
30 public class FileDataStore : IDataStore | 30 public class FileDataStore : IDataStore |
31 { | 31 { |
32 readonly string folderPath; | 32 readonly string folderPath; |
33 /// <summary>Gets the full folder path.</summary> | 33 /// <summary>Gets the full folder path.</summary> |
34 public string FolderPath { get { return folderPath; } } | 34 public string FolderPath { get { return folderPath; } } |
35 | 35 |
36 /// <summary> | 36 /// <summary> |
37 /// Constructs a new file data store with the specified folder. This fol der is created (if it's not exist yet)· | 37 /// Constructs a new file data store with the specified folder. This fol der is created (if it doesn't exist· |
class
2013/09/16 23:47:29
if it's not exist yet - rephrase as "if it doesn't
peleyal
2013/09/17 15:53:40
Done.
| |
38 /// under <seealso cref="Environment.SpecialFolder.ApplicationData"/>. | 38 /// yet) under <seealso cref="Environment.SpecialFolder.ApplicationData" />. |
39 /// </summary> | 39 /// </summary> |
40 /// <param name="folder">Folder name</param> | 40 /// <param name="folder">Folder name</param> |
41 public FileDataStore(string folder) | 41 public FileDataStore(string folder) |
42 { | 42 { |
43 folderPath = Path.Combine(Environment.GetFolderPath(Environment.Spec ialFolder.ApplicationData), folder); | 43 folderPath = Path.Combine(Environment.GetFolderPath(Environment.Spec ialFolder.ApplicationData), folder); |
44 if (!Directory.Exists(folderPath)) | 44 if (!Directory.Exists(folderPath)) |
45 { | 45 { |
46 Directory.CreateDirectory(folderPath); | 46 Directory.CreateDirectory(folderPath); |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 /// <summary> | 50 /// <summary> |
51 /// Stores the given value for the given key. It creates a new file (nam ed <see cref="GetStoredKey"/>) in· | 51 /// Stores the given value for the given key. It creates a new file (nam ed <see cref="GetStoredKey"/>) in· |
52 /// <see cref="FolderPath"/>. | 52 /// <see cref="FolderPath"/>. |
53 /// </summary> | 53 /// </summary> |
class
2013/09/16 23:47:29
Please add parameter tags here.
peleyal
2013/09/17 15:53:40
Done. I'm not sure that I really added important i
| |
54 /// <typeparam name="T">The type to store in the data store</typeparam> | |
55 /// <param name="key">The key</param> | |
56 /// <param name="value">The value to store in the data store</param> | |
54 public Task Store<T>(string key, T value) | 57 public Task Store<T>(string key, T value) |
55 { | 58 { |
56 if (string.IsNullOrEmpty(key)) | 59 if (string.IsNullOrEmpty(key)) |
57 { | 60 { |
58 throw new ArgumentException("Key MUST have a value"); | 61 throw new ArgumentException("Key MUST have a value"); |
59 } | 62 } |
60 | 63 |
61 var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value); | 64 var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value); |
62 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)) ); | 65 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)) ); |
63 File.WriteAllText(filePath, serialized); | 66 File.WriteAllText(filePath, serialized); |
64 return TaskEx.Delay(0); | 67 return TaskEx.Delay(0); |
65 } | 68 } |
66 | 69 |
67 /// <summary> | 70 /// <summary> |
68 /// Deletes the given key. It deletes the <see cref="GetStoredKey"/> nam ed file in <see cref="FolderPath"/>. | 71 /// Deletes the given key. It deletes the <see cref="GetStoredKey"/> nam ed file in <see cref="FolderPath"/>. |
69 /// </summary> | 72 /// </summary> |
class
2013/09/16 23:47:29
Please add parameters, these are probably the same
peleyal
2013/09/17 15:53:40
Done.
| |
73 /// <param name="key">The key to delete from the data store</param> | |
74 /// <param name="t">The type of the stored value</param> | |
70 public Task Delete(string key, Type t) | 75 public Task Delete(string key, Type t) |
71 { | 76 { |
72 if (string.IsNullOrEmpty(key)) | 77 if (string.IsNullOrEmpty(key)) |
73 { | 78 { |
74 throw new ArgumentException("Key MUST have a value"); | 79 throw new ArgumentException("Key MUST have a value"); |
75 } | 80 } |
76 if (t == null) | 81 if (t == null) |
77 { | 82 { |
78 throw new ArgumentException("Type can't be null"); | 83 throw new ArgumentException("Type can't be null"); |
79 } | 84 } |
80 | 85 |
81 var filePath = Path.Combine(folderPath, GetStoredKey(key, t)); | 86 var filePath = Path.Combine(folderPath, GetStoredKey(key, t)); |
82 if (File.Exists(filePath)) | 87 if (File.Exists(filePath)) |
83 { | 88 { |
84 File.Delete(filePath); | 89 File.Delete(filePath); |
85 } | 90 } |
86 return TaskEx.Delay(0); | 91 return TaskEx.Delay(0); |
87 } | 92 } |
88 | 93 |
89 /// <summary> | 94 /// <summary> |
90 /// Returns the stored value for the given key or <c>null</c> if the ma tching file (<see cref="GetStoredKey"/> | 95 /// Returns the stored value for the given key or <c>null</c> if the mat ching file (<see cref="GetStoredKey"/> |
91 /// in <see cref="FolderPath"/> doesn't exists. | 96 /// in <see cref="FolderPath"/> doesn't exist. |
class
2013/09/16 23:47:29
Singular / plural on the summary:
in <see cref=
peleyal
2013/09/17 15:53:40
Done.
| |
92 /// </summary> | 97 /// </summary> |
98 /// <typeparam name="T">The type to retrieve</typeparam> | |
99 /// <param name="key">The key to retrieve from the data store</param> | |
100 /// <returns>The stored object</returns> | |
93 public Task<T> Get<T>(string key) | 101 public Task<T> Get<T>(string key) |
class
2013/09/16 23:47:29
key needs a <param> tag
peleyal
2013/09/17 15:53:40
Done.
| |
94 { | 102 { |
95 if (string.IsNullOrEmpty(key)) | 103 if (string.IsNullOrEmpty(key)) |
96 { | 104 { |
97 throw new ArgumentException("Key MUST have a value"); | 105 throw new ArgumentException("Key MUST have a value"); |
98 } | 106 } |
99 | 107 |
100 TaskCompletionSource<T> tcs = new TaskCompletionSource<T>(); | 108 TaskCompletionSource<T> tcs = new TaskCompletionSource<T>(); |
101 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)) ); | 109 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T)) ); |
102 if (File.Exists(filePath)) | 110 if (File.Exists(filePath)) |
103 { | 111 { |
104 try | 112 try |
105 { | 113 { |
106 var obj = File.ReadAllText(filePath); | 114 var obj = File.ReadAllText(filePath); |
107 tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize< T>(obj)); | 115 tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize< T>(obj)); |
108 } | 116 } |
109 catch (Exception ex) | 117 catch (Exception ex) |
110 { | 118 { |
111 tcs.SetException(ex); | 119 tcs.SetException(ex); |
112 } | 120 } |
113 } | 121 } |
114 else | 122 else |
115 { | 123 { |
116 tcs.SetResult(default(T)); | 124 tcs.SetResult(default(T)); |
117 } | 125 } |
118 return tcs.Task; | 126 return tcs.Task; |
119 } | 127 } |
120 | 128 |
121 /// <summary> | 129 /// <summary> |
122 /// Clear all values in the data store. This method deletes all files in <see cref="FolderPath"/>. | 130 /// Clears all values in the data store. This method deletes all files i n <see cref="FolderPath"/>. |
123 /// </summary> | 131 /// </summary> |
124 public Task Clear() | 132 public Task Clear() |
125 { | 133 { |
126 if (Directory.Exists(folderPath)) | 134 if (Directory.Exists(folderPath)) |
127 { | 135 { |
128 Directory.Delete(folderPath, true); | 136 Directory.Delete(folderPath, true); |
129 Directory.CreateDirectory(folderPath); | 137 Directory.CreateDirectory(folderPath); |
130 } | 138 } |
131 | 139 |
132 return TaskEx.Delay(0); | 140 return TaskEx.Delay(0); |
133 } | 141 } |
134 | 142 |
135 /// <summary>Creates a unique stored key based on the key and the class type.</summary> | 143 /// <summary>Creates a unique stored key based on the key and the class type.</summary> |
136 /// <param name="key">The object key</param> | 144 /// <param name="key">The object key</param> |
137 /// <param name="t">The type to store or retrieve</param> | 145 /// <param name="t">The type to store or retrieve</param> |
class
2013/09/16 23:47:29
What is the "type" here, is there an enum somewher
peleyal
2013/09/17 15:53:40
Type is the type of the class (http://msdn.microso
| |
138 public static string GetStoredKey(string key, Type t) | 146 public static string GetStoredKey(string key, Type t) |
139 { | 147 { |
140 return string.Format("{0}-{1}", t.FullName, key); | 148 return string.Format("{0}-{1}", t.FullName, key); |
141 } | 149 } |
142 } | 150 } |
143 } | 151 } |
LEFT | RIGHT |