/*
* 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 algorithm name
/// The user id
/// The project id
/// The user token
/// The job controls instance
public void Initialize(string algorithmName, int userId, int projectId, string userToken, Controls controls)
{
_store.Initialize(algorithmName, userId, projectId, userToken, controls);
}
///
/// Determines whether the store contains data for the specified key
///
/// The object key
/// True if the key was found
public bool ContainsKey(string key)
{
return _store.ContainsKey(key);
}
///
/// Returns the object data for the specified key
///
/// The object key
/// A byte array containing the data
public byte[] ReadBytes(string key)
{
return _store.ReadBytes(key);
}
///
/// Saves the object data for the specified key
///
/// The object key
/// The object data
/// True if the save operation was successful
public bool SaveBytes(string key, byte[] contents)
{
return _store.SaveBytes(key, contents);
}
///
/// Deletes the object data for the specified key
///
/// The object key
/// True if the delete operation was successful
public bool Delete(string key)
{
return _store.Delete(key);
}
///
/// Returns the file path for the specified key
///
/// The object key
/// The path for the file
public string GetFilePath(string key)
{
return _store.GetFilePath(key);
}
///
/// Returns the string object data for the specified key
///
/// The object key
/// The string encoding used
/// A string containing the data
public string Read(string key, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
return encoding.GetString(_store.ReadBytes(key));
}
///
/// Returns the string object data for the specified key
///
/// The object key
/// The string encoding used
/// A string containing the data
public string ReadString(string key, Encoding encoding = null)
{
return Read(key, encoding);
}
///
/// Returns the JSON deserialized object data for the specified key
///
/// The object key
/// The string encoding used
/// The settings used by the JSON deserializer
/// An object containing the data
public T ReadJson(string key, Encoding encoding = null, JsonSerializerSettings settings = null)
{
encoding = encoding ?? Encoding.UTF8;
var json = Read(key, encoding);
return JsonConvert.DeserializeObject(json, settings);
}
///
/// Returns the XML deserialized object data for the specified key
///
/// The object key
/// The string encoding used
/// An object containing the data
public T ReadXml(string key, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
var xml = Read(key, encoding);
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(xml))
{
return (T)serializer.Deserialize(reader);
}
}
///
/// Saves the object data in text format for the specified key
///
/// The object key
/// The string object to be saved
/// The string encoding used
/// True if the object was saved successfully
public bool Save(string key, string text, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
return _store.SaveBytes(key, encoding.GetBytes(text));
}
///
/// Saves the object data in text format for the specified key
///
/// The object key
/// The string object to be saved
/// The string encoding used
/// True if the object was saved successfully
public bool SaveString(string key, string text, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
return _store.SaveBytes(key, encoding.GetBytes(text));
}
///
/// Saves the object data in JSON format for the specified key
///
/// The object key
/// 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 key, T obj, Encoding encoding = null, JsonSerializerSettings settings = null)
{
encoding = encoding ?? Encoding.UTF8;
var json = JsonConvert.SerializeObject(obj, settings);
return SaveString(key, json, encoding);
}
///
/// Saves the object data in XML format for the specified key
///
/// The object key
/// The object to be saved
/// The string encoding used
/// True if the object was saved successfully
public bool SaveXml(string key, 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(key, 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();
}
}
}