-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathBasicCCDBManager.h
More file actions
172 lines (141 loc) · 6.31 KB
/
BasicCCDBManager.h
File metadata and controls
172 lines (141 loc) · 6.31 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
// 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.
//
// Created by Sandro Wenzel on 2019-08-14.
//
#ifndef O2_BASICCDBMANAGER_H
#define O2_BASICCDBMANAGER_H
#include "CCDB/CcdbApi.h"
#include "CCDB/CCDBTimeStampUtils.h"
#include <string>
#include <map>
#include <unordered_map>
#include <memory>
// #include <FairLogger.h>
namespace o2::ccdb
{
/// A simple (singleton) class offering simplified access to CCDB (mainly for MC simulation)
/// The class encapsulates timestamp and URL and is easily usable from detector code.
class BasicCCDBManager
{
struct CachedObject {
std::shared_ptr<void> objPtr;
std::string uuid;
long startvalidity = 0;
long endvalidity = 0;
bool isValid(long ts) { return ts < endvalidity && ts > startvalidity; }
};
public:
static BasicCCDBManager& instance()
{
const std::string ccdbUrl{"http://ccdb-test.cern.ch:8080"};
static BasicCCDBManager inst{ccdbUrl};
return inst;
}
/// set a URL to query from
void setURL(const std::string& url);
/// set timestamp cache for all queries
void setTimestamp(long t)
{
if (t >= 0) {
mTimestamp = t;
}
}
/// query current URL
std::string const& getURL() const { return mCCDBAccessor.getURL(); }
/// query timestamp
long getTimestamp() const { return mTimestamp; }
/// retrieve an object of type T from CCDB as stored under path and timestamp
template <typename T>
T* getForTimeStamp(std::string const& path, long timestamp);
/// retrieve an object of type T from CCDB as stored under path; will use the timestamp member
template <typename T>
T* get(std::string const& path)
{
// TODO: add some error info/handling when failing
return getForTimeStamp<T>(path, mTimestamp);
}
bool isHostReachable() const { return mCCDBAccessor.isHostReachable(); }
/// clear all entries in the cache
void clearCache() { mCache.clear(); }
/// clear particular entry in the cache
void clearCache(std::string const& path) { mCache.erase(path); }
/// check if caching is enabled
bool isCachingEnabled() const { return mCachingEnabled; }
/// disable or enable caching
void setCaching(bool v)
{
mCachingEnabled = v;
if (!v) {
clearCache();
}
}
/// check if checks of object validity before CCDB query is enabled
bool isLocalObjectValidityCheckingEnabled() const { return mCheckObjValidityEnabled; }
/// set the flag to check object validity before CCDB query
void setLocalObjectValidityChecking(bool v = true) { mCheckObjValidityEnabled = v; }
/// set the object upper validity limit
void setCreatedNotAfter(long v) { mCreatedNotAfter = v; }
/// get the object upper validity limit
long getCreatedNotAfter() const { return mCreatedNotAfter; }
/// reset the object upper validity limit
void resetCreatedNotAfter() { mCreatedNotAfter = 0; }
/// set the object upper validity limit
void setCreatedNotBefore(long v) { mCreatedNotBefore = v; }
/// get the object upper validity limit
long getCreatedNotBefore() const { return mCreatedNotBefore; }
/// reset the object upper validity limit
void resetCreatedNotBefore() { mCreatedNotBefore = 0; }
private:
BasicCCDBManager(std::string const& path) : mCCDBAccessor{}
{
mCCDBAccessor.init(path);
}
// we access the CCDB via the CURL based C++ API
o2::ccdb::CcdbApi mCCDBAccessor;
std::unordered_map<std::string, CachedObject> mCache; //! map for {path, CachedObject} associations
std::map<std::string, std::string> mMetaData; // some dummy object needed to talk to CCDB API
std::map<std::string, std::string> mHeaders; // headers to retrieve tags
long mTimestamp{o2::ccdb::getCurrentTimestamp()}; // timestamp to be used for query (by default "now")
bool mCanDefault = false; // whether default is ok --> useful for testing purposes done standalone/isolation
bool mCachingEnabled = true; // whether caching is enabled
bool mCheckObjValidityEnabled = false; // wether the validity of cached object is checked before proceeding to a CCDB API query
long mCreatedNotAfter = 0; // upper limit for object creation timestamp (TimeMachine mode) - If-Not-After HTTP header
long mCreatedNotBefore = 0; // lower limit for object creation timestamp (TimeMachine mode) - If-Not-Before HTTP header
};
template <typename T>
T* BasicCCDBManager::getForTimeStamp(std::string const& path, long timestamp)
{
if (!isCachingEnabled()) {
return mCCDBAccessor.retrieveFromTFileAny<T>(path, mMetaData, timestamp, nullptr, "",
mCreatedNotAfter ? std::to_string(mCreatedNotAfter) : "",
mCreatedNotBefore ? std::to_string(mCreatedNotBefore) : "");
}
auto& cached = mCache[path];
if (mCheckObjValidityEnabled && cached.isValid(timestamp))
return reinterpret_cast<T*>(cached.objPtr.get());
T* ptr = mCCDBAccessor.retrieveFromTFileAny<T>(path, mMetaData, timestamp, &mHeaders, cached.uuid,
mCreatedNotAfter ? std::to_string(mCreatedNotAfter) : "",
mCreatedNotBefore ? std::to_string(mCreatedNotBefore) : "");
if (ptr) { // new object was shipped, old one (if any) is not valid anymore
cached.objPtr.reset(ptr);
cached.uuid = mHeaders["ETag"];
cached.startvalidity = std::stol(mHeaders["Valid-From"]);
cached.endvalidity = std::stol(mHeaders["Valid-Until"]);
} else if (mHeaders.count("Error")) { // in case of errors the pointer is 0 and headers["Error"] should be set
clearCache(path); // in case of any error clear cache for this object
} else { // the old object is valid
ptr = reinterpret_cast<T*>(cached.objPtr.get());
}
mHeaders.clear();
return ptr;
}
} // namespace o2::ccdb
#endif //O2_BASICCCDBMANAGER_H