/*
* 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.IO;
using System.Collections.Concurrent;
namespace QuantConnect.Lean.Engine.DataFeeds
{
///
/// Base downloader implementation with some helper methods
///
public abstract class BaseDownloaderDataProvider : DefaultDataProvider
{
private readonly ConcurrentDictionary _currentDownloads = new ConcurrentDictionary();
///
/// Helper method which guarantees each requested key is downloaded only once concurrently if required based on
///
/// A string representing where the data is stored
/// The download operation we want to perform once concurrently per key
/// A of the data requested
protected Stream DownloadOnce(string key, Action download)
{
// If we don't already have this file or its out of date, download it
if (NeedToDownload(key))
{
lock (key)
{
// only one thread can add a path at the same time
// - The thread that adds the path, downloads the file and removes the path from the collection after it finishes.
// - Threads that don't add the path, will get the value in the collection and try taking a lock on it, since the downloading
// thread takes the lock on it first, they will wait till he finishes.
// This will allow different threads to download different paths at the same time.
if (_currentDownloads.TryAdd(key, key))
{
download(key);
_currentDownloads.TryRemove(key, out _);
return base.Fetch(key);
}
// this is rare
_currentDownloads.TryGetValue(key, out var existingKey);
lock (existingKey ?? new object())
{
return base.Fetch(key);
}
}
}
return base.Fetch(key);
}
///
/// Main filter to determine if this file needs to be downloaded
///
/// File we are looking at
/// True if should download
protected abstract bool NeedToDownload(string filePath);
}
}