6#include "CrestApi/CrestCondException.h"
11using json = nlohmann::json;
29 auto a = std::stol(as);
30 auto b = std::stol(bs);
43 if(t.second.compare(
type)==0){
48 throw CommonCrestException(
"The type of parameter is not defined.");
54 [&name](
const auto &column)
55 { return column.first == name; });
59 m_row[name] = nlohmann::json::value_t::null;
70 if (column.first != name)
75 switch (column.second)
78 m_row[name] = va_arg(ap,
int)==1;
81 m_row[name] =
static_cast<unsigned char>(va_arg(ap,
int));
85 m_row[name] = va_arg(ap,
int);
89 m_row[name] = va_arg(ap,
unsigned int);
92 m_row[name] = va_arg(ap, uint64_t);
95 m_row[name] = va_arg(ap, int64_t);
99 m_row[name] = va_arg(ap,
double);
109 m_row[name] = std::string(va_arg(ap,
const char *));
112 throw CommonCrestException(
"Unsupported column type.");
125 nlohmann::json arr_data =
m_isVectorPayload ? nlohmann::json::array() : nlohmann::json();
127 for (
const auto &data_row :
data)
131 m_payload[channel_id] = std::move(arr_data);
142 nlohmann::json row_arr_data = nlohmann::json::array();
145 if (data_row.find(column.first) == data_row.end())
147 std::string
msg =
"The data does not contain the column: " + column.first;
148 throw CommonCrestException(
msg.c_str());
150 row_arr_data.push_back(data_row[column.first]);
178 throw CommonCrestException(
"No IOV in CrestContainer");
184 std::vector<std::string> chs;
186 chs.push_back(
x.key());
205 std::string
msg =
"Channel id " + std::string(channel_id) +
" is not found.";
206 throw CommonCrestException(
msg.c_str());
214 [&name](
const auto &column)
215 { return column.first == name; });
223 throw CommonCrestException(
"The column name is not found.");
236 std::string
msg =
"The payload is not a vector.";
237 throw CommonCrestException(
msg.c_str());
252 std::string
msg =
"The payload is empty.";
253 throw CommonCrestException(
msg.c_str());
266 std::string
msg =
"The iov data is empty.";
267 throw CommonCrestException(
msg.c_str());
294 json pspec_data=json::array();
300 throw CommonCrestException(
"Type do not exist in the map.");
302 j[column.first] = pos->second;
304 pspec_data.push_back(j);
313 for (
const auto &column : j)
315 for (
const auto &[name,
type] : column.items())
323 for (
const auto &[name,
type] : j.items())
349 std::ofstream
file(filename);
352 file << std::setprecision(6) << j.dump(4);
357 std::cerr <<
"CondContainer::dumpJsonToFile: Error opening file: " << filename << std::endl;
358 throw std::runtime_error(
"CondContainer::dumpJsonToFile: Error opening file.");
366 std::ifstream specfile(spec_filename);
367 nlohmann::json jspec;
368 if (specfile.is_open())
375 std::cerr <<
"CondContainer::readJsonFromFile: Error opening file: " << spec_filename << std::endl;
376 throw std::runtime_error(
"CondContainer::readJsonFromFile: Error opening file. ");
381 std::ifstream
file(filename);
390 std::cerr <<
"CondContainer::readJsonFromFile: Error opening file: " << filename << std::endl;
391 throw std::runtime_error(
"CondContainer::readJsonFromFile: Error opening file. ");
398 if (thisVal.is_null()){
399 m_row[colName] =
"NULL";
403 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
404 strVal=strVal.substr(1,strVal.size()-2);
405 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
410 m_row[colName] =
"NULL";
416 const bool newVal=(strVal ==
"true");
417 m_row[colName] = newVal;
422 m_row[colName]=std::stoul(strVal);
427 m_row[colName]=std::stol(strVal);
432 m_row[colName]=std::stoul(strVal);
437 m_row[colName]=std::stoi(strVal);
442 m_row[colName]=std::stoull(strVal);
447 m_row[colName]=std::stoull(strVal);
452 m_row[colName]=std::stoll(strVal);
457 m_row[colName]=std::stof(strVal);
462 m_row[colName]=std::stod(strVal);
471 m_row[colName]=thisVal.get<std::string>();
478 m_row[colName]=thisVal.get<std::string>();
483 throw std::runtime_error(
"UNTREATED TYPE!");
488 catch(json::exception& e){
489 std::cerr << e.what() << std::endl;
490 throw std::runtime_error(e.what());
497 for (
size_t i = 0; i < values.size(); ++i)
508 if(values[i].is_null()){
509 m_row[colName] =
nullptr;
515 m_row[colName] = values[i].get<
bool>();
518 m_row[colName] = values[i].get<
unsigned char>();
522 m_row[colName] = values[i].get<
int>();
526 m_row[colName] = values[i].get<
unsigned int>();
529 m_row[colName] = values[i].get<uint64_t>();
532 m_row[colName] = values[i].get<int64_t>();
536 m_row[colName] = values[i].get<
double>();
543 m_row[colName] = values[i].get<std::string>();
548 m_row[colName] = values[i].get<std::string>();
551 throw CommonCrestException(
"CrestContainer::parseData: Unsupported column type.");
557 if(!values.is_array())
558 std::cerr <<
"CrestContainer::parseData values is not array"<<std::endl;
560 std::cerr <<
"CrestContainer::parseData values number="<<values.size() <<
" m_payload_spec.size()="<<
m_payload_spec.size()<<std::endl;
561 throw CommonCrestException(
"CrestContainer::parseData: Mismatch in number of values.");
568 nlohmann::json j = j_in;
572 ss >> std::quoted(st);
576 if (j.contains(
"data") && j[
"data"].is_object())
581 else if(j.is_object()){
586 std::cerr <<
"CrestContainer::fromJson json:"<<j<<std::endl;
587 throw CommonCrestException(
"CrestContainer::fromJson: JSON is not a JSON object.");
589 std::vector<uint64_t> ret;
590 ret.push_back(since);
599 throw CommonCrestException(
"CrestContainer::fromJson: Unsupported type of payload.");
604 nlohmann::json j = j_in;
608 ss >> std::quoted(st);
611 nlohmann::json dcs_data;
612 if (j.contains(
"data") && j[
"data"].is_object())
614 dcs_data = j[
"data"];
618 std::vector<uint64_t> ret;
621 for (
const auto& [key, value] : dcs_data.items()){
622 uint64_t since=std::stoull(key);
624 ret.push_back(since);
636 for (
const auto &channel : j_in.items())
638 std::string channelKey = channel.key();
639 const auto &data_ch = channel.value();
640 nlohmann::json vecJson(json::value_t::array);
644 vecJson.push_back(data_ch);
646 for (
const auto &values : vecJson)
660 nlohmann::json resources = nlohmann::json::array();
662 nlohmann::json storeDto;
666 storeDto[
"since"] = key;
667 storeDto[
"data"] = value.dump();
668 storeDto[
"streamerInfo"] =
"";
669 resources.push_back(storeDto);
674 nlohmann::json storeDto;
675 nlohmann::json dcs_payload;
679 curPayload.update(value);
680 dcs_payload[std::to_string(key)]=curPayload;
681 if(period>0 && (key-since)>period){
682 storeDto[
"since"]=since;
683 storeDto[
"data"]=dcs_payload.dump();
684 storeDto[
"streamerInfo"] =
"";
685 resources.push_back(storeDto);
690 if(dcs_payload.size()>0){
691 storeDto[
"since"]=since;
692 storeDto[
"data"]=dcs_payload.dump();
693 storeDto[
"streamerInfo"] =
"";
694 resources.push_back(storeDto);
697 j[
"resources"] = resources;
698 j[
"format"] =
"StoreSetDto";
699 j[
"datatype"] =
"data";
700 j[
"size"] = resources.size();
bool compareStrTimestamp(std::string &as, std::string &bs)
Header file for CrestContainer class.
char data[hepevt_bytes_allocation_ATLAS]
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
std::string getJsonPayload()
It returns the json representation of the container.
std::vector< uint64_t > fromJson(uint64_t since, const nlohmann::json &j)
It reads a json object to fill the container.
bool isVectorPayload()
It sets the Vector mode of the container.
int getColumnIndex(const std::string &name)
It returns the index of the column with the given name.
nlohmann::json getStoreSetDto(uint64_t period=-1)
It return StoreSetDto in json format.
void dumpJsonToFile(const nlohmann::json &j, const std::string &filename)
It creates a file with the json representation of the container.
void parseOldFormat(const std::string &colName, const TypeId &typespec, const nlohmann::json &j)
void addIov(const uint64_t since)
It adds an IOV to the json object m_iov_data.
std::string getJsonIovData()
It returns the json representation of the container.
std::vector< std::string > channelIds()
Return list of channel id in string format.
void addRecord(const std::string &name, int number,...)
It adds a record to the payload.
void readCommonType(uint64_t since, const nlohmann::json &j_in)
nlohmann::json getPayloadSpec()
Return payload specification in json format.
void selectIov(const uint64_t since)
It select timestamp as active.
CrestContainer(ModeId mode=ModeId::Standard)
Constructor of CrestContainer.
uint64_t getFirstTime()
It return minimal timestamp in concainer.
void addColumn(const std::string &name, TypeId type)
It adds a column to the payload specification.
const nlohmann::json & getIovData()
Return selected IOV in json format.
void addExternalData(const std::string &channel_id, const nlohmann::json &data)
It associate the payload row to a channel_id.
nlohmann::json m_vector_data
nlohmann::json m_iov_data
const nlohmann::json getPayloadChannel(const std::string &channel_id)
Return payload in json format for selected channel id.
std::vector< std::pair< std::string, TypeId > > m_payload_spec
void parseData(const nlohmann::json &values)
It reads a json object, parse it and fill the container.
void addNullRecord(const std::string &name)
It adds a null value to the payload.
~CrestContainer()
Destructor of CrestContainer.
const std::vector< std::pair< std::string, TypeId > > & getMPayloadSpec()
Return tag specification in vector format.
std::map< uint64_t, nlohmann::json > m_full_data
void flush()
It reinitializes the containers.
nlohmann::json createRowArray(const nlohmann::json &data_row) const
const nlohmann::json & getPayload()
Return current payload in json format.
void setPayloadSpec(const nlohmann::json &j)
std::vector< uint64_t > readDcsFullType(const nlohmann::json &j_in)
const nlohmann::json & getRow()
void setIovData(const nlohmann::json &j)
void clear()
It clear data the container.
void addData(const std::string &channel_id)
It associate the payload row to a channel_id.
void putRow2Vector()
It adds row data to vector.
nlohmann::json readJsonFromFile(const std::string &filename, const std::string &spec_filename)
It reads a json file and returns the json object.
void setPayload(const nlohmann::json &j)
void setVectorPayload(bool isVectorPayload)
Set the Vectore mode of the container.
static const std::map< TypeId, std::string > s_typeToString
std::string number(const double &d, const std::string &s)