ROOTANA
Loading...
Searching...
No Matches
midasodb.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: midasodb.cxx
4 Created by: K.Olchanski
5
6 Contents: MIDAS implementation of MVOdb ODB interface
7
8\********************************************************************/
9
10#undef NDEBUG // midas required assert() to be always enabled
11
12#include <stdio.h>
13#include <string.h> // strlen()
14#include <assert.h>
15#include <stdlib.h> // malloc()
16
17#include "mvodb.h"
18#include "midas.h"
19#include "mstrlcpy.h"
20
21static std::string toString(int value)
22{
23 char buf[256];
24 sprintf(buf, "%d", value);
25 return buf;
26}
27
28class MidasOdb: public MVOdb
29{
30public:
31 HNDLE fDB = 0;
32 std::string fRoot;
33 bool fPrintError = true;
34 bool fPrintWarning = false;
35
36public:
37 MidasOdb(HNDLE hDB, const char* root)
38 {
39 fDB = hDB;
40 fRoot = root;
41 }
42
43 ~MidasOdb() // dtor
44 {
45 // poison the data members
46 fDB = 0;
47 }
48
49 std::string Path(const char* varname)
50 {
51 std::string path;
52 path += fRoot;
53 path += "/";
54 path += varname;
55 return path;
56 }
57
58 void SetPrintError(bool v)
59 {
60 fPrintError = v;
61 }
62
63 bool GetPrintError() const
64 {
65 return fPrintError;
66 }
67
68 bool IsReadOnly() const
69 {
70 return false;
71 }
72
73 MVOdb* Chdir(const char* subdir, bool create, MVOdbError* error)
74 {
75 std::string path = Path(subdir);
76 HNDLE hkey;
77 int status = db_find_key(fDB, 0, path.c_str(), &hkey);
78 if (status == DB_SUCCESS) {
79 KEY key;
80 status = db_get_key(fDB, hkey, &key);
81 if (status != DB_SUCCESS) {
82 SetMidasStatus(error, fPrintError, path, "db_get_key", status);
83 return MakeNullOdb();
84 }
85 if (key.type != TID_KEY) {
86 SetError(error, fPrintError, path, "ODB path is not a directory");
87 if (create)
88 return MakeNullOdb();
89 else
90 return NULL;
91 }
92 return new MidasOdb(fDB, path.c_str());
93 } else if (!create) {
94 SetMidasStatus(error, fPrintWarning, path, "db_find_key", status);
95 return NULL;
96 } else {
97 status = db_create_key(fDB, 0, path.c_str(), TID_KEY);
98 if (status != DB_SUCCESS) {
99 SetMidasStatus(error, fPrintError, path, "db_create_key", status);
100 return MakeNullOdb();
101 }
102 return new MidasOdb(fDB, path.c_str());
103 }
104 }
105
106 void RAInfo(const char* varname, int* num_elements, int* element_size, MVOdbError* error)
107 {
108 std::string path = Path(varname);
109
110 if (num_elements)
111 *num_elements = 0;
112 if (element_size)
113 *element_size = 0;
114
115 int status;
116 HNDLE hkey;
117 status = db_find_key(fDB, 0, path.c_str(), &hkey);
118 if (status != DB_SUCCESS)
119 return;
120
121 KEY key;
122 status = db_get_key(fDB, hkey, &key);
123 if (status != DB_SUCCESS)
124 return;
125
126 if (num_elements)
127 *num_elements = key.num_values;
128
129 if (element_size)
130 *element_size = key.item_size;
131 }
132
133 void ReadKey(const char* varname, int *tid, int *num_values, int *total_size, int *item_size, MVOdbError* error)
134 {
135 if (tid) *tid = 0;
136 if (num_values) *num_values = 0;
137 if (total_size) *total_size = 0;
138 if (item_size) *item_size = 0;
139
140 std::string path = Path(varname);
141
142 int status;
143 HNDLE hkey;
144
145 status = db_find_key(fDB, 0, path.c_str(), &hkey);
146 if (status != DB_SUCCESS) {
147 SetMidasStatus(error, fPrintWarning, path, "db_find_key", status);
148 return;
149 }
150
151 KEY key;
152 status = db_get_key(fDB, hkey, &key);
153 if (status != DB_SUCCESS) {
154 SetMidasStatus(error, fPrintError, path, "db_get_key", status);
155 return;
156 }
157
158 if (tid)
159 *tid = key.type;
160
161 if (num_values)
162 *num_values = key.num_values;
163
164 if (total_size)
165 *total_size = key.total_size;
166
167 if (item_size)
168 *item_size = key.item_size;
169
170 SetOk(error);
171 }
172
173 void ReadKeyLastWritten(const char* varname, int *last_written, MVOdbError* error)
174 {
175 if (last_written) *last_written = 0;
176
177 std::string path = Path(varname);
178
179 int status;
180 HNDLE hkey;
181
182 status = db_find_key(fDB, 0, path.c_str(), &hkey);
183 if (status != DB_SUCCESS) {
184 SetMidasStatus(error, fPrintWarning, path, "db_find_key", status);
185 return;
186 }
187
188 KEY key;
189 status = db_get_key(fDB, hkey, &key);
190 if (status != DB_SUCCESS) {
191 SetMidasStatus(error, fPrintError, path, "db_get_key", status);
192 return;
193 }
194
195 if (last_written)
196 *last_written = key.last_written;
197
198 SetOk(error);
199 }
200
201 void ReadDir(std::vector<std::string>* varname, std::vector<int> *tid, std::vector<int> *num_values, std::vector<int> *total_size, std::vector<int> *item_size, MVOdbError* error)
202 {
203 std::vector<KEY> keys;
204 int status = db_enum(fDB, 0, fRoot.c_str(), NULL, &keys);
205
206 for (size_t i=0; i<keys.size(); i++) {
207 if (tid)
208 tid->push_back(keys[i].type);
209 if (num_values)
210 num_values->push_back(keys[i].num_values);
211 if (total_size)
212 total_size->push_back(keys[i].total_size);
213 if (item_size)
214 item_size->push_back(keys[i].item_size);
215 if (varname)
216 varname->push_back(keys[i].name);
217 }
218
219 if (status != DB_SUCCESS) {
220 SetMidasStatus(error, fPrintError, fRoot.c_str(), "db_enum", status);
221 return;
222 }
223
224 SetOk(error);
225 }
226
227 void ResizeArray(const char* varname, int new_size, MVOdbError* error)
228 {
229 std::string path = Path(varname);
230
231 int status;
232 HNDLE hkey;
233
234 status = db_find_key(fDB, 0, path.c_str(), &hkey);
235
236 if (status != DB_SUCCESS) {
237 SetMidasStatus(error, fPrintWarning, path, "db_find_key", status);
238 return;
239 }
240
241 status = db_set_num_values(fDB, hkey, new_size);
242 if (status != SUCCESS) {
243 SetMidasStatus(error, fPrintError, path, "db_set_num_values", status);
244 return;
245 }
246
247 SetOk(error);
248 }
249
250 void ResizeStringArray(const char* varname, int new_size, int new_string_length, MVOdbError* error)
251 {
252 std::string path = Path(varname);
253
254 int status = db_resize_string(fDB, 0, path.c_str(), new_size, new_string_length);
255 if (status != SUCCESS) {
256 SetMidasStatus(error, fPrintError, path, "db_resize_string", status);
257 return;
258 }
259
260 SetOk(error);
261 }
262
263 bool R(const char* varname, int tid, void *value, int size, bool create, MVOdbError* error)
264 {
265 assert(value);
266 std::string path = Path(varname);
267 int status = db_get_value(fDB, 0, path.c_str(), value, &size, tid, create);
268 if (status != DB_SUCCESS) {
269 SetMidasStatus(error, fPrintError, path, "db_get_value", status);
270 return false;
271 }
272 SetOk(error);
273 return true;
274 }
275
276 void RI(const char* varname, int *value, bool create, MVOdbError* error)
277 {
278 R(varname, TID_INT, value, sizeof(int), create, error);
279 }
280
281 void RU16(const char* varname, uint16_t *value, bool create, MVOdbError* error)
282 {
283 R(varname, TID_WORD, value, sizeof(uint16_t), create, error);
284 }
285
286 void RU32(const char* varname, uint32_t *value, bool create, MVOdbError* error)
287 {
288 R(varname, TID_DWORD, value, sizeof(uint32_t), create, error);
289 }
290
291 void RU64(const char* varname, uint64_t *value, bool create, MVOdbError* error)
292 {
293 R(varname, TID_QWORD, value, sizeof(uint64_t), create, error);
294 }
295
296 void RD(const char* varname, double *value, bool create, MVOdbError* error)
297 {
298 bool ok = R(varname, TID_DOUBLE, value, sizeof(double), create, error);
299 if (!ok) {
300 float fvalue = *value;
301 ok = R(varname, TID_FLOAT, &fvalue, sizeof(float), create, error);
302 if (fPrintError && ok) {
303 std::string path = Path(varname);
304 fprintf(stderr, "MVOdb::RD: Sucessfully read ODB \"%s\" of type FLOAT instead of DOUBLE\n", path.c_str());
305 }
306 *value = fvalue;
307 }
308 }
309
310 void RF(const char* varname, float *value, bool create, MVOdbError* error)
311 {
312 R(varname, TID_FLOAT, value, sizeof(float), create, error);
313 }
314
315 void RB(const char* varname, bool *value, bool create, MVOdbError* error)
316 {
317 assert(value);
318 BOOL v = *value;
319 R(varname, TID_BOOL, &v, sizeof(BOOL), create, error);
320 *value = v;
321 }
322
323 void RS(const char* varname, std::string* value, bool create, int create_string_length, MVOdbError* error)
324 {
325 assert(value);
326 std::string path = Path(varname);
327
328#ifdef HAVE_DB_GET_VALUE_STRING_CREATE_STRING_LENGTH
329 int status = db_get_value_string(fDB, 0, path.c_str(), 0, value, create, create_string_length);
330#else
331#warning This MIDAS has an old version of db_get_value_string() and RS() will ignore the create_string_length argument.
332 int status = db_get_value_string(fDB, 0, path.c_str(), 0, value, create);
333#endif
334
335 if (status != DB_SUCCESS) {
336 SetMidasStatus(error, fPrintError, path, "db_get_value_string", status);
337 return;
338 }
339
340 SetOk(error);
341 }
342
343 void RAI(const char* varname, int index, int tid, void *value, int size, MVOdbError* error)
344 {
345 assert(value);
346 std::string path = Path(varname);
347 path += "[";
348 path += toString(index);
349 path += "]";
350 if (index < 0) {
351 SetError(error, fPrintError, path, "RxAI() called with negative array index");
352 return;
353 }
354 int status = db_get_value(fDB, 0, path.c_str(), value, &size, tid, FALSE);
355 if (status != DB_SUCCESS) {
356 SetMidasStatus(error, fPrintError, path, "db_get_value", status);
357 return;
358 }
359 SetOk(error);
360 }
361
362 void RIAI(const char* varname, int index, int *value, MVOdbError* error)
363 {
364 RAI(varname, index, TID_INT, value, sizeof(int), error);
365 }
366
367 void RU16AI(const char* varname, int index, uint16_t *value, MVOdbError* error)
368 {
369 RAI(varname, index, TID_WORD, value, sizeof(uint16_t), error);
370 }
371
372 void RU32AI(const char* varname, int index, uint32_t *value, MVOdbError* error)
373 {
374 RAI(varname, index, TID_DWORD, value, sizeof(uint32_t), error);
375 }
376
377 void RU64AI(const char* varname, int index, uint64_t *value, MVOdbError* error)
378 {
379 RAI(varname, index, TID_QWORD, value, sizeof(uint64_t), error);
380 }
381
382 void RDAI(const char* varname, int index, double *value, MVOdbError* error)
383 {
384 RAI(varname, index, TID_DOUBLE, value, sizeof(double), error);
385 }
386
387 void RFAI(const char* varname, int index, float *value, MVOdbError* error)
388 {
389 RAI(varname, index, TID_FLOAT, value, sizeof(float), error);
390 }
391
392 void RBAI(const char* varname, int index, bool *value, MVOdbError* error)
393 {
394 assert(value);
395 BOOL v = *value;
396 RAI(varname, index, TID_BOOL, &v, sizeof(BOOL), error);
397 *value = v;
398 }
399
400 void RSAI(const char* varname, int index, std::string* value, MVOdbError* error)
401 {
402 assert(value);
403 std::string path = Path(varname);
404
405 if (index < 0) {
406 SetError(error, fPrintError, path, "RSAI() called with negative array index");
407 return;
408 }
409
410 int status = db_get_value_string(fDB, 0, path.c_str(), index, value, FALSE);
411
412 if (status != DB_SUCCESS) {
413 SetMidasStatus(error, fPrintError, path, "db_get_value_string", status);
414 return;
415 }
416
417 SetOk(error);
418 }
419
420 bool RA(const std::string& path, int tid, void* buf, int size, MVOdbError* error)
421 {
422 int status = db_get_value(fDB, 0, path.c_str(), buf, &size, tid, FALSE);
423
424 if (status != DB_SUCCESS) {
425 SetMidasStatus(error, fPrintError, path, "db_get_value", status);
426 return false;
427 }
428
429 SetOk(error);
430 return true;
431 }
432
433 void GetArraySize(const char* varname, int* pnum_values, int* pitem_size, MVOdbError* error)
434 {
435 int xtid = 0;
436 //int xnum_values = 0;
437 int xtotal_size = 0;
438 //int xitem_size = 0;
439
440 ReadKey(varname, &xtid, pnum_values, &xtotal_size, pitem_size, error);
441
442 if (xtid == TID_KEY) {
443 *pnum_values = -1;
444 *pitem_size = -1;
445 }
446
447 if (xtid == 0) {
448 *pnum_values = -1;
449 *pitem_size = -1;
450 }
451 }
452
453 template <class X> bool RXA(const char* varname, int tid, std::vector<X> *value, bool create, int create_size, MVOdbError* error)
454 {
455 std::string path = Path(varname);
456
457 int num_values = 0;
458 int item_size = 0;
459
460 GetArraySize(varname, &num_values, &item_size, error);
461
462 if (value == NULL) {
463 if (create && create_size > 0) {
464 if (num_values < 0) {
465 // does not exist, create it
466 X v = 0;
467 W(varname, tid, &v, sizeof(X), error);
468 if (error && error->fError)
469 return false;
470 ResizeArray(varname, create_size, error);
471 } else if (num_values != create_size) {
472 // wrong size, resize it
473 ResizeArray(varname, create_size, error);
474 return true;
475 }
476 }
477 return true;
478 }
479
480 if (num_values > 0) { // array exists
481 value->resize(num_values);
482 return RA(path, tid, &((*value)[0]), num_values*sizeof(X), error);
483 }
484
485 // array does not exist
486
487 if (!create)
488 return false;
489
490 WA(varname, tid, &((*value)[0]), value->size()*sizeof(X), value->size(), error);
491
492 if (error && error->fError)
493 return true;
494
495 if (create_size > 0) {
496 if (create_size != (int)value->size()) {
497 ResizeArray(varname, create_size, error);
498 }
499 }
500
501 return true;
502 }
503
504 void RIA(const char* varname, std::vector<int> *value, bool create, int create_size, MVOdbError* error)
505 {
506 RXA<int>(varname, TID_INT, value, create, create_size, error);
507 }
508
509 void RFA(const char* varname, std::vector<float> *value, bool create, int create_size, MVOdbError* error)
510 {
511 RXA<float>(varname, TID_FLOAT, value, create, create_size, error);
512 }
513
514 void RDA(const char* varname, std::vector<double> *value, bool create, int create_size, MVOdbError* error)
515 {
516 bool ok = RXA<double>(varname, TID_DOUBLE, value, create, create_size, error);
517 if (!ok) {
518 std::vector<float> fvalue;
519 std::vector<float> *fvalue_ptr = NULL;
520 if (value) {
521 fvalue_ptr = &fvalue;
522 for (size_t i=0; i<value->size(); i++) {
523 fvalue.push_back((*value)[i]);
524 }
525 }
526 ok = RXA<float>(varname, TID_FLOAT, fvalue_ptr, create, create_size, error);
527 if (ok && fvalue_ptr) {
528 if (fPrintError) {
529 std::string path = Path(varname);
530 fprintf(stderr, "MVOdb::RDA: Sucessfully read ODB \"%s\" of type FLOAT instead of DOUBLE\n", path.c_str());
531 }
532 value->clear();
533 for (size_t i=0; i<fvalue.size(); i++) {
534 value->push_back(fvalue[i]);
535 }
536 }
537 }
538 }
539
540 void RU16A(const char* varname, std::vector<uint16_t> *value, bool create, int create_size, MVOdbError* error)
541 {
542 RXA<uint16_t>(varname, TID_WORD, value, create, create_size, error);
543 }
544
545 void RU32A(const char* varname, std::vector<uint32_t> *value, bool create, int create_size, MVOdbError* error)
546 {
547 RXA<uint32_t>(varname, TID_DWORD, value, create, create_size, error);
548 }
549
550 void RU64A(const char* varname, std::vector<uint64_t> *value, bool create, int create_size, MVOdbError* error)
551 {
552 RXA<uint64_t>(varname, TID_QWORD, value, create, create_size, error);
553 }
554
555 void RBA(const char* varname, std::vector<bool> *value, bool create, int create_size, MVOdbError* error)
556 {
557 std::vector<BOOL> xvalue;
558 std::vector<BOOL> *xvalue_ptr = NULL;
559
560 if (value) {
561 for (std::size_t i=0; i<value->size(); i++) {
562 if ((*value)[i])
563 xvalue.push_back(TRUE);
564 else
565 xvalue.push_back(FALSE);
566 }
567 xvalue_ptr = &xvalue;
568 }
569
570 RXA<BOOL>(varname, TID_BOOL, xvalue_ptr, create, create_size, error);
571
572 if (value) {
573 value->clear();
574 for (std::size_t i=0; i<xvalue.size(); i++) {
575 if (xvalue[i])
576 value->push_back(true);
577 else
578 value->push_back(false);
579 }
580 }
581 }
582
583 void RSA(const char* varname, std::vector<std::string> *value, bool create, int create_size, int create_string_length, MVOdbError* error)
584 {
585 std::string path = Path(varname);
586
587 int num_values = 0;
588 int item_size = 0;
589
590 GetArraySize(varname, &num_values, &item_size, error);
591
592 if (value == NULL) {
593 if (create && (create_size > 0) && (create_string_length > 0)) {
594 if (num_values < 0) {
595 // does not exist, create it
596 WS(varname, "", create_string_length, error);
597 if (error && error->fError)
598 return;
599 ResizeStringArray(varname, create_size, create_string_length, error);
600 } else if ((num_values != create_size) || (item_size != create_string_length)) {
601 // wrong size, resize it
602 ResizeStringArray(varname, create_size, create_string_length, error);
603 return;
604 }
605 }
606 return;
607 }
608
609 // array exists, read it
610
611 if (num_values > 0) {
612 value->clear();
613 int bufsize = num_values*item_size;
614 char* buf = (char*)malloc(bufsize);
615 assert(buf != NULL);
616 memset(buf, 0, bufsize);
617 RA(path, TID_STRING, buf, bufsize, error);
618 for (int i=0; i<num_values; i++) {
619 value->push_back(buf+i*item_size);
620 }
621 free(buf);
622 buf = NULL;
623 return;
624 }
625
626 // array does not exist
627
628 if (!create)
629 return;
630
631 //if (!(create_string_length > 0)) {
632 // SetError(error, fPrintError, path, "RSA() with create==true must have create_string_length>0");
633 // return;
634 //}
635
636 int string_length = 0;
637 for (size_t i = 0; i < value->size(); i++) {
638 if (((int)(*value)[i].length()) > string_length)
639 string_length = (*value)[i].length();
640 }
641 string_length += 1; // add space for string terminator NUL character '\0'
642
643 if (create_string_length > string_length)
644 string_length = create_string_length;
645
646 char* buf = NULL;
647
648 int bufsize = value->size()*string_length;
649
650 if (bufsize > 0) {
651 buf = (char*)malloc(bufsize);
652 assert(buf != NULL);
653 memset(buf, 0, bufsize);
654
655 for (size_t i=0; i<value->size(); i++) {
656 mstrlcpy(buf+i*string_length, (*value)[i].c_str(), string_length);
657 }
658 }
659
660 WA(varname, TID_STRING, buf, bufsize, value->size(), error);
661
662 if (buf) {
663 free(buf);
664 buf = NULL;
665 }
666
667 if (error && error->fError)
668 return;
669
670 if ((create_size > 0) && (create_string_length > 0)) {
671 if ((((int)value->size()) != create_size) || (string_length != create_string_length)) {
672 // wrong size, resize it
673 ResizeStringArray(varname, create_size, create_string_length, error);
674 }
675 }
676 }
677
678 void W(const char* varname, int tid, const void* v, int size, MVOdbError* error)
679 {
680 std::string path = Path(varname);
681
682 int status = db_set_value(fDB, 0, path.c_str(), v, size, 1, tid);
683
684 if (status == DB_TYPE_MISMATCH) {
685 if (fPrintError) {
686 fprintf(stderr, "MVOdb::W: Data type mismatch when writing to ODB \"%s\", deleting the old entry\n", path.c_str());
687 }
688
689 Delete(varname, error);
690
691 status = db_set_value(fDB, 0, path.c_str(), v, size, 1, tid);
692 }
693
694 if (status != DB_SUCCESS) {
695 SetMidasStatus(error, fPrintError, path, "db_set_value", status);
696 return;
697 }
698
699 SetOk(error);
700 return;
701 }
702
703 void WB(const char* varname, bool v, MVOdbError* error)
704 {
705 BOOL vv = v;
706 W(varname, TID_BOOL, &vv, sizeof(BOOL), error);
707 }
708
709 void WI(const char* varname, int v, MVOdbError* error)
710 {
711 W(varname, TID_INT, &v, sizeof(int), error);
712 }
713
714 void WU16(const char* varname, uint16_t v, MVOdbError* error)
715 {
716 W(varname, TID_WORD, &v, sizeof(uint16_t), error);
717 }
718
719 void WU32(const char* varname, uint32_t v, MVOdbError* error)
720 {
721 W(varname, TID_DWORD, &v, sizeof(uint32_t), error);
722 }
723
724 void WU64(const char* varname, uint64_t v, MVOdbError* error)
725 {
726 W(varname, TID_QWORD, &v, sizeof(uint64_t), error);
727 }
728
729 void WD(const char* varname, double v, MVOdbError* error)
730 {
731 W(varname, TID_DOUBLE, &v, sizeof(double), error);
732 }
733
734 void WF(const char* varname, float v, MVOdbError* error)
735 {
736 W(varname, TID_FLOAT, &v, sizeof(float), error);
737 }
738
739 void WS(const char* varname, const char* v, int string_length, MVOdbError* error)
740 {
741 if (string_length > 0) {
742 char* buf = (char*)malloc(string_length);
743 assert(buf);
744 mstrlcpy(buf, v, string_length);
745 W(varname, TID_STRING, buf, string_length, error);
746 free(buf);
747 } else {
748 int len = strlen(v);
749 W(varname, TID_STRING, v, len+1, error);
750 }
751 }
752
753 void WAI(const char* varname, int index, int tid, const void* v, int size, MVOdbError* error)
754 {
755 std::string path = Path(varname);
756
757 if (index < 0) {
758 SetError(error, fPrintError, path, "WxAI() called with negative array index");
759 return;
760 }
761
762 //printf("WAI(\"%s\", [%d], %d) path [%s], size %d\n", varname, index, tid, path.c_str(), size);
763
764 int status;
765 HNDLE hkey;
766
767 status = db_find_key(fDB, 0, path.c_str(), &hkey);
768
769 if (status != DB_SUCCESS) {
770 SetMidasStatus(error, fPrintError, path, "db_find_key", status);
771 return;
772 }
773
774 status = db_set_data_index(fDB, hkey, v, size, index, tid);
775
776 if (status != DB_SUCCESS) {
777 SetMidasStatus(error, fPrintError, path, "db_set_value", status);
778 return;
779 }
780
781 SetOk(error);
782 }
783
784 void WBAI(const char* varname, int index, bool v, MVOdbError* error)
785 {
786 BOOL vv = v;
787 WAI(varname, index, TID_BOOL, &vv, sizeof(BOOL), error);
788 }
789
790 void WIAI(const char* varname, int index, int v, MVOdbError* error)
791 {
792 WAI(varname, index, TID_INT, &v, sizeof(int), error);
793 }
794
795 void WU16AI(const char* varname, int index, uint16_t v, MVOdbError* error)
796 {
797 WAI(varname, index, TID_WORD, &v, sizeof(uint16_t), error);
798 }
799
800 void WU32AI(const char* varname, int index, uint32_t v, MVOdbError* error)
801 {
802 WAI(varname, index, TID_DWORD, &v, sizeof(uint32_t), error);
803 }
804
805 void WU64AI(const char* varname, int index, uint64_t v, MVOdbError* error)
806 {
807 WAI(varname, index, TID_QWORD, &v, sizeof(uint64_t), error);
808 }
809
810 void WDAI(const char* varname, int index, double v, MVOdbError* error)
811 {
812 WAI(varname, index, TID_DOUBLE, &v, sizeof(double), error);
813 }
814
815 void WFAI(const char* varname, int index, float v, MVOdbError* error)
816 {
817 WAI(varname, index, TID_FLOAT, &v, sizeof(float), error);
818 }
819
820 void WSAI(const char* varname, int index, const char* v, MVOdbError* error)
821 {
822 int num_elements = 0;
823 int element_size = 0;
824 RAInfo(varname, &num_elements, &element_size, error);
825 if (error && error->fError)
826 return;
827 if (element_size <= 0)
828 return;
829 char* buf = (char*)malloc(element_size);
830 assert(buf);
831 mstrlcpy(buf, v, element_size);
832 WAI(varname, index, TID_STRING, buf, element_size, error);
833 free(buf);
834 }
835
836 void WA(const char* varname, int tid, const void* v, int size, int count, MVOdbError* error)
837 {
838 std::string path = Path(varname);
839
840 //printf("WA(tid %d, size %d, count %d)\n", tid, size, count);
841
842 if (size == 0) {
843 int status = db_create_key(fDB, 0, path.c_str(), tid);
844
845 if (status == DB_TYPE_MISMATCH) {
846 if (fPrintError) {
847 fprintf(stderr, "MVOdb::WA: Data type mismatch when writing to ODB \"%s\", deleting the old entry\n", path.c_str());
848 }
849
850 Delete(varname, error);
851
852 status = db_create_key(fDB, 0, path.c_str(), tid);
853 }
854
855 if (status != DB_SUCCESS) {
856 SetMidasStatus(error, fPrintError, path, "db_create_key", status);
857 return;
858 }
859 } else {
860 int status = db_set_value(fDB, 0, path.c_str(), v, size, count, tid);
861
862 //printf("WA db_set_value(tid %d, size %d, count %d) status %d\n", tid, size, count, status);
863
864 if (status == DB_TYPE_MISMATCH) {
865 if (fPrintError) {
866 fprintf(stderr, "MVOdb::WA: Data type mismatch when writing to ODB \"%s\", deleting the old entry\n", path.c_str());
867 }
868
869 Delete(varname, error);
870
871 status = db_set_value(fDB, 0, path.c_str(), v, size, count, tid);
872 }
873
874 if (status != DB_SUCCESS) {
875 SetMidasStatus(error, fPrintError, path, "db_set_value", status);
876 return;
877 }
878 }
879
880 SetOk(error);
881 }
882
883 void WBA(const char* varname, const std::vector<bool>& v, MVOdbError* error)
884 {
885 unsigned num = v.size();
886 BOOL val[num];
887
888 for (unsigned i=0; i<num; i++) {
889 val[i] = v[i];
890 }
891
892 WA(varname, TID_BOOL, val, num*sizeof(BOOL), num, error);
893 }
894
895 void WU16A(const char* varname, const std::vector<uint16_t>& v, MVOdbError* error)
896 {
897 WA(varname, TID_WORD, &v[0], v.size()*sizeof(uint16_t), v.size(), error);
898 }
899
900 void WU32A(const char* varname, const std::vector<uint32_t>& v, MVOdbError* error)
901 {
902 WA(varname, TID_DWORD, &v[0], v.size()*sizeof(uint32_t), v.size(), error);
903 }
904
905 void WU64A(const char* varname, const std::vector<uint64_t>& v, MVOdbError* error)
906 {
907 WA(varname, TID_QWORD, &v[0], v.size()*sizeof(uint64_t), v.size(), error);
908 }
909
910 void WIA(const char* varname, const std::vector<int>& v, MVOdbError* error)
911 {
912 WA(varname, TID_INT, &v[0], v.size()*sizeof(int), v.size(), error);
913 }
914
915 void WFA(const char* varname, const std::vector<float>& v, MVOdbError* error)
916 {
917 WA(varname, TID_FLOAT, &v[0], v.size()*sizeof(float), v.size(), error);
918 }
919
920 void WDA(const char* varname, const std::vector<double>& v, MVOdbError* error)
921 {
922 WA(varname, TID_DOUBLE, &v[0], v.size()*sizeof(double), v.size(), error);
923 }
924
925 void WSA(const char* varname, const std::vector<std::string>& v, int odb_string_size, MVOdbError* error)
926 {
927 unsigned num = v.size();
928 unsigned length = odb_string_size;
929
930 if (length == 0) {
931 for (unsigned i=0; i<v.size(); i++) {
932 if (v[i].length() > length)
933 length = v[i].length();
934 }
935 length += 1; // for the string terminator NUL character
936 }
937
938 char val[length*num];
939 memset(val, 0, length*num);
940
941 for (unsigned i=0; i<num; i++)
942 mstrlcpy(val+length*i, v[i].c_str(), length);
943
944 WA(varname, TID_STRING, val, num*length, num, error);
945 }
946
947 void Delete(const char* odbname, MVOdbError* error)
948 {
949 std::string path = Path(odbname);
950
951 //printf("Delete(%s)\n", path.c_str());
952
953 int status = db_delete(fDB, 0, path.c_str());
954
955 if (status == DB_NO_KEY) {
956 SetOk(error);
957 return;
958 }
959
960 if (status != DB_SUCCESS) {
961 SetMidasStatus(error, fPrintError, path, "db_delete_key", status);
962 return;
963 }
964
965 SetOk(error);
966 };
967};
968
970{
971 SetOk(error);
972 return new MidasOdb(hDB, "");
973}
974
975/* emacs
976 * Local Variables:
977 * tab-width: 8
978 * c-basic-offset: 3
979 * indent-tabs-mode: nil
980 * End:
981 */
bool fError
Definition mvodb.h:196
Definition mvodb.h:21
void RIA(const char *varname, std::vector< int > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:504
void RU64(const char *varname, uint64_t *value, bool create, MVOdbError *error)
Definition midasodb.cxx:291
void WU16(const char *varname, uint16_t v, MVOdbError *error)
Definition midasodb.cxx:714
bool GetPrintError() const
Definition midasodb.cxx:63
MVOdb * Chdir(const char *subdir, bool create, MVOdbError *error)
Definition midasodb.cxx:73
void SetPrintError(bool v)
Definition midasodb.cxx:58
void WU16AI(const char *varname, int index, uint16_t v, MVOdbError *error)
Definition midasodb.cxx:795
void RD(const char *varname, double *value, bool create, MVOdbError *error)
Definition midasodb.cxx:296
bool R(const char *varname, int tid, void *value, int size, bool create, MVOdbError *error)
Definition midasodb.cxx:263
void RF(const char *varname, float *value, bool create, MVOdbError *error)
Definition midasodb.cxx:310
void WU64A(const char *varname, const std::vector< uint64_t > &v, MVOdbError *error)
Definition midasodb.cxx:905
void WIAI(const char *varname, int index, int v, MVOdbError *error)
Definition midasodb.cxx:790
bool fPrintError
Definition midasodb.cxx:33
MidasOdb(HNDLE hDB, const char *root)
Definition midasodb.cxx:37
bool fPrintWarning
Definition midasodb.cxx:34
void W(const char *varname, int tid, const void *v, int size, MVOdbError *error)
Definition midasodb.cxx:678
void RIAI(const char *varname, int index, int *value, MVOdbError *error)
Definition midasodb.cxx:362
void RFAI(const char *varname, int index, float *value, MVOdbError *error)
Definition midasodb.cxx:387
void ResizeStringArray(const char *varname, int new_size, int new_string_length, MVOdbError *error)
Definition midasodb.cxx:250
void RSA(const char *varname, std::vector< std::string > *value, bool create, int create_size, int create_string_length, MVOdbError *error)
Definition midasodb.cxx:583
void RAInfo(const char *varname, int *num_elements, int *element_size, MVOdbError *error)
Definition midasodb.cxx:106
void GetArraySize(const char *varname, int *pnum_values, int *pitem_size, MVOdbError *error)
Definition midasodb.cxx:433
void WU32(const char *varname, uint32_t v, MVOdbError *error)
Definition midasodb.cxx:719
void RU64A(const char *varname, std::vector< uint64_t > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:550
void RDA(const char *varname, std::vector< double > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:514
void RB(const char *varname, bool *value, bool create, MVOdbError *error)
Definition midasodb.cxx:315
void WU64(const char *varname, uint64_t v, MVOdbError *error)
Definition midasodb.cxx:724
void WDA(const char *varname, const std::vector< double > &v, MVOdbError *error)
Definition midasodb.cxx:920
void RSAI(const char *varname, int index, std::string *value, MVOdbError *error)
Definition midasodb.cxx:400
void WIA(const char *varname, const std::vector< int > &v, MVOdbError *error)
Definition midasodb.cxx:910
HNDLE fDB
Definition midasodb.cxx:31
void RU32AI(const char *varname, int index, uint32_t *value, MVOdbError *error)
Definition midasodb.cxx:372
void RAI(const char *varname, int index, int tid, void *value, int size, MVOdbError *error)
Definition midasodb.cxx:343
bool RXA(const char *varname, int tid, std::vector< X > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:453
std::string Path(const char *varname)
Definition midasodb.cxx:49
void WI(const char *varname, int v, MVOdbError *error)
Definition midasodb.cxx:709
void WS(const char *varname, const char *v, int string_length, MVOdbError *error)
Definition midasodb.cxx:739
void ReadKeyLastWritten(const char *varname, int *last_written, MVOdbError *error)
Definition midasodb.cxx:173
void WU64AI(const char *varname, int index, uint64_t v, MVOdbError *error)
Definition midasodb.cxx:805
bool IsReadOnly() const
Definition midasodb.cxx:68
void RI(const char *varname, int *value, bool create, MVOdbError *error)
Definition midasodb.cxx:276
void RU32A(const char *varname, std::vector< uint32_t > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:545
void WSA(const char *varname, const std::vector< std::string > &v, int odb_string_size, MVOdbError *error)
Definition midasodb.cxx:925
void ResizeArray(const char *varname, int new_size, MVOdbError *error)
Definition midasodb.cxx:227
void RDAI(const char *varname, int index, double *value, MVOdbError *error)
Definition midasodb.cxx:382
void WBAI(const char *varname, int index, bool v, MVOdbError *error)
Definition midasodb.cxx:784
void RBA(const char *varname, std::vector< bool > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:555
void RFA(const char *varname, std::vector< float > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:509
void Delete(const char *odbname, MVOdbError *error)
Definition midasodb.cxx:947
void WB(const char *varname, bool v, MVOdbError *error)
Definition midasodb.cxx:703
void RU64AI(const char *varname, int index, uint64_t *value, MVOdbError *error)
Definition midasodb.cxx:377
void WU16A(const char *varname, const std::vector< uint16_t > &v, MVOdbError *error)
Definition midasodb.cxx:895
void ReadKey(const char *varname, int *tid, int *num_values, int *total_size, int *item_size, MVOdbError *error)
Definition midasodb.cxx:133
void WDAI(const char *varname, int index, double v, MVOdbError *error)
Definition midasodb.cxx:810
void WBA(const char *varname, const std::vector< bool > &v, MVOdbError *error)
Definition midasodb.cxx:883
void ReadDir(std::vector< std::string > *varname, std::vector< int > *tid, std::vector< int > *num_values, std::vector< int > *total_size, std::vector< int > *item_size, MVOdbError *error)
Definition midasodb.cxx:201
void RU16A(const char *varname, std::vector< uint16_t > *value, bool create, int create_size, MVOdbError *error)
Definition midasodb.cxx:540
void RU16(const char *varname, uint16_t *value, bool create, MVOdbError *error)
Definition midasodb.cxx:281
void WU32A(const char *varname, const std::vector< uint32_t > &v, MVOdbError *error)
Definition midasodb.cxx:900
void WFA(const char *varname, const std::vector< float > &v, MVOdbError *error)
Definition midasodb.cxx:915
void WD(const char *varname, double v, MVOdbError *error)
Definition midasodb.cxx:729
void WSAI(const char *varname, int index, const char *v, MVOdbError *error)
Definition midasodb.cxx:820
void RS(const char *varname, std::string *value, bool create, int create_string_length, MVOdbError *error)
Definition midasodb.cxx:323
bool RA(const std::string &path, int tid, void *buf, int size, MVOdbError *error)
Definition midasodb.cxx:420
std::string fRoot
Definition midasodb.cxx:32
void RBAI(const char *varname, int index, bool *value, MVOdbError *error)
Definition midasodb.cxx:392
void WU32AI(const char *varname, int index, uint32_t v, MVOdbError *error)
Definition midasodb.cxx:800
void RU16AI(const char *varname, int index, uint16_t *value, MVOdbError *error)
Definition midasodb.cxx:367
void WF(const char *varname, float v, MVOdbError *error)
Definition midasodb.cxx:734
void RU32(const char *varname, uint32_t *value, bool create, MVOdbError *error)
Definition midasodb.cxx:286
void WA(const char *varname, int tid, const void *v, int size, int count, MVOdbError *error)
Definition midasodb.cxx:836
void WAI(const char *varname, int index, int tid, const void *v, int size, MVOdbError *error)
Definition midasodb.cxx:753
void WFAI(const char *varname, int index, float v, MVOdbError *error)
Definition midasodb.cxx:815
#define TID_DOUBLE
Definition midasio.h:29
#define TID_KEY
Definition midasio.h:35
#define TID_QWORD
Definition midasio.h:39
#define TID_BOOL
Definition midasio.h:26
#define TID_WORD
Definition midasio.h:18
#define TID_STRING
Definition midasio.h:32
#define TID_INT
Definition midasio.h:24
#define TID_FLOAT
Definition midasio.h:27
#define TID_DWORD
Definition midasio.h:22
void SetMidasStatus(MVOdbError *error, bool print, const std::string &path, const char *midas_func_name, int status)
Definition mvodb.cxx:43
void SetError(MVOdbError *error, bool print, const std::string &path, const std::string &message)
Definition mvodb.cxx:72
MVOdb * MakeNullOdb()
Definition nullodb.cxx:137
void SetOk(MVOdbError *error)
Definition mvodb.cxx:33
MVOdb * MakeMidasOdb(int hDB, MVOdbError *error)
Definition midasodb.cxx:969
static std::string toString(int value)
Definition midasodb.cxx:21
#define TRUE
Definition mxml.cxx:75
#define FALSE
Definition mxml.cxx:76