17#include <initializer_list>
51 mthrow(
"Operation only possible if connected to an online ODB");
56 mthrow(
"Please call cm_connect_experiment() before accessing the ODB");
65 for (
int i = 0;
i <
po->m_num_values;
i++) {
92 std::cout <<
"Delete key " +
name <<
" not found in ODB" << std::endl;
96 std::cout <<
"Delete ODB key " +
name << std::endl;
101 void odb::load(
const std::string &filename,
const std::string &odb_path) {
104 mthrow(
"Cannot connect to ODB");
112 mthrow(
"ODB path " + odb_path +
" not found in ODB");
117 mthrow(
"ODB path " + odb_path +
" does not point to a directory");
125 if (
po->m_data ==
nullptr)
126 mthrow(
"Callback received for a midas::odb object which went out of scope");
128 if (
poh ==
nullptr) {
130 mthrow(
"New key \"" +
s +
"\" has been created in ODB after calling watch()");
134 po->m_watch_callback(*
poh);
135 poh->m_last_index = -1;
145 mthrow(
"Cannot create key " + std::string(
name) +
", db_create_key() status = " + std::to_string(
status));
155 m_data[
i].get_odb().set_flags_recursively(f);
165 mthrow(
"ODB key \"" +
str +
"\" not found in ODB");
179 mthrow(
"Cannot retrieve XML data, status = " + std::to_string(
status));
182 std::cout <<
"Retrieved XML tree for \"" +
str +
"\"" << std::endl;
184 char error[256] =
"";
187 std::cout <<
"MXML error, buffer =\n" << buffer << std::endl;
188 mthrow(
"MXML error: " + std::string(error));
199 char error[256] =
"";
202 std::cout <<
"MXML error, buffer =\n" <<
str << std::endl;
203 mthrow(
"MXML error: " + std::string(error));
212 std::string dir,
xpath;
213 while (std::getline(
ss, dir,
'/'))
215 xpath +=
"/dir[@name=\"" + dir +
"\"]";
219 if (
root ==
nullptr) {
221 size_t pos =
xpath.rfind(
"dir");
222 if (pos != std::string::npos)
223 xpath.replace(pos, 3,
"key");
243 MJsonNode*
node = MJsonNode::Parse(
str.c_str());
320 pc->set_parent(
this);
333 d.m_num_values =
s.m_num_values;
335 d.m_watch_callback =
s.m_watch_callback;
337 for (
int i = 0;
i <
d.m_num_values;
i++) {
339 d.m_data[
i].set_parent(&
d);
342 d.m_data[
i].set_string(
s.m_data[
i]);
351 d.m_data[
i] =
s.m_data[
i];
352 d.m_data[
i].set_parent(
this);
376 std::size_t
i =
s.find_last_of(
"/");
393 "\" failed with status " + std::to_string(
status));
405 "\" failed with status " + std::to_string(
status));
436 s +=
"\"" +
m_name +
"\": {\n";
469 s +=
"\"" +
m_name +
"\": {\n";
485 s +=
"\"" +
m_name +
"/key\": ";
486 s +=
"{ \"type\": " + std::to_string(
m_tid) +
", ";
506 std::string header =
"{\n";
507 header +=
" \"/MIDAS version\" : \"2.1\",\n";
508 header +=
" \"/filename\" : \"" + filename +
"\",\n";
509 header +=
" \"/ODB path\" : \"" +
get_full_path() +
"\",\n\n";
515 std::ofstream f(filename);
517 mthrow(
"Cannot open file \"" + filename);
519 f << header << buffer;
531 if (
str.find(
'/') != std::string::npos) {
559 std::cout <<
"Created ODB key \"" +
m_name +
"\"" << std::endl;
561 std::cout <<
"Created ODB key \"" +
get_full_path() +
"\"" << std::endl;
564 if (
m_name.find_last_of(
'/') != std::string::npos)
567 mthrow(
"Invalid key \"" +
m_name +
"\" does not have subkeys");
575 if (
str.find(
'/') != std::string::npos) {
617 mthrow(
"get_sub-keys called with invalid m_hKey for ODB key \"" +
m_name +
"\"");
620 std::vector<HNDLE>
hlist;
622 for (
int i = 0;;
i++) {
648 mthrow(
"db_get_key for ODB key \"" + path +
649 "\" failed with status " + std::to_string(
status));
654 "\" has different type than specified");
657 std::cout <<
"Get definition for ODB key \"" +
get_full_path() +
"\"" << std::endl;
681 "\" failed with status " + std::to_string(
status));
695 std::vector<std::string>
name;
732 mthrow(
"ODB key \"" + path +
"\" cannot be created");
735 mthrow(
"ODB key \"" + path +
"\" not found after creation");
738 std::cout <<
"Created ODB key \"" + path +
"\"" << std::endl;
740 std::cout <<
"Created ODB key \"" +
get_full_path() +
"\"" << std::endl;
743 mthrow(
"ODB key \"" + path +
"\" cannot be found");
749 mthrow(
"db_get_key for ODB key \"" + path +
750 "\" failed with status " + std::to_string(
status));
760 mthrow(
"db_delete_key for ODB key \"" + path +
761 "\" failed with status " + std::to_string(
status));
764 mthrow(
"ODB key \"" + path +
"\" cannot be created");
767 mthrow(
"ODB key \"" + path +
"\" not found after creation");
769 std::cout <<
"Re-created ODB key \"" +
get_full_path() <<
"\" with different type" << std::endl;
773 "\" has differnt type than specified");
775 std::cout <<
"Validated ODB key \"" +
get_full_path() +
"\"" << std::endl;
789 mthrow(
"ODB key \"" +
m_name +
"\" cannot be pulled because it has been deleted");
803 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
817 std::vector<std::string>
name;
879 m_data[
i].
set(std::string(
static_cast<const char *
>(p)));
881 m_data[
i].
set(std::string(
static_cast<const char *
>(p)));
883 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
892 "\" failed with status " + std::to_string(
status));
899 get(
s,
false,
false);
903 std::to_string(
m_num_values - 1) +
"]\": [\"" +
s +
"\"]" << std::endl;
906 std::to_string(
m_num_values - 1) +
"]\": [" +
s +
"]" << std::endl;
909 std::cout <<
"Get ODB key \"" +
get_full_path() +
"\": \"" +
s +
"\"" << std::endl;
911 std::cout <<
"Get ODB key \"" +
get_full_path() +
"\": " +
s << std::endl;
972 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
979 "\" failed with status " + std::to_string(
status));
985 std::to_string(
index) +
"]\": [\"" +
s +
"\"]" << std::endl;
988 std::to_string(
index) +
"]\": [" +
s +
"]" << std::endl;
1003 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
1005 }
else if (
m_hKey == 0) {
1013 std::cout <<
"Created ODB key \"" +
to_create +
"\"" << std::endl;
1016 if (
m_name.find_last_of(
'/') != std::string::npos)
1019 mthrow(
"Write of un-connected ODB key \"" +
m_name +
"\" not possible");
1035 size =
s.size() + 1;
1050 std::cout <<
"ODB string size mismatch for \"" <<
get_full_path() <<
1064 std::cout <<
"Set ODB key \"" +
get_full_path() +
"[" + std::to_string(
index) +
"]\" = \"" +
s
1065 +
"\"" << std::endl;
1067 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = \"" +
s +
"\""<< std::endl;
1073 BOOL b =
static_cast<bool>(
u);
1082 std::cout <<
"Set ODB key \"" +
get_full_path() +
"[" + std::to_string(
index) +
"]\" = " +
s
1085 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1090 "\" failed with status " + std::to_string(
status));
1098 mthrow(
"ODB key \"" +
m_name +
"\" cannot be written because it has been deleted");
1115 "\" is not possible because of invalid key handle");
1135 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
1137 }
else if (
m_hKey == 0) {
1145 std::cout <<
"Created ODB key \"" +
to_create +
"\"" << std::endl;
1148 if (
m_name.find_last_of(
'/') != std::string::npos)
1151 mthrow(
"Write of un-connected ODB key \"" +
m_name +
"\" not possible");
1164 if ((
int)
d.size() + 1 >
size)
1165 size =
d.size() + 1;
1168 size = (((
size - 1) / 32) + 1) * 32;
1185 get(
s,
true,
false);
1187 "[0..." + std::to_string(
m_num_values - 1) +
"]\" = [" +
s +
"]" << std::endl;
1197 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1220 get(
s,
false,
false);
1223 "]\" = [" +
s +
"]" << std::endl;
1225 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1231 "\" failed with status " + std::to_string(
status));
1242 for (
int i = 0;;
i++) {
1256 std::cout <<
"Deleting " <<
full_path <<
" as not in list of defaults" << std::endl;
1363 std::string path(p);
1366 mthrow(
"odb::connect() cannot be called with an empty ODB path");
1369 mthrow(
"odb::connect(\"" + path +
"\"): path must start with leading \"/\"");
1371 if (path.back() !=
'/')
1412 mthrow(
"odb::connect() cannot be called with an empty ODB path");
1415 mthrow(
"odb::connect(\"" +
str +
"\"): path must start with leading \"/\"");
1418 mthrow(
"odb::connect(\"" +
str +
"\"): root ODB tree is not allowed");
1420 if (
str.back() ==
'/')
1426 name =
str.substr(
str.find_last_of(
'/') + 1);
1427 path =
str.substr(0,
str.find_last_of(
'/') + 1);
1441 std::map<std::string, std::vector<std::string> >
user_order;
1457 mthrow(
"Cannot modify write protected key \"" +
m_name +
"\"");
1464 "\" returnd error code " + std::to_string(
status));
1467 std::cout <<
"Deleted ODB key \"" +
m_name +
"\"" << std::endl;
1491 "\" returnd error code " + std::to_string(
status));
1494 std::cout <<
"Set mode of ODB key \"" +
get_full_path() +
"\" to " <<
mode << std::endl;
1506 "\" returnd error code " + std::to_string(
status));
1520 "\" returnd error code " + std::to_string(
status));
1528 "\" which is not connected to ODB");
1534 ow->m_watch_callback = f;
1568 s = (
s ==
"y" ||
s ==
"1") ?
"1" :
"0";
1583 s = (
s ==
"y" ||
s ==
"1") ?
"1" :
"0";
1648 s = std::string(
m_bool ?
"true" :
"false");
1659 else if (
m_tid == 0)
1662 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
1793 m_parent_odb->set_last_index(-1);
1798 m_parent_odb->set_last_index(-1);
1803 m_parent_odb->set_last_index(-1);
1808 m_parent_odb->set_last_index(-1);
1813 m_parent_odb->set_last_index(-1);
1818 m_parent_odb->set_last_index(-1);
1823 m_parent_odb->set_last_index(-1);
1828 m_parent_odb->set_last_index(-1);
1833 m_parent_odb->set_last_index(-1);
1838 m_parent_odb->set_last_index(-1);
1843 m_parent_odb->set_last_index(-1);
1846 u_odb::operator std::string() {
1848 m_parent_odb->set_last_index(-1);
1853 u_odb::operator
const char *() {
1855 m_parent_odb->set_last_index(-1);
1857 mthrow(
"Only ODB string keys can be converted to \"const char *\"");
1858 return m_string->c_str();
1864 mthrow(
"Only ODB directories can be converted to \"midas::odb &\"");
1887 mthrow(
"Invalid arithmetic operation for ODB key \"" +
1912 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)
bool is_auto_refresh_read() const
static void unwatch_all()
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)
bool is_auto_refresh_write() const
midas::odb * odb_from_json(const MJsonNode *node, std::string name, odb *o)
bool read_key(const std::string &path)
static std::string s_odb_source_str
void set_preserve_string_size(bool f)
void set_trigger_hotlink(bool f)
void set_auto_refresh_write(bool f)
void odb_from_xml_remote(const std::string &str)
void set_auto_create(bool f)
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()
int get_subkeys(std::vector< std::string > &name)
void watch(std::function< void(midas::odb &)> f)
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)
static odb_source s_odb_source
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)
midas::odb * odb_from_xml(PMXML_NODE node, odb *o)
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=" ")
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)
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
static double sub(double a, double b)