Belle II Software  release-08-01-10
PayloadProvider.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <framework/database/PayloadMetadata.h>
11 #include <framework/database/Downloader.h>
12 #include <framework/utilities/FileSystem.h>
13 
14 #include <vector>
15 #include <string>
16 #include <unordered_map>
17 
18 namespace Belle2::Conditions {
21  public:
22 
24  struct PayloadLocation {
26  std::string base;
28  bool isRemote;
29  };
30 
32  enum class EDirectoryLayout {
35  c_flat,
39  c_hashed,
40  };
41 
64  explicit PayloadProvider(const std::vector<std::string>& locations, const std::string& cachedir = "", int timeout = 60);
65 
73  bool find(PayloadMetadata& meta);
74  private:
77  bool getLocalFile(const PayloadLocation& loc, PayloadMetadata& meta) const;
80  bool getRemoteFile(const PayloadLocation& loc, PayloadMetadata& meta);
85  bool getTemporaryFile(const std::string& url, PayloadMetadata& meta, bool silentOnMissing);
87  std::string getFilename(EDirectoryLayout structure, const PayloadMetadata& payload) const;
91  std::vector<PayloadLocation> m_locations;
97  std::unordered_map<std::string, std::unique_ptr<FileSystem::TemporaryFile>> m_temporaryFiles;
99  int m_timeout;
100  };
101 } // Belle2::Conditions namespace
Simple class to encapsulate libcurl as used by the ConditionsDatabase.
Definition: Downloader.h:21
static Downloader & getDefaultInstance()
Return the default instance.
Definition: Downloader.cc:133
Class to find payload files in a list of locations.
PayloadProvider(const std::vector< std::string > &locations, const std::string &cachedir="", int timeout=60)
Constructor for a given list of locations and optionally the location where downloaded payloads shoul...
bool getTemporaryFile(const std::string &url, PayloadMetadata &meta, bool silentOnMissing)
Try to download url into a temporary file, if successful set the filename member of the metadata and ...
PayloadLocation m_cacheDir
Location of the cache directory where/how we want to store downloaded payloads.
bool getLocalFile(const PayloadLocation &loc, PayloadMetadata &meta) const
Look for a payload in the local directory location, set the filename member of the metadata instance ...
std::vector< PayloadLocation > m_locations
List of configured lookup locations: The first one will always be the cache directory and the last on...
std::unordered_map< std::string, std::unique_ptr< FileSystem::TemporaryFile > > m_temporaryFiles
Map of all active temporary files we downloaded and keep around until they can be closed.
int m_timeout
Timeout to wait for a write look when trying to download payloads.
EDirectoryLayout
Enumeration of different directory layouts.
@ c_flat
Flat directory containing the payloads in the form dbstore_{NAME}_rev_{REVISION}.root
@ c_hashed
Hashed directory structure containing the payloads in the form AB/{NAME}_r{REVISION}....
std::string getFilename(EDirectoryLayout structure, const PayloadMetadata &payload) const
Return the filename of a payload to look for given a directory structure and some metadata.
Downloader & m_downloader
Instance to the database file downloading instance.
bool find(PayloadMetadata &meta)
Try to find a payload, return true on success, false if it cannot be found.
bool getRemoteFile(const PayloadLocation &loc, PayloadMetadata &meta)
Look for a payload on a remote server and download if possible, set the filename member of the metada...
Simple struct to group all information necessary for a single payload.
Simple struct to represent a lookup location.
bool isRemote
Is this a remote location where we want to download the files?