10#include "CrestApi/CrestApi.h"
11#include "CrestApi/CrestApiFs.h"
12#include "CoralBase/AttributeList.h"
13#include "CoralBase/Attribute.h"
14#include "CoolKernel/StorageType.h"
15#include "CoolKernel/RecordSpecification.h"
16#include "CoolKernel/Record.h"
21#include "GaudiKernel/MsgStream.h"
22#include "GaudiKernel/SystemOfUnits.h"
25 const std::map<std::string, cool::StorageType::TypeId> typeCorrespondance={
26 {
"Bool", cool::StorageType::Bool},
27 {
"UChar",cool::StorageType::UChar},
28 {
"Int16", cool::StorageType::Int16},
29 {
"UInt16", cool::StorageType::UInt16},
30 {
"Int32", cool::StorageType::Int32},
31 {
"UInt32", cool::StorageType::UInt32},
32 {
"UInt63",cool::StorageType::UInt63},
33 {
"Int64", cool::StorageType::Int64},
34 {
"Float", cool::StorageType::Float},
35 {
"Double", cool::StorageType::Double},
36 {
"String255", cool::StorageType::String255},
37 {
"String4k", cool::StorageType::String4k},
38 {
"String64k", cool::StorageType::String64k},
39 {
"String16M", cool::StorageType::String16M},
40 {
"String128M", cool::StorageType::String128M},
41 {
"Blob64k", cool::StorageType::Blob64k},
42 {
"Blob16M", cool::StorageType::Blob16M},
43 {
"Blob128M", cool::StorageType::Blob128M}
46 const std::string colonDelimiter{
" : "};
49 if(crest_path.length()==0)
52 m_crestCl = std::make_unique<Crest::CrestApi>(Crest::CrestApi(crest_path));
55 m_crestCl = std::make_unique<Crest::CrestApiFs>(Crest::CrestApiFs(true,crest_path));
61 Crest::CrestApiBase* crestCl=NULL;
63 crestCl =
new Crest::CrestApi(crest_path);
66 crestCl =
new Crest::CrestApiFs(
true,crest_path);
68 std::map<std::string, std::string> tagmap;
70 Crest::GlobalTagMapSetDto dto = crestCl->findGlobalTagMap(globaltag,
"Trace");
71 for (
const auto &tagMapDto : dto.getResources()){
72 tagmap[tagMapDto.getLabel()]=tagMapDto.getTagName();
74 }
catch (std::exception & e){
76 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a global tag map for " << globaltag<<
endmsg;
86 std::string regex=R
"delim(<typeName>\s*([^\s]+)\s*</typeName>)delim";
88 std::smatch typeMatch;
90 return (
match) ? std::string(typeMatch[1]) : std::string(
"");
95 std::string folderDescription = info.getFolderDescription();
97 const std::string typeName =
parseTypeName(folderDescription);
98 if (typeName==
"CondAttrListVec"){
103 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
104 std::vector< std::pair<std::string,std::string> > chs = info.getChannels().getChannels();
105 for (
auto &p : spec){
106 if(p.first==
"PoolRef" && p.second==
"String4k"){
107 int id=std::stoll(chs[0].first);
108 if(chs.size()==1 &&
id==0)
131 }
catch (std::exception & e){
133 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a tag meta info " <<
m_crestTag<<
endmsg;
141 return m_TagMeta.value().getTagInfoDto();
145 if(!
m_Tag.has_value()){
148 return m_Tag.value();
157 return info.getFolderDescription();
164 std::vector<cool::ChannelId> list;
165 std::vector<std::string> names;
166 std::vector< std::pair<std::string,std::string> >
res = info.getChannels().getChannels();
168 list.push_back(std::stoll(p.first));
169 names.push_back(p.second);
171 return std::make_pair(std::move(list), std::move(names));
176 std::vector< std::pair<std::string,std::string> > spec_vec= info.getPayloadSpec().getColumns();
177 auto * spec =
new coral::AttributeListSpecification();
178 for (
auto &p : spec_vec){
179 auto it = typeCorrespondance.find(p.second);
180 if (it != typeCorrespondance.end()) {
181 spec->extend(p.first,cool::StorageType::storageType(it->second).cppType());
193 if(tag.getObjectType()==
"crest-json-multi-iov")
196 std::vector< std::pair<std::string,std::string> > spec= info.getPayloadSpec().getColumns();
197 for (
auto &p : spec){
198 cr_cont.
addColumn(p.first,p.second.c_str());
206 std::pair<uint64_t,uint64_t>
208 uint64_t new_since = 0;
209 uint64_t new_until = 0;
213 gLog << MSG::ERROR <<
"Wrong since/until."<<
endmsg;
214 return std::make_pair(0,0);
216 const std::size_t N = v.size();
219 if(v[i] <= since && since < v[i+1]){
226 if(v[i] < until && until <= v[i+1]){
232 return std::make_pair(new_since,new_until);
235 std::pair<uint64_t,uint64_t>
237 Crest::IovSetDto dto =
m_crestCl->selectGroups(tag, 0, 10000, 0,
"id.since:ASC");
238 std::vector<uint64_t> v = dto.getListSince();
239 v.push_back(cool::ValidityKeyMax);
246 std::vector<std::pair<cool::ValidityKey,std::string>>
res;
247 Crest::IovSetDto dto;
248 if(iovNumber <=1000){
253 uint64_t s_time = ppt.first;
254 uint64_t u_time = ppt.second;
256 if (s_time == 0 && u_time == 0){
263 for (
auto &p : dto.getResources()){
264 res.push_back(std::make_pair((cool::ValidityKey)p.getSince(),p.getPayloadHash()));
275 }
catch (std::exception & e){
277 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": "<<e.what()<<
" while trying to find the payload"<<
endmsg;
278 throw std::runtime_error(e.what());
285 }
catch (std::exception & e){
287 gLog << MSG::ERROR <<
"LoadPayloadForHash:"<<e.what()<<
" while trying to parse the payload. Since="<<since<<
", hash="<<hash<<
endmsg;
288 throw std::runtime_error(e.what());
296 std::stringstream
res;
298 std::vector<std::string> chIds =
m_crest_cont.value().channelIds();
301 for (
auto &ch : chIds){
310 for (
const auto & vitr:attr){
321 coral::AttributeList attr=
getPayload(pspec,ch);
327 coral::AttributeList attr=
getPayload(pspec,ch);
328 std::ostringstream os;
329 attr[0].toOutputStream(os);
331 const auto separatorPosition =
str.find(colonDelimiter);
332 const std::string payloadOnly=
str.substr(separatorPosition+3);
333 res<<
"\""<<payloadOnly<<
"\"";
340 res<<
" a_data_value";
342 if (sep.empty()) sep=
",";
351 nlohmann::json j=
m_crest_cont.value().getPayloadChannel(chId.c_str());
356 std::vector<coral::AttributeList>
res;
357 nlohmann::json vecJ=
m_crest_cont.value().getPayloadChannel(chId.c_str());
358 for (
auto &p : vecJ){
375 coral::AttributeList attr(*pSpec,
true);
376 unsigned int s=attr.size();
378 json::const_iterator it = j.begin();
379 for (
unsigned int i(0);i!=s;++i){
382 auto & att =
const_cast<coral::Attribute&
>(attr[i]);
386 const auto thisVal = it.value();
390 if (thisVal.is_null()){
394 cool::StorageType::TypeId typespec=cool::StorageType::Bool;
395 for(
auto &p : tSpec){
396 if(p.first.compare(att.specification().name())==0){
399 throw std::runtime_error(
"CoralCrestManager::createAttributeList: name not found.");
401 std::string str_spec = pElement ->second;
402 auto pTypespec = typeCorrespondance.find(str_spec);
403 if (pTypespec == typeCorrespondance.end()){
404 throw std::runtime_error(
"CoralCrestManager::createAttributeList: typespec not found.");
406 typespec=pTypespec->second;
411 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
412 strVal=strVal.substr(1,strVal.size()-2);
414 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
415 (typespec==cool::StorageType::Bool || typespec==cool::StorageType::Int16 || typespec==cool::StorageType::UInt16
416 || typespec==cool::StorageType::Int32 || typespec==cool::StorageType::UInt32
417 || typespec==cool::StorageType::Int64 || typespec==cool::StorageType::UInt63
418 || typespec==cool::StorageType::Float || typespec==cool::StorageType::Double)){
423 case cool::StorageType::Bool:
425 const bool newVal=(strVal ==
"true");
426 att.setValue<
bool>(newVal);
429 case cool::StorageType::UChar:
431 const unsigned char newVal=std::stoul(strVal);
432 att.setValue<
unsigned char>(newVal);
435 case cool::StorageType::Int16:
437 const short newVal=std::stol(strVal);
438 att.setValue<
short>(newVal);
441 case cool::StorageType::UInt16:
443 const unsigned short newVal=std::stoul(strVal);
444 att.setValue<
unsigned short>(newVal);
447 case cool::StorageType::Int32:
449 const int newVal=std::stoi(strVal);
450 att.setValue<
int>(newVal);
453 case cool::StorageType::UInt32:
455 const unsigned int newVal=std::stoull(strVal);
456 att.setValue<
unsigned int>(newVal);
459 case cool::StorageType::UInt63:
461 const unsigned long long newVal=std::stoull(strVal);
462 att.setValue<
unsigned long long>(newVal);
465 case cool::StorageType::Int64:
467 const long long newVal=std::stoll(strVal);
468 att.setValue<
long long>(newVal);
471 case cool::StorageType::Float:
473 const float newVal=std::stof(strVal);
474 att.setValue<
float>(newVal);
477 case cool::StorageType::Double:
479 const double newVal=std::stod(strVal);
480 att.setValue<
double>(newVal);
483 case cool::StorageType::String255:
484 case cool::StorageType::String4k:
485 case cool::StorageType::String64k:
486 case cool::StorageType::String16M:
487 case cool::StorageType::String128M:
489 att.setValue<std::string>(thisVal.get<std::string>());
492 case cool::StorageType::Blob128M:
493 case cool::StorageType::Blob16M:
494 case cool::StorageType::Blob64k:
497 coral::Blob blob(charVec.size());
498 memcpy(blob.startingAddress(), charVec.data(), charVec.size());
499 att.setValue<coral::Blob>(blob);
504 std::string errorMessage(
"UNTREATED TYPE! " + std::to_string(typespec));
506 gLog << MSG::ERROR <<
"LoadPayloadForHash:" <<errorMessage<<
endmsg;
507 throw std::runtime_error(errorMessage);
511 catch (json::exception& e){
513 gLog << MSG::ERROR <<
"Error CoralCrestManager::createAttributeList: "<<e.what()<<
endmsg;
514 throw std::runtime_error(e.what());
const boost::regex re(r_e)
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, '{'