BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
FileCatalogue.h
Go to the documentation of this file.
1/** Interface to the File Catalog (through REST API service) to add, update, delete and search for file metadata
2 *
3 * @author I.S.Zhironkin // based on K.Gertsenberger template
4 * @version 1
5 * @since 09.01.24
6 *
7 */
8#ifndef FILECATALOG_H
9#define FILECATALOG_H
10
11// #include "TObject.h"
12
13#include <chrono>
14#include <cstdlib>
15#include <curl/curl.h>
16#include <iostream>
17#include <memory>
18#include <random>
19#include <set>
20#include <string>
21#include <vector>
22using namespace std;
23
24/// Class presenting File Info (Metadata) Structure
26{
27 public:
28 static const int Version = 1; ///< structure (API) version
29
30 int PeriodNumber = -1; ///< number of experiment session, when the events was collected
31 int RunNumber = -1; ///< number of experiment run, when the events was collected
32 int RunType = -1; ///< run type (1 - physics run, 2 - calibration run, 3 - test run)
33
35 "1970-01-01 00-00-00"; ///< start datetime of the stored event set (YYYY-MM-DD HH-MM-SS format)
36 string EndDateTime = "1970-01-01 00-00-00"; ///< end datetime of the stored event set (YYYY-MM-DD HH-MM-SS format)
37 string BeamParticle = ""; ///< beam particle name
38 string TargetParticle = ""; ///< target particle name
39
40 float Energy = 0.0; ///< beam energy
41 float FieldVoltage = 0.0; ///< field voltage
42
43 int StartEvent = -1; ///< event number from which stored event set begins
44 int EndEvent = -1; ///< event number with which event set ends
45 int EventCount = -1; ///< event count stored in the file
46
47 float FileSize = -1; ///< file size in bytes
48
49 /// Print all file metadata to the standard output
50 void Print() const
51 {
52 cout << "FileInfo (version " << Version << "):" << endl;
53 cout << " Period:Run = " << PeriodNumber << ":" << RunNumber << endl;
54 cout << " Run Type = " << RunType << endl;
55 cout << " Time = " << StartDateTime << " - " << EndDateTime << endl;
56 cout << " Beam = " << BeamParticle << endl;
57 cout << " Energy = " << Energy << " GeV" << endl;
58 cout << " Target = " << TargetParticle << endl;
59 cout << " MagField = " << FieldVoltage << " mV" << endl;
60 cout << " Event Range = " << StartEvent << " - " << EndEvent << endl;
61 cout << " Event Count = " << EventCount << endl;
62 cout << " File Size = " << FileSize << endl;
63 }
64};
65
66/// Class for describing a condition to search for necessary file metadata
68{
69 public:
70 /// enumeration for attributes, which can be used in searching file metadata
88 /// enumeration for conditions, which can be used for matching with a specified value
97
98 /*!
99 * Create condition for searching file metadata
100 * \param[in] attribute Used for searching file metadata
101 * \param[in] condition Defining the operation of comparison with the value
102 */
103 FileCondition(searchAttributes attribute, matchCriteria condition);
104 FileCondition(searchAttributes attribute, matchCriteria condition, int value);
105 FileCondition(searchAttributes attribute, matchCriteria condition, unsigned int value);
106 FileCondition(searchAttributes attribute, matchCriteria condition, float value);
107 FileCondition(searchAttributes attribute, matchCriteria condition, string value);
108 virtual ~FileCondition() {}
109
110 searchAttributes getFileAttribute();
111 matchCriteria getMatchCriteria();
112 string getValueAsString();
113
114 private:
116
117 // 1 - int, 2 - unsigned int, 3 - float, 4 - TString
120 unsigned int uiValue;
121 float fValue;
122 string strValue;
123
126};
127
128/// Main Interface Class to work with the File Catalog of the experiment
129/// Throws runtime_exception if a critical error occurrs while the constructor
130/// Error Codes: -1 - sending command via CURL failed, -401 - unauthorized request, -403 - token invalid or expired,
131/// -404 - data not found, -409 - incorrect database request (e.g. violating constraints), -422 - incorrect input
132/// request, -500 - internal server error, -503 - service unavaialble, -504 - gateway timeout
133
135{
136 public:
138 virtual ~FileCatalogue();
139
140 /*!
141 * Function for searching for file metadata in the File Catalog using specified conditions
142 * \param[in] file_conditions A set of conditions to specify criteria for file metadata search
143 * \param[out] filepaths Vector of the filepaths to files containing selected file metadata (empty if not found or
144 * some errors occured
145 * \return 0 - if successful, error_code (<0) in case of failures
146 */
147 int GetFileList(vector<unique_ptr<FileCondition>> file_conditions, vector<string>& filepaths);
148
149 /*!
150 * Function to get metadata of the file stored at the given location
151 * \param[in] LFN Logical File Name of the file
152 * \param[out] file_info FileInfo object containing file metadata
153 * \return 0 - if successful, error_code (<0) in case of failures
154 */
155 int GetFileInfo(string LFN, FileInfo& file_info);
156
157 /*!
158 * Function to get metadata of the directory at the given path
159 * \param[in] path Path to the directory in the File Catalog
160 * \param[out] directory_info String containing metadata fields and it's values
161 * \return 0 - if successful, error_code (<0) in case of failures
162 */
163 int GetDirectoryInfo(string path, string& directory_info);
164
165 /*!
166 * Function to get status of the last successful consistency check. Date and ID.
167 * \return ID (>=0) or error_code (<0) in case of failures
168 */
170
171 /*!
172 * Function to get result of the last successful consistency check. Presumably up to hundreds megabytes of data
173 * \param[out] eos_missing_count number of files that exist in File Catalog but missing on EOS
174 * \param[out] fc_missing_count number of files that exist in EOS but missing from File Catalog
175 * \param[out] eos_missing paths to files (lfns) in File Catalog that are missing from EOS
176 * \param[out] fc_missing paths to files in EOS that are missing from File Catalog
177 * \return 0 - if successful, error_code (<0) in case of failures
178 */
179 int GetConsistencyCheckResults(int& eos_missing_count,
180 int& fc_missing_count,
181 vector<string>& eos_missing,
182 vector<string>& fc_missing);
183
184 /*!
185 * Function to download file from the CICC (Dirac FC)
186 * \param[in] LFN LFN of the downloaded file
187 * \param[in] save_path path to place where you want to save it. locally or by xrootd URL
188 * \return 0 - if successful, error_code (<0) in case of failures
189 */
190 int GetFile(string LFN, string save_path);
191
192 /*!
193 * Function to get file PFN (Physical File Name)
194 * \param[in] LFN LFN of the file
195 * \param[out] PFNs Vector of PFNs of the file
196 * \return 0 - if successful, error_code (<0) in case of failures
197 */
198 int GetPFNs(string LFN, vector<string>& PFNs);
199
200 /*!
201 * Function to add metadata for the file with specific LFN
202 * \param[in] LFN Logical File Name of the file
203 * \param[in] file_metadata 'FileInfo' object containing file metadata
204 * \return 0 - if successful, error_code (<0) in case of failures
205 */
206 int AddFileInfo(string LFN, FileInfo file_metadata);
207
208 /*!
209 * Function to add file metadata to the File Catalog using input JSON file path
210 * \param[in] json_file_path path of the JSON file containing a set of file metadata to be stored
211 * \return 0 - if successful, error_code (<0) in case of failures
212 */
213 int AddFileInfo(string json_file_path);
214
215 /*!
216 * Function to add directory metadata to the File Catalog using input JSON directory path. dir_path field for path
217 * to directory
218 * \param[in] json_file_path path of the JSON file containing a set of directory metadata to be stored
219 * \return 0 - if successful, error_code (<0) in case of failures
220 */
221 int AddDirectoryInfo(string json_file_path);
222
223 /*!
224 * Function to add new metadata field to the File Catalog
225 * \param[in] field_name metadata field name to be added
226 * \param[in] field_type metadata field type (INT, DATETIME, FLOAT, VARCHAR(128))
227 * \param[in] isDirectoryType which type of field is needed. Directory or file
228 * \return 0 - if successfully added, error_code (<0) in case of failures
229 */
230 int AddMetadataField(string field_name, string field_type, bool isDirectoryType = true);
231
232 /*!
233 * Function to add file to the File Catalog and upload to CICC
234 * \param[in] input_path local path to the file
235 * \param[in] LFN LFN of the output file on the CICC and FileCatalogue
236 * \return 0 - if successfully added, error_code (<0) in case of failures
237 */
238 int AddFile(string input_path, string LFN);
239
240 /*!
241 * Function to update metadata for the file with specific LFN
242 * \param[in] LFN Logical File Name of the file
243 * \param[in] file_metadata 'FileInfo' object containing file metadata
244 * \return 0 - if successfully updated, <0 - an error occured
245 */
246 int UpdateFileInfo(string LFN, FileInfo file_metadata);
247
248 /*!
249 * Function to delete metadata for the file with specific LFN
250 * \param[in] LFN Logical File Name of the file
251 * \return 0 - if successfully deleted, <0 - an error occured
252 */
253 int DeleteFileInfo(string LFN);
254
255 /*!
256 * Function to delete metadata for the directory at specific path
257 * \param[in] path Path to the directory inside File Catalog
258 * \return 0 - if successfully deleted, <0 - an error occured
259 */
260 int DeleteDirectoryInfo(string path);
261
262 /*!
263 * Function to delete metadata field from the File Catalog. Either for file or directory
264 * \param[in] metadatafield_name Metadatafield name which you want to delete
265 * \return 0 - if successfully deleted, <0 - an error occured
266 */
267 int DeleteMetadataField(string metadatafield_name);
268
269 /*!
270 * Function to delete file from the File Catalog and CICC
271 * \param[in] LFN Logical File Name of the file
272 * \return 0 - if successfully deleted, error_code (<0) in case of failures
273 */
274 int DeleteFile(string LFN);
275
276 /*!
277 * Function to check metadata for the file with specific LFN
278 * \param[in] LFN Logical File Name of the file
279 * \return 0 - if file metadata is avaialble, -1 - if file metadata is not set, error_code (<0) in case of
280 * failures
281 */
282 int CheckFileInfo(string LFN);
283
284 /*!
285 * Function to check whether file exist or not
286 * \param[in] LFN Logical File Name of the file
287 * \return 1 - if file exist, 0 - if not, <0 - an error occured
288 */
289 int CheckFileExist(string LFN);
290
291 /*!
292 * Function to print metadata for the file stored at the given location
293 * \param[in] LFN Logical File Name of the file
294 * \return 0 - if successfull, -2 - if file was not found at the location,
295 * error_code (<0) in case of failures
296 */
297 int PrintFileInfo(string LFN);
298
299 /*!
300 * Function to print current metadata fields
301 * \return 0 - if succesfull, <0 - an error occured
302 */
304
305 /*!
306 * Function for searching for files matching specific metadata in the File Catalog using REST API request as an
307 * HTTP(s) string
308 * \param[in] rest_api_request String HTTP command representing a request using REST API format
309 * \param[out] filepaths Vector of the filepath containing selected file metadata (empty if not found or some errors
310 * occured)
311 * \return 0 - if succesfull, <0 - an error occured
312 */
313
314 int FileCatalogRequest(string rest_api_request, vector<string>& filepaths);
315
316 /*!
317 * Function to perform a consistency check in the File Catalog
318 * \return check ID if successfull, <0 - an error occured
319 */
321
322 /*!
323 * Function to collect summary statistics on the File Catalog
324 * \param[out] files_count Number of files in the File Catalog
325 * \param[out] files_without_replicas Number of files without replicas
326 * \param[out] replicas_count Number of replicas in the File Catalog
327 * \param[out] replicas_without_files Number of replicas without files in the File Catalog
328 * \return 0 - if successfull, <0 - an error occured
329 */
330 int FileCatalogStats(int& files_count,
331 int& files_without_replicas,
332 int& replicas_count,
333 int& replicas_without_files);
334
335 /*!
336 * Setting Verbose level: 0 - only errors, 1 - normal output, 2 - detailed output, 3 - all with debug info
337 * \param[in] verbose_level Verbose level for output information
338 */
339 void SetVerbose(int verbose_level = 0) { VerboseLevel = verbose_level; }
340
341 private:
345
347
348 /*!
349 * Perfrom common curl read/write shenanigans
350 * \param[in] conditions String of conditions/path/commands which goes after URL when perfrorming API request
351 * \param[in] json_content String containing json with request as append content
352 * \param[in] qType type of request
353 * \return Response string from the API server
354 */
355 string performRequest(string conditions, string json_content, string qType);
356
357 // curl call for file download
358 int DownloadFile(string conditions, string savePath);
359
360 // curl call for file upload
361 int UploadFile(string conditions, string filepath);
362
363 // ClassDef(FileCatalogue, 1)
364};
365
366#endif /* !FileCatalog_H */
int CheckFileExist(string LFN)
Function to check whether file exist or not.
int DeleteDirectoryInfo(string path)
Function to delete metadata for the directory at specific path.
int AddDirectoryInfo(string json_file_path)
Function to add directory metadata to the File Catalog using input JSON directory path.
int DeleteFile(string LFN)
Function to delete file from the File Catalog and CICC.
int AddMetadataField(string field_name, string field_type, bool isDirectoryType=true)
Function to add new metadata field to the File Catalog.
int FileCatalogRequest(string rest_api_request, vector< string > &filepaths)
Function for searching for files matching specific metadata in the File Catalog using REST API reques...
int FileCatalogStats(int &files_count, int &files_without_replicas, int &replicas_count, int &replicas_without_files)
Function to collect summary statistics on the File Catalog.
int UploadFile(string conditions, string filepath)
int PrintFileInfo(string LFN)
Function to print metadata for the file stored at the given location.
int GetDirectoryInfo(string path, string &directory_info)
Function to get metadata of the directory at the given path.
virtual ~FileCatalogue()
int DeleteMetadataField(string metadatafield_name)
Function to delete metadata field from the File Catalog.
void SetVerbose(int verbose_level=0)
Setting Verbose level: 0 - only errors, 1 - normal output, 2 - detailed output, 3 - all with debug in...
string ServiceAccount
int FileCatalogConsistencyCheck()
Function to perform a consistency check in the File Catalog.
int CheckFileInfo(string LFN)
Function to check metadata for the file with specific LFN.
int GetFile(string LFN, string save_path)
Function to download file from the CICC (Dirac FC).
int GetFileList(vector< unique_ptr< FileCondition > > file_conditions, vector< string > &filepaths)
Function for searching for file metadata in the File Catalog using specified conditions.
int UpdateFileInfo(string LFN, FileInfo file_metadata)
Function to update metadata for the file with specific LFN.
int DeleteFileInfo(string LFN)
Function to delete metadata for the file with specific LFN.
int GetConsistencyCheckResults(int &eos_missing_count, int &fc_missing_count, vector< string > &eos_missing, vector< string > &fc_missing)
Function to get result of the last successful consistency check.
int DownloadFile(string conditions, string savePath)
int AddFileInfo(string LFN, FileInfo file_metadata)
Function to add metadata for the file with specific LFN.
string performRequest(string conditions, string json_content, string qType)
Perfrom common curl read/write shenanigans.
int AddFile(string input_path, string LFN)
Function to add file to the File Catalog and upload to CICC.
int GetFileInfo(string LFN, FileInfo &file_info)
Function to get metadata of the file stored at the given location.
int GetPFNs(string LFN, vector< string > &PFNs)
Function to get file PFN (Physical File Name).
int PrintMetadataFields()
Function to print current metadata fields.
int GetConsistencyCheckStatus()
Function to get status of the last successful consistency check.
searchAttributes
enumeration for attributes, which can be used in searching file metadata
string getValueAsString()
matchCriteria
enumeration for conditions, which can be used for matching with a specified value
FileCondition(searchAttributes attribute, matchCriteria condition)
Create condition for searching file metadata.
matchCriteria getMatchCriteria()
matchCriteria FileMatchCriteria
searchAttributes getFileAttribute()
virtual ~FileCondition()
unsigned int uiValue
searchAttributes FileAttribute
Class presenting File Info (Metadata) Structure.
int StartEvent
event number from which stored event set begins
float FileSize
file size in bytes
int RunType
run type (1 - physics run, 2 - calibration run, 3 - test run)
static const int Version
structure (API) version
string BeamParticle
beam particle name
int EndEvent
event number with which event set ends
int EventCount
event count stored in the file
float Energy
beam energy
void Print() const
Print all file metadata to the standard output.
string EndDateTime
end datetime of the stored event set (YYYY-MM-DD HH-MM-SS format)
int PeriodNumber
number of experiment session, when the events was collected
string StartDateTime
start datetime of the stored event set (YYYY-MM-DD HH-MM-SS format)
int RunNumber
number of experiment run, when the events was collected
string TargetParticle
target particle name
float FieldVoltage
field voltage
Definition json.hpp:4446