17#include <initializer_list>
52 mthrow(
"Operation only possible if connected to an online ODB");
57 mthrow(
"Please call cm_connect_experiment() before accessing the ODB");
93 std::cout <<
"Delete key " +
name <<
" not found in ODB" << std::endl;
97 std::cout <<
"Delete ODB key " +
name << std::endl;
102 void odb::load(
const std::string &filename,
const std::string &odb_path) {
105 mthrow(
"Cannot connect to ODB");
113 mthrow(
"ODB path " + odb_path +
" not found in ODB");
118 mthrow(
"ODB path " + odb_path +
" does not point to a directory");
126 if (po->
m_data ==
nullptr)
127 mthrow(
"Callback received for a midas::odb object which went out of scope");
129 if (poh ==
nullptr) {
131 mthrow(
"New key \"" +
s +
"\" has been created in ODB after calling watch()");
146 mthrow(
"Cannot create key " + std::string(
name) +
", db_create_key() status = " + std::to_string(
status));
156 m_data[
i].get_odb().set_flags_recursively(f);
166 mthrow(
"ODB key \"" +
str +
"\" not found in ODB");
169 char *buffer = (
char *)malloc(bsize);
175 buffer = (
char *)realloc(buffer, bsize);
180 mthrow(
"Cannot retrieve XML data, status = " + std::to_string(
status));
183 std::cout <<
"Retrieved XML tree for \"" +
str +
"\"" << std::endl;
185 char error[256] =
"";
186 PMXML_NODE tree = mxml_parse_buffer(buffer, error,
sizeof(error), NULL);
188 std::cout <<
"MXML error, buffer =\n" << buffer << std::endl;
189 mthrow(
"MXML error: " + std::string(error));
196 mxml_free_tree(tree);
200 char error[256] =
"";
201 PMXML_NODE tree = mxml_parse_buffer(
str.c_str(), error,
sizeof(error), NULL);
203 std::cout <<
"MXML error, buffer =\n" <<
str << std::endl;
204 mthrow(
"MXML error: " + std::string(error));
208 root = &tree->child[2];
212 mxml_free_tree(tree);
222 std::vector<std::string>
split(std::string input,
char delimiter =
'/') {
223 std::vector<std::string> result;
226 if (!input.empty() && input.front() ==
'/')
228 if (!input.empty() && input.back() ==
'/')
231 std::stringstream ss(input);
233 while (std::getline(ss, item, delimiter)) {
234 result.push_back(item);
242 MJsonNode* node = MJsonNode::Parse(
str.c_str());
243 if (node->GetType() == MJSON_ERROR)
244 mthrow(
"Error parsing JSON: " + node->GetError());
273 new_array[
i].set_string(
"");
285 void odb::get(std::string &s,
bool quotes,
bool refresh) {
337 d.m_num_values =
s.m_num_values;
339 d.m_watch_callback =
s.m_watch_callback;
341 for (
int i = 0;
i <
d.m_num_values;
i++) {
343 d.m_data[
i].set_parent(&
d);
346 d.m_data[
i].set_string(
s.m_data[
i]);
355 d.m_data[
i] =
s.m_data[
i];
356 d.m_data[
i].set_parent(
this);
380 std::size_t
i =
s.find_last_of(
"/");
397 "\" failed with status " + std::to_string(
status));
409 "\" failed with status " + std::to_string(
status));
411 if (
size > old_size) {
412 for (
int i=old_size ;
i<
size ;
i++)
440 s +=
"\"" +
m_name +
"\": {\n";
477 s +=
"\"" +
m_name +
"\": {\n";
494 s +=
"\"" +
m_name +
"/key\": ";
495 s +=
"{ \"type\": " + std::to_string(
m_tid) +
", ";
521 std::string header =
"{\n";
522 header +=
" \"/MIDAS version\" : \"2.1\",\n";
523 header +=
" \"/filename\" : \"" + filename +
"\",\n";
528 header +=
" \"/ODB path\" : \"" + path +
"\",\n\n";
534 std::ofstream f(filename);
536 mthrow(
"Cannot open file \"" + filename);
538 f << header << buffer;
548 std::string first =
str;
550 if (
str.find(
'/') != std::string::npos) {
551 first =
str.substr(0,
str.find(
'/'));
552 tail =
str.substr(
str.find(
'/') + 1);
578 std::cout <<
"Created ODB key \"" +
m_name +
"\"" << std::endl;
580 std::cout <<
"Created ODB key \"" +
get_full_path() +
"\"" << std::endl;
583 if (
m_name.find_last_of(
'/') != std::string::npos)
586 mthrow(
"Invalid key \"" +
m_name +
"\" does not have subkeys");
592 std::string first =
str;
594 if (
str.find(
'/') != std::string::npos) {
595 first =
str.substr(0,
str.find(
'/'));
596 tail =
str.substr(
str.find(
'/') + 1);
636 mthrow(
"get_sub-keys called with invalid m_hKey for ODB key \"" +
m_name +
"\"");
639 std::vector<HNDLE> hlist;
641 for (
int i = 0;;
i++) {
667 mthrow(
"db_get_key for ODB key \"" + path +
668 "\" failed with status " + std::to_string(
status));
673 "\" has different type than specified");
676 std::cout <<
"Get definition for ODB key \"" +
get_full_path() +
"\"" << std::endl;
700 "\" failed with status " + std::to_string(
status));
714 std::vector<std::string>
name;
751 mthrow(
"ODB key \"" + path +
"\" cannot be created");
754 mthrow(
"ODB key \"" + path +
"\" not found after creation");
757 std::cout <<
"Created ODB key \"" + path +
"\"" << std::endl;
759 std::cout <<
"Created ODB key \"" +
get_full_path() +
"\"" << std::endl;
762 mthrow(
"ODB key \"" + path +
"\" cannot be found");
768 mthrow(
"db_get_key for ODB key \"" + path +
769 "\" failed with status " + std::to_string(
status));
779 mthrow(
"db_delete_key for ODB key \"" + path +
780 "\" failed with status " + std::to_string(
status));
783 mthrow(
"ODB key \"" + path +
"\" cannot be created");
786 mthrow(
"ODB key \"" + path +
"\" not found after creation");
788 std::cout <<
"Re-created ODB key \"" +
get_full_path() <<
"\" with different type" << std::endl;
792 "\" has differnt type than specified");
794 std::cout <<
"Validated ODB key \"" +
get_full_path() +
"\"" << std::endl;
808 mthrow(
"ODB key \"" +
m_name +
"\" cannot be pulled because it has been deleted");
822 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
836 std::vector<std::string>
name;
871 void *buffer = malloc(
size);
898 m_data[
i].
set(std::string(
static_cast<const char *
>(p)));
900 m_data[
i].
set(std::string(
static_cast<const char *
>(p)));
902 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
911 "\" failed with status " + std::to_string(
status));
918 get(
s,
false,
false);
922 std::to_string(
m_num_values - 1) +
"]\": [\"" +
s +
"\"]" << std::endl;
925 std::to_string(
m_num_values - 1) +
"]\": [" +
s +
"]" << std::endl;
928 std::cout <<
"Get ODB key \"" +
get_full_path() +
"\": \"" +
s +
"\"" << std::endl;
930 std::cout <<
"Get ODB key \"" +
get_full_path() +
"\": " +
s << std::endl;
961 void *buffer = malloc(
size);
991 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
998 "\" failed with status " + std::to_string(
status));
1004 std::to_string(
index) +
"]\": [\"" +
s +
"\"]" << std::endl;
1007 std::to_string(
index) +
"]\": [" +
s +
"]" << std::endl;
1022 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
1024 }
else if (
m_hKey == 0) {
1029 mthrow(
"Cannot create ODB key \"" + to_create +
"\", status =" + std::to_string(
status));
1032 std::cout <<
"Created ODB key \"" + to_create +
"\"" << std::endl;
1035 if (
m_name.find_last_of(
'/') != std::string::npos)
1038 mthrow(
"Write of un-connected ODB key \"" +
m_name +
"\" not possible");
1054 size =
s.size() + 1;
1057 char *ss = (
char *)malloc(
size+1);
1058 mstrlcpy(ss,
s.c_str(),
size);
1069 std::cout <<
"ODB string size mismatch for \"" <<
get_full_path() <<
1070 "\" (" <<
key.
item_size <<
" vs " << str_size <<
"). ODB key recreated."
1083 std::cout <<
"Set ODB key \"" +
get_full_path() +
"[" + std::to_string(
index) +
"]\" = \"" +
s
1084 +
"\"" << std::endl;
1086 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = \"" +
s +
"\""<< std::endl;
1092 BOOL b =
static_cast<bool>(u);
1101 std::cout <<
"Set ODB key \"" +
get_full_path() +
"[" + std::to_string(
index) +
"]\" = " +
s
1104 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1109 "\" failed with status " + std::to_string(
status));
1117 mthrow(
"ODB key \"" +
m_name +
"\" cannot be written because it has been deleted");
1129 if (m_tid < 1 || m_tid >=
TID_LAST)
1134 "\" is not possible because of invalid key handle");
1154 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
1156 }
else if (
m_hKey == 0) {
1161 mthrow(
"Cannot create ODB key \"" + to_create +
"\", status" + std::to_string(
status));
1164 std::cout <<
"Created ODB key \"" + to_create +
"\"" << std::endl;
1167 if (
m_name.find_last_of(
'/') != std::string::npos)
1170 mthrow(
"Write of un-connected ODB key \"" +
m_name +
"\" not possible");
1183 if ((
int)
d.size() + 1 >
size)
1184 size =
d.size() + 1;
1187 size = (((
size - 1) / 32) + 1) * 32;
1204 get(
s,
true,
false);
1206 "[0..." + std::to_string(
m_num_values - 1) +
"]\" = [" +
s +
"]" << std::endl;
1216 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1220 uint8_t *buffer = (uint8_t *) malloc(
size);
1221 uint8_t *p = buffer;
1239 get(
s,
false,
false);
1242 "]\" = [" +
s +
"]" << std::endl;
1244 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1250 "\" failed with status " + std::to_string(
status));
1259 std::vector<std::string> to_delete;
1261 for (
int i = 0;;
i++) {
1269 std::string full_path = path +
"/" + subKey.
name;
1272 to_delete.push_back(subKey.
name);
1275 std::cout <<
"Deleting " << full_path <<
" as not in list of defaults" << std::endl;
1282 for (
auto name : to_delete) {
1296 retval[path].push_back(
sub.get_name());
1303 if (user_order.find(path) != user_order.end()) {
1304 default_odb.
fix_order(user_order[path]);
1308 if (it.get_tid() ==
TID_KEY) {
1321 std::vector<std::string> curr_order;
1328 if (target_order.size() != curr_order.size() || (
int)target_order.size() !=
m_num_values) {
1333 bool force_order =
false;
1340 if (force_order || curr_order[
i] != target_order[
i]) {
1350 auto curr_it = std::find(curr_order.begin(), curr_order.end(), target_order[
i]);
1352 if (curr_it == curr_order.end()) {
1354 delete[] new_m_data;
1358 int curr_idx = curr_it - curr_order.begin();
1359 new_m_data[
i] =
m_data[curr_idx];
1373 delete[] new_m_data;
1377 void odb::connect(
const std::string &p,
const std::string &
name,
bool write_defaults,
bool delete_keys_not_in_defaults) {
1382 std::string path(p);
1385 mthrow(
"odb::connect() cannot be called with an empty ODB path");
1388 mthrow(
"odb::connect(\"" + path +
"\"): path must start with leading \"/\"");
1390 if (path.back() !=
'/')
1398 bool created =
false;
1400 if (key_exists && delete_keys_not_in_defaults) {
1407 if (!key_exists || write_defaults) {
1408 created =
write_key(path, write_defaults);
1420 }
else if (created || write_defaults) {
1428 void odb::connect(std::string
str,
bool write_defaults,
bool delete_keys_not_in_defaults) {
1431 mthrow(
"odb::connect() cannot be called with an empty ODB path");
1434 mthrow(
"odb::connect(\"" +
str +
"\"): path must start with leading \"/\"");
1437 mthrow(
"odb::connect(\"" +
str +
"\"): root ODB tree is not allowed");
1440 mthrow(
"odb::connect(\"" +
str +
"\"): not allowed in STRING or FILE mode");
1442 if (
str.back() ==
'/')
1448 name =
str.substr(
str.find_last_of(
'/') + 1);
1449 path =
str.substr(0,
str.find_last_of(
'/') + 1);
1451 connect(path,
name, write_defaults, delete_keys_not_in_defaults);
1463 std::map<std::string, std::vector<std::string> > user_order;
1479 mthrow(
"Cannot modify write protected key \"" +
m_name +
"\"");
1486 "\" returnd error code " + std::to_string(
status));
1489 std::cout <<
"Deleted ODB key \"" +
m_name +
"\"" << std::endl;
1513 "\" returnd error code " + std::to_string(
status));
1516 std::cout <<
"Set mode of ODB key \"" +
get_full_path() +
"\" to " << mode << std::endl;
1528 "\" returnd error code " + std::to_string(
status));
1542 "\" returnd error code " + std::to_string(
status));
1550 "\" which is not connected to ODB");
1590 s = (
s ==
"y" ||
s ==
"1") ?
"1" :
"0";
1605 s = (
s ==
"y" ||
s ==
"1") ?
"1" :
"0";
1645 if (
s_odb !=
nullptr)
1653 mthrow(
"File \"" +
str +
"\" not found");
1654 std::ostringstream ss;
1719 s = std::string(
m_bool ?
"true" :
"false");
1730 else if (
m_tid == 0)
1733 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
1862 u_odb::operator uint8_t() {
1864 m_parent_odb->set_last_index(-1);
1865 return get<uint8_t>();
1867 u_odb::operator int8_t() {
1869 m_parent_odb->set_last_index(-1);
1870 return get<int8_t>();
1872 u_odb::operator uint16_t() {
1874 m_parent_odb->set_last_index(-1);
1875 return get<uint16_t>();
1877 u_odb::operator int16_t() {
1879 m_parent_odb->set_last_index(-1);
1880 return get<int16_t>();
1882 u_odb::operator uint32_t() {
1884 m_parent_odb->set_last_index(-1);
1885 return get<uint32_t>();
1887 u_odb::operator int32_t() {
1889 m_parent_odb->set_last_index(-1);
1890 return get<int32_t>();
1892 u_odb::operator uint64_t() {
1894 m_parent_odb->set_last_index(-1);
1895 return get<uint64_t>();
1897 u_odb::operator int64_t() {
1899 m_parent_odb->set_last_index(-1);
1900 return get<int64_t>();
1902 u_odb::operator bool() {
1904 m_parent_odb->set_last_index(-1);
1907 u_odb::operator float() {
1909 m_parent_odb->set_last_index(-1);
1910 return get<float>();
1912 u_odb::operator double() {
1914 m_parent_odb->set_last_index(-1);
1915 return get<double>();
1917 u_odb::operator std::string() {
1919 m_parent_odb->set_last_index(-1);
1924 u_odb::operator
const char *() {
1926 m_parent_odb->set_last_index(-1);
1928 mthrow(
"Only ODB string keys can be converted to \"const char *\"");
1929 return m_string->c_str();
1935 mthrow(
"Only ODB directories can be converted to \"midas::odb &\"");
1954 m_float +=
static_cast<float>(inc);
1958 mthrow(
"Invalid arithmetic operation for ODB key \"" +
1983 mthrow(
"Invalid operation for ODB key \"" +
void set_string_size(std::string s, int size)
std::string get_parent_path()
std::string get_full_path()
void set(std::string str)
static bool exists(const std::string &name)
void write(int str_size=0)
void set_auto_refresh_read(bool f)
void set_auto_enlarge_array(bool f)
void set_odb(odb *o, int i)
void deep_copy(odb &d, const odb &s)
static midas::odb * odb_from_xml(PMXML_NODE node, odb *o)
bool is_auto_refresh_read() const
static void unwatch_all()
static void set_odb_source(odb::odb_source s)
std::function< void(midas::odb &)> m_watch_callback
bool is_auto_create() const
bool is_preserve_string_size() const
static void watch_callback(int hDB, int hKey, int index, void *info)
static midas::odb * odb_from_json(const MJsonNode *node, std::string name, int tid, odb *o)
bool is_auto_refresh_write() const
bool read_key(const std::string &path)
void set_preserve_string_size(bool f)
static thread_local odb_source s_odb_source
void set_trigger_hotlink(bool f)
void set_flags(uint32_t f)
void set_auto_refresh_write(bool f)
void odb_from_xml_remote(const std::string &str)
void set_auto_create(bool f)
static thread_local std::string s_odb_source_str
void odb_from_xml_string(const std::string &str, const std::string &subkey)
void resize_mdata(int size)
odb & get_subkey(std::string str)
void odb_from_json_string(const std::string &str, const std::string &subkey)
void set_flags_recursively(uint32_t f)
unsigned int get_last_written()
void set_name(std::string s)
int get_subkeys(std::vector< std::string > &name)
void watch(std::function< void(midas::odb &)> f)
static thread_local odb * s_odb
void save(const std::string &filename)
void set_last_index(int i)
static int create(const char *name, int type=TID_KEY)
bool is_subkey(std::string str)
void fix_order(std::vector< std::string > target_subkey_order)
void connect_and_fix_structure(std::string path)
static bool s_connected_odb
bool is_trigger_hotlink() const
bool write_key(std::string &path, bool write_defaults)
static midas::odb * search_hkey(midas::odb *po, int hKey)
static bool is_connected_odb()
static void load(const std::string &filename, const std::string &odb_path)
void set_parent(midas::odb *p)
void set_write_protect(bool f)
bool is_write_protect() const
void connect(const std::string &path, const std::string &name, bool write_defaults, bool delete_keys_not_in_defaults=false)
uint8_t operator=(uint8_t v)
void mult(double f, bool push=true)
void set_string(std::string s)
void add(double inc, bool push=true)
void set_string_size(std::string s, int size)
void set_string_ptr(std::string *s)
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
#define DB_INVALID_HANDLE
BOOL equal_ustring(const char *str1, const char *str2)
INT db_get_data_index(HNDLE hDB, HNDLE hKey, void *data, INT *buf_size, INT idx, DWORD type)
INT db_delete_key(HNDLE hDB, HNDLE hKey, BOOL follow_links)
INT db_reorder_key(HNDLE hDB, HNDLE hKey, INT idx)
INT db_get_path(HNDLE hDB, HNDLE hKey, char *path, INT buf_size)
INT db_get_data(HNDLE hDB, HNDLE hKey, void *data, INT *buf_size, DWORD type)
INT db_create_key(HNDLE hDB, HNDLE hKey, const char *key_name, DWORD type)
INT db_copy_xml(HNDLE hDB, HNDLE hKey, char *buffer, int *buffer_size, bool header)
INT db_unwatch(HNDLE hDB, HNDLE hKey)
INT db_set_mode(HNDLE hDB, HNDLE hKey, WORD mode, BOOL recurse)
INT db_get_key(HNDLE hDB, HNDLE hKey, KEY *key)
INT db_load(HNDLE hDB, HNDLE hKeyRoot, const char *filename, BOOL bRemote)
INT db_watch(HNDLE hDB, HNDLE hKey, void(*dispatcher)(INT, INT, INT, void *), void *info)
INT db_set_data(HNDLE hDB, HNDLE hKey, const void *data, INT buf_size, INT num_values, DWORD type)
INT db_set_data1(HNDLE hDB, HNDLE hKey, const void *data, INT buf_size, INT num_values, DWORD type)
INT db_find_key(HNDLE hDB, HNDLE hKey, const char *key_name, HNDLE *subhKey)
INT db_set_data_index1(HNDLE hDB, HNDLE hKey, const void *data, INT data_size, INT idx, DWORD type, BOOL bNotify)
INT db_enum_key(HNDLE hDB, HNDLE hKey, INT idx, HNDLE *subkey_handle)
INT db_set_num_values(HNDLE hDB, HNDLE hKey, INT num_values)
#define write(n, a, f, d)
static std::string indent(int x, const char *p=" ")
std::vector< std::string > split(std::string input, char delimiter='/')
void recurse_fix_order(midas::odb &default_odb, std::map< std::string, std::vector< std::string > > &user_order)
void recurse_get_defaults_order(std::string path, midas::odb &default_odb, std::map< std::string, std::vector< std::string > > &retval)
std::vector< midas::odb * > g_watchlist
void recurse_del_keys_not_in_defaults(std::string path, HNDLE hDB, HNDLE hKey, midas::odb &default_odb)
static double sub(double a, double b)