Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(74)

Delta Between Two Patch Sets: Src/GoogleApis.DotNet4/Apis/Util/Store/FileDataStore.cs

Issue 13412046: Reimplement OAuth2 library - Step 1 (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Left Patch Set: all tests are running Created 10 years, 6 months ago
Right Patch Set: minor Created 10 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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
11 distributed under the License is distributed on an "AS IS" BASIS, 11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and 13 See the License for the specific language governing permissions and
14 limitations under the License. 14 limitations under the License.
15 */ 15 */
16 16
17 using System; 17 using System;
18 using System.IO; 18 using System.IO;
19 using System.Threading.Tasks; 19 using System.Threading.Tasks;
20 20
21 using Google.Apis.Util.Store; 21 using Google.Apis.Util.Store;
22 using Google.Apis.Json; 22 using Google.Apis.Json;
23 23
24 namespace Google.Apis.Apis.Util.Store 24 namespace Google.Apis.Apis.Util.Store
25 { 25 {
26 /// <summary> 26 /// <summary>
27 /// File data store that implements <see cref="IDataStore"/>. This store cre ates a different file for each· 27 /// File data store that implements <seealso cref="IDataStore"/>. This store creates a different file for each·
peleyal 2013/09/10 01:14:44 <seealso?
peleyal 2013/09/10 12:32:22 Done.
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 going to be created (if it's not 37 /// Constructs a new file data store with the specified folder. This fol der is created (if it doesn't exist
peleyal 2013/09/10 01:14:44 is created
peleyal 2013/09/10 12:32:22 Done.
38 /// exist yet) under <seealso cref="Environment.SpecialFolder.Applicatio nData"/>. 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·
peleyal 2013/09/10 01:14:44 change GetStoredKey to be static public
peleyal 2013/09/10 12:32:22 Done.
52 /// <see cref="FolderPath"/>. 52 /// <see cref="FolderPath"/>.
53 /// </summary> 53 /// </summary>
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>
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.
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)
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 /// <returns></returns>
peleyal 2013/09/10 01:14:44 remove returns statement
peleyal 2013/09/10 12:32:22 Done.
125 public Task Clear() 132 public Task Clear()
126 { 133 {
127 if (Directory.Exists(folderPath)) 134 if (Directory.Exists(folderPath))
128 { 135 {
129 Directory.Delete(folderPath, true); 136 Directory.Delete(folderPath, true);
130 Directory.CreateDirectory(folderPath); 137 Directory.CreateDirectory(folderPath);
131 } 138 }
132 139
133 return TaskEx.Delay(0); 140 return TaskEx.Delay(0);
134 } 141 }
135 142
136 private string GetStoredKey(string key, Type t) 143 /// <summary>Creates a unique stored key based on the key and the class type.</summary>
144 /// <param name="key">The object key</param>
145 /// <param name="t">The type to store or retrieve</param>
146 public static string GetStoredKey(string key, Type t)
137 { 147 {
138 return string.Format("{0}-{1}", t.FullName, key); 148 return string.Format("{0}-{1}", t.FullName, key);
139 } 149 }
140 } 150 }
141 } 151 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b