ATLAS Offline Software
Loading...
Searching...
No Matches
CoralCrestManager.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4// @file CoralCrestManager.cxx
5// Implementation for CrestFunctions utilities
6// @author Evgeny Alexandrov
7// @date 24 February 2025
8
9#include "CoralCrestManager.h"
10#include <memory>
11#include "CrestApi/CrestApi.h"
12#include "CrestApi/CrestApiFs.h"
13#include "CoralBase/AttributeList.h"
14#include "CoralBase/Attribute.h"
15#include "CoolKernel/StorageType.h"
16#include "CoolKernel/RecordSpecification.h"
17#include "CoolKernel/Record.h"
19#include "CxxUtils/base64.h"
20
22#include "GaudiKernel/MsgStream.h"
23#include "GaudiKernel/SystemOfUnits.h"
24
25namespace{
26 const std::map<std::string, cool::StorageType::TypeId> typeCorrespondance={
27 {"Bool", cool::StorageType::Bool},
28 {"UChar",cool::StorageType::UChar},
29 {"Int16", cool::StorageType::Int16},
30 {"UInt16", cool::StorageType::UInt16},
31 {"Int32", cool::StorageType::Int32},
32 {"UInt32", cool::StorageType::UInt32},
33 {"UInt63",cool::StorageType::UInt63},
34 {"Int64", cool::StorageType::Int64},
35 {"Float", cool::StorageType::Float},
36 {"Double", cool::StorageType::Double},
37 {"String255", cool::StorageType::String255},
38 {"String4k", cool::StorageType::String4k},
39 {"String64k", cool::StorageType::String64k},
40 {"String16M", cool::StorageType::String16M},
41 {"String128M", cool::StorageType::String128M},
42 {"Blob64k", cool::StorageType::Blob64k},
43 {"Blob16M", cool::StorageType::Blob16M},
44 {"Blob128M", cool::StorageType::Blob128M}
45 };
46
47 const std::string colonDelimiter{" : "};
48}
49 CoralCrestManager::CoralCrestManager(const std::string & crest_path, const std::string & crestTag):m_crestTag(crestTag){ //AthMessaging("CoralCrestManager")
50 if(crest_path.length()==0)
51 return;
52 if (crest_path.starts_with(CoralCrestManager::prefix1) || crest_path.starts_with(CoralCrestManager::prefix2)){
53 m_crestCl = std::make_unique<Crest::CrestApi>(Crest::CrestApi(crest_path));
54 }
55 else{
56 m_crestCl = std::make_unique<Crest::CrestApiFs>(Crest::CrestApiFs(false,crest_path));
57 }
58
59 }
60
61 std::map<std::string, std::string> CoralCrestManager::getGlobalTagMap(const std::string & crest_path, const std::string& globaltag){
62 std::unique_ptr<Crest::CrestApiBase> crestCl;
63 if (crest_path.starts_with(CoralCrestManager::prefix1) || crest_path.starts_with(CoralCrestManager::prefix2)){
64 crestCl.reset(new Crest::CrestApi(crest_path));
65 }
66 else{
67 crestCl.reset(new Crest::CrestApiFs(true,crest_path));
68 }
69 std::map<std::string, std::string> tagmap;
70 try{
71 Crest::GlobalTagMapSetDto dto = crestCl->findGlobalTagMap(globaltag,"Trace");
72 for (const auto &tagMapDto : dto.getResources()){
73 tagmap[tagMapDto.getLabel()]=tagMapDto.getTagName();
74 }
75 } catch (std::exception & e){
76 MsgStream gLog(Athena::getMessageSvc(), "CoralCrestManager");
77 gLog << MSG::ERROR << __FILE__<<":"<<__LINE__<< ": " << e.what() << " Cannot get a global tag map for " << globaltag<<endmsg;
78 }
79 return tagmap;
80 }
81
82 std::string CoralCrestManager::parseTypeName(const std::string & description){
83 std::string regex=R"delim(<typeName>\s*([^\s]+)\s*</typeName>)delim";
84 std::regex re(regex);
85 std::smatch typeMatch;
86 bool match=std::regex_search(description, typeMatch,re);
87 return (match) ? std::string(typeMatch[1]) : std::string("");
88 }
89
91 Crest::TagInfoDto info = getTagInfoDto();
92 std::string folderDescription = info.getFolderDescription();
93 if (folderDescription.find("<coracool>") != std::string::npos) return IOVDbNamespace::CoraCool;
94 const std::string typeName = parseTypeName(folderDescription);
95 if (typeName=="CondAttrListVec"){
96 m_isVectorPayload = true;
98 }
99 m_isVectorPayload = false;
100 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
101 std::vector< std::pair<std::string,std::string> > chs = info.getChannels().getChannels();
102 for (auto &p : spec){
103 if(p.first=="PoolRef" && p.second=="String4k"){
104 int id=std::stoll(chs[0].first);
105 if(chs.size()==1 && id==0)
107 else
109 }
110 }
111 if (typeName == "CondAttrListCollection") return IOVDbNamespace::AttrListColl;
113 }
114
116 if(!m_isVectorPayload.has_value())
118 return m_isVectorPayload.value();
119 }
120
122 if(m_TagMeta.has_value()){
123 return;
124 }
125
126 try{
127 m_TagMeta = m_crestCl->findTagMeta(m_crestTag);
128 } catch (std::exception & e){
129 MsgStream gLog(Athena::getMessageSvc(), "CoralCrestManager");
130 gLog << MSG::ERROR << __FILE__<<":"<<__LINE__<< ": " << e.what() << " Cannot get a tag meta info " << m_crestTag<<endmsg;
131 }
132 return;
133 }
134
136 if(!m_TagMeta.has_value())
137 loadTagInfo();
138 return m_TagMeta.value().getTagInfoDto();
139 }
140
142 if(!m_Tag.has_value()){
143 m_Tag = m_crestCl->findTag(m_crestTag);
144 }
145 return m_Tag.value();
146 }
147
149 return getTagInfoDto().getPayloadSpec().toJson().dump();
150 }
151
153 Crest::TagInfoDto info = getTagInfoDto();
154 return info.getFolderDescription();
155 }
156
157 std::pair<std::vector<cool::ChannelId> , std::vector<std::string>> CoralCrestManager::getChannelList(){
158 if(!m_TagMeta.has_value())
159 loadTagInfo();
160 Crest::TagInfoDto info = getTagInfoDto();
161 std::vector<cool::ChannelId> list;
162 std::vector<std::string> names;
163 std::vector< std::pair<std::string,std::string> > res = info.getChannels().getChannels();
164 for (auto &p : res){
165 list.push_back(std::stoll(p.first));
166 names.push_back(p.second);
167 }
168 return std::make_pair(std::move(list), std::move(names));
169 }
170
171 coral::AttributeListSpecification* CoralCrestManager::getAttributeListSpec(){
172 Crest::TagInfoDto info = getTagInfoDto();
173 std::vector< std::pair<std::string,std::string> > spec_vec= info.getPayloadSpec().getColumns();
174 auto * spec = new coral::AttributeListSpecification();
175 for (auto &p : spec_vec){
176 auto it = typeCorrespondance.find(p.second);
177 if (it != typeCorrespondance.end()) {
178 spec->extend(p.first,cool::StorageType::storageType(it->second).cppType());
179 }
180 }
181 return spec;
182 }
183
185 if(m_crest_cont.has_value())
186 return;
187 Crest::TagInfoDto info = getTagInfoDto();
188 Crest::TagDto tag = getTagDto();
190 if(tag.getObjectType()=="crest-json-multi-iov")
192 Crest::CrestContainer cr_cont(mId);
193 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
194 for (auto &p : spec){
195 cr_cont.addColumn(p.first,p.second.c_str());
196 }
197 if(!m_isVectorPayload.has_value()) determineFolderType();
198 cr_cont.setVectorPayload(m_isVectorPayload.value());
199 m_crest_cont.emplace(cr_cont);
200 return;
201 }
202
203 std::pair<uint64_t,uint64_t>
204 CoralCrestManager::getSinceUntilPair(std::vector<uint64_t>& v, const uint64_t since, const uint64_t until){
205 uint64_t new_since = 0;
206 uint64_t new_until = 0;
207
208 if (until < since){
209 MsgStream gLog(Athena::getMessageSvc(), "CoralCrestManager");
210 gLog << MSG::ERROR << "Wrong since/until."<<endmsg;
211 return std::make_pair(0,0);
212 }
213 const std::size_t N = v.size();
214 std::size_t i = 0;
215 for (; i < N; ++i) {
216 if(v[i] <= since && since < v[i+1]){
217 new_since = v[i];
218 break;
219 }
220 }
221
222 for (; i < N; ++i) {
223 if(v[i] < until && until <= v[i+1]){
224 new_until = v[i+1];
225 break;
226 }
227 }
228
229 return std::make_pair(new_since,new_until);
230 }
231
232 std::pair<uint64_t,uint64_t>
233 CoralCrestManager::getIovInterval(const std::string& tag, const uint64_t since, const uint64_t until){
234 Crest::IovSetDto dto = m_crestCl->selectGroups(tag, 0, 10000, 0, "id.since:ASC");
235 std::vector<uint64_t> v = dto.getListSince();
236 v.push_back(cool::ValidityKeyMax); // added "infinity" as the last item 9223372036854775807
237 return getSinceUntilPair(v, since, until);
238 }
239
240 std::vector<std::pair<cool::ValidityKey,std::string>> CoralCrestManager::getIovsForTag(uint64_t since, uint64_t until){
242 int iovNumber = m_crestCl->getSize(m_crestTag);
243 std::vector<std::pair<cool::ValidityKey,std::string>> res;
244 Crest::IovSetDto dto;
245 if(iovNumber <=1000){
246 dto = m_crestCl->selectIovs(m_crestTag, 0, -1, 0, 10000, 0, "id.since:ASC");
247 }
248 else{
249 std::pair<uint64_t,uint64_t> ppt = getIovInterval(m_crestTag, since, until);
250 uint64_t s_time = ppt.first;
251 uint64_t u_time = ppt.second;
252
253 if (s_time == 0 && u_time == 0){ // data out of range
254 return res;
255 }
256 else {
257 dto = m_crestCl->selectIovs(m_crestTag, s_time, u_time, 0, 10000, 0, "id.since:ASC");
258 }
259 }
260 for (auto &p : dto.getResources()){
261 res.push_back(std::make_pair((cool::ValidityKey)p.getSince(),p.getPayloadHash()));
262 }
263 return res;
264 }
265
266 std::vector<uint64_t> CoralCrestManager::loadPayloadForHash(uint64_t since, const std::string & hash){
268 std::string reply;
269 try{
270 // get payload from Crest server
271 reply = m_crestCl->getPayload(hash);
272 } catch (std::exception & e){
273 MsgStream gLog(Athena::getMessageSvc(), "CoralCrestManager");
274 gLog << MSG::ERROR << __FILE__<<":"<<__LINE__<< ": "<<e.what()<<" while trying to find the payload"<<endmsg;
275 throw std::runtime_error(e.what());
276 }
277 try{
278 // parse payload according to type of payload and put it to CrestConteiner.
279 // Store only one value before 'since'.
280 // Returns a list of timestamp for which data has been loaded
281 return m_crest_cont.value().fromJson(since,reply);
282 } catch (std::exception & e){
283 MsgStream gLog(Athena::getMessageSvc(), "CoralCrestManager");
284 gLog << MSG::ERROR << "LoadPayloadForHash:"<<e.what()<<" while trying to parse the payload. Since="<<since<<", hash="<<hash<<endmsg;
285 throw std::runtime_error(e.what());
286 }
287 }
288
289 //put payload for selected since in json string
290 std::string CoralCrestManager::dumpPayload(cool::ValidityKey since){
291 m_crest_cont.value().selectIov(since);
293 std::stringstream res;
294 res<<"[";
295 std::vector<std::string> chIds = m_crest_cont.value().channelIds();
296 auto* pspec=getAttributeListSpec();
297 std::string sep="";
298 for (auto &ch : chIds){
299 res<<sep;
300 res<<IOVDbNamespace::s_openJson<<"\""<<ch<<"\" : ";
301 switch (ftype){
303 {
304 std::vector<coral::AttributeList> attr=getVectorPayload(pspec,ch);
305 res<<"[";
306 std::string sep2="";
307 for (const auto & vitr:attr){
309 if (sep2.empty()) sep2 =IOVDbNamespace::s_delimiterJson;
310 }
311 res<<"]";
312 break;
313 }
317 {
318 coral::AttributeList attr=getPayload(pspec,ch);
320 break;
321 }
323 {
324 coral::AttributeList attr=getPayload(pspec,ch);
325 std::ostringstream os;
326 attr[0].toOutputStream(os);
327 auto str=os.str();
328 const auto separatorPosition = str.find(colonDelimiter);
329 const std::string payloadOnly=str.substr(separatorPosition+3);
330 res<<"\""<<payloadOnly<<"\"";
331 break;
332 }
334 res<< " CoraCool";
335 break;
336 default:
337 res<<" a_data_value";
338 }
339 if (sep.empty()) sep=",";
341 }
342 res<<"]";
343 pspec->release();
344 return res.str();
345 }
346
347 coral::AttributeList CoralCrestManager::getPayload(coral::AttributeListSpecification * pSpec,const std::string & chId){
348 nlohmann::json j=m_crest_cont.value().getPayloadChannel(chId.c_str());
349 return createAttributeList(pSpec,j,m_crest_cont.value().getMPayloadSpec());
350 }
351
352 std::vector<coral::AttributeList> CoralCrestManager::getVectorPayload(coral::AttributeListSpecification* pSpec,const std::string & chId){
353 std::vector<coral::AttributeList> res;
354 nlohmann::json vecJ=m_crest_cont.value().getPayloadChannel(chId.c_str());
355 for (auto &p : vecJ){
356 coral::AttributeList att=createAttributeList(pSpec,p,m_crest_cont.value().getMPayloadSpec());
357 res.push_back(att);
358 }
359 return res;
360 }
361
362 void CoralCrestManager::selectIov(cool::ValidityKey since){
363 m_crest_cont.value().selectIov(since);
364 }
365
366 std::vector<std::string> CoralCrestManager::channelIds(cool::ValidityKey since){
367 selectIov(since);
368 return m_crest_cont.value().channelIds();
369 }
370
371 coral::AttributeList CoralCrestManager::createAttributeList(coral::AttributeListSpecification * pSpec,nlohmann::json& j,const std::vector<std::pair<std::string, Crest::TypeId>> & tSpec){
372 coral::AttributeList attr(*pSpec,true);
373 unsigned int s=attr.size();
374
375 json::const_iterator it = j.begin();
376 for (unsigned int i(0);i!=s;++i){
377 // cool::Record does not provide non-const access to AttributeList.
378 // But this is safe because we are filling a local instance.
379 auto & att = const_cast<coral::Attribute&>(attr[i]);
380 if (it == j.end()){
381 continue;
382 }
383 const auto thisVal = it.value();
384 ++it;
385
386 try{
387 if (thisVal.is_null()){
388 att.setNull();
389 continue;
390 }
391 cool::StorageType::TypeId typespec=cool::StorageType::Bool;
392 for(auto &p : tSpec){
393 if(p.first.compare(att.specification().name())==0){
394 auto pElement = Crest::s_typeToString.find(p.second);
395 if (pElement == Crest::s_typeToString.end()){
396 throw std::runtime_error("CoralCrestManager::createAttributeList: name not found.");
397 }
398 std::string str_spec = pElement ->second;
399 auto pTypespec = typeCorrespondance.find(str_spec);
400 if (pTypespec == typeCorrespondance.end()){
401 throw std::runtime_error("CoralCrestManager::createAttributeList: typespec not found.");
402 }
403 typespec=pTypespec->second;
404 break;
405 }
406 }
407 std::string strVal = to_string(thisVal);
408 if(strVal.size()>2&& strVal[0]=='"'&& strVal[strVal.size()-1]=='"')
409 strVal=strVal.substr(1,strVal.size()-2);
410
411 if((strVal.compare("NULL")==0||strVal.compare("null")==0)&&
412 (typespec==cool::StorageType::Bool || typespec==cool::StorageType::Int16 || typespec==cool::StorageType::UInt16
413 || typespec==cool::StorageType::Int32 || typespec==cool::StorageType::UInt32
414 || typespec==cool::StorageType::Int64 || typespec==cool::StorageType::UInt63
415 || typespec==cool::StorageType::Float || typespec==cool::StorageType::Double)){
416 att.setNull();
417 continue;
418 }
419 switch (typespec) {
420 case cool::StorageType::Bool:
421 {
422 const bool newVal=(strVal == "true");
423 att.setValue<bool>(newVal);
424 break;
425 }
426 case cool::StorageType::UChar:
427 {
428 const unsigned char newVal=std::stoul(strVal);
429 att.setValue<unsigned char>(newVal);
430 break;
431 }
432 case cool::StorageType::Int16:
433 {
434 const short newVal=std::stol(strVal);
435 att.setValue<short>(newVal);
436 break;
437 }
438 case cool::StorageType::UInt16:
439 {
440 const unsigned short newVal=std::stoul(strVal);
441 att.setValue<unsigned short>(newVal);
442 break;
443 }
444 case cool::StorageType::Int32:
445 {
446 const int newVal=std::stoi(strVal);
447 att.setValue<int>(newVal);
448 break;
449 }
450 case cool::StorageType::UInt32:
451 {
452 const unsigned int newVal=std::stoull(strVal);
453 att.setValue<unsigned int>(newVal);
454 break;
455 }
456 case cool::StorageType::UInt63:
457 {
458 const unsigned long long newVal=std::stoull(strVal);
459 att.setValue<unsigned long long>(newVal);
460 break;
461 }
462 case cool::StorageType::Int64:
463 {
464 const long long newVal=std::stoll(strVal);
465 att.setValue< long long>(newVal);
466 break;
467 }
468 case cool::StorageType::Float:
469 {
470 const float newVal=std::stof(strVal);
471 att.setValue<float>(newVal);
472 break;
473 }
474 case cool::StorageType::Double:
475 {
476 const double newVal=std::stod(strVal);
477 att.setValue<double>(newVal);
478 break;
479 }
480 case cool::StorageType::String255:
481 case cool::StorageType::String4k:
482 case cool::StorageType::String64k:
483 case cool::StorageType::String16M:
484 case cool::StorageType::String128M:
485 {
486 att.setValue<std::string>(thisVal.get<std::string>());
487 break;
488 }
489 case cool::StorageType::Blob128M:
490 case cool::StorageType::Blob16M:
491 case cool::StorageType::Blob64k:
492 {
493 const auto &charVec = CxxUtils::base64_decode(strVal);
494 coral::Blob blob(charVec.size());
495 memcpy(blob.startingAddress(), charVec.data(), charVec.size());
496 att.setValue<coral::Blob>(blob);
497 break;
498 }
499 default:
500 {
501 std::string errorMessage("UNTREATED TYPE! " + std::to_string(typespec));
502 MsgStream gLog(Athena::getMessageSvc(), "CoralCrestManager");
503 gLog << MSG::ERROR << "LoadPayloadForHash:" <<errorMessage<<endmsg;
504 throw std::runtime_error(errorMessage);
505 }
506 }
507 }
508 catch (json::exception& e){
509 MsgStream gLog(Athena::getMessageSvc(), "CoralCrestManager");
510 gLog << MSG::ERROR << "Error CoralCrestManager::createAttributeList: "<<e.what()<<endmsg;
511 throw std::runtime_error(e.what());
512 }
513 }
514 return attr;
515 }
516
517
518
519
const std::regex re(r_e)
#define endmsg
Header for CoralCrestManager class.
static std::string to_string(const std::vector< T > &v)
std::pair< std::vector< unsigned int >, bool > res
if(pathvar)
static std::map< std::string, std::string > getGlobalTagMap(const std::string &crest_path, const std::string &globaltag)
std::vector< std::pair< cool::ValidityKey, std::string > > getIovsForTag(uint64_t since, uint64_t until)
std::vector< std::string > channelIds(cool::ValidityKey since)
IOVDbNamespace::FolderType determineFolderType()
std::vector< uint64_t > loadPayloadForHash(uint64_t since, const std::string &hash)
std::string dumpPayload(cool::ValidityKey since)
Crest::TagDto & getTagDto()
std::vector< coral::AttributeList > getVectorPayload(coral::AttributeListSpecification *pSpec, const std::string &chId)
std::optional< Crest::TagMetaDto > m_TagMeta
std::string getFolderDescription()
coral::AttributeList createAttributeList(coral::AttributeListSpecification *pSpec, nlohmann::json &j, const std::vector< std::pair< std::string, Crest::TypeId > > &tSpec)
std::optional< Crest::CrestContainer > m_crest_cont
std::optional< bool > m_isVectorPayload
std::optional< Crest::TagDto > m_Tag
std::string parseTypeName(const std::string &description)
CoralCrestManager(const std::string &crest_path, const std::string &crestTag)
static const std::string prefix1
std::pair< uint64_t, uint64_t > getIovInterval(const std::string &tag, const uint64_t since, const uint64_t until)
std::string getPayloadSpec()
std::pair< std::vector< cool::ChannelId >, std::vector< std::string > > getChannelList()
coral::AttributeList getPayload(coral::AttributeListSpecification *pSpec, const std::string &chId)
std::pair< uint64_t, uint64_t > getSinceUntilPair(std::vector< uint64_t > &v, const uint64_t since, const uint64_t until)
std::unique_ptr< Crest::CrestApiBase > m_crestCl
Crest::TagInfoDto getTagInfoDto()
coral::AttributeListSpecification * getAttributeListSpec()
static const std::string prefix2
const std::string m_crestTag
void selectIov(cool::ValidityKey since)
It compares timestamp in string format.
void addColumn(const std::string &name, TypeId type)
It adds a column to the payload specification.
void setVectorPayload(bool isVectorPayload)
Set the Vectore mode of the container.
singleton-like access to IMessageSvc via open function and helper
std::string description
glabal timer - how long have I taken so far?
Definition hcg.cxx:93
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:359
IMessageSvc * getMessageSvc(bool quiet=false)
static const std::map< TypeId, std::string > s_typeToString
std::vector< unsigned char > base64_decode(const std::string &)
Definition base64.cxx:97
std::string jsonAttributeList(const coral::AttributeList &atrlist)
Produce a representation of a coral::AttributeList as a json string.
static const std::string s_delimiterJson
json standard delimiter ', '
static const std::string s_closeJson
json close tag, '}'
static const std::string s_openJson
json open tag, '{'