|
| 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.IO; |
| 18 | +using System.Linq; |
| 19 | +using QuantConnect.Configuration; |
| 20 | +using QuantConnect.Interfaces; |
| 21 | +using QuantConnect.Logging; |
| 22 | +using QuantConnect.Packets; |
| 23 | + |
| 24 | +namespace QuantConnect.Storage |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// A local disk implementation of <see cref="IObjectStore"/>. |
| 28 | + /// </summary> |
| 29 | + public class LocalObjectStore : IObjectStore |
| 30 | + { |
| 31 | + private static readonly string StorageRoot = Path.GetFullPath(Config.Get("object-store-root", "./storage")); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// The root storage folder for the algorithm |
| 35 | + /// </summary> |
| 36 | + protected string AlgorithmStorageRoot { get; private set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes the object store |
| 40 | + /// </summary> |
| 41 | + /// <param name="algorithmName">The algorithm name</param> |
| 42 | + /// <param name="userId">The user id</param> |
| 43 | + /// <param name="projectId">The project id</param> |
| 44 | + /// <param name="userToken">The user token</param> |
| 45 | + /// <param name="controls">The job controls instance</param> |
| 46 | + public virtual void Initialize(string algorithmName, int userId, int projectId, string userToken, Controls controls) |
| 47 | + { |
| 48 | + // absolute path including algorithm name |
| 49 | + AlgorithmStorageRoot = Path.Combine(StorageRoot, algorithmName); |
| 50 | + |
| 51 | + // create the root path if it does not exist |
| 52 | + Directory.CreateDirectory(AlgorithmStorageRoot); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Determines whether the store contains data for the specified key |
| 57 | + /// </summary> |
| 58 | + /// <param name="key">The object key</param> |
| 59 | + /// <returns>True if the key was found</returns> |
| 60 | + public bool ContainsKey(string key) |
| 61 | + { |
| 62 | + if (key == null) |
| 63 | + { |
| 64 | + throw new ArgumentNullException(nameof(key)); |
| 65 | + } |
| 66 | + |
| 67 | + try |
| 68 | + { |
| 69 | + var fileName = GetFilePath(key); |
| 70 | + |
| 71 | + return File.Exists(fileName); |
| 72 | + } |
| 73 | + catch (Exception exception) |
| 74 | + { |
| 75 | + Log.Error(exception, $"Error checking file existence, key: [{key}]"); |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Returns the object data for the specified key |
| 82 | + /// </summary> |
| 83 | + /// <param name="key">The object key</param> |
| 84 | + /// <returns>A byte array containing the data</returns> |
| 85 | + public byte[] Read(string key) |
| 86 | + { |
| 87 | + if (key == null) |
| 88 | + { |
| 89 | + throw new ArgumentNullException(nameof(key)); |
| 90 | + } |
| 91 | + |
| 92 | + try |
| 93 | + { |
| 94 | + var fileName = GetFilePath(key); |
| 95 | + |
| 96 | + return File.ReadAllBytes(fileName); |
| 97 | + } |
| 98 | + catch (Exception exception) |
| 99 | + { |
| 100 | + Log.Error(exception, $"Error reading file, key: [{key}]"); |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Saves the object data for the specified key |
| 107 | + /// </summary> |
| 108 | + /// <param name="key">The object key</param> |
| 109 | + /// <param name="contents">The object data</param> |
| 110 | + /// <returns>True if the save operation was successful</returns> |
| 111 | + public bool Save(string key, byte[] contents) |
| 112 | + { |
| 113 | + if (key == null) |
| 114 | + { |
| 115 | + throw new ArgumentNullException(nameof(key)); |
| 116 | + } |
| 117 | + |
| 118 | + try |
| 119 | + { |
| 120 | + var fileName = GetFilePath(key); |
| 121 | + |
| 122 | + File.WriteAllBytes(fileName, contents); |
| 123 | + } |
| 124 | + catch (Exception exception) |
| 125 | + { |
| 126 | + Log.Error(exception, $"Error saving file, key: [{key}]"); |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Deletes the object data for the specified key |
| 135 | + /// </summary> |
| 136 | + /// <param name="key">The object key</param> |
| 137 | + /// <returns>True if the delete operation was successful</returns> |
| 138 | + public bool Delete(string key) |
| 139 | + { |
| 140 | + if (key == null) |
| 141 | + { |
| 142 | + throw new ArgumentNullException(nameof(key)); |
| 143 | + } |
| 144 | + |
| 145 | + try |
| 146 | + { |
| 147 | + var fileName = GetFilePath(key); |
| 148 | + |
| 149 | + File.Delete(fileName); |
| 150 | + } |
| 151 | + catch (Exception exception) |
| 152 | + { |
| 153 | + Log.Error(exception, $"Error deleting file, key: [{key}]"); |
| 154 | + return false; |
| 155 | + } |
| 156 | + |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Returns the file path for the specified key |
| 162 | + /// </summary> |
| 163 | + /// <param name="key">The object key</param> |
| 164 | + /// <returns>The path for the file</returns> |
| 165 | + public string GetFilePath(string key) |
| 166 | + { |
| 167 | + if (key == null) |
| 168 | + { |
| 169 | + throw new ArgumentNullException(nameof(key)); |
| 170 | + } |
| 171 | + |
| 172 | + return Path.Combine(AlgorithmStorageRoot, $"{key.ToMD5()}.dat"); |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 177 | + /// </summary> |
| 178 | + public virtual void Dispose() |
| 179 | + { |
| 180 | + try |
| 181 | + { |
| 182 | + // if the object store was not used, delete the empty storage directory created in Initialize |
| 183 | + if (!Directory.EnumerateFileSystemEntries(AlgorithmStorageRoot).Any()) |
| 184 | + { |
| 185 | + Directory.Delete(AlgorithmStorageRoot); |
| 186 | + } |
| 187 | + } |
| 188 | + catch (Exception exception) |
| 189 | + { |
| 190 | + Log.Error(exception, "Error deleting storage directory."); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments