Belle II Software  release-05-01-25
PayloadProvider.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/database/PayloadMetadata.h>
13 #include <framework/database/Downloader.h>
14 #include <framework/utilities/FileSystem.h>
15 
16 #include <vector>
17 #include <string>
18 #include <unordered_map>
19 
20 namespace Belle2::Conditions {
22  class PayloadProvider {
23  public:
24 
26  struct PayloadLocation {
28  std::string base;
30  bool isRemote;
31  };
32 
34  enum class EDirectoryLayout {
37  c_flat,
41  c_hashed,
42  };
43 
66  explicit PayloadProvider(const std::vector<std::string>& locations, const std::string& cachedir = "", int timeout = 60);
67 
75  bool find(PayloadMetadata& meta);
76  private:
79  bool getLocalFile(const PayloadLocation& loc, PayloadMetadata& meta) const;
82  bool getRemoteFile(const PayloadLocation& loc, PayloadMetadata& meta);
87  bool getTemporaryFile(const std::string& url, PayloadMetadata& meta, bool silentOnMissing);
89  std::string getFilename(EDirectoryLayout structure, const PayloadMetadata& payload) const;
93  std::vector<PayloadLocation> m_locations;
99  std::unordered_map<std::string, std::unique_ptr<FileSystem::TemporaryFile>> m_temporaryFiles;
101  int m_timeout;
102  };
103 } // Belle2::Conditions namespace
Belle2::Conditions::PayloadProvider::PayloadProvider
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...
Definition: PayloadProvider.cc:31
Belle2::Conditions::PayloadProvider::EDirectoryLayout
EDirectoryLayout
Enumeration of different directory layouts.
Definition: PayloadProvider.h:50
Belle2::Conditions::PayloadProvider::m_downloader
Downloader & m_downloader
Instance to the database file downloading instance.
Definition: PayloadProvider.h:113
Belle2::Conditions::PayloadProvider::getRemoteFile
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...
Definition: PayloadProvider.cc:103
Belle2::Conditions::PayloadProvider::PayloadLocation
Simple struct to represent a lookup location.
Definition: PayloadProvider.h:42
Belle2::Conditions::PayloadProvider::getLocalFile
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 ...
Definition: PayloadProvider.cc:82
Belle2::Conditions::Downloader::getDefaultInstance
static Downloader & getDefaultInstance()
Return the default instance.
Definition: Downloader.cc:143
Belle2::Conditions::PayloadProvider::m_timeout
int m_timeout
Timeout to wait for a write look when trying to download payloads.
Definition: PayloadProvider.h:117
Belle2::Conditions::PayloadProvider::getTemporaryFile
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 ...
Definition: PayloadProvider.cc:194
Belle2::Conditions::PayloadProvider::PayloadLocation::base
std::string base
base path or uri
Definition: PayloadProvider.h:52
Belle2::Conditions::Downloader
Simple class to encapsulate libcurl as used by the ConditionsDatabase.
Definition: Downloader.h:31
Belle2::Conditions::PayloadProvider::find
bool find(PayloadMetadata &meta)
Try to find a payload, return true on success, false if it cannot be found.
Definition: PayloadProvider.cc:74
Belle2::Conditions::PayloadMetadata
Simple struct to group all information necessary for a single payload.
Definition: PayloadMetadata.h:25
Belle2::Conditions::PayloadProvider::m_locations
std::vector< PayloadLocation > m_locations
List of configured lookup locations: The first one will always be the cache directory and the last on...
Definition: PayloadProvider.h:109
Belle2::Conditions::PayloadProvider::EDirectoryLayout::c_flat
@ c_flat
Flat directory containing the payloads in the form dbstore_{NAME}_rev_{REVISION}.root
Belle2::Conditions::PayloadProvider::m_temporaryFiles
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.
Definition: PayloadProvider.h:115
Belle2::Conditions::PayloadProvider::getFilename
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.
Definition: PayloadProvider.cc:178
Belle2::Conditions::PayloadProvider::m_cacheDir
PayloadLocation m_cacheDir
Location of the cache directory where/how we want to store downloaded payloads.
Definition: PayloadProvider.h:111
Belle2::Conditions::PayloadProvider::EDirectoryLayout::c_hashed
@ c_hashed
Hashed directory structure containing the payloads in the form AB/{NAME}_r{REVISION}....
Belle2::Conditions::PayloadProvider::PayloadLocation::isRemote
bool isRemote
Is this a remote location where we want to download the files?
Definition: PayloadProvider.h:54