Belle II Software  release-05-02-19
FileCatalog Class Reference

This class provides an interface to the file (metadata) catalog. More...

#include <FileCatalog.h>

Collaboration diagram for FileCatalog:

Public Member Functions

virtual bool registerFile (const std::string &fileName, FileMetaData &metaData, const std::string &oldLFN="")
 Register a file in the (local) file catalog. More...
 
virtual bool getMetaData (std::string &fileName, FileMetaData &metaData)
 Get the metadata of a file with given (logical) file name. More...
 
virtual std::string getPhysicalFileName (const std::string &lfn)
 Get the physical file name for the LFN. More...
 

Static Public Member Functions

static FileCatalogInstance ()
 Static method to get a reference to the FileCatalog instance. More...
 

Private Types

typedef std::map< std::string, std::pair< std::string, FileMetaData > > FileMap
 Map with file catalog content.
 

Private Member Functions

 FileCatalog ()
 Constructor: locate local database file.
 
bool readCatalog (FileMap &fileMap)
 Read the file catalog from the local file. More...
 
bool writeCatalog (const FileMap &fileMap)
 Write the file catalog to the local file. More...
 

Private Attributes

std::string m_fileName
 Name of the file catalog file.
 

Detailed Description

This class provides an interface to the file (metadata) catalog.

Definition at line 34 of file FileCatalog.h.

Member Function Documentation

◆ getMetaData()

bool getMetaData ( std::string &  fileName,
FileMetaData metaData 
)
virtual

Get the metadata of a file with given (logical) file name.

Parameters
fileNameThe (logical) name of the file. Will be set to the physical file name.
metaDataThe meta data information of the file.
Returns
True if the file was found in the catalog.

Definition at line 144 of file FileCatalog.cc.

145 {
146  metaData = FileMetaData();
147  if (m_fileName.empty()) return false;
148  if (!fs::exists(m_fileName)) return false;
149 
150  // get lock for read access to file catalog
151  FileSystem::Lock lock(m_fileName, true);
152  if (!lock.lock()) {
153  B2ERROR("Locking of file catalog " << m_fileName << " failed.");
154  return false;
155  }
156 
157  // read the file catalog
158  FileMap fileMap;
159  if (!readCatalog(fileMap)) {
160  B2ERROR("Failed to read file catalog " << m_fileName);
161  return false;
162  }
163 
164  // find the entry with given LFN
165  auto iEntry = fileMap.find(fileName);
166  if (iEntry != fileMap.end()) {
167  metaData = iEntry->second.second;
168  if (!iEntry->second.first.empty()) fileName = iEntry->second.first;
169  return true;
170  }
171  for (const auto& entry : fileMap) {
172  if (fileName.compare(entry.second.first) == 0) {
173  metaData = entry.second.second;
174  return true;
175  }
176  }
177 
178  return false;
179 }

◆ getPhysicalFileName()

std::string getPhysicalFileName ( const std::string &  lfn)
virtual

Get the physical file name for the LFN.

Parameters
lfnThe logical file name.
Returns
the physical file name or an empty string if the lfn is not in the catalog.

Definition at line 182 of file FileCatalog.cc.

◆ Instance()

FileCatalog & Instance ( )
static

Static method to get a reference to the FileCatalog instance.

Returns
A reference to an instance of this class.

Definition at line 25 of file FileCatalog.cc.

◆ readCatalog()

bool readCatalog ( FileCatalog::FileMap fileMap)
private

Read the file catalog from the local file.

Parameters
fileMapThe map of file catalog entries.
Returns
True if the catalog was read successfully.

Definition at line 56 of file FileCatalog.cc.

◆ registerFile()

bool registerFile ( const std::string &  fileName,
FileMetaData metaData,
const std::string &  oldLFN = "" 
)
virtual

Register a file in the (local) file catalog.

Parameters
fileNameThe name of the file to be registered.
metaDataThe meta data information of the file to be registered. Will be updated.
oldLFNIf not empty, update the file catalog: only register the file if the oldLFN is found in the catalog and if so, also remove any file with the old LFN from the catalog.
Returns
True if the registration succeeded.

Definition at line 92 of file FileCatalog.cc.

◆ writeCatalog()

bool writeCatalog ( const FileMap fileMap)
private

Write the file catalog to the local file.

Parameters
fileMapThe map of file catalog entries.
Returns
True if the catalog was written successfully.

Definition at line 79 of file FileCatalog.cc.


The documentation for this class was generated from the following files:
Belle2::FileCatalog::m_fileName
std::string m_fileName
Name of the file catalog file.
Definition: FileCatalog.h:99
Belle2::FileMetaData
Metadata information about a file.
Definition: FileMetaData.h:39
Belle2::FileSystem::Lock
Helper class for locking a file.
Definition: FileSystem.h:107
Belle2::FileCatalog::readCatalog
bool readCatalog(FileMap &fileMap)
Read the file catalog from the local file.
Definition: FileCatalog.cc:56
Belle2::FileCatalog::FileMap
std::map< std::string, std::pair< std::string, FileMetaData > > FileMap
Map with file catalog content.
Definition: FileCatalog.h:81