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"
22#include "GaudiKernel/MsgStream.h"
23#include "GaudiKernel/SystemOfUnits.h"
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 {
"String",cool::StorageType::String128M},
43 {
"Blob64k", cool::StorageType::Blob64k},
44 {
"Blob16M", cool::StorageType::Blob16M},
45 {
"Blob128M", cool::StorageType::Blob128M},
46 {
"Blob", cool::StorageType::Blob128M}
49 const std::string colonDelimiter{
" : "};
52 if(crest_path.length()==0)
55 m_crestCl = std::make_unique<Crest::CrestApi>(Crest::CrestApi(crest_path));
58 m_crestCl = std::make_unique<Crest::CrestApiFs>(Crest::CrestApiFs(false,crest_path));
64 std::unique_ptr<Crest::CrestApiBase> crestCl;
66 crestCl.reset(
new Crest::CrestApi(crest_path));
69 crestCl.reset(
new Crest::CrestApiFs(
true,crest_path));
71 std::map<std::string, std::string> tagmap;
73 Crest::GlobalTagMapSetDto dto = crestCl->findGlobalTagMap(globaltag,
"Trace");
74 for (
const auto &tagMapDto : dto.getResources()){
75 tagmap[tagMapDto.getLabel()]=tagMapDto.getTagName();
77 }
catch (std::exception & e){
79 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a global tag map for " << globaltag<<
endmsg;
85 std::string regex=R
"delim(<typeName>\s*([^\s]+)\s*</typeName>)delim";
87 std::smatch typeMatch;
89 return (
match) ? std::string(typeMatch[1]) : std::string(
"");
94 std::string folderDescription = info.getFolderDescription();
96 const std::string typeName =
parseTypeName(folderDescription);
97 if (typeName==
"CondAttrListVec"){
102 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
103 std::vector< std::pair<std::string,std::string> > chs = info.getChannels().getChannels();
104 for (
auto &p : spec){
105 if(p.first==
"PoolRef" && p.second==
"String4k"){
106 int id=std::stoll(chs[0].first);
107 if(chs.size()==1 &&
id==0)
130 }
catch (std::exception & e){
132 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a tag meta info " <<
m_crestTag<<
endmsg;
140 return m_TagMeta.value().getTagInfoDto();
144 if(!
m_Tag.has_value()){
147 return m_Tag.value();
156 return info.getFolderDescription();
163 std::vector<cool::ChannelId> list;
164 std::vector<std::string> names;
165 std::vector< std::pair<std::string,std::string> >
res = info.getChannels().getChannels();
167 list.push_back(std::stoll(p.first));
168 names.push_back(p.second);
170 return std::make_pair(std::move(list), std::move(names));
175 std::vector< std::pair<std::string,std::string> > spec_vec= info.getPayloadSpec().getColumns();
176 auto * spec =
new coral::AttributeListSpecification();
177 for (
auto &p : spec_vec){
178 auto it = typeCorrespondance.find(p.second);
179 if (it != typeCorrespondance.end()) {
180 spec->extend(p.first,cool::StorageType::storageType(it->second).cppType());
192 if(tag.getObjectType()==
"crest-json-multi-iov")
195 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
196 for (
auto &p : spec){
197 cr_cont.
addColumn(p.first,p.second.c_str());
205 std::pair<uint64_t,uint64_t>
207 uint64_t new_since = 0;
208 uint64_t new_until = 0;
212 gLog << MSG::ERROR <<
"Wrong since/until."<<
endmsg;
213 return std::make_pair(0,0);
215 const std::size_t N = v.size();
218 if(v[i] <= since && since < v[i+1]){
225 if(v[i] < until && until <= v[i+1]){
231 return std::make_pair(new_since,new_until);
234 std::pair<uint64_t,uint64_t>
236 Crest::IovSetDto dto =
m_crestCl->selectGroups(tag, 0, 10000, 0,
"id.since:ASC");
237 std::vector<uint64_t> v = dto.getListSince();
238 v.push_back(cool::ValidityKeyMax);
245 std::vector<std::pair<cool::ValidityKey,std::string>>
res;
246 Crest::IovSetDto dto;
247 if(iovNumber <=1000){
252 uint64_t s_time = ppt.first;
253 uint64_t u_time = ppt.second;
255 if (s_time == 0 && u_time == 0){
262 for (
auto &p : dto.getResources()){
263 res.push_back(std::make_pair((cool::ValidityKey)p.getSince(),p.getPayloadHash()));
274 }
catch (std::exception & e){
276 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": "<<e.what()<<
" while trying to find the payload"<<
endmsg;
277 throw std::runtime_error(e.what());
284 }
catch (std::exception & e){
286 gLog << MSG::ERROR <<
"LoadPayloadForHash:"<<e.what()<<
" while trying to parse the payload. Since="<<since<<
", hash="<<hash<<
endmsg;
287 throw std::runtime_error(e.what());
295 std::stringstream
res;
297 std::vector<std::string> chIds =
m_crest_cont.value().channelIds();
300 for (
auto &ch : chIds){
309 for (
const auto & vitr:attr){
320 coral::AttributeList attr=
getPayload(pspec,ch);
326 coral::AttributeList attr=
getPayload(pspec,ch);
327 std::ostringstream os;
328 attr[0].toOutputStream(os);
330 const auto separatorPosition =
str.find(colonDelimiter);
331 const std::string payloadOnly=
str.substr(separatorPosition+3);
332 res<<
"\""<<payloadOnly<<
"\"";
339 res<<
" a_data_value";
341 if (sep.empty()) sep=
",";
350 nlohmann::json j=
m_crest_cont.value().getPayloadChannel(chId.c_str());
355 std::vector<coral::AttributeList>
res;
356 nlohmann::json vecJ=
m_crest_cont.value().getPayloadChannel(chId.c_str());
357 for (
auto &p : vecJ){
374 coral::AttributeList attr(*pSpec,
true);
375 unsigned int s=attr.size();
377 json::const_iterator it = j.begin();
378 for (
unsigned int i(0);i!=s;++i){
381 auto & att =
const_cast<coral::Attribute&
>(attr[i]);
385 const auto thisVal = it.value();
389 if (thisVal.is_null()){
393 cool::StorageType::TypeId typespec=cool::StorageType::Bool;
394 for(
auto &p : tSpec){
395 if(p.first.compare(att.specification().name())==0){
398 throw std::runtime_error(
"CoralCrestManager::createAttributeList: name not found.");
400 std::string str_spec = pElement ->second;
401 auto pTypespec = typeCorrespondance.find(str_spec);
402 if (pTypespec == typeCorrespondance.end()){
403 throw std::runtime_error(
"CoralCrestManager::createAttributeList: typespec not found.");
405 typespec=pTypespec->second;
410 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
411 strVal=strVal.substr(1,strVal.size()-2);
413 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
414 (typespec==cool::StorageType::Bool || typespec==cool::StorageType::Int16 || typespec==cool::StorageType::UInt16
415 || typespec==cool::StorageType::Int32 || typespec==cool::StorageType::UInt32
416 || typespec==cool::StorageType::Int64 || typespec==cool::StorageType::UInt63
417 || typespec==cool::StorageType::Float || typespec==cool::StorageType::Double)){
422 case cool::StorageType::Bool:
424 const bool newVal=(strVal ==
"true");
425 att.setValue<
bool>(newVal);
428 case cool::StorageType::UChar:
430 const unsigned char newVal=std::stoul(strVal);
431 att.setValue<
unsigned char>(newVal);
434 case cool::StorageType::Int16:
436 const short newVal=std::stol(strVal);
437 att.setValue<
short>(newVal);
440 case cool::StorageType::UInt16:
442 const unsigned short newVal=std::stoul(strVal);
443 att.setValue<
unsigned short>(newVal);
446 case cool::StorageType::Int32:
448 const int newVal=std::stoi(strVal);
449 att.setValue<
int>(newVal);
452 case cool::StorageType::UInt32:
454 const unsigned int newVal=std::stoull(strVal);
455 att.setValue<
unsigned int>(newVal);
458 case cool::StorageType::UInt63:
460 const unsigned long long newVal=std::stoull(strVal);
461 att.setValue<
unsigned long long>(newVal);
464 case cool::StorageType::Int64:
466 const long long newVal=std::stoll(strVal);
467 att.setValue<
long long>(newVal);
470 case cool::StorageType::Float:
472 const float newVal=std::stof(strVal);
473 att.setValue<
float>(newVal);
476 case cool::StorageType::Double:
478 const double newVal=std::stod(strVal);
479 att.setValue<
double>(newVal);
482 case cool::StorageType::String255:
483 case cool::StorageType::String4k:
484 case cool::StorageType::String64k:
485 case cool::StorageType::String16M:
486 case cool::StorageType::String128M:
488 att.setValue<std::string>(thisVal.get<std::string>());
491 case cool::StorageType::Blob128M:
492 case cool::StorageType::Blob16M:
493 case cool::StorageType::Blob64k:
496 coral::Blob blob(charVec.size());
497 memcpy(blob.startingAddress(), charVec.data(), charVec.size());
498 att.setValue<coral::Blob>(blob);
503 std::string errorMessage(
"UNTREATED TYPE! " + std::to_string(typespec));
505 gLog << MSG::ERROR <<
"LoadPayloadForHash:" <<errorMessage<<
endmsg;
506 throw std::runtime_error(errorMessage);
510 catch (json::exception& e){
512 gLog << MSG::ERROR <<
"Error CoralCrestManager::createAttributeList: "<<e.what()<<
endmsg;
513 throw std::runtime_error(e.what());
Header for CoralCrestManager class.
std::pair< std::vector< unsigned int >, bool > res
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)
void initCrestContainer()
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?
bool match(std::string s1, std::string s2)
match the individual directories of two strings
IMessageSvc * getMessageSvc(bool quiet=false)
static const std::map< TypeId, std::string > s_typeToString
std::vector< unsigned char > base64_decode(const std::string &)
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, '{'