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 {
"Blob64k", cool::StorageType::Blob64k},
43 {
"Blob16M", cool::StorageType::Blob16M},
44 {
"Blob128M", cool::StorageType::Blob128M},
45 {
"Blob", cool::StorageType::Blob128M}
48 const std::string colonDelimiter{
" : "};
51 if(crest_path.length()==0)
54 m_crestCl = std::make_unique<Crest::CrestApi>(Crest::CrestApi(crest_path));
57 m_crestCl = std::make_unique<Crest::CrestApiFs>(Crest::CrestApiFs(false,crest_path));
63 std::unique_ptr<Crest::CrestApiBase> crestCl;
65 crestCl.reset(
new Crest::CrestApi(crest_path));
68 crestCl.reset(
new Crest::CrestApiFs(
true,crest_path));
70 std::map<std::string, std::string> tagmap;
72 Crest::GlobalTagMapSetDto dto = crestCl->findGlobalTagMap(globaltag,
"Trace");
73 for (
const auto &tagMapDto : dto.getResources()){
74 tagmap[tagMapDto.getLabel()]=tagMapDto.getTagName();
76 }
catch (std::exception & e){
78 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a global tag map for " << globaltag<<
endmsg;
84 std::string regex=R
"delim(<typeName>\s*([^\s]+)\s*</typeName>)delim";
86 std::smatch typeMatch;
88 return (
match) ? std::string(typeMatch[1]) : std::string(
"");
93 std::string folderDescription = info.getFolderDescription();
95 const std::string typeName =
parseTypeName(folderDescription);
96 if (typeName==
"CondAttrListVec"){
101 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
102 std::vector< std::pair<std::string,std::string> > chs = info.getChannels().getChannels();
103 for (
auto &p : spec){
104 if(p.first==
"PoolRef" && p.second==
"String4k"){
105 int id=std::stoll(chs[0].first);
106 if(chs.size()==1 &&
id==0)
129 }
catch (std::exception & e){
131 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a tag meta info " <<
m_crestTag<<
endmsg;
139 return m_TagMeta.value().getTagInfoDto();
143 if(!
m_Tag.has_value()){
146 return m_Tag.value();
155 return info.getFolderDescription();
162 std::vector<cool::ChannelId> list;
163 std::vector<std::string> names;
164 std::vector< std::pair<std::string,std::string> >
res = info.getChannels().getChannels();
166 list.push_back(std::stoll(p.first));
167 names.push_back(p.second);
169 return std::make_pair(std::move(list), std::move(names));
174 std::vector< std::pair<std::string,std::string> > spec_vec= info.getPayloadSpec().getColumns();
175 auto * spec =
new coral::AttributeListSpecification();
176 for (
auto &p : spec_vec){
177 auto it = typeCorrespondance.find(p.second);
178 if (it != typeCorrespondance.end()) {
179 spec->extend(p.first,cool::StorageType::storageType(it->second).cppType());
191 if(tag.getObjectType()==
"crest-json-multi-iov")
194 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
195 for (
auto &p : spec){
196 cr_cont.
addColumn(p.first,p.second.c_str());
204 std::pair<uint64_t,uint64_t>
206 uint64_t new_since = 0;
207 uint64_t new_until = 0;
211 gLog << MSG::ERROR <<
"Wrong since/until."<<
endmsg;
212 return std::make_pair(0,0);
214 const std::size_t N = v.size();
217 if(v[i] <= since && since < v[i+1]){
224 if(v[i] < until && until <= v[i+1]){
230 return std::make_pair(new_since,new_until);
233 std::pair<uint64_t,uint64_t>
235 Crest::IovSetDto dto =
m_crestCl->selectGroups(tag, 0, 10000, 0,
"id.since:ASC");
236 std::vector<uint64_t> v = dto.getListSince();
237 v.push_back(cool::ValidityKeyMax);
244 std::vector<std::pair<cool::ValidityKey,std::string>>
res;
245 Crest::IovSetDto dto;
246 if(iovNumber <=1000){
251 uint64_t s_time = ppt.first;
252 uint64_t u_time = ppt.second;
254 if (s_time == 0 && u_time == 0){
261 for (
auto &p : dto.getResources()){
262 res.push_back(std::make_pair((cool::ValidityKey)p.getSince(),p.getPayloadHash()));
273 }
catch (std::exception & e){
275 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": "<<e.what()<<
" while trying to find the payload"<<
endmsg;
276 throw std::runtime_error(e.what());
283 }
catch (std::exception & e){
285 gLog << MSG::ERROR <<
"LoadPayloadForHash:"<<e.what()<<
" while trying to parse the payload. Since="<<since<<
", hash="<<hash<<
endmsg;
286 throw std::runtime_error(e.what());
294 std::stringstream
res;
296 std::vector<std::string> chIds =
m_crest_cont.value().channelIds();
299 for (
auto &ch : chIds){
308 for (
const auto & vitr:attr){
319 coral::AttributeList attr=
getPayload(pspec,ch);
325 coral::AttributeList attr=
getPayload(pspec,ch);
326 std::ostringstream os;
327 attr[0].toOutputStream(os);
329 const auto separatorPosition =
str.find(colonDelimiter);
330 const std::string payloadOnly=
str.substr(separatorPosition+3);
331 res<<
"\""<<payloadOnly<<
"\"";
338 res<<
" a_data_value";
340 if (sep.empty()) sep=
",";
349 nlohmann::json j=
m_crest_cont.value().getPayloadChannel(chId.c_str());
354 std::vector<coral::AttributeList>
res;
355 nlohmann::json vecJ=
m_crest_cont.value().getPayloadChannel(chId.c_str());
356 for (
auto &p : vecJ){
373 coral::AttributeList attr(*pSpec,
true);
374 unsigned int s=attr.size();
376 json::const_iterator it = j.begin();
377 for (
unsigned int i(0);i!=s;++i){
380 auto & att =
const_cast<coral::Attribute&
>(attr[i]);
384 const auto thisVal = it.value();
388 if (thisVal.is_null()){
392 cool::StorageType::TypeId typespec=cool::StorageType::Bool;
393 for(
auto &p : tSpec){
394 if(p.first.compare(att.specification().name())==0){
397 throw std::runtime_error(
"CoralCrestManager::createAttributeList: name not found.");
399 std::string str_spec = pElement ->second;
400 auto pTypespec = typeCorrespondance.find(str_spec);
401 if (pTypespec == typeCorrespondance.end()){
402 throw std::runtime_error(
"CoralCrestManager::createAttributeList: typespec not found.");
404 typespec=pTypespec->second;
409 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
410 strVal=strVal.substr(1,strVal.size()-2);
412 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
413 (typespec==cool::StorageType::Bool || typespec==cool::StorageType::Int16 || typespec==cool::StorageType::UInt16
414 || typespec==cool::StorageType::Int32 || typespec==cool::StorageType::UInt32
415 || typespec==cool::StorageType::Int64 || typespec==cool::StorageType::UInt63
416 || typespec==cool::StorageType::Float || typespec==cool::StorageType::Double)){
421 case cool::StorageType::Bool:
423 const bool newVal=(strVal ==
"true");
424 att.setValue<
bool>(newVal);
427 case cool::StorageType::UChar:
429 const unsigned char newVal=std::stoul(strVal);
430 att.setValue<
unsigned char>(newVal);
433 case cool::StorageType::Int16:
435 const short newVal=std::stol(strVal);
436 att.setValue<
short>(newVal);
439 case cool::StorageType::UInt16:
441 const unsigned short newVal=std::stoul(strVal);
442 att.setValue<
unsigned short>(newVal);
445 case cool::StorageType::Int32:
447 const int newVal=std::stoi(strVal);
448 att.setValue<
int>(newVal);
451 case cool::StorageType::UInt32:
453 const unsigned int newVal=std::stoull(strVal);
454 att.setValue<
unsigned int>(newVal);
457 case cool::StorageType::UInt63:
459 const unsigned long long newVal=std::stoull(strVal);
460 att.setValue<
unsigned long long>(newVal);
463 case cool::StorageType::Int64:
465 const long long newVal=std::stoll(strVal);
466 att.setValue<
long long>(newVal);
469 case cool::StorageType::Float:
471 const float newVal=std::stof(strVal);
472 att.setValue<
float>(newVal);
475 case cool::StorageType::Double:
477 const double newVal=std::stod(strVal);
478 att.setValue<
double>(newVal);
481 case cool::StorageType::String255:
482 case cool::StorageType::String4k:
483 case cool::StorageType::String64k:
484 case cool::StorageType::String16M:
485 case cool::StorageType::String128M:
487 att.setValue<std::string>(thisVal.get<std::string>());
490 case cool::StorageType::Blob128M:
491 case cool::StorageType::Blob16M:
492 case cool::StorageType::Blob64k:
495 coral::Blob blob(charVec.size());
496 memcpy(blob.startingAddress(), charVec.data(), charVec.size());
497 att.setValue<coral::Blob>(blob);
502 std::string errorMessage(
"UNTREATED TYPE! " + std::to_string(typespec));
504 gLog << MSG::ERROR <<
"LoadPayloadForHash:" <<errorMessage<<
endmsg;
505 throw std::runtime_error(errorMessage);
509 catch (json::exception& e){
511 gLog << MSG::ERROR <<
"Error CoralCrestManager::createAttributeList: "<<e.what()<<
endmsg;
512 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, '{'