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 {
"Bool", cool::StorageType::Bool},
26 {
"UChar",cool::StorageType::UChar},
27 {
"Int16", cool::StorageType::Int16},
28 {
"UInt16", cool::StorageType::UInt16},
29 {
"Int32", cool::StorageType::Int32},
30 {
"UInt32", cool::StorageType::UInt32},
31 {
"UInt63",cool::StorageType::UInt63},
32 {
"Int64", cool::StorageType::Int64},
33 {
"Float", cool::StorageType::Float},
34 {
"Double", cool::StorageType::Double},
35 {
"String255", cool::StorageType::String255},
36 {
"String4k", cool::StorageType::String4k},
37 {
"String64k", cool::StorageType::String64k},
38 {
"String16M", cool::StorageType::String16M},
39 {
"String128M", cool::StorageType::String128M},
40 {
"Blob64k", cool::StorageType::Blob64k},
41 {
"Blob16M", cool::StorageType::Blob16M},
42 {
"Blob128M", cool::StorageType::Blob128M}
46 if(crest_path.length()==0)
49 m_crestCl = std::make_unique<Crest::CrestApi>(Crest::CrestApi(crest_path));
52 m_crestCl = std::make_unique<Crest::CrestApiFs>(Crest::CrestApiFs(true,crest_path));
58 Crest::CrestApiBase* crestCl=NULL;
60 crestCl =
new Crest::CrestApi(crest_path);
63 crestCl =
new Crest::CrestApiFs(
true,crest_path);
65 std::map<std::string, std::string> tagmap;
67 Crest::GlobalTagMapSetDto dto = crestCl->findGlobalTagMap(globaltag,
"Trace");
68 for (
const auto &tagMapDto : dto.getResources()){
69 tagmap[tagMapDto.getLabel()]=tagMapDto.getTagName();
71 }
catch (std::exception & e){
73 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a global tag map for " << globaltag<<
endmsg;
83 std::string regex=R
"delim(<typeName>\s*([^\s]+)\s*</typeName>)delim";
85 std::smatch typeMatch;
87 return (
match) ? std::string(typeMatch[1]) : std::string(
"");
92 std::string folderDescription = info.getFolderDescription();
94 const std::string typeName =
parseTypeName(folderDescription);
95 if (typeName==
"CondAttrListVec"){
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)
128 }
catch (std::exception & e){
130 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": " << e.what() <<
" Cannot get a tag meta info " <<
m_crestTag<<
endmsg;
138 return m_TagMeta.value().getTagInfoDto();
142 if(!
m_Tag.has_value()){
145 return m_Tag.value();
154 return info.getFolderDescription();
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();
165 list.push_back(std::stoll(p.first));
166 names.push_back(p.second);
168 return std::make_pair(std::move(list), std::move(names));
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){
178 spec->extend(p.first,cool::StorageType::storageType(it->second).cppType());
190 if(tag.getObjectType()==
"crest-json-multi-iov")
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());
203 std::pair<uint64_t,uint64_t>
205 uint64_t new_since = 0;
206 uint64_t new_until = 0;
210 gLog << MSG::ERROR <<
"Wrong since/until."<<
endmsg;
211 return std::make_pair(0,0);
213 const std::size_t N = v.size();
216 if(v[i] <= since && since < v[i+1]){
223 if(v[i] < until && until <= v[i+1]){
229 return std::make_pair(new_since,new_until);
232 std::pair<uint64_t,uint64_t>
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);
243 std::vector<std::pair<cool::ValidityKey,std::string>>
res;
244 Crest::IovSetDto dto;
245 if(iovNumber <=1000){
250 uint64_t s_time = ppt.first;
251 uint64_t u_time = ppt.second;
253 if (s_time == 0 && u_time == 0){
260 for (
auto &p : dto.getResources()){
261 res.push_back(std::make_pair((cool::ValidityKey)p.getSince(),p.getPayloadHash()));
272 }
catch (std::exception & e){
274 gLog << MSG::ERROR << __FILE__<<
":"<<__LINE__<<
": "<<e.what()<<
" while trying to find the payload"<<
endmsg;
275 throw std::runtime_error(e.what());
282 }
catch (std::exception & e){
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());
293 std::stringstream
res;
295 std::vector<std::string> chIds =
m_crest_cont.value().channelIds();
298 for (
auto &ch : chIds){
307 for (
const auto & vitr:attr){
318 coral::AttributeList attr=
getPayload(pspec,ch);
324 coral::AttributeList attr=
getPayload(pspec,ch);
325 std::ostringstream os;
326 attr[0].toOutputStream(os);
328 const std::string del(
" : ");
329 const auto separatorPosition =
str.find(del);
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;
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());
const boost::regex re(r_e)
const std::map< std::string, cool::StorageType::TypeId > typeCorrespondance
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, '{'