forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.cxx
More file actions
522 lines (409 loc) · 14.6 KB
/
Copy pathStorage.cxx
File metadata and controls
522 lines (409 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "CCDB/Storage.h"
#include <fairlogger/Logger.h> // for LOG
#include <TH1.h> // for TH1
#include <TKey.h> // for TKey
#include <TNtuple.h> // for TNtuple
#include "CCDB/Condition.h" // for Condition
#include "CCDB/Manager.h" // for Manager
using namespace o2::ccdb;
ClassImp(Storage)
Storage::Storage()
: mValidFileIds(),
mRun(-1),
mPathFilter(),
mVersion(-1),
mConditionMetaDataFilter(nullptr),
mSelections(),
mURI(),
mType(),
mBaseFolder(),
mNretry(0),
mInitRetrySeconds(0)
{
// constructor
mValidFileIds.SetOwner(1);
mSelections.SetOwner(1);
}
Storage::~Storage()
{
// destructor
removeAllSelections();
mValidFileIds.Clear();
delete mConditionMetaDataFilter;
}
void Storage::getSelection(/*const*/ ConditionId *id)
{
// return required version and subversion from the list of selection criteria
TIter iter(&mSelections);
ConditionId *aSelection;
// loop on the list of selection criteria
while ((aSelection = (ConditionId *) iter.Next())) {
// check if selection element contains id's path and run (range)
if (aSelection->isSupersetOf(*id)) {
LOG(DEBUG) << "Using selection criterion: " << aSelection->ToString().Data() << " ";
// return required version and subversion
id->setVersion(aSelection->getVersion());
id->setSubVersion(aSelection->getSubVersion());
return;
}
}
// no valid element is found in the list of selection criteria -> return
LOG(DEBUG) << "Looking for objects with most recent version";
return;
}
void Storage::readSelectionFromFile(const char *fileName)
{
// read selection criteria list from file
removeAllSelections();
TList *list = getIdListFromFile(fileName);
if (!list) {
return;
}
list->SetOwner();
Int_t nId = list->GetEntries();
ConditionId *id;
TKey *key;
for (int i = nId - 1; i >= 0; i--) {
key = (TKey *) list->At(i);
id = (ConditionId *) key->ReadObj();
if (id) {
addSelection(*id);
}
}
delete list;
LOG(INFO) << "Selection criteria list filled with " << mSelections.GetEntries() << " entries";
printSelectionList();
}
void Storage::addSelection(const ConditionId &selection)
{
// add a selection criterion
IdPath path = selection.getPath();
if (!path.isValid()) {
return;
}
TIter iter(&mSelections);
const ConditionId *anId;
while ((anId = (ConditionId *) iter.Next())) {
if (selection.isSupersetOf(*anId)) {
LOG(WARNING) << "This selection is more general than a previous one and will hide it!";
LOG(WARNING) << (anId->ToString()).Data();
mSelections.AddBefore(anId, new ConditionId(selection));
return;
}
}
mSelections.AddFirst(new ConditionId(selection));
}
void Storage::addSelection(const IdPath &path, const IdRunRange &runRange, Int_t version, Int_t subVersion)
{
// add a selection criterion
addSelection(ConditionId(path, runRange, version, subVersion));
}
void Storage::addSelection(const IdPath &path, Int_t firstRun, Int_t lastRun, Int_t version, Int_t subVersion)
{
// add a selection criterion
addSelection(ConditionId(path, firstRun, lastRun, version, subVersion));
}
void Storage::removeSelection(const ConditionId &selection)
{
// remove a selection criterion
TIter iter(&mSelections);
ConditionId *aSelection;
while ((aSelection = (ConditionId *) iter.Next())) {
if (selection.isSupersetOf(*aSelection)) {
mSelections.Remove(aSelection);
}
}
}
void Storage::removeSelection(const IdPath &path, const IdRunRange &runRange)
{
// remove a selection criterion
removeSelection(ConditionId(path, runRange, -1, -1));
}
void Storage::removeSelection(const IdPath &path, Int_t firstRun, Int_t lastRun)
{
// remove a selection criterion
removeSelection(ConditionId(path, firstRun, lastRun, -1, -1));
}
void Storage::removeSelection(int position)
{
// remove a selection criterion from its position in the list
delete mSelections.RemoveAt(position);
}
void Storage::removeAllSelections()
{
// remove all selection criteria
mSelections.Clear();
}
void Storage::printSelectionList()
{
// prints the list of selection criteria
TIter iter(&mSelections);
ConditionId *aSelection;
// loop on the list of selection criteria
int index = 0;
while ((aSelection = (ConditionId *) iter.Next())) {
LOG(INFO) << "index " << index++ << " -> selection: " << aSelection->ToString().Data();
}
}
Condition *Storage::getObject(const ConditionId &query)
{
// get an Condition object from the database
// check if query's path and runRange are valid
// query is invalid also if version is not specified and subversion is!
if (!query.isValid()) {
LOG(ERROR) << "Invalid query: " << query.ToString().Data();
return nullptr;
}
// query is not specified if path contains wildcard or runrange = [-1,-1]
if (!query.isSpecified()) {
LOG(ERROR) << "Unspecified query: " << query.ToString().Data();
return nullptr;
}
// This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
Bool_t oldStatus = TH1::AddDirectoryStatus();
TH1::AddDirectory(kFALSE);
Condition *entry = getCondition(query);
if (oldStatus != kFALSE) {
TH1::AddDirectory(kTRUE);
}
// if drain storage is set, drain entry into drain storage
if (entry && (Manager::Instance())->isdrainSet()) {
Manager::Instance()->drain(entry);
}
return entry;
}
Condition *Storage::getObject(const IdPath &path, Int_t runNumber, Int_t version, Int_t subVersion)
{
// get an Condition object from the database
return getObject(ConditionId(path, runNumber, runNumber, version, subVersion));
}
Condition *Storage::getObject(const IdPath &path, const IdRunRange &runRange, Int_t version, Int_t subVersion)
{
// get an Condition object from the database
return getObject(ConditionId(path, runRange, version, subVersion));
}
TList *Storage::getAllObjects(const ConditionId &query)
{
// get multiple Condition objects from the database
if (!query.isValid()) {
LOG(ERROR) << "Invalid query: " << query.ToString().Data();
return nullptr;
}
if (query.isAnyRange()) {
LOG(ERROR) << "Unspecified run or runrange: " << query.ToString().Data();
return nullptr;
}
// This is needed otherwise TH1 objects (histos, TTree's) are lost when file is closed!
Bool_t oldStatus = TH1::AddDirectoryStatus();
TH1::AddDirectory(kFALSE);
TList *result = getAllEntries(query);
if (oldStatus != kFALSE) {
TH1::AddDirectory(kTRUE);
}
Int_t nEntries = result->GetEntries();
LOG(INFO) << nEntries << " objects retrieved. Request was: " << query.ToString().Data();
for (int i = 0; i < nEntries; i++) {
Condition *entry = (Condition *) result->At(i);
LOG(INFO) << entry->getId().ToString().Data();
}
// if drain storage is set, drain entries into drain storage
if ((Manager::Instance())->isdrainSet()) {
for (int i = 0; i < result->GetEntries(); i++) {
Condition *entry = (Condition *) result->At(i);
Manager::Instance()->drain(entry);
}
}
return result;
}
TList *Storage::getAllObjects(const IdPath &path, Int_t runNumber, Int_t version, Int_t subVersion)
{
// get multiple Condition objects from the database
return getAllObjects(ConditionId(path, runNumber, runNumber, version, subVersion));
}
TList *Storage::getAllObjects(const IdPath &path, const IdRunRange &runRange, Int_t version, Int_t subVersion)
{
// get multiple Condition objects from the database
return getAllObjects(ConditionId(path, runRange, version, subVersion));
}
ConditionId *Storage::getId(const ConditionId &query)
{
// get the ConditionId of the valid object from the database (does not open the file)
// check if query's path and runRange are valid
// query is invalid also if version is not specified and subversion is!
if (!query.isValid()) {
LOG(ERROR) << "Invalid query: " << query.ToString().Data();
return nullptr;
}
// query is not specified if path contains wildcard or runrange = [-1,-1]
if (!query.isSpecified()) {
LOG(ERROR) << "Unspecified query: " << query.ToString().Data();
return nullptr;
}
ConditionId *id = getConditionId(query);
return id;
}
ConditionId *Storage::getId(const IdPath &path, Int_t runNumber, Int_t version, Int_t subVersion)
{
// get the ConditionId of the valid object from the database (does not open the file)
return getId(ConditionId(path, runNumber, runNumber, version, subVersion));
}
ConditionId *Storage::getId(const IdPath &path, const IdRunRange &runRange, Int_t version, Int_t subVersion)
{
// get the ConditionId of the valid object from the database (does not open the file)
return getId(ConditionId(path, runRange, version, subVersion));
}
Bool_t Storage::putObject(TObject *object, ConditionId &id, ConditionMetaData *metaData, const char *mirrors)
{
// store an Condition object into the database
if (object == nullptr) {
LOG(ERROR) << "Null Condition! No storage will be done!";
return kFALSE;
}
Condition anCondition(object, id, metaData);
return putObject(&anCondition, mirrors);
}
Bool_t Storage::putObject(Condition *entry, const char *mirrors)
{
// store an Condition object into the database
if (!entry) {
LOG(ERROR) << "No entry!";
return kFALSE;
}
if (entry->getObject() == nullptr) {
LOG(ERROR) << "No valid object in CDB entry!";
return kFALSE;
}
if (!entry->getId().isValid()) {
LOG(ERROR) << "Invalid entry ID: " << entry->getId().ToString().Data();
return kFALSE;
}
if (!entry->getId().isSpecified()) {
LOG(ERROR) << "Unspecified entry ID: " << entry->getId().ToString().Data();
return kFALSE;
}
TString strMirrors(mirrors);
if (!strMirrors.IsNull() && !strMirrors.IsWhitespace()) {
return putCondition(entry, mirrors);
} else {
return putCondition(entry);
}
}
void Storage::queryStorages(Int_t run, const char *pathFilter, Int_t version, ConditionMetaData *md)
{
// query CDB for files valid for given run, and fill list mValidFileIds
// Actual query is done in virtual function queryValidFiles()
// If version is not specified, the query will fill mValidFileIds
// with highest versions
mRun = run;
mPathFilter = pathFilter;
if (!mPathFilter.isValid()) {
LOG(ERROR) << "Filter not valid: " << pathFilter;
mPathFilter = "*";
return;
}
mVersion = version;
LOG(INFO) << "Querying files valid for run " << mRun << R"( and path ")" << pathFilter << R"(" into CDB storage ")"
<< mType.Data() << "://" << mBaseFolder.Data() << R"(")";
// In mValidFileIds, clear id for the same 3level path, if any
LOG(DEBUG) << R"(Clearing list of CDB ConditionId's previously loaded for path ")" << pathFilter << R"(")";
IdPath filter(pathFilter);
for (Int_t i = mValidFileIds.GetEntries() - 1; i >= 0; --i) {
ConditionId *rmMe = dynamic_cast<ConditionId *>(mValidFileIds.At(i));
IdPath rmPath = rmMe->getPathString();
if (filter.isSupersetOf(rmPath)) {
LOG(DEBUG) << R"(Removing id ")" << rmPath.getPathString().Data() << R"(" matching: ")" << pathFilter << R"(")";
delete mValidFileIds.RemoveAt(i);
}
}
if (mConditionMetaDataFilter) {
delete mConditionMetaDataFilter;
mConditionMetaDataFilter = nullptr;
}
if (md) {
mConditionMetaDataFilter = dynamic_cast<ConditionMetaData *>(md->Clone());
}
queryValidFiles();
LOG(INFO) << mValidFileIds.GetEntries() << " valid files found!";
}
void Storage::printrQueryStorages()
{
// print parameters used to load list of CDB ConditionId's (mRun, mPathFilter, mVersion)
ConditionId paramId(mPathFilter, mRun, mRun, mVersion);
LOG(INFO) << "**** queryStorages Parameters **** \n\t\"" << paramId.ToString().Data() << R"(")";
if (mConditionMetaDataFilter) {
mConditionMetaDataFilter->printConditionMetaData();
}
TString message = "**** ConditionId's of valid objects found *****\n";
TIter iter(&mValidFileIds);
ConditionId *anId = nullptr;
// loop on the list of selection criteria
while ((anId = dynamic_cast<ConditionId *>(iter.Next()))) {
message += Form("\t%s\n", anId->ToString().Data());
}
message += Form("\n\tTotal: %d objects found\n", mValidFileIds.GetEntriesFast());
LOG(INFO) << message.Data();
}
void Storage::setMirrorSEs(const char *mirrors)
{
// if the current storage is not of "alien" type, just issue a warning
// GridStorage implements its own setMirrorSEs method, classes for other storage types do not
TString storageType = getStorageType();
if (storageType != "alien") {
LOG(WARNING) << R"(The current storage is of type ")" << storageType.Data() << R"(". Setting of SEs to ")"
<< mirrors
<< R"(" skipped!)";
return;
}
LOG(ERROR) << "We should never get here!! GridStorage must have masked this virtual method!";
return;
}
const char *Storage::getMirrorSEs() const
{
// if the current storage is not of "alien" type, just issue a warning
// GridStorage implements its own getMirrorSEs method, classes for other storage types do not
TString storageType = getStorageType();
if (storageType != "alien") {
LOG(WARNING) << R"(The current storage is of type ")" << storageType.Data()
<< R"(" and cannot handle SEs. Returning empty string!)";
return "";
}
LOG(ERROR) << "We should never get here!! GridStorage must have masked this virtual method!";
return "";
}
void Storage::loadTreeFromFile(Condition *entry) const
{
// Checks whether entry contains a TTree and in case loads it into memory
TObject *obj = (TObject *) entry->getObject();
if (!obj) {
LOG(ERROR) << "Cannot retrieve the object:";
entry->printConditionMetaData();
return;
}
if (!strcmp(obj->ClassName(), TTree::Class_Name())) {
LOG(WARNING) << "Condition contains a TTree! Loading baskets...";
TTree *tree = dynamic_cast<TTree *>(obj);
if (!tree) {
return;
}
tree->LoadBaskets();
tree->SetDirectory(nullptr);
} else if (!strcmp(obj->ClassName(), TNtuple::Class_Name())) {
LOG(WARNING) << "Condition contains a TNtuple! Loading baskets...";
TNtuple *ntu = dynamic_cast<TNtuple *>(obj);
if (!ntu) {
return;
}
ntu->LoadBaskets();
ntu->SetDirectory(nullptr);
}
return;
}