Lean  $LEAN_TAG$
LocalDiskMapFileProvider.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.IO;
18 using System.Threading;
19 using QuantConnect.Logging;
21 using System.Collections.Concurrent;
22 
24 {
25  /// <summary>
26  /// Provides a default implementation of <see cref="IMapFileProvider"/> that reads from
27  /// the local disk
28  /// </summary>
30  {
31  private static int _wroteTraceStatement;
32  private readonly ConcurrentDictionary<AuxiliaryDataKey, MapFileResolver> _cache;
33  private IDataProvider _dataProvider;
34 
35  /// <summary>
36  /// Creates a new instance of the <see cref="LocalDiskFactorFileProvider"/>
37  /// </summary>
39  {
40  _cache = new ConcurrentDictionary<AuxiliaryDataKey, MapFileResolver>();
41  }
42 
43  /// <summary>
44  /// Initializes our MapFileProvider by supplying our dataProvider
45  /// </summary>
46  /// <param name="dataProvider">DataProvider to use</param>
47  public void Initialize(IDataProvider dataProvider)
48  {
49  _dataProvider = dataProvider;
50  }
51 
52  /// <summary>
53  /// Gets a <see cref="MapFileResolver"/> representing all the map
54  /// files for the specified market
55  /// </summary>
56  /// <param name="auxiliaryDataKey">Key used to fetch a map file resolver. Specifying market and security type</param>
57  /// <returns>A <see cref="MapFileRow"/> containing all map files for the specified market</returns>
58  public MapFileResolver Get(AuxiliaryDataKey auxiliaryDataKey)
59  {
60  return _cache.GetOrAdd(auxiliaryDataKey, GetMapFileResolver);
61  }
62 
63  private MapFileResolver GetMapFileResolver(AuxiliaryDataKey key)
64  {
65  var securityType = key.SecurityType;
66  var market = key.Market;
67 
68  var mapFileDirectory = Globals.GetDataFolderPath(MapFile.GetRelativeMapFilePath(market, securityType));
69  if (!Directory.Exists(mapFileDirectory))
70  {
71  // only write this message once per application instance
72  if (Interlocked.CompareExchange(ref _wroteTraceStatement, 1, 0) == 0)
73  {
74  Log.Error($"LocalDiskMapFileProvider.GetMapFileResolver({market}): " +
75  $"The specified directory does not exist: {mapFileDirectory}"
76  );
77  }
78  return MapFileResolver.Empty;
79  }
80  return new MapFileResolver(MapFile.GetMapFiles(mapFileDirectory, market, securityType, _dataProvider));
81  }
82  }
83 }