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");
242 std::vector<std::string> result;
253 result.push_back(
item);
261 MJsonNode*
node = MJsonNode::Parse(
str.c_str());
263 mthrow(
"Error parsing JSON: " +
node->GetError());
278 for (std::string subdir :
dirs) {
284 const char *
name = (*names)[
i].c_str();
289 if (
name == subdir) {
290 auto key =
node->FindObjectNode((std::string(
name) +
"/key").c_str());
292 tid =
key->FindObjectNode(
"type")->GetInt();
302 mthrow(
"Subdirectory \"" + subdir +
"\" not found in JSON");
381 pc->set_parent(
this);
394 d.m_num_values =
s.m_num_values;
396 d.m_watch_callback =
s.m_watch_callback;
398 for (
int i = 0;
i <
d.m_num_values;
i++) {
400 d.m_data[
i].set_parent(&
d);
403 d.m_data[
i].set_string(
s.m_data[
i]);
412 d.m_data[
i] =
s.m_data[
i];
413 d.m_data[
i].set_parent(
this);
437 std::size_t
i =
s.find_last_of(
"/");
454 "\" failed with status " + std::to_string(
status));
466 "\" failed with status " + std::to_string(
status));
497 s +=
"\"" +
m_name +
"\": {\n";
530 s +=
"\"" +
m_name +
"\": {\n";
546 s +=
"\"" +
m_name +
"/key\": ";
547 s +=
"{ \"type\": " + std::to_string(
m_tid) +
", ";
567 std::string header =
"{\n";
568 header +=
" \"/MIDAS version\" : \"2.1\",\n";
569 header +=
" \"/filename\" : \"" + filename +
"\",\n";
570 header +=
" \"/ODB path\" : \"" +
get_full_path() +
"\",\n\n";
576 std::ofstream f(filename);
578 mthrow(
"Cannot open file \"" + filename);
580 f << header << buffer;
592 if (
str.find(
'/') != std::string::npos) {
620 std::cout <<
"Created ODB key \"" +
m_name +
"\"" << std::endl;
622 std::cout <<
"Created ODB key \"" +
get_full_path() +
"\"" << std::endl;
625 if (
m_name.find_last_of(
'/') != std::string::npos)
628 mthrow(
"Invalid key \"" +
m_name +
"\" does not have subkeys");
636 if (
str.find(
'/') != std::string::npos) {
678 mthrow(
"get_sub-keys called with invalid m_hKey for ODB key \"" +
m_name +
"\"");
681 std::vector<HNDLE>
hlist;
683 for (
int i = 0;;
i++) {
709 mthrow(
"db_get_key for ODB key \"" + path +
710 "\" failed with status " + std::to_string(
status));
715 "\" has different type than specified");
718 std::cout <<
"Get definition for ODB key \"" +
get_full_path() +
"\"" << std::endl;
742 "\" failed with status " + std::to_string(
status));
756 std::vector<std::string>
name;
793 mthrow(
"ODB key \"" + path +
"\" cannot be created");
796 mthrow(
"ODB key \"" + path +
"\" not found after creation");
799 std::cout <<
"Created ODB key \"" + path +
"\"" << std::endl;
801 std::cout <<
"Created ODB key \"" +
get_full_path() +
"\"" << std::endl;
804 mthrow(
"ODB key \"" + path +
"\" cannot be found");
810 mthrow(
"db_get_key for ODB key \"" + path +
811 "\" failed with status " + std::to_string(
status));
821 mthrow(
"db_delete_key for ODB key \"" + path +
822 "\" failed with status " + std::to_string(
status));
825 mthrow(
"ODB key \"" + path +
"\" cannot be created");
828 mthrow(
"ODB key \"" + path +
"\" not found after creation");
830 std::cout <<
"Re-created ODB key \"" +
get_full_path() <<
"\" with different type" << std::endl;
834 "\" has differnt type than specified");
836 std::cout <<
"Validated ODB key \"" +
get_full_path() +
"\"" << std::endl;
850 mthrow(
"ODB key \"" +
m_name +
"\" cannot be pulled because it has been deleted");
864 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
878 std::vector<std::string>
name;
940 m_data[
i].
set(std::string(
static_cast<const char *
>(p)));
942 m_data[
i].
set(std::string(
static_cast<const char *
>(p)));
944 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
953 "\" failed with status " + std::to_string(
status));
960 get(
s,
false,
false);
964 std::to_string(
m_num_values - 1) +
"]\": [\"" +
s +
"\"]" << std::endl;
967 std::to_string(
m_num_values - 1) +
"]\": [" +
s +
"]" << std::endl;
970 std::cout <<
"Get ODB key \"" +
get_full_path() +
"\": \"" +
s +
"\"" << std::endl;
972 std::cout <<
"Get ODB key \"" +
get_full_path() +
"\": " +
s << std::endl;
1033 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
1040 "\" failed with status " + std::to_string(
status));
1046 std::to_string(
index) +
"]\": [\"" +
s +
"\"]" << std::endl;
1049 std::to_string(
index) +
"]\": [" +
s +
"]" << std::endl;
1064 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
1066 }
else if (
m_hKey == 0) {
1074 std::cout <<
"Created ODB key \"" +
to_create +
"\"" << std::endl;
1077 if (
m_name.find_last_of(
'/') != std::string::npos)
1080 mthrow(
"Write of un-connected ODB key \"" +
m_name +
"\" not possible");
1096 size =
s.size() + 1;
1111 std::cout <<
"ODB string size mismatch for \"" <<
get_full_path() <<
1125 std::cout <<
"Set ODB key \"" +
get_full_path() +
"[" + std::to_string(
index) +
"]\" = \"" +
s
1126 +
"\"" << std::endl;
1128 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = \"" +
s +
"\""<< std::endl;
1134 BOOL b =
static_cast<bool>(
u);
1143 std::cout <<
"Set ODB key \"" +
get_full_path() +
"[" + std::to_string(
index) +
"]\" = " +
s
1146 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1151 "\" failed with status " + std::to_string(
status));
1159 mthrow(
"ODB key \"" +
m_name +
"\" cannot be written because it has been deleted");
1176 "\" is not possible because of invalid key handle");
1196 mthrow(
"Cannot connect key \"" + path +
"\" to ODB");
1198 }
else if (
m_hKey == 0) {
1206 std::cout <<
"Created ODB key \"" +
to_create +
"\"" << std::endl;
1209 if (
m_name.find_last_of(
'/') != std::string::npos)
1212 mthrow(
"Write of un-connected ODB key \"" +
m_name +
"\" not possible");
1225 if ((
int)
d.size() + 1 >
size)
1226 size =
d.size() + 1;
1229 size = (((
size - 1) / 32) + 1) * 32;
1246 get(
s,
true,
false);
1248 "[0..." + std::to_string(
m_num_values - 1) +
"]\" = [" +
s +
"]" << std::endl;
1258 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1281 get(
s,
false,
false);
1284 "]\" = [" +
s +
"]" << std::endl;
1286 std::cout <<
"Set ODB key \"" +
get_full_path() +
"\" = " +
s << std::endl;
1292 "\" failed with status " + std::to_string(
status));
1303 for (
int i = 0;;
i++) {
1317 std::cout <<
"Deleting " <<
full_path <<
" as not in list of defaults" << std::endl;
1424 std::string path(p);
1427 mthrow(
"odb::connect() cannot be called with an empty ODB path");
1430 mthrow(
"odb::connect(\"" + path +
"\"): path must start with leading \"/\"");
1432 if (path.back() !=
'/')
1473 mthrow(
"odb::connect() cannot be called with an empty ODB path");
1476 mthrow(
"odb::connect(\"" +
str +
"\"): path must start with leading \"/\"");
1479 mthrow(
"odb::connect(\"" +
str +
"\"): root ODB tree is not allowed");
1481 if (
str.back() ==
'/')
1487 name =
str.substr(
str.find_last_of(
'/') + 1);
1488 path =
str.substr(0,
str.find_last_of(
'/') + 1);
1502 std::map<std::string, std::vector<std::string> >
user_order;
1518 mthrow(
"Cannot modify write protected key \"" +
m_name +
"\"");
1525 "\" returnd error code " + std::to_string(
status));
1528 std::cout <<
"Deleted ODB key \"" +
m_name +
"\"" << std::endl;
1552 "\" returnd error code " + std::to_string(
status));
1555 std::cout <<
"Set mode of ODB key \"" +
get_full_path() +
"\" to " <<
mode << std::endl;
1567 "\" returnd error code " + std::to_string(
status));
1581 "\" returnd error code " + std::to_string(
status));
1589 "\" which is not connected to ODB");
1595 ow->m_watch_callback = f;
1629 s = (
s ==
"y" ||
s ==
"1") ?
"1" :
"0";
1644 s = (
s ==
"y" ||
s ==
"1") ?
"1" :
"0";
1709 s = std::string(
m_bool ?
"true" :
"false");
1720 else if (
m_tid == 0)
1723 mthrow(
"Invalid type ID " + std::to_string(
m_tid));
1854 m_parent_odb->set_last_index(-1);
1859 m_parent_odb->set_last_index(-1);
1864 m_parent_odb->set_last_index(-1);
1869 m_parent_odb->set_last_index(-1);
1874 m_parent_odb->set_last_index(-1);
1879 m_parent_odb->set_last_index(-1);
1884 m_parent_odb->set_last_index(-1);
1889 m_parent_odb->set_last_index(-1);
1894 m_parent_odb->set_last_index(-1);
1899 m_parent_odb->set_last_index(-1);
1904 m_parent_odb->set_last_index(-1);
1907 u_odb::operator std::string() {
1909 m_parent_odb->set_last_index(-1);
1914 u_odb::operator
const char *() {
1916 m_parent_odb->set_last_index(-1);
1918 mthrow(
"Only ODB string keys can be converted to \"const char *\"");
1919 return m_string->c_str();
1925 mthrow(
"Only ODB directories can be converted to \"midas::odb &\"");
1948 mthrow(
"Invalid arithmetic operation for ODB key \"" +
1973 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
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()
midas::odb * odb_from_json(const MJsonNode *node, std::string name, int tid, odb *o)
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=" ")
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)
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
static double sub(double a, double b)