/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml.Serialization; using Newtonsoft.Json; using QuantConnect.Interfaces; using QuantConnect.Packets; namespace QuantConnect.Storage { /// /// Helper class for easier access to methods /// public class ObjectStore : IObjectStore { /// /// Event raised each time there's an error /// public event EventHandler ErrorRaised { add { _store.ErrorRaised += value; } remove { _store.ErrorRaised -= value; } } private readonly IObjectStore _store; /// /// Initializes a new instance of the class /// /// The instance to wrap public ObjectStore(IObjectStore store) { _store = store; } /// /// Initializes the object store /// /// The user id /// The project id /// The user token /// The job controls instance public void Initialize(int userId, int projectId, string userToken, Controls controls) { _store.Initialize(userId, projectId, userToken, controls); } /// /// Returns the file paths present in the object store. This is specially useful not to load the object store into memory /// public ICollection Keys => _store.Keys; /// /// Will clear the object store state cache. This is useful when the object store is used concurrently by nodes which want to share information /// public void Clear() => _store.Clear(); /// /// Determines whether the store contains data for the specified path /// /// The object path /// True if the key was found public bool ContainsKey(string path) { return _store.ContainsKey(path); } /// /// Returns the object data for the specified path /// /// The object path /// A byte array containing the data public byte[] ReadBytes(string path) { return _store.ReadBytes(path); } /// /// Saves the object data for the specified path /// /// The object path /// The object data /// True if the save operation was successful public bool SaveBytes(string path, byte[] contents) { return _store.SaveBytes(path, contents); } /// /// Deletes the object data for the specified path /// /// The object path /// True if the delete operation was successful public bool Delete(string path) { return _store.Delete(path); } /// /// Returns the file path for the specified path /// /// The object path /// The path for the file public string GetFilePath(string path) { return _store.GetFilePath(path); } /// /// Returns the string object data for the specified path /// /// The object path /// The string encoding used /// A string containing the data public string Read(string path, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; var data = _store.ReadBytes(path); return data != null ? encoding.GetString(data) : null; } /// /// Returns the string object data for the specified path /// /// The object path /// The string encoding used /// A string containing the data public string ReadString(string path, Encoding encoding = null) { return Read(path, encoding); } /// /// Returns the JSON deserialized object data for the specified path /// /// The object path /// The string encoding used /// The settings used by the JSON deserializer /// An object containing the data public T ReadJson(string path, Encoding encoding = null, JsonSerializerSettings settings = null) { encoding = encoding ?? Encoding.UTF8; var json = Read(path, encoding); return JsonConvert.DeserializeObject(json, settings); } /// /// Returns the XML deserialized object data for the specified path /// /// The object path /// The string encoding used /// An object containing the data public T ReadXml(string path, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; var xml = Read(path, encoding); var serializer = new XmlSerializer(typeof(T)); using (var reader = new StringReader(xml)) { return (T)serializer.Deserialize(reader); } } /// /// Saves the data from a local file path associated with the specified path /// /// If the file does not exist it will throw an exception /// The object path /// True if the object was saved successfully public bool Save(string path) { // Check the file exists var filePath = GetFilePath(path); if (!File.Exists(filePath)) { throw new ArgumentException($"There is no file associated with path {path} in '{filePath}'"); } var bytes = File.ReadAllBytes(filePath); return _store.SaveBytes(path, bytes); } /// /// Saves the object data in text format for the specified path /// /// The object path /// The string object to be saved /// The string encoding used, by default /// True if the object was saved successfully public bool Save(string path, string text, Encoding encoding = null) { encoding ??= Encoding.UTF8; return _store.SaveBytes(path, encoding.GetBytes(text)); } /// /// Saves the object data in text format for the specified path /// /// The object path /// The string object to be saved /// The string encoding used /// True if the object was saved successfully public bool SaveString(string path, string text, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; return _store.SaveBytes(path, encoding.GetBytes(text)); } /// /// Saves the object data in JSON format for the specified path /// /// The object path /// The object to be saved /// The string encoding used /// The settings used by the JSON serializer /// True if the object was saved successfully public bool SaveJson(string path, T obj, Encoding encoding = null, JsonSerializerSettings settings = null) { encoding = encoding ?? Encoding.UTF8; var json = JsonConvert.SerializeObject(obj, settings); return SaveString(path, json, encoding); } /// /// Saves the object data in XML format for the specified path /// /// The object path /// The object to be saved /// The string encoding used /// True if the object was saved successfully public bool SaveXml(string path, T obj, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; using (var writer = new StringWriter()) { var serializer = new XmlSerializer(typeof(T)); serializer.Serialize(writer, obj); var xml = writer.ToString(); return SaveString(path, xml, encoding); } } /// Returns an enumerator that iterates through the collection. /// A that can be used to iterate through the collection. /// 1 public IEnumerator> GetEnumerator() { return _store.GetEnumerator(); } /// Returns an enumerator that iterates through a collection. /// An object that can be used to iterate through the collection. /// 2 IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable) _store).GetEnumerator(); } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// 2 public void Dispose() { _store.Dispose(); } } }