Lean  $LEAN_TAG$
SingleEntryDataCacheProvider.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 
17 using System;
18 using System.IO;
19 using Ionic.Zip;
20 using System.Linq;
21 using QuantConnect.Util;
22 using QuantConnect.Logging;
24 using System.Collections.Generic;
25 
27 {
28  /// <summary>
29  /// Default implementation of the <see cref="IDataCacheProvider"/>
30  /// Does not cache data. If the data is a zip, the first entry is returned
31  /// </summary>
33  {
34  private readonly IDataProvider _dataProvider;
35  private ZipFile _zipFile;
36  private Stream _zipFileStream;
37 
38  /// <summary>
39  /// Property indicating the data is temporary in nature and should not be cached.
40  /// </summary>
41  public bool IsDataEphemeral { get; }
42 
43  /// <summary>
44  /// Constructor that takes the <see cref="IDataProvider"/> to be used to retrieve data
45  /// </summary>
46  public SingleEntryDataCacheProvider(IDataProvider dataProvider, bool isDataEphemeral = true)
47  {
48  _dataProvider = dataProvider;
49  IsDataEphemeral = isDataEphemeral;
50  }
51 
52  /// <summary>
53  /// Fetch data from the cache
54  /// </summary>
55  /// <param name="key">A string representing the key of the cached data</param>
56  /// <returns>An <see cref="Stream"/> of the cached data</returns>
57  public Stream Fetch(string key)
58  {
59  LeanData.ParseKey(key, out var filePath, out var entryName);
60  var stream = _dataProvider.Fetch(filePath);
61 
62  if (filePath.EndsWith(".zip") && stream != null)
63  {
64  // get the first entry from the zip file
65  try
66  {
67  var entryStream = Compression.UnzipStream(stream, out _zipFile, entryName);
68 
69  // save the file stream so it can be disposed later
70  _zipFileStream = stream;
71 
72  return entryStream;
73  }
74  catch (ZipException exception)
75  {
76  Log.Error("SingleEntryDataCacheProvider.Fetch(): Corrupt file: " + key + " Error: " + exception);
77  stream.DisposeSafely();
78  return null;
79  }
80  }
81 
82  return stream;
83  }
84 
85  /// <summary>
86  /// Not implemented
87  /// </summary>
88  /// <param name="key">The source of the data, used as a key to retrieve data in the cache</param>
89  /// <param name="data">The data to cache as a byte array</param>
90  public void Store(string key, byte[] data)
91  {
92  //
93  }
94 
95  /// <summary>
96  /// Returns a list of zip entries in a provided zip file
97  /// </summary>
98  public List<string> GetZipEntries(string zipFile)
99  {
100  var stream = _dataProvider.Fetch(zipFile);
101  if (stream == null)
102  {
103  throw new ArgumentException($"Failed to create source stream {zipFile}");
104  }
105  var entryNames = Compression.GetZipEntryFileNames(stream).ToList();
106  stream.DisposeSafely();
107 
108  return entryNames;
109  }
110 
111  /// <summary>
112  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
113  /// </summary>
114  public void Dispose()
115  {
116  _zipFile?.DisposeSafely();
117  _zipFileStream?.DisposeSafely();
118  }
119  }
120 }