Lean  $LEAN_TAG$
ObjectStore.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Text;
21 using System.Xml.Serialization;
22 using Newtonsoft.Json;
24 using QuantConnect.Packets;
25 
27 {
28  /// <summary>
29  /// Helper class for easier access to <see cref="IObjectStore"/> methods
30  /// </summary>
31  public class ObjectStore : IObjectStore
32  {
33  /// <summary>
34  /// Event raised each time there's an error
35  /// </summary>
36  public event EventHandler<ObjectStoreErrorRaisedEventArgs> ErrorRaised
37  {
38  add { _store.ErrorRaised += value; }
39  remove { _store.ErrorRaised -= value; }
40  }
41 
42  private readonly IObjectStore _store;
43 
44  /// <summary>
45  /// Initializes a new instance of the <see cref="ObjectStore"/> class
46  /// </summary>
47  /// <param name="store">The <see cref="IObjectStore"/> instance to wrap</param>
48  public ObjectStore(IObjectStore store)
49  {
50  _store = store;
51  }
52 
53  /// <summary>
54  /// Initializes the object store
55  /// </summary>
56  /// <param name="userId">The user id</param>
57  /// <param name="projectId">The project id</param>
58  /// <param name="userToken">The user token</param>
59  /// <param name="controls">The job controls instance</param>
60  public void Initialize(int userId, int projectId, string userToken, Controls controls)
61  {
62  _store.Initialize(userId, projectId, userToken, controls);
63  }
64 
65  /// <summary>
66  /// Returns the file paths present in the object store. This is specially useful not to load the object store into memory
67  /// </summary>
68  public ICollection<string> Keys => _store.Keys;
69 
70  /// <summary>
71  /// Will clear the object store state cache. This is useful when the object store is used concurrently by nodes which want to share information
72  /// </summary>
73  public void Clear() => _store.Clear();
74 
75  /// <summary>
76  /// Determines whether the store contains data for the specified path
77  /// </summary>
78  /// <param name="path">The object path</param>
79  /// <returns>True if the key was found</returns>
80  public bool ContainsKey(string path)
81  {
82  return _store.ContainsKey(path);
83  }
84 
85  /// <summary>
86  /// Returns the object data for the specified path
87  /// </summary>
88  /// <param name="path">The object path</param>
89  /// <returns>A byte array containing the data</returns>
90  public byte[] ReadBytes(string path)
91  {
92  return _store.ReadBytes(path);
93  }
94 
95  /// <summary>
96  /// Saves the object data for the specified path
97  /// </summary>
98  /// <param name="path">The object path</param>
99  /// <param name="contents">The object data</param>
100  /// <returns>True if the save operation was successful</returns>
101  public bool SaveBytes(string path, byte[] contents)
102  {
103  return _store.SaveBytes(path, contents);
104  }
105 
106  /// <summary>
107  /// Deletes the object data for the specified path
108  /// </summary>
109  /// <param name="path">The object path</param>
110  /// <returns>True if the delete operation was successful</returns>
111  public bool Delete(string path)
112  {
113  return _store.Delete(path);
114  }
115 
116  /// <summary>
117  /// Returns the file path for the specified path
118  /// </summary>
119  /// <param name="path">The object path</param>
120  /// <returns>The path for the file</returns>
121  public string GetFilePath(string path)
122  {
123  return _store.GetFilePath(path);
124  }
125 
126  /// <summary>
127  /// Returns the string object data for the specified path
128  /// </summary>
129  /// <param name="path">The object path</param>
130  /// <param name="encoding">The string encoding used</param>
131  /// <returns>A string containing the data</returns>
132  public string Read(string path, Encoding encoding = null)
133  {
134  encoding = encoding ?? Encoding.UTF8;
135 
136  var data = _store.ReadBytes(path);
137  return data != null ? encoding.GetString(data) : null;
138  }
139 
140  /// <summary>
141  /// Returns the string object data for the specified path
142  /// </summary>
143  /// <param name="path">The object path</param>
144  /// <param name="encoding">The string encoding used</param>
145  /// <returns>A string containing the data</returns>
146  public string ReadString(string path, Encoding encoding = null)
147  {
148  return Read(path, encoding);
149  }
150 
151  /// <summary>
152  /// Returns the JSON deserialized object data for the specified path
153  /// </summary>
154  /// <param name="path">The object path</param>
155  /// <param name="encoding">The string encoding used</param>
156  /// <param name="settings">The settings used by the JSON deserializer</param>
157  /// <returns>An object containing the data</returns>
158  public T ReadJson<T>(string path, Encoding encoding = null, JsonSerializerSettings settings = null)
159  {
160  encoding = encoding ?? Encoding.UTF8;
161 
162  var json = Read(path, encoding);
163  return JsonConvert.DeserializeObject<T>(json, settings);
164  }
165 
166  /// <summary>
167  /// Returns the XML deserialized object data for the specified path
168  /// </summary>
169  /// <param name="path">The object path</param>
170  /// <param name="encoding">The string encoding used</param>
171  /// <returns>An object containing the data</returns>
172  public T ReadXml<T>(string path, Encoding encoding = null)
173  {
174  encoding = encoding ?? Encoding.UTF8;
175 
176  var xml = Read(path, encoding);
177 
178  var serializer = new XmlSerializer(typeof(T));
179  using (var reader = new StringReader(xml))
180  {
181  return (T)serializer.Deserialize(reader);
182  }
183  }
184 
185  /// <summary>
186  /// Saves the data from a local file path associated with the specified path
187  /// </summary>
188  /// <remarks>If the file does not exist it will throw an exception</remarks>
189  /// <param name="path">The object path</param>
190  /// <returns>True if the object was saved successfully</returns>
191  public bool Save(string path)
192  {
193  // Check the file exists
194  var filePath = GetFilePath(path);
195  if (!File.Exists(filePath))
196  {
197  throw new ArgumentException($"There is no file associated with path {path} in '{filePath}'");
198  }
199  var bytes = File.ReadAllBytes(filePath);
200 
201  return _store.SaveBytes(path, bytes);
202  }
203 
204  /// <summary>
205  /// Saves the object data in text format for the specified path
206  /// </summary>
207  /// <param name="path">The object path</param>
208  /// <param name="text">The string object to be saved</param>
209  /// <param name="encoding">The string encoding used, <see cref="Encoding.UTF8"/> by default</param>
210  /// <returns>True if the object was saved successfully</returns>
211  public bool Save(string path, string text, Encoding encoding = null)
212  {
213  encoding ??= Encoding.UTF8;
214  return _store.SaveBytes(path, encoding.GetBytes(text));
215  }
216 
217  /// <summary>
218  /// Saves the object data in text format for the specified path
219  /// </summary>
220  /// <param name="path">The object path</param>
221  /// <param name="text">The string object to be saved</param>
222  /// <param name="encoding">The string encoding used</param>
223  /// <returns>True if the object was saved successfully</returns>
224  public bool SaveString(string path, string text, Encoding encoding = null)
225  {
226  encoding = encoding ?? Encoding.UTF8;
227 
228  return _store.SaveBytes(path, encoding.GetBytes(text));
229  }
230 
231  /// <summary>
232  /// Saves the object data in JSON format for the specified path
233  /// </summary>
234  /// <param name="path">The object path</param>
235  /// <param name="obj">The object to be saved</param>
236  /// <param name="encoding">The string encoding used</param>
237  /// <param name="settings">The settings used by the JSON serializer</param>
238  /// <returns>True if the object was saved successfully</returns>
239  public bool SaveJson<T>(string path, T obj, Encoding encoding = null, JsonSerializerSettings settings = null)
240  {
241  encoding = encoding ?? Encoding.UTF8;
242 
243  var json = JsonConvert.SerializeObject(obj, settings);
244  return SaveString(path, json, encoding);
245  }
246 
247  /// <summary>
248  /// Saves the object data in XML format for the specified path
249  /// </summary>
250  /// <param name="path">The object path</param>
251  /// <param name="obj">The object to be saved</param>
252  /// <param name="encoding">The string encoding used</param>
253  /// <returns>True if the object was saved successfully</returns>
254  public bool SaveXml<T>(string path, T obj, Encoding encoding = null)
255  {
256  encoding = encoding ?? Encoding.UTF8;
257 
258  using (var writer = new StringWriter())
259  {
260  var serializer = new XmlSerializer(typeof(T));
261  serializer.Serialize(writer, obj);
262 
263  var xml = writer.ToString();
264  return SaveString(path, xml, encoding);
265  }
266  }
267 
268  /// <summary>Returns an enumerator that iterates through the collection.</summary>
269  /// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
270  /// <filterpriority>1</filterpriority>
271  public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
272  {
273  return _store.GetEnumerator();
274  }
275 
276  /// <summary>Returns an enumerator that iterates through a collection.</summary>
277  /// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns>
278  /// <filterpriority>2</filterpriority>
279  IEnumerator IEnumerable.GetEnumerator()
280  {
281  return ((IEnumerable) _store).GetEnumerator();
282  }
283 
284  /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
285  /// <filterpriority>2</filterpriority>
286  public void Dispose()
287  {
288  _store.Dispose();
289  }
290  }
291 }