| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : arbdb.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #if defined(DARWIN) |
|---|
| 12 | #include <cstdio> |
|---|
| 13 | #endif // DARWIN |
|---|
| 14 | |
|---|
| 15 | #include <rpc/types.h> |
|---|
| 16 | #include <rpc/xdr.h> |
|---|
| 17 | |
|---|
| 18 | #include "ad_hcb.h" |
|---|
| 19 | #include "gb_comm.h" |
|---|
| 20 | #include "gb_compress.h" |
|---|
| 21 | #include "gb_localdata.h" |
|---|
| 22 | #include "gb_ta.h" |
|---|
| 23 | #include "gb_ts.h" |
|---|
| 24 | #include "gb_index.h" |
|---|
| 25 | #include <arb_strarray.h> |
|---|
| 26 | |
|---|
| 27 | #include <glib.h> |
|---|
| 28 | #include <glib/gprintf.h> |
|---|
| 29 | |
|---|
| 30 | gb_local_data *gb_local = 0; |
|---|
| 31 | |
|---|
| 32 | #define INIT_TYPE_NAME(t) GB_TYPES_name[t] = #t |
|---|
| 33 | |
|---|
| 34 | static const char *GB_TYPES_2_name(GB_TYPES type) { |
|---|
| 35 | static const char *GB_TYPES_name[GB_TYPE_MAX]; |
|---|
| 36 | static bool initialized = false; |
|---|
| 37 | |
|---|
| 38 | if (!initialized) { |
|---|
| 39 | memset(GB_TYPES_name, 0, sizeof(GB_TYPES_name)); |
|---|
| 40 | INIT_TYPE_NAME(GB_NONE); |
|---|
| 41 | INIT_TYPE_NAME(GB_BIT); |
|---|
| 42 | INIT_TYPE_NAME(GB_BYTE); |
|---|
| 43 | INIT_TYPE_NAME(GB_INT); |
|---|
| 44 | INIT_TYPE_NAME(GB_FLOAT); |
|---|
| 45 | INIT_TYPE_NAME(GB_POINTER); |
|---|
| 46 | INIT_TYPE_NAME(GB_BITS); |
|---|
| 47 | INIT_TYPE_NAME(GB_BYTES); |
|---|
| 48 | INIT_TYPE_NAME(GB_INTS); |
|---|
| 49 | INIT_TYPE_NAME(GB_FLOATS); |
|---|
| 50 | INIT_TYPE_NAME(GB_LINK); |
|---|
| 51 | INIT_TYPE_NAME(GB_STRING); |
|---|
| 52 | INIT_TYPE_NAME(GB_STRING_SHRT); |
|---|
| 53 | INIT_TYPE_NAME(GB_DB); |
|---|
| 54 | initialized = true; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | const char *name = NULL; |
|---|
| 58 | if (type >= 0 && type<GB_TYPE_MAX) name = GB_TYPES_name[type]; |
|---|
| 59 | if (!name) { |
|---|
| 60 | static char *unknownType = 0; |
|---|
| 61 | freeset(unknownType, GBS_global_string_copy("<invalid-type=%i>", type)); |
|---|
| 62 | name = unknownType; |
|---|
| 63 | } |
|---|
| 64 | return name; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | const char *GB_get_type_name(GBDATA *gbd) { |
|---|
| 68 | return GB_TYPES_2_name(gbd->type()); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | inline GB_ERROR gb_transactable_type(GB_TYPES type, GBDATA *gbd) { |
|---|
| 72 | GB_ERROR error = NULL; |
|---|
| 73 | if (GB_MAIN(gbd)->get_transaction_level() == 0) { |
|---|
| 74 | error = "No transaction running"; |
|---|
| 75 | } |
|---|
| 76 | else if (GB_ARRAY_FLAGS(gbd).changed == GB_DELETED) { |
|---|
| 77 | error = "Entry has been deleted"; |
|---|
| 78 | } |
|---|
| 79 | else { |
|---|
| 80 | GB_TYPES gb_type = gbd->type(); |
|---|
| 81 | if (gb_type != type && (type != GB_STRING || gb_type != GB_LINK)) { |
|---|
| 82 | char *rtype = strdup(GB_TYPES_2_name(type)); |
|---|
| 83 | char *rgb_type = strdup(GB_TYPES_2_name(gb_type)); |
|---|
| 84 | |
|---|
| 85 | error = GBS_global_string("type mismatch (want='%s', got='%s') in '%s'", rtype, rgb_type, GB_get_db_path(gbd)); |
|---|
| 86 | |
|---|
| 87 | free(rgb_type); |
|---|
| 88 | free(rtype); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | if (error) { |
|---|
| 92 | GBK_dump_backtrace(stderr, error); // it's a bug: none of the above errors should ever happen |
|---|
| 93 | gb_assert(0); |
|---|
| 94 | } |
|---|
| 95 | return error; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | __ATTR__USERESULT static GB_ERROR gb_security_error(GBDATA *gbd) { |
|---|
| 99 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 100 | const char *error = GBS_global_string("Protection: Attempt to change a level-%i-'%s'-entry,\n" |
|---|
| 101 | "but your current security level is only %i", |
|---|
| 102 | GB_GET_SECURITY_WRITE(gbd), |
|---|
| 103 | GB_read_key_pntr(gbd), |
|---|
| 104 | Main->security_level); |
|---|
| 105 | #if defined(DEBUG) |
|---|
| 106 | fprintf(stderr, "%s\n", error); |
|---|
| 107 | #endif // DEBUG |
|---|
| 108 | return error; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | inline GB_ERROR gb_type_writeable_to(GB_TYPES type, GBDATA *gbd) { |
|---|
| 112 | GB_ERROR error = gb_transactable_type(type, gbd); |
|---|
| 113 | if (!error) { |
|---|
| 114 | if (GB_GET_SECURITY_WRITE(gbd) > GB_MAIN(gbd)->security_level) { |
|---|
| 115 | error = gb_security_error(gbd); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | return error; |
|---|
| 119 | } |
|---|
| 120 | inline GB_ERROR gb_type_readable_from(GB_TYPES type, GBDATA *gbd) { |
|---|
| 121 | return gb_transactable_type(type, gbd); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | inline GB_ERROR error_with_dbentry(const char *action, GBDATA *gbd, GB_ERROR error) { |
|---|
| 125 | if (error) { |
|---|
| 126 | char *error_copy = strdup(error); |
|---|
| 127 | const char *path = GB_get_db_path(gbd); |
|---|
| 128 | error = GBS_global_string("Can't %s '%s':\n%s", action, path, error_copy); |
|---|
| 129 | free(error_copy); |
|---|
| 130 | } |
|---|
| 131 | return error; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | #define RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd, type) \ |
|---|
| 136 | do { \ |
|---|
| 137 | GB_ERROR error = gb_type_writeable_to(type, gbd); \ |
|---|
| 138 | if (error) { \ |
|---|
| 139 | return error_with_dbentry("write", gbd, error); \ |
|---|
| 140 | } \ |
|---|
| 141 | } while(0) |
|---|
| 142 | |
|---|
| 143 | #define EXPORT_ERROR_AND_RETURN_0_IF_NOT_READABLE_AS_TYPE(gbd, type) \ |
|---|
| 144 | do { \ |
|---|
| 145 | GB_ERROR error = gb_type_readable_from(type, gbd); \ |
|---|
| 146 | if (error) { \ |
|---|
| 147 | error = error_with_dbentry("read", gbd, error); \ |
|---|
| 148 | GB_export_error(error); \ |
|---|
| 149 | return 0; \ |
|---|
| 150 | } \ |
|---|
| 151 | } while(0) \ |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | #if defined(WARN_TODO) |
|---|
| 155 | #warning replace GB_TEST_READ / GB_TEST_READ by new names later |
|---|
| 156 | #endif |
|---|
| 157 | |
|---|
| 158 | #define GB_TEST_READ(gbd, type, ignored) EXPORT_ERROR_AND_RETURN_0_IF_NOT_READABLE_AS_TYPE(gbd, type) |
|---|
| 159 | #define GB_TEST_WRITE(gbd, type, ignored) RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd, type) |
|---|
| 160 | |
|---|
| 161 | #define GB_TEST_NON_BUFFER(x, gerror) \ |
|---|
| 162 | do { \ |
|---|
| 163 | if (GB_is_in_buffer(x)) { \ |
|---|
| 164 | GBK_terminatef("%s: you are not allowed to write any data, which you get by pntr", gerror); \ |
|---|
| 165 | } \ |
|---|
| 166 | } while (0) |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | static GB_ERROR GB_safe_atof(const char *str, double *res) { |
|---|
| 170 | GB_ERROR error = NULL; |
|---|
| 171 | char *end; |
|---|
| 172 | *res = strtod(str, &end); |
|---|
| 173 | if (end == str || end[0] != 0) { |
|---|
| 174 | if (!str[0]) { |
|---|
| 175 | *res = 0.0; |
|---|
| 176 | } |
|---|
| 177 | else { |
|---|
| 178 | error = GBS_global_string("cannot convert '%s' to double", str); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | return error; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | double GB_atof(const char *str) { |
|---|
| 185 | // convert ASCII to double |
|---|
| 186 | double res = 0; |
|---|
| 187 | GB_ERROR err = GB_safe_atof(str, &res); |
|---|
| 188 | if (err) { |
|---|
| 189 | // expected double in 'str'- better use GB_safe_atof() |
|---|
| 190 | GBK_terminatef("GB_safe_atof(\"%s\", ..) returns error: %s", str, err); |
|---|
| 191 | } |
|---|
| 192 | return res; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | // --------------------------- |
|---|
| 196 | // compression tables |
|---|
| 197 | |
|---|
| 198 | const int gb_convert_type_2_compression_flags[] = { |
|---|
| 199 | GB_COMPRESSION_NONE, // GB_NONE 0 |
|---|
| 200 | GB_COMPRESSION_NONE, // GB_BIT 1 |
|---|
| 201 | GB_COMPRESSION_NONE, // GB_BYTE 2 |
|---|
| 202 | GB_COMPRESSION_NONE, // GB_INT 3 |
|---|
| 203 | GB_COMPRESSION_NONE, // GB_FLOAT 4 |
|---|
| 204 | GB_COMPRESSION_NONE, // GB_?? 5 |
|---|
| 205 | GB_COMPRESSION_BITS, // GB_BITS 6 |
|---|
| 206 | GB_COMPRESSION_NONE, // GB_?? 7 |
|---|
| 207 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN, // GB_BYTES 8 |
|---|
| 208 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_INTS 9 |
|---|
| 209 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_FLTS 10 |
|---|
| 210 | GB_COMPRESSION_NONE, // GB_LINK 11 |
|---|
| 211 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_DICTIONARY, // GB_STR 12 |
|---|
| 212 | GB_COMPRESSION_NONE, // GB_STRS 13 |
|---|
| 213 | GB_COMPRESSION_NONE, // GB?? 14 |
|---|
| 214 | GB_COMPRESSION_NONE // GB_DB 15 |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | int gb_convert_type_2_sizeof[] = { /* contains the unit-size of data stored in DB, |
|---|
| 218 | * i.e. realsize = unit_size * size() |
|---|
| 219 | */ |
|---|
| 220 | 0, // GB_NONE 0 |
|---|
| 221 | 0, // GB_BIT 1 |
|---|
| 222 | sizeof(char), // GB_BYTE 2 |
|---|
| 223 | sizeof(int), // GB_INT 3 |
|---|
| 224 | sizeof(float), // GB_FLOAT 4 |
|---|
| 225 | 0, // GB_?? 5 |
|---|
| 226 | 0, // GB_BITS 6 |
|---|
| 227 | 0, // GB_?? 7 |
|---|
| 228 | sizeof(char), // GB_BYTES 8 |
|---|
| 229 | sizeof(int), // GB_INTS 9 |
|---|
| 230 | sizeof(float), // GB_FLTS 10 |
|---|
| 231 | sizeof(char), // GB_LINK 11 |
|---|
| 232 | sizeof(char), // GB_STR 12 |
|---|
| 233 | sizeof(char), // GB_STRS 13 |
|---|
| 234 | 0, // GB_?? 14 |
|---|
| 235 | 0, // GB_DB 15 |
|---|
| 236 | }; |
|---|
| 237 | |
|---|
| 238 | int gb_convert_type_2_appendix_size[] = { /* contains the size of the suffix (aka terminator element) |
|---|
| 239 | * size is in bytes |
|---|
| 240 | */ |
|---|
| 241 | |
|---|
| 242 | 0, // GB_NONE 0 |
|---|
| 243 | 0, // GB_BIT 1 |
|---|
| 244 | 0, // GB_BYTE 2 |
|---|
| 245 | 0, // GB_INT 3 |
|---|
| 246 | 0, // GB_FLOAT 4 |
|---|
| 247 | 0, // GB_?? 5 |
|---|
| 248 | 0, // GB_BITS 6 |
|---|
| 249 | 0, // GB_?? 7 |
|---|
| 250 | 0, // GB_BYTES 8 |
|---|
| 251 | 0, // GB_INTS 9 |
|---|
| 252 | 0, // GB_FLTS 10 |
|---|
| 253 | 1, // GB_LINK 11 (zero terminated) |
|---|
| 254 | 1, // GB_STR 12 (zero terminated) |
|---|
| 255 | 1, // GB_STRS 13 (zero terminated) |
|---|
| 256 | 0, // GB_?? 14 |
|---|
| 257 | 0, // GB_DB 15 |
|---|
| 258 | }; |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | // --------------------------------- |
|---|
| 262 | // local buffer management |
|---|
| 263 | |
|---|
| 264 | static void init_buffer(gb_buffer *buf, size_t initial_size) { |
|---|
| 265 | buf->size = initial_size; |
|---|
| 266 | buf->mem = buf->size ? (char*)malloc(buf->size) : NULL; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | static char *check_out_buffer(gb_buffer *buf) { |
|---|
| 270 | char *checkOut = buf->mem; |
|---|
| 271 | |
|---|
| 272 | buf->mem = 0; |
|---|
| 273 | buf->size = 0; |
|---|
| 274 | |
|---|
| 275 | return checkOut; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | static void alloc_buffer(gb_buffer *buf, size_t size) { |
|---|
| 279 | free(buf->mem); |
|---|
| 280 | buf->size = size; |
|---|
| 281 | #if (MEMORY_TEST==1) |
|---|
| 282 | buf->mem = (char *)malloc(buf->size); |
|---|
| 283 | #else |
|---|
| 284 | buf->mem = (char *)GB_calloc(buf->size, 1); |
|---|
| 285 | #endif |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | static GB_BUFFER give_buffer(gb_buffer *buf, size_t size) { |
|---|
| 289 | #if (MEMORY_TEST==1) |
|---|
| 290 | alloc_buffer(buf, size); // do NOT reuse buffer if testing memory |
|---|
| 291 | #else |
|---|
| 292 | if (size >= buf->size) { |
|---|
| 293 | alloc_buffer(buf, size); |
|---|
| 294 | } |
|---|
| 295 | #endif |
|---|
| 296 | return buf->mem; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | static int is_in_buffer(gb_buffer *buf, GB_CBUFFER ptr) { |
|---|
| 300 | return ptr >= buf->mem && ptr < buf->mem+buf->size; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | // ------------------------------ |
|---|
| 304 | |
|---|
| 305 | GB_BUFFER GB_give_buffer(size_t size) { |
|---|
| 306 | // return a pointer to a static piece of memory at least size bytes long |
|---|
| 307 | return give_buffer(&gb_local->buf1, size); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | GB_BUFFER GB_increase_buffer(size_t size) { |
|---|
| 311 | if (size < gb_local->buf1.size) { |
|---|
| 312 | char *old_buffer = gb_local->buf1.mem; |
|---|
| 313 | size_t old_size = gb_local->buf1.size; |
|---|
| 314 | |
|---|
| 315 | gb_local->buf1.mem = NULL; |
|---|
| 316 | alloc_buffer(&gb_local->buf1, size); |
|---|
| 317 | memcpy(gb_local->buf1.mem, old_buffer, old_size); |
|---|
| 318 | |
|---|
| 319 | free(old_buffer); |
|---|
| 320 | } |
|---|
| 321 | return gb_local->buf1.mem; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | NOT4PERL int GB_give_buffer_size() { |
|---|
| 325 | return gb_local->buf1.size; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | GB_BUFFER GB_give_buffer2(long size) { |
|---|
| 329 | return give_buffer(&gb_local->buf2, size); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | static int GB_is_in_buffer(GB_CBUFFER ptr) { |
|---|
| 333 | /* returns 1 or 2 if 'ptr' points to gb_local->buf1/buf2 |
|---|
| 334 | * returns 0 otherwise |
|---|
| 335 | */ |
|---|
| 336 | int buffer = 0; |
|---|
| 337 | |
|---|
| 338 | if (is_in_buffer(&gb_local->buf1, ptr)) buffer = 1; |
|---|
| 339 | else if (is_in_buffer(&gb_local->buf2, ptr)) buffer = 2; |
|---|
| 340 | |
|---|
| 341 | return buffer; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | char *GB_check_out_buffer(GB_CBUFFER buffer) { |
|---|
| 345 | /* Check a piece of memory out of the buffer management |
|---|
| 346 | * after it is checked out, the user has the full control to use and free it |
|---|
| 347 | * Returns a pointer to the start of the buffer (even if 'buffer' points inside the buffer!) |
|---|
| 348 | */ |
|---|
| 349 | char *old = 0; |
|---|
| 350 | |
|---|
| 351 | if (is_in_buffer(&gb_local->buf1, buffer)) old = check_out_buffer(&gb_local->buf1); |
|---|
| 352 | else if (is_in_buffer(&gb_local->buf2, buffer)) old = check_out_buffer(&gb_local->buf2); |
|---|
| 353 | |
|---|
| 354 | return old; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | GB_BUFFER GB_give_other_buffer(GB_CBUFFER buffer, long size) { |
|---|
| 358 | return is_in_buffer(&gb_local->buf1, buffer) |
|---|
| 359 | ? GB_give_buffer2(size) |
|---|
| 360 | : GB_give_buffer(size); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | static unsigned char GB_BIT_compress_data[] = { |
|---|
| 364 | 0x1d, GB_CS_OK, 0, 0, |
|---|
| 365 | 0x04, GB_CS_OK, 0, 1, |
|---|
| 366 | 0x0a, GB_CS_OK, 0, 2, |
|---|
| 367 | 0x0b, GB_CS_OK, 0, 3, |
|---|
| 368 | 0x0c, GB_CS_OK, 0, 4, |
|---|
| 369 | 0x1a, GB_CS_OK, 0, 5, |
|---|
| 370 | 0x1b, GB_CS_OK, 0, 6, |
|---|
| 371 | 0x1c, GB_CS_OK, 0, 7, |
|---|
| 372 | 0xf0, GB_CS_OK, 0, 8, |
|---|
| 373 | 0xf1, GB_CS_OK, 0, 9, |
|---|
| 374 | 0xf2, GB_CS_OK, 0, 10, |
|---|
| 375 | 0xf3, GB_CS_OK, 0, 11, |
|---|
| 376 | 0xf4, GB_CS_OK, 0, 12, |
|---|
| 377 | 0xf5, GB_CS_OK, 0, 13, |
|---|
| 378 | 0xf6, GB_CS_OK, 0, 14, |
|---|
| 379 | 0xf7, GB_CS_OK, 0, 15, |
|---|
| 380 | 0xf8, GB_CS_SUB, 0, 16, |
|---|
| 381 | 0xf9, GB_CS_SUB, 0, 32, |
|---|
| 382 | 0xfa, GB_CS_SUB, 0, 48, |
|---|
| 383 | 0xfb, GB_CS_SUB, 0, 64, |
|---|
| 384 | 0xfc, GB_CS_SUB, 0, 128, |
|---|
| 385 | 0xfd, GB_CS_SUB, 1, 0, |
|---|
| 386 | 0xfe, GB_CS_SUB, 2, 0, |
|---|
| 387 | 0xff, GB_CS_SUB, 4, 0, |
|---|
| 388 | 0 |
|---|
| 389 | }; |
|---|
| 390 | |
|---|
| 391 | struct gb_exitfun { |
|---|
| 392 | void (*exitfun)(); |
|---|
| 393 | gb_exitfun *next; |
|---|
| 394 | }; |
|---|
| 395 | |
|---|
| 396 | void GB_atexit(void (*exitfun)()) { |
|---|
| 397 | // called when GB_shell is destroyed (use similar to atexit()) |
|---|
| 398 | // |
|---|
| 399 | // Since the program does not neccessarily terminate, your code calling |
|---|
| 400 | // GB_atexit() may run multiple times. Make sure everything is completely reset by your 'exitfun' |
|---|
| 401 | |
|---|
| 402 | gb_exitfun *fun = new gb_exitfun; |
|---|
| 403 | fun->exitfun = exitfun; |
|---|
| 404 | |
|---|
| 405 | fun->next = gb_local->atgbexit; |
|---|
| 406 | gb_local->atgbexit = fun; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | static void run_and_destroy_exit_functions(gb_exitfun *fun) { |
|---|
| 410 | if (fun) { |
|---|
| 411 | fun->exitfun(); |
|---|
| 412 | run_and_destroy_exit_functions(fun->next); |
|---|
| 413 | delete fun; |
|---|
| 414 | } |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | static void GB_exit_gb() { |
|---|
| 418 | GB_shell::ensure_inside(); |
|---|
| 419 | |
|---|
| 420 | if (gb_local) { |
|---|
| 421 | gb_local->~gb_local_data(); // inplace-dtor |
|---|
| 422 | gbm_free_mem(gb_local, sizeof(*gb_local), 0); |
|---|
| 423 | gb_local = NULL; |
|---|
| 424 | gbm_flush_mem(); |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | gb_local_data::~gb_local_data() { |
|---|
| 429 | gb_assert(openedDBs == closedDBs); |
|---|
| 430 | |
|---|
| 431 | run_and_destroy_exit_functions(atgbexit); |
|---|
| 432 | |
|---|
| 433 | free(bitcompress); |
|---|
| 434 | gb_free_compress_tree(bituncompress); |
|---|
| 435 | free(write_buffer); |
|---|
| 436 | |
|---|
| 437 | free(check_out_buffer(&buf2)); |
|---|
| 438 | free(check_out_buffer(&buf1)); |
|---|
| 439 | free(open_gb_mains); |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | // ----------------- |
|---|
| 443 | // GB_shell |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | static GB_shell *inside_shell = NULL; |
|---|
| 447 | |
|---|
| 448 | GB_shell::GB_shell() { |
|---|
| 449 | if (inside_shell) GBK_terminate("only one GB_shell allowed"); |
|---|
| 450 | inside_shell = this; |
|---|
| 451 | } |
|---|
| 452 | GB_shell::~GB_shell() { |
|---|
| 453 | gb_assert(inside_shell == this); |
|---|
| 454 | GB_exit_gb(); |
|---|
| 455 | inside_shell = NULL; |
|---|
| 456 | } |
|---|
| 457 | void GB_shell::ensure_inside() { if (!inside_shell) GBK_terminate("Not inside GB_shell"); } |
|---|
| 458 | |
|---|
| 459 | bool GB_shell::in_shell() { // used by code based on ARBDB (Kai IIRC) |
|---|
| 460 | return inside_shell; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | struct GB_test_shell_closed { |
|---|
| 464 | ~GB_test_shell_closed() { |
|---|
| 465 | if (GB_shell::in_shell()) { // leave that call |
|---|
| 466 | inside_shell->~GB_shell(); // call dtor |
|---|
| 467 | } |
|---|
| 468 | } |
|---|
| 469 | }; |
|---|
| 470 | static GB_test_shell_closed test; |
|---|
| 471 | |
|---|
| 472 | #if defined(UNIT_TESTS) |
|---|
| 473 | static bool closed_open_shell_for_unit_tests() { |
|---|
| 474 | bool was_open = inside_shell; |
|---|
| 475 | if (was_open) { |
|---|
| 476 | if (gb_local) gb_local->fake_closed_DBs(); |
|---|
| 477 | inside_shell->~GB_shell(); // just call dtor (not delete) |
|---|
| 478 | } |
|---|
| 479 | return was_open; |
|---|
| 480 | } |
|---|
| 481 | #endif |
|---|
| 482 | |
|---|
| 483 | void GB_init_gb() { |
|---|
| 484 | GB_shell::ensure_inside(); |
|---|
| 485 | if (!gb_local) { |
|---|
| 486 | GBK_install_SIGSEGV_handler(true); // never uninstalled |
|---|
| 487 | gbm_init_mem(); |
|---|
| 488 | gb_local = (gb_local_data *)gbm_get_mem(sizeof(gb_local_data), 0); |
|---|
| 489 | ::new(gb_local) gb_local_data(); // inplace-ctor |
|---|
| 490 | } |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | int GB_open_DBs() { return gb_local ? gb_local->open_dbs() : 0; } |
|---|
| 494 | |
|---|
| 495 | gb_local_data::gb_local_data() |
|---|
| 496 | { |
|---|
| 497 | init_buffer(&buf1, 4000); |
|---|
| 498 | init_buffer(&buf2, 4000); |
|---|
| 499 | |
|---|
| 500 | write_bufsize = GBCM_BUFFER; |
|---|
| 501 | write_buffer = (char *)malloc((size_t)write_bufsize); |
|---|
| 502 | write_ptr = write_buffer; |
|---|
| 503 | write_free = write_bufsize; |
|---|
| 504 | |
|---|
| 505 | bituncompress = gb_build_uncompress_tree(GB_BIT_compress_data, 1, 0); |
|---|
| 506 | bitcompress = gb_build_compress_list(GB_BIT_compress_data, 1, &(bc_size)); |
|---|
| 507 | |
|---|
| 508 | openedDBs = 0; |
|---|
| 509 | closedDBs = 0; |
|---|
| 510 | |
|---|
| 511 | open_gb_mains = NULL; |
|---|
| 512 | open_gb_alloc = 0; |
|---|
| 513 | |
|---|
| 514 | atgbexit = NULL; |
|---|
| 515 | |
|---|
| 516 | iamclient = false; |
|---|
| 517 | search_system_folder = false; |
|---|
| 518 | running_client_transaction = ARB_NO_TRANS; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | void gb_local_data::announce_db_open(GB_MAIN_TYPE *Main) { |
|---|
| 522 | gb_assert(Main); |
|---|
| 523 | int idx = open_dbs(); |
|---|
| 524 | if (idx >= open_gb_alloc) { |
|---|
| 525 | int new_alloc = open_gb_alloc+10; |
|---|
| 526 | GB_MAIN_TYPE **new_mains = (GB_MAIN_TYPE**)realloc(open_gb_mains, new_alloc*sizeof(*new_mains)); |
|---|
| 527 | memset(new_mains+open_gb_alloc, 0, 10*sizeof(*new_mains)); |
|---|
| 528 | open_gb_alloc = new_alloc; |
|---|
| 529 | open_gb_mains = new_mains; |
|---|
| 530 | } |
|---|
| 531 | open_gb_mains[idx] = Main; |
|---|
| 532 | openedDBs++; |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | void gb_local_data::announce_db_close(GB_MAIN_TYPE *Main) { |
|---|
| 536 | gb_assert(Main); |
|---|
| 537 | int open = open_dbs(); |
|---|
| 538 | int idx; |
|---|
| 539 | for (idx = 0; idx<open; ++idx) if (open_gb_mains[idx] == Main) break; |
|---|
| 540 | |
|---|
| 541 | gb_assert(idx<open); // passed gb_main is unknown |
|---|
| 542 | if (idx<open) { |
|---|
| 543 | if (idx<(open-1)) { // not last |
|---|
| 544 | open_gb_mains[idx] = open_gb_mains[open-1]; |
|---|
| 545 | } |
|---|
| 546 | closedDBs++; |
|---|
| 547 | } |
|---|
| 548 | if (closedDBs == openedDBs) { |
|---|
| 549 | GB_exit_gb(); // free most memory allocated by ARBDB library |
|---|
| 550 | // Caution: calling GB_exit_gb() frees 'this'! |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | static GBDATA *gb_remembered_db() { |
|---|
| 555 | GB_MAIN_TYPE *Main = gb_local ? gb_local->get_any_open_db() : NULL; |
|---|
| 556 | return Main ? Main->gb_main() : NULL; |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | GB_ERROR gb_unfold(GBCONTAINER *gbc, long deep, int index_pos) { |
|---|
| 560 | /*! get data from server. |
|---|
| 561 | * |
|---|
| 562 | * @param gbc container to unfold |
|---|
| 563 | * @param deep if != 0, then get subitems too. |
|---|
| 564 | * @param index_pos |
|---|
| 565 | * - >= 0, get indexed item from server |
|---|
| 566 | * - <0, get all items |
|---|
| 567 | * |
|---|
| 568 | * @return error on failure |
|---|
| 569 | */ |
|---|
| 570 | |
|---|
| 571 | GB_ERROR error; |
|---|
| 572 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
|---|
| 573 | |
|---|
| 574 | if (!gbc->flags2.folded_container) return 0; |
|---|
| 575 | if (index_pos> gbc->d.nheader) gb_create_header_array(gbc, index_pos + 1); |
|---|
| 576 | if (index_pos >= 0 && GB_HEADER_LIST_GBD(header[index_pos])) return 0; |
|---|
| 577 | |
|---|
| 578 | if (GBCONTAINER_MAIN(gbc)->is_server()) { |
|---|
| 579 | GB_internal_error("Cannot unfold in server"); |
|---|
| 580 | return 0; |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | do { |
|---|
| 584 | if (index_pos<0) break; |
|---|
| 585 | if (index_pos >= gbc->d.nheader) break; |
|---|
| 586 | if (header[index_pos].flags.changed >= GB_DELETED) { |
|---|
| 587 | GB_internal_error("Tried to unfold a deleted item"); |
|---|
| 588 | return 0; |
|---|
| 589 | } |
|---|
| 590 | if (GB_HEADER_LIST_GBD(header[index_pos])) return 0; // already unfolded |
|---|
| 591 | } while (0); |
|---|
| 592 | |
|---|
| 593 | error = gbcm_unfold_client(gbc, deep, index_pos); |
|---|
| 594 | if (error) { |
|---|
| 595 | GB_print_error(); |
|---|
| 596 | return error; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | if (index_pos<0) { |
|---|
| 600 | gb_untouch_children(gbc); |
|---|
| 601 | gbc->flags2.folded_container = 0; |
|---|
| 602 | } |
|---|
| 603 | else { |
|---|
| 604 | GBDATA *gb2 = GBCONTAINER_ELEM(gbc, index_pos); |
|---|
| 605 | if (gb2) { |
|---|
| 606 | if (gb2->is_container()) { |
|---|
| 607 | gb_untouch_children_and_me(gb2->as_container()); |
|---|
| 608 | } |
|---|
| 609 | else { |
|---|
| 610 | gb_untouch_me(gb2->as_entry()); |
|---|
| 611 | } |
|---|
| 612 | } |
|---|
| 613 | } |
|---|
| 614 | return 0; |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | // ----------------------- |
|---|
| 618 | // close database |
|---|
| 619 | |
|---|
| 620 | typedef void (*gb_close_callback)(GBDATA *gb_main, void *client_data); |
|---|
| 621 | |
|---|
| 622 | struct gb_close_callback_list { |
|---|
| 623 | gb_close_callback_list *next; |
|---|
| 624 | gb_close_callback cb; |
|---|
| 625 | void *client_data; |
|---|
| 626 | }; |
|---|
| 627 | |
|---|
| 628 | #if defined(ASSERTION_USED) |
|---|
| 629 | static bool atclose_cb_exists(gb_close_callback_list *gccs, gb_close_callback cb) { |
|---|
| 630 | return gccs && (gccs->cb == cb || atclose_cb_exists(gccs->next, cb)); |
|---|
| 631 | } |
|---|
| 632 | #endif // ASSERTION_USED |
|---|
| 633 | |
|---|
| 634 | void GB_atclose(GBDATA *gbd, void (*fun)(GBDATA *gb_main, void *client_data), void *client_data) { |
|---|
| 635 | /*! Add a callback, which gets called directly before GB_close destroys all data. |
|---|
| 636 | * This is the recommended way to remove all callbacks from DB elements. |
|---|
| 637 | */ |
|---|
| 638 | |
|---|
| 639 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 640 | |
|---|
| 641 | gb_assert(!atclose_cb_exists(Main->close_callbacks, fun)); // each close callback should only exist once |
|---|
| 642 | |
|---|
| 643 | gb_close_callback_list *gccs = (gb_close_callback_list *)malloc(sizeof(*gccs)); |
|---|
| 644 | |
|---|
| 645 | gccs->next = Main->close_callbacks; |
|---|
| 646 | gccs->cb = fun; |
|---|
| 647 | gccs->client_data = client_data; |
|---|
| 648 | |
|---|
| 649 | Main->close_callbacks = gccs; |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | static void run_close_callbacks(GBDATA *gb_main, gb_close_callback_list *gccs) { |
|---|
| 653 | while (gccs) { |
|---|
| 654 | gccs->cb(gb_main, gccs->client_data); |
|---|
| 655 | gb_close_callback_list *next = gccs->next; |
|---|
| 656 | free(gccs); |
|---|
| 657 | gccs = next; |
|---|
| 658 | } |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | static gb_triggered_callback *currently_called_back = NULL; // points to callback during callback; NULL otherwise |
|---|
| 662 | static GB_MAIN_TYPE *inside_callback_main = NULL; // points to DB root during callback; NULL otherwise |
|---|
| 663 | |
|---|
| 664 | void gb_pending_callbacks::call_and_forget(GB_CB_TYPE allowedTypes) { |
|---|
| 665 | #if defined(ASSERTION_USED) |
|---|
| 666 | const gb_triggered_callback *tail = get_tail(); |
|---|
| 667 | #endif |
|---|
| 668 | |
|---|
| 669 | for (itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
|---|
| 670 | currently_called_back = &*cb; |
|---|
| 671 | gb_assert(currently_called_back); |
|---|
| 672 | currently_called_back->spec(cb->gbd, allowedTypes); |
|---|
| 673 | currently_called_back = NULL; |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | gb_assert(tail == get_tail()); |
|---|
| 677 | |
|---|
| 678 | callbacks.clear(); |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | void GB_MAIN_TYPE::call_pending_callbacks() { |
|---|
| 682 | inside_callback_main = this; |
|---|
| 683 | |
|---|
| 684 | deleteCBs.pending.call_and_forget(GB_CB_DELETE); // first all delete callbacks: |
|---|
| 685 | changeCBs.pending.call_and_forget(GB_CB_ALL_BUT_DELETE); // then all change callbacks: |
|---|
| 686 | |
|---|
| 687 | inside_callback_main = NULL; |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | inline void GB_MAIN_TYPE::callback_group::forget_hcbs() { |
|---|
| 691 | delete hierarchy_cbs; |
|---|
| 692 | hierarchy_cbs = NULL; |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | void GB_MAIN_TYPE::forget_hierarchy_cbs() { |
|---|
| 696 | changeCBs.forget_hcbs(); |
|---|
| 697 | deleteCBs.forget_hcbs(); |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | void GB_close(GBDATA *gbd) { |
|---|
| 701 | GB_ERROR error = NULL; |
|---|
| 702 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 703 | |
|---|
| 704 | gb_assert(Main->get_transaction_level() <= 0); // transaction running - you can't close DB yet! |
|---|
| 705 | |
|---|
| 706 | Main->forget_hierarchy_cbs(); |
|---|
| 707 | |
|---|
| 708 | gb_assert(Main->gb_main() == gbd); |
|---|
| 709 | run_close_callbacks(gbd, Main->close_callbacks); |
|---|
| 710 | Main->close_callbacks = 0; |
|---|
| 711 | |
|---|
| 712 | if (Main->is_client()) { |
|---|
| 713 | long result = gbcmc_close(Main->c_link); |
|---|
| 714 | if (result != 0) error = GBS_global_string("gbcmc_close returns %li", result); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | gbcm_logout(Main, NULL); // logout default user |
|---|
| 718 | |
|---|
| 719 | if (!error) { |
|---|
| 720 | gb_assert(Main->close_callbacks == 0); |
|---|
| 721 | |
|---|
| 722 | gb_delete_dummy_father(Main->dummy_father); |
|---|
| 723 | Main->root_container = NULL; |
|---|
| 724 | |
|---|
| 725 | /* ARBDB applications using awars easily crash in call_pending_callbacks(), |
|---|
| 726 | * if AWARs are still bound to elements in the closed database. |
|---|
| 727 | * |
|---|
| 728 | * To unlink awars call AW_root::unlink_awars_from_DB(). |
|---|
| 729 | * If that doesn't help, test Main->data (often aka as GLOBAL_gb_main) |
|---|
| 730 | */ |
|---|
| 731 | Main->call_pending_callbacks(); // do all callbacks |
|---|
| 732 | delete Main; |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | if (error) { |
|---|
| 736 | GB_warningf("Error in GB_close: %s", error); |
|---|
| 737 | } |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | void gb_close_unclosed_DBs() { |
|---|
| 741 | GBDATA *gb_main; |
|---|
| 742 | while ((gb_main = gb_remembered_db())) { |
|---|
| 743 | GB_close(gb_main); |
|---|
| 744 | } |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | // ------------------ |
|---|
| 748 | // read data |
|---|
| 749 | |
|---|
| 750 | long GB_read_int(GBDATA *gbd) |
|---|
| 751 | { |
|---|
| 752 | GB_TEST_READ(gbd, GB_INT, "GB_read_int"); |
|---|
| 753 | return gbd->as_entry()->info.i; |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | int GB_read_byte(GBDATA *gbd) |
|---|
| 757 | { |
|---|
| 758 | GB_TEST_READ(gbd, GB_BYTE, "GB_read_byte"); |
|---|
| 759 | return gbd->as_entry()->info.i; |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | GBDATA *GB_read_pointer(GBDATA *gbd) { |
|---|
| 763 | GB_TEST_READ(gbd, GB_POINTER, "GB_read_pointer"); |
|---|
| 764 | return gbd->as_entry()->info.ptr; |
|---|
| 765 | } |
|---|
| 766 | |
|---|
| 767 | double GB_read_float(GBDATA *gbd) // @@@ change return type to float (that's what's stored in DB) |
|---|
| 768 | { |
|---|
| 769 | XDR xdrs; |
|---|
| 770 | static float f; // @@@ why static? |
|---|
| 771 | |
|---|
| 772 | GB_TEST_READ(gbd, GB_FLOAT, "GB_read_float"); |
|---|
| 773 | xdrmem_create(&xdrs, &gbd->as_entry()->info.in.data[0], SIZOFINTERN, XDR_DECODE); |
|---|
| 774 | xdr_float(&xdrs, &f); |
|---|
| 775 | xdr_destroy(&xdrs); |
|---|
| 776 | |
|---|
| 777 | gb_assert(f == f); // !nan |
|---|
| 778 | |
|---|
| 779 | return (double)f; |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | long GB_read_count(GBDATA *gbd) { |
|---|
| 783 | return gbd->as_entry()->size(); |
|---|
| 784 | } |
|---|
| 785 | |
|---|
| 786 | long GB_read_memuse(GBDATA *gbd) { |
|---|
| 787 | return gbd->as_entry()->memsize(); |
|---|
| 788 | } |
|---|
| 789 | |
|---|
| 790 | #if defined(DEBUG) |
|---|
| 791 | |
|---|
| 792 | #define MIN_CBLISTNODE_SIZE 48 // minimum (found) callbacklist-elementsize |
|---|
| 793 | |
|---|
| 794 | #if defined(DARWIN) |
|---|
| 795 | |
|---|
| 796 | #define CBLISTNODE_SIZE MIN_CBLISTNODE_SIZE // assume known minimum (doesnt really matter; only used in db-browser) |
|---|
| 797 | |
|---|
| 798 | #else // linux: |
|---|
| 799 | |
|---|
| 800 | typedef std::_List_node<gb_callback_list::cbtype> CBLISTNODE_TYPE; |
|---|
| 801 | const size_t CBLISTNODE_SIZE = sizeof(CBLISTNODE_TYPE); |
|---|
| 802 | |
|---|
| 803 | #if defined(ARB_64) |
|---|
| 804 | // ignore smaller 32-bit implementations |
|---|
| 805 | STATIC_ASSERT_ANNOTATED(MIN_CBLISTNODE_SIZE<=CBLISTNODE_SIZE, "MIN_CBLISTNODE_SIZE too big (smaller implementation detected)"); |
|---|
| 806 | #endif |
|---|
| 807 | |
|---|
| 808 | #endif |
|---|
| 809 | |
|---|
| 810 | inline long calc_size(gb_callback_list *gbcbl) { |
|---|
| 811 | return gbcbl |
|---|
| 812 | ? sizeof(*gbcbl) + gbcbl->callbacks.size()* CBLISTNODE_SIZE |
|---|
| 813 | : 0; |
|---|
| 814 | } |
|---|
| 815 | inline long calc_size(gb_transaction_save *gbts) { |
|---|
| 816 | return gbts |
|---|
| 817 | ? sizeof(*gbts) |
|---|
| 818 | : 0; |
|---|
| 819 | } |
|---|
| 820 | inline long calc_size(gb_if_entries *gbie) { |
|---|
| 821 | return gbie |
|---|
| 822 | ? sizeof(*gbie) + calc_size(GB_IF_ENTRIES_NEXT(gbie)) |
|---|
| 823 | : 0; |
|---|
| 824 | } |
|---|
| 825 | inline long calc_size(GB_REL_IFES *gbri, int table_size) { |
|---|
| 826 | long size = 0; |
|---|
| 827 | |
|---|
| 828 | gb_if_entries *ifes; |
|---|
| 829 | for (int idx = 0; idx<table_size; ++idx) { |
|---|
| 830 | for (ifes = GB_ENTRIES_ENTRY(gbri, idx); |
|---|
| 831 | ifes; |
|---|
| 832 | ifes = GB_IF_ENTRIES_NEXT(ifes)) |
|---|
| 833 | { |
|---|
| 834 | size += calc_size(ifes); |
|---|
| 835 | } |
|---|
| 836 | } |
|---|
| 837 | return size; |
|---|
| 838 | } |
|---|
| 839 | inline long calc_size(gb_index_files *gbif) { |
|---|
| 840 | return gbif |
|---|
| 841 | ? sizeof(*gbif) + calc_size(GB_INDEX_FILES_NEXT(gbif)) + calc_size(GB_INDEX_FILES_ENTRIES(gbif), gbif->hash_table_size) |
|---|
| 842 | : 0; |
|---|
| 843 | } |
|---|
| 844 | inline long calc_size(gb_db_extended *gbe) { |
|---|
| 845 | return gbe |
|---|
| 846 | ? sizeof(*gbe) + calc_size(gbe->callback) + calc_size(gbe->old) |
|---|
| 847 | : 0; |
|---|
| 848 | } |
|---|
| 849 | inline long calc_size(GBENTRY *gbe) { |
|---|
| 850 | return gbe |
|---|
| 851 | ? sizeof(*gbe) + calc_size(gbe->ext) |
|---|
| 852 | : 0; |
|---|
| 853 | } |
|---|
| 854 | inline long calc_size(GBCONTAINER *gbc) { |
|---|
| 855 | return gbc |
|---|
| 856 | ? sizeof(*gbc) + calc_size(gbc->ext) + calc_size(GBCONTAINER_IFS(gbc)) |
|---|
| 857 | : 0; |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | long GB_calc_structure_size(GBDATA *gbd) { |
|---|
| 861 | long size = 0; |
|---|
| 862 | if (gbd->is_container()) { |
|---|
| 863 | size = calc_size(gbd->as_container()); |
|---|
| 864 | } |
|---|
| 865 | else { |
|---|
| 866 | size = calc_size(gbd->as_entry()); |
|---|
| 867 | } |
|---|
| 868 | return size; |
|---|
| 869 | } |
|---|
| 870 | |
|---|
| 871 | void GB_SizeInfo::collect(GBDATA *gbd) { |
|---|
| 872 | if (gbd->is_container()) { |
|---|
| 873 | ++containers; |
|---|
| 874 | for (GBDATA *gb_child = GB_child(gbd); gb_child; gb_child = GB_nextChild(gb_child)) { |
|---|
| 875 | collect(gb_child); |
|---|
| 876 | } |
|---|
| 877 | } |
|---|
| 878 | else { |
|---|
| 879 | ++terminals; |
|---|
| 880 | mem += GB_read_memuse(gbd); |
|---|
| 881 | |
|---|
| 882 | long size; |
|---|
| 883 | switch (gbd->type()) { |
|---|
| 884 | case GB_INT: size = sizeof(int); break; |
|---|
| 885 | case GB_FLOAT: size = sizeof(float); break; |
|---|
| 886 | case GB_BYTE: size = sizeof(char); break; |
|---|
| 887 | case GB_POINTER: size = sizeof(GBDATA*); break; |
|---|
| 888 | case GB_STRING: size = GB_read_count(gbd); break; // accept 0 sized data for strings |
|---|
| 889 | |
|---|
| 890 | default: |
|---|
| 891 | size = GB_read_count(gbd); |
|---|
| 892 | gb_assert(size>0); // terminal w/o data - really? |
|---|
| 893 | break; |
|---|
| 894 | } |
|---|
| 895 | data += size; |
|---|
| 896 | } |
|---|
| 897 | structure += GB_calc_structure_size(gbd); |
|---|
| 898 | } |
|---|
| 899 | #endif |
|---|
| 900 | |
|---|
| 901 | GB_CSTR GB_read_pntr(GBDATA *gbd) { |
|---|
| 902 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 903 | const char *data = gbe->data(); |
|---|
| 904 | |
|---|
| 905 | if (data) { |
|---|
| 906 | if (gbe->flags.compressed_data) { // uncompressed data return pntr to database entry |
|---|
| 907 | char *ca = gb_read_cache(gbe); |
|---|
| 908 | |
|---|
| 909 | if (!ca) { |
|---|
| 910 | size_t size = gbe->uncompressed_size(); |
|---|
| 911 | const char *da = gb_uncompress_data(gbe, data, size); |
|---|
| 912 | |
|---|
| 913 | if (da) { |
|---|
| 914 | ca = gb_alloc_cache_index(gbe, size); |
|---|
| 915 | memcpy(ca, da, size); |
|---|
| 916 | } |
|---|
| 917 | } |
|---|
| 918 | data = ca; |
|---|
| 919 | } |
|---|
| 920 | } |
|---|
| 921 | return data; |
|---|
| 922 | } |
|---|
| 923 | |
|---|
| 924 | int gb_read_nr(GBDATA *gbd) { |
|---|
| 925 | return gbd->index; |
|---|
| 926 | } |
|---|
| 927 | |
|---|
| 928 | GB_CSTR GB_read_char_pntr(GBDATA *gbd) { |
|---|
| 929 | GB_TEST_READ(gbd, GB_STRING, "GB_read_char_pntr"); |
|---|
| 930 | return GB_read_pntr(gbd); |
|---|
| 931 | } |
|---|
| 932 | |
|---|
| 933 | char *GB_read_string(GBDATA *gbd) { |
|---|
| 934 | GB_TEST_READ(gbd, GB_STRING, "GB_read_string"); |
|---|
| 935 | const char *d = GB_read_pntr(gbd); |
|---|
| 936 | if (!d) return NULL; |
|---|
| 937 | return GB_memdup(d, gbd->as_entry()->size()+1); |
|---|
| 938 | } |
|---|
| 939 | |
|---|
| 940 | size_t GB_read_string_count(GBDATA *gbd) { |
|---|
| 941 | GB_TEST_READ(gbd, GB_STRING, "GB_read_string_count"); |
|---|
| 942 | return gbd->as_entry()->size(); |
|---|
| 943 | } |
|---|
| 944 | |
|---|
| 945 | GB_CSTR GB_read_link_pntr(GBDATA *gbd) { |
|---|
| 946 | GB_TEST_READ(gbd, GB_LINK, "GB_read_link_pntr"); |
|---|
| 947 | return GB_read_pntr(gbd); |
|---|
| 948 | } |
|---|
| 949 | |
|---|
| 950 | static char *GB_read_link(GBDATA *gbd) { |
|---|
| 951 | const char *d; |
|---|
| 952 | GB_TEST_READ(gbd, GB_LINK, "GB_read_link_pntr"); |
|---|
| 953 | d = GB_read_pntr(gbd); |
|---|
| 954 | if (!d) return NULL; |
|---|
| 955 | return GB_memdup(d, gbd->as_entry()->size()+1); |
|---|
| 956 | } |
|---|
| 957 | |
|---|
| 958 | long GB_read_bits_count(GBDATA *gbd) { |
|---|
| 959 | GB_TEST_READ(gbd, GB_BITS, "GB_read_bits_count"); |
|---|
| 960 | return gbd->as_entry()->size(); |
|---|
| 961 | } |
|---|
| 962 | |
|---|
| 963 | GB_CSTR GB_read_bits_pntr(GBDATA *gbd, char c_0, char c_1) { |
|---|
| 964 | GB_TEST_READ(gbd, GB_BITS, "GB_read_bits_pntr"); |
|---|
| 965 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 966 | long size = gbe->size(); |
|---|
| 967 | if (size) { |
|---|
| 968 | char *ca = gb_read_cache(gbe); |
|---|
| 969 | if (ca) return ca; |
|---|
| 970 | |
|---|
| 971 | ca = gb_alloc_cache_index(gbe, size+1); |
|---|
| 972 | const char *data = gbe->data(); |
|---|
| 973 | char *da = gb_uncompress_bits(data, size, c_0, c_1); |
|---|
| 974 | if (ca) { |
|---|
| 975 | memcpy(ca, da, size+1); |
|---|
| 976 | return ca; |
|---|
| 977 | } |
|---|
| 978 | return da; |
|---|
| 979 | } |
|---|
| 980 | return 0; |
|---|
| 981 | } |
|---|
| 982 | |
|---|
| 983 | char *GB_read_bits(GBDATA *gbd, char c_0, char c_1) { |
|---|
| 984 | GB_CSTR d = GB_read_bits_pntr(gbd, c_0, c_1); |
|---|
| 985 | return d ? GB_memdup(d, gbd->as_entry()->size()+1) : 0; |
|---|
| 986 | } |
|---|
| 987 | |
|---|
| 988 | |
|---|
| 989 | GB_CSTR GB_read_bytes_pntr(GBDATA *gbd) |
|---|
| 990 | { |
|---|
| 991 | GB_TEST_READ(gbd, GB_BYTES, "GB_read_bytes_pntr"); |
|---|
| 992 | return GB_read_pntr(gbd); |
|---|
| 993 | } |
|---|
| 994 | |
|---|
| 995 | long GB_read_bytes_count(GBDATA *gbd) |
|---|
| 996 | { |
|---|
| 997 | GB_TEST_READ(gbd, GB_BYTES, "GB_read_bytes_count"); |
|---|
| 998 | return gbd->as_entry()->size(); |
|---|
| 999 | } |
|---|
| 1000 | |
|---|
| 1001 | char *GB_read_bytes(GBDATA *gbd) { |
|---|
| 1002 | GB_CSTR d = GB_read_bytes_pntr(gbd); |
|---|
| 1003 | return d ? GB_memdup(d, gbd->as_entry()->size()) : 0; |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | GB_CUINT4 *GB_read_ints_pntr(GBDATA *gbd) |
|---|
| 1007 | { |
|---|
| 1008 | GB_TEST_READ(gbd, GB_INTS, "GB_read_ints_pntr"); |
|---|
| 1009 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1010 | |
|---|
| 1011 | GB_UINT4 *res; |
|---|
| 1012 | if (gbe->flags.compressed_data) { |
|---|
| 1013 | res = (GB_UINT4 *)GB_read_pntr(gbe); |
|---|
| 1014 | } |
|---|
| 1015 | else { |
|---|
| 1016 | res = (GB_UINT4 *)gbe->data(); |
|---|
| 1017 | } |
|---|
| 1018 | if (!res) return NULL; |
|---|
| 1019 | |
|---|
| 1020 | if (0x01020304U == htonl(0x01020304U)) { |
|---|
| 1021 | return res; |
|---|
| 1022 | } |
|---|
| 1023 | else { |
|---|
| 1024 | int size = gbe->size(); |
|---|
| 1025 | char *buf2 = GB_give_other_buffer((char *)res, size<<2); |
|---|
| 1026 | GB_UINT4 *s = (GB_UINT4 *)res; |
|---|
| 1027 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
|---|
| 1028 | |
|---|
| 1029 | for (long i=size; i; i--) { |
|---|
| 1030 | *(d++) = htonl(*(s++)); |
|---|
| 1031 | } |
|---|
| 1032 | return (GB_UINT4 *)buf2; |
|---|
| 1033 | } |
|---|
| 1034 | } |
|---|
| 1035 | |
|---|
| 1036 | long GB_read_ints_count(GBDATA *gbd) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_ints_count |
|---|
| 1037 | GB_TEST_READ(gbd, GB_INTS, "GB_read_ints_count"); |
|---|
| 1038 | return gbd->as_entry()->size(); |
|---|
| 1039 | } |
|---|
| 1040 | |
|---|
| 1041 | GB_UINT4 *GB_read_ints(GBDATA *gbd) |
|---|
| 1042 | { |
|---|
| 1043 | GB_CUINT4 *i = GB_read_ints_pntr(gbd); |
|---|
| 1044 | if (!i) return NULL; |
|---|
| 1045 | return (GB_UINT4 *)GB_memdup((char *)i, gbd->as_entry()->size()*sizeof(GB_UINT4)); |
|---|
| 1046 | } |
|---|
| 1047 | |
|---|
| 1048 | GB_CFLOAT *GB_read_floats_pntr(GBDATA *gbd) |
|---|
| 1049 | { |
|---|
| 1050 | GB_TEST_READ(gbd, GB_FLOATS, "GB_read_floats_pntr"); |
|---|
| 1051 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1052 | char *res; |
|---|
| 1053 | if (gbe->flags.compressed_data) { |
|---|
| 1054 | res = (char *)GB_read_pntr(gbe); |
|---|
| 1055 | } |
|---|
| 1056 | else { |
|---|
| 1057 | res = (char *)gbe->data(); |
|---|
| 1058 | } |
|---|
| 1059 | if (res) { |
|---|
| 1060 | long size = gbe->size(); |
|---|
| 1061 | long full_size = size*sizeof(float); |
|---|
| 1062 | |
|---|
| 1063 | XDR xdrs; |
|---|
| 1064 | xdrmem_create(&xdrs, res, (int)(full_size), XDR_DECODE); |
|---|
| 1065 | |
|---|
| 1066 | char *buf2 = GB_give_other_buffer(res, full_size); |
|---|
| 1067 | float *d = (float *)(void*)buf2; |
|---|
| 1068 | for (long i=size; i; i--) { |
|---|
| 1069 | xdr_float(&xdrs, d); |
|---|
| 1070 | d++; |
|---|
| 1071 | } |
|---|
| 1072 | xdr_destroy(&xdrs); |
|---|
| 1073 | return (float *)(void*)buf2; |
|---|
| 1074 | } |
|---|
| 1075 | return NULL; |
|---|
| 1076 | } |
|---|
| 1077 | |
|---|
| 1078 | static long GB_read_floats_count(GBDATA *gbd) |
|---|
| 1079 | { |
|---|
| 1080 | GB_TEST_READ(gbd, GB_FLOATS, "GB_read_floats_count"); |
|---|
| 1081 | return gbd->as_entry()->size(); |
|---|
| 1082 | } |
|---|
| 1083 | |
|---|
| 1084 | float *GB_read_floats(GBDATA *gbd) { // @@@ only used in unittest - check usage of floats |
|---|
| 1085 | GB_CFLOAT *f; |
|---|
| 1086 | f = GB_read_floats_pntr(gbd); |
|---|
| 1087 | if (!f) return NULL; |
|---|
| 1088 | return (float *)GB_memdup((char *)f, gbd->as_entry()->size()*sizeof(float)); |
|---|
| 1089 | } |
|---|
| 1090 | |
|---|
| 1091 | char *GB_read_as_string(GBDATA *gbd) { |
|---|
| 1092 | switch (gbd->type()) { |
|---|
| 1093 | case GB_STRING: return GB_read_string(gbd); |
|---|
| 1094 | case GB_LINK: return GB_read_link(gbd); |
|---|
| 1095 | case GB_BYTE: return GBS_global_string_copy("%i", GB_read_byte(gbd)); |
|---|
| 1096 | case GB_INT: return GBS_global_string_copy("%li", GB_read_int(gbd)); |
|---|
| 1097 | case GB_FLOAT: return GBS_global_string_copy("%g", GB_read_float(gbd)); |
|---|
| 1098 | case GB_BITS: return GB_read_bits(gbd, '0', '1'); |
|---|
| 1099 | /* Be careful : When adding new types here, you have to make sure that |
|---|
| 1100 | * GB_write_as_string is able to write them back and that this makes sense. |
|---|
| 1101 | */ |
|---|
| 1102 | default: return NULL; |
|---|
| 1103 | } |
|---|
| 1104 | } |
|---|
| 1105 | |
|---|
| 1106 | // ------------------------------------------------------------ |
|---|
| 1107 | // array type access functions (intended for perl use) |
|---|
| 1108 | |
|---|
| 1109 | long GB_read_from_ints(GBDATA *gbd, long index) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_from_ints |
|---|
| 1110 | static GBDATA *last_gbd = 0; |
|---|
| 1111 | static long count = 0; |
|---|
| 1112 | static GB_CUINT4 *i = 0; |
|---|
| 1113 | |
|---|
| 1114 | if (gbd != last_gbd) { |
|---|
| 1115 | count = GB_read_ints_count(gbd); |
|---|
| 1116 | i = GB_read_ints_pntr(gbd); |
|---|
| 1117 | last_gbd = gbd; |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | if (index >= 0 && index < count) { |
|---|
| 1121 | return i[index]; |
|---|
| 1122 | } |
|---|
| 1123 | return -1; |
|---|
| 1124 | } |
|---|
| 1125 | |
|---|
| 1126 | double GB_read_from_floats(GBDATA *gbd, long index) { // @@@ unused |
|---|
| 1127 | static GBDATA *last_gbd = 0; |
|---|
| 1128 | static long count = 0; |
|---|
| 1129 | static GB_CFLOAT *f = 0; |
|---|
| 1130 | |
|---|
| 1131 | if (gbd != last_gbd) { |
|---|
| 1132 | count = GB_read_floats_count(gbd); |
|---|
| 1133 | f = GB_read_floats_pntr(gbd); |
|---|
| 1134 | last_gbd = gbd; |
|---|
| 1135 | } |
|---|
| 1136 | |
|---|
| 1137 | if (index >= 0 && index < count) { |
|---|
| 1138 | return f[index]; |
|---|
| 1139 | } |
|---|
| 1140 | return -1; |
|---|
| 1141 | } |
|---|
| 1142 | |
|---|
| 1143 | // ------------------- |
|---|
| 1144 | // write data |
|---|
| 1145 | |
|---|
| 1146 | static void gb_remove_callbacks_marked_for_deletion(GBDATA *gbd); |
|---|
| 1147 | |
|---|
| 1148 | static void gb_do_callbacks(GBDATA *gbd) { |
|---|
| 1149 | gb_assert(GB_MAIN(gbd)->get_transaction_level() < 0); // only use in NO_TRANSACTION_MODE! |
|---|
| 1150 | |
|---|
| 1151 | while (gbd) { |
|---|
| 1152 | GBDATA *gbdn = GB_get_father(gbd); |
|---|
| 1153 | gb_callback_list *cbl = gbd->get_callbacks(); |
|---|
| 1154 | if (cbl && cbl->call(gbd, GB_CB_CHANGED)) { |
|---|
| 1155 | gb_remove_callbacks_marked_for_deletion(gbd); |
|---|
| 1156 | } |
|---|
| 1157 | gbd = gbdn; |
|---|
| 1158 | } |
|---|
| 1159 | } |
|---|
| 1160 | |
|---|
| 1161 | #define GB_DO_CALLBACKS(gbd) do { if (GB_MAIN(gbd)->get_transaction_level() < 0) gb_do_callbacks(gbd); } while (0) |
|---|
| 1162 | |
|---|
| 1163 | GB_ERROR GB_write_byte(GBDATA *gbd, int i) |
|---|
| 1164 | { |
|---|
| 1165 | GB_TEST_WRITE(gbd, GB_BYTE, "GB_write_byte"); |
|---|
| 1166 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1167 | if (gbe->info.i != i) { |
|---|
| 1168 | gb_save_extern_data_in_ts(gbe); |
|---|
| 1169 | gbe->info.i = i & 0xff; |
|---|
| 1170 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
|---|
| 1171 | GB_DO_CALLBACKS(gbe); |
|---|
| 1172 | } |
|---|
| 1173 | return 0; |
|---|
| 1174 | } |
|---|
| 1175 | |
|---|
| 1176 | GB_ERROR GB_write_int(GBDATA *gbd, long i) { |
|---|
| 1177 | #if defined(ARB_64) |
|---|
| 1178 | #if defined(WARN_TODO) |
|---|
| 1179 | #warning GB_write_int should be GB_ERROR GB_write_int(GBDATA *gbd,int32_t i) |
|---|
| 1180 | #endif |
|---|
| 1181 | #endif |
|---|
| 1182 | |
|---|
| 1183 | GB_TEST_WRITE(gbd, GB_INT, "GB_write_int"); |
|---|
| 1184 | if ((long)((int32_t)i) != i) { |
|---|
| 1185 | gb_assert(0); |
|---|
| 1186 | GB_warningf("Warning: 64bit incompatibility detected\nNo data written to '%s'\n", GB_get_db_path(gbd)); |
|---|
| 1187 | return "GB_INT out of range (signed, 32bit)"; |
|---|
| 1188 | } |
|---|
| 1189 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1190 | if (gbe->info.i != (int32_t)i) { |
|---|
| 1191 | gb_save_extern_data_in_ts(gbe); |
|---|
| 1192 | gbe->info.i = i; |
|---|
| 1193 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
|---|
| 1194 | GB_DO_CALLBACKS(gbe); |
|---|
| 1195 | } |
|---|
| 1196 | return 0; |
|---|
| 1197 | } |
|---|
| 1198 | |
|---|
| 1199 | GB_ERROR GB_write_pointer(GBDATA *gbd, GBDATA *pointer) { |
|---|
| 1200 | GB_TEST_WRITE(gbd, GB_POINTER, "GB_write_pointer"); |
|---|
| 1201 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1202 | if (gbe->info.ptr != pointer) { |
|---|
| 1203 | gb_save_extern_data_in_ts(gbe); |
|---|
| 1204 | gbe->info.ptr = pointer; |
|---|
| 1205 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
|---|
| 1206 | GB_DO_CALLBACKS(gbe); |
|---|
| 1207 | } |
|---|
| 1208 | return 0; |
|---|
| 1209 | } |
|---|
| 1210 | |
|---|
| 1211 | GB_ERROR GB_write_float(GBDATA *gbd, double f) |
|---|
| 1212 | { |
|---|
| 1213 | gb_assert(f == f); // !nan |
|---|
| 1214 | |
|---|
| 1215 | XDR xdrs; |
|---|
| 1216 | static float f2; |
|---|
| 1217 | |
|---|
| 1218 | GB_TEST_WRITE(gbd, GB_FLOAT, "GB_write_float"); |
|---|
| 1219 | |
|---|
| 1220 | #if defined(WARN_TODO) |
|---|
| 1221 | #warning call GB_read_float here! |
|---|
| 1222 | #endif |
|---|
| 1223 | GB_TEST_READ(gbd, GB_FLOAT, "GB_read_float"); |
|---|
| 1224 | |
|---|
| 1225 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1226 | xdrmem_create(&xdrs, &gbe->info.in.data[0], SIZOFINTERN, XDR_DECODE); |
|---|
| 1227 | xdr_float(&xdrs, &f2); |
|---|
| 1228 | xdr_destroy(&xdrs); |
|---|
| 1229 | |
|---|
| 1230 | if (f2 != f) { |
|---|
| 1231 | f2 = f; |
|---|
| 1232 | gb_save_extern_data_in_ts(gbe); |
|---|
| 1233 | xdrmem_create(&xdrs, &gbe->info.in.data[0], SIZOFINTERN, XDR_ENCODE); |
|---|
| 1234 | xdr_float(&xdrs, &f2); |
|---|
| 1235 | xdr_destroy(&xdrs); |
|---|
| 1236 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
|---|
| 1237 | GB_DO_CALLBACKS(gbe); |
|---|
| 1238 | } |
|---|
| 1239 | xdr_destroy(&xdrs); |
|---|
| 1240 | return 0; |
|---|
| 1241 | } |
|---|
| 1242 | |
|---|
| 1243 | GB_ERROR gb_write_compressed_pntr(GBENTRY *gbe, const char *s, long memsize, long stored_size) { |
|---|
| 1244 | gb_uncache(gbe); |
|---|
| 1245 | gb_save_extern_data_in_ts(gbe); |
|---|
| 1246 | gbe->flags.compressed_data = 1; |
|---|
| 1247 | gbe->insert_data((char *)s, stored_size, (size_t)memsize); |
|---|
| 1248 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
|---|
| 1249 | |
|---|
| 1250 | return 0; |
|---|
| 1251 | } |
|---|
| 1252 | |
|---|
| 1253 | int gb_get_compression_mask(GB_MAIN_TYPE *Main, GBQUARK key, int gb_type) { |
|---|
| 1254 | gb_Key *ks = &Main->keys[key]; |
|---|
| 1255 | int compression_mask; |
|---|
| 1256 | |
|---|
| 1257 | if (ks->gb_key_disabled) { |
|---|
| 1258 | compression_mask = 0; |
|---|
| 1259 | } |
|---|
| 1260 | else { |
|---|
| 1261 | if (!ks->gb_key) gb_load_single_key_data(Main->gb_main(), key); |
|---|
| 1262 | compression_mask = gb_convert_type_2_compression_flags[gb_type] & ks->compression_mask; |
|---|
| 1263 | } |
|---|
| 1264 | |
|---|
| 1265 | return compression_mask; |
|---|
| 1266 | } |
|---|
| 1267 | |
|---|
| 1268 | GB_ERROR GB_write_pntr(GBDATA *gbd, const char *s, size_t bytes_size, size_t stored_size) |
|---|
| 1269 | { |
|---|
| 1270 | // 'bytes_size' is the size of what 's' points to. |
|---|
| 1271 | // 'stored_size' is the size-information written into the DB |
|---|
| 1272 | // |
|---|
| 1273 | // e.g. for strings : stored_size = bytes_size-1, cause stored_size is string len, |
|---|
| 1274 | // but bytes_size includes zero byte. |
|---|
| 1275 | |
|---|
| 1276 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1277 | GB_MAIN_TYPE *Main = GB_MAIN(gbe); |
|---|
| 1278 | GBQUARK key = GB_KEY_QUARK(gbe); |
|---|
| 1279 | GB_TYPES type = gbe->type(); |
|---|
| 1280 | |
|---|
| 1281 | gb_assert(implicated(type == GB_STRING, stored_size == bytes_size-1)); // size constraint for strings not fulfilled! |
|---|
| 1282 | |
|---|
| 1283 | gb_uncache(gbe); |
|---|
| 1284 | gb_save_extern_data_in_ts(gbe); |
|---|
| 1285 | |
|---|
| 1286 | int compression_mask = gb_get_compression_mask(Main, key, type); |
|---|
| 1287 | |
|---|
| 1288 | const char *d; |
|---|
| 1289 | size_t memsize; |
|---|
| 1290 | if (compression_mask) { |
|---|
| 1291 | d = gb_compress_data(gbe, key, s, bytes_size, &memsize, compression_mask, false); |
|---|
| 1292 | } |
|---|
| 1293 | else { |
|---|
| 1294 | d = NULL; |
|---|
| 1295 | } |
|---|
| 1296 | if (d) { |
|---|
| 1297 | gbe->flags.compressed_data = 1; |
|---|
| 1298 | } |
|---|
| 1299 | else { |
|---|
| 1300 | d = s; |
|---|
| 1301 | gbe->flags.compressed_data = 0; |
|---|
| 1302 | memsize = bytes_size; |
|---|
| 1303 | } |
|---|
| 1304 | |
|---|
| 1305 | gbe->insert_data(d, stored_size, memsize); |
|---|
| 1306 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
|---|
| 1307 | GB_DO_CALLBACKS(gbe); |
|---|
| 1308 | |
|---|
| 1309 | return 0; |
|---|
| 1310 | } |
|---|
| 1311 | |
|---|
| 1312 | GB_ERROR GB_write_string(GBDATA *gbd, const char *s) { |
|---|
| 1313 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1314 | GB_TEST_WRITE(gbe, GB_STRING, "GB_write_string"); |
|---|
| 1315 | GB_TEST_NON_BUFFER(s, "GB_write_string"); // compress would destroy the other buffer |
|---|
| 1316 | |
|---|
| 1317 | if (!s) s = ""; |
|---|
| 1318 | size_t size = strlen(s); |
|---|
| 1319 | |
|---|
| 1320 | // no zero len strings allowed |
|---|
| 1321 | if (gbe->memsize() && (size == gbe->size())) |
|---|
| 1322 | { |
|---|
| 1323 | if (!strcmp(s, GB_read_pntr(gbe))) |
|---|
| 1324 | return 0; |
|---|
| 1325 | } |
|---|
| 1326 | #if defined(DEBUG) && 0 |
|---|
| 1327 | // check for error (in compression) |
|---|
| 1328 | { |
|---|
| 1329 | GB_ERROR error = GB_write_pntr(gbe, s, size+1, size); |
|---|
| 1330 | if (!error) { |
|---|
| 1331 | char *check = GB_read_string(gbe); |
|---|
| 1332 | |
|---|
| 1333 | gb_assert(check); |
|---|
| 1334 | gb_assert(strcmp(check, s) == 0); |
|---|
| 1335 | |
|---|
| 1336 | free(check); |
|---|
| 1337 | } |
|---|
| 1338 | return error; |
|---|
| 1339 | } |
|---|
| 1340 | #else |
|---|
| 1341 | return GB_write_pntr(gbe, s, size+1, size); |
|---|
| 1342 | #endif // DEBUG |
|---|
| 1343 | } |
|---|
| 1344 | |
|---|
| 1345 | GB_ERROR GB_write_link(GBDATA *gbd, const char *s) |
|---|
| 1346 | { |
|---|
| 1347 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1348 | GB_TEST_WRITE(gbe, GB_STRING, "GB_write_link"); |
|---|
| 1349 | GB_TEST_NON_BUFFER(s, "GB_write_link"); // compress would destroy the other buffer |
|---|
| 1350 | |
|---|
| 1351 | if (!s) s = ""; |
|---|
| 1352 | size_t size = strlen(s); |
|---|
| 1353 | |
|---|
| 1354 | // no zero len strings allowed |
|---|
| 1355 | if (gbe->memsize() && (size == gbe->size())) |
|---|
| 1356 | { |
|---|
| 1357 | if (!strcmp(s, GB_read_pntr(gbe))) |
|---|
| 1358 | return 0; |
|---|
| 1359 | } |
|---|
| 1360 | return GB_write_pntr(gbe, s, size+1, size); |
|---|
| 1361 | } |
|---|
| 1362 | |
|---|
| 1363 | |
|---|
| 1364 | GB_ERROR GB_write_bits(GBDATA *gbd, const char *bits, long size, const char *c_0) |
|---|
| 1365 | { |
|---|
| 1366 | GBENTRY *gbe = gbd->as_entry(); |
|---|
| 1367 | GB_TEST_WRITE(gbe, GB_BITS, "GB_write_bits"); |
|---|
| 1368 | GB_TEST_NON_BUFFER(bits, "GB_write_bits"); // compress would destroy the other buffer |
|---|
| 1369 | gb_save_extern_data_in_ts(gbe); |
|---|
| 1370 | |
|---|
| 1371 | long memsize; |
|---|
| 1372 | char *d = gb_compress_bits(bits, size, (const unsigned char *)c_0, &memsize); |
|---|
| 1373 | |
|---|
| 1374 | gbe->flags.compressed_data = 1; |
|---|
| 1375 | gbe->insert_data(d, size, memsize); |
|---|
| 1376 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
|---|
| 1377 | GB_DO_CALLBACKS(gbe); |
|---|
| 1378 | return 0; |
|---|
| 1379 | } |
|---|
| 1380 | |
|---|
| 1381 | GB_ERROR GB_write_bytes(GBDATA *gbd, const char *s, long size) |
|---|
| 1382 | { |
|---|
| 1383 | GB_TEST_WRITE(gbd, GB_BYTES, "GB_write_bytes"); |
|---|
| 1384 | return GB_write_pntr(gbd, s, size, size); |
|---|
| 1385 | } |
|---|
| 1386 | |
|---|
| 1387 | GB_ERROR GB_write_ints(GBDATA *gbd, const GB_UINT4 *i, long size) |
|---|
| 1388 | { |
|---|
| 1389 | |
|---|
| 1390 | GB_TEST_WRITE(gbd, GB_INTS, "GB_write_ints"); |
|---|
| 1391 | GB_TEST_NON_BUFFER((char *)i, "GB_write_ints"); // compress would destroy the other buffer |
|---|
| 1392 | |
|---|
| 1393 | if (0x01020304 != htonl((GB_UINT4)0x01020304)) { |
|---|
| 1394 | long j; |
|---|
| 1395 | char *buf2 = GB_give_other_buffer((char *)i, size<<2); |
|---|
| 1396 | GB_UINT4 *s = (GB_UINT4 *)i; |
|---|
| 1397 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
|---|
| 1398 | |
|---|
| 1399 | for (j=size; j; j--) { |
|---|
| 1400 | *(d++) = htonl(*(s++)); |
|---|
| 1401 | } |
|---|
| 1402 | i = (GB_UINT4 *)buf2; |
|---|
| 1403 | } |
|---|
| 1404 | return GB_write_pntr(gbd, (char *)i, size* 4 /* sizeof(long4) */, size); |
|---|
| 1405 | } |
|---|
| 1406 | |
|---|
| 1407 | GB_ERROR GB_write_floats(GBDATA *gbd, const float *f, long size) |
|---|
| 1408 | { |
|---|
| 1409 | long fullsize = size * sizeof(float); |
|---|
| 1410 | GB_TEST_WRITE(gbd, GB_FLOATS, "GB_write_floats"); |
|---|
| 1411 | GB_TEST_NON_BUFFER((char *)f, "GB_write_floats"); // compress would destroy the other buffer |
|---|
| 1412 | |
|---|
| 1413 | { |
|---|
| 1414 | XDR xdrs; |
|---|
| 1415 | long i; |
|---|
| 1416 | char *buf2 = GB_give_other_buffer((char *)f, fullsize); |
|---|
| 1417 | float *s = (float *)f; |
|---|
| 1418 | |
|---|
| 1419 | xdrmem_create(&xdrs, buf2, (int)fullsize, XDR_ENCODE); |
|---|
| 1420 | for (i=size; i; i--) { |
|---|
| 1421 | xdr_float(&xdrs, s); |
|---|
| 1422 | s++; |
|---|
| 1423 | } |
|---|
| 1424 | xdr_destroy (&xdrs); |
|---|
| 1425 | f = (float*)(void*)buf2; |
|---|
| 1426 | } |
|---|
| 1427 | return GB_write_pntr(gbd, (char *)f, size*sizeof(float), size); |
|---|
| 1428 | } |
|---|
| 1429 | |
|---|
| 1430 | GB_ERROR GB_write_as_string(GBDATA *gbd, const char *val) { |
|---|
| 1431 | switch (gbd->type()) { |
|---|
| 1432 | case GB_STRING: return GB_write_string(gbd, val); |
|---|
| 1433 | case GB_LINK: return GB_write_link(gbd, val); |
|---|
| 1434 | case GB_BYTE: return GB_write_byte(gbd, atoi(val)); |
|---|
| 1435 | case GB_INT: return GB_write_int(gbd, atoi(val)); |
|---|
| 1436 | case GB_FLOAT: return GB_write_float(gbd, GB_atof(val)); |
|---|
| 1437 | case GB_BITS: return GB_write_bits(gbd, val, strlen(val), "0"); |
|---|
| 1438 | default: return GB_export_errorf("Error: You cannot use GB_write_as_string on this type of entry (%s)", GB_read_key_pntr(gbd)); |
|---|
| 1439 | } |
|---|
| 1440 | } |
|---|
| 1441 | |
|---|
| 1442 | // --------------------------- |
|---|
| 1443 | // security functions |
|---|
| 1444 | |
|---|
| 1445 | int GB_read_security_write(GBDATA *gbd) { |
|---|
| 1446 | GB_test_transaction(gbd); |
|---|
| 1447 | return GB_GET_SECURITY_WRITE(gbd); |
|---|
| 1448 | } |
|---|
| 1449 | int GB_read_security_read(GBDATA *gbd) { |
|---|
| 1450 | GB_test_transaction(gbd); |
|---|
| 1451 | return GB_GET_SECURITY_READ(gbd); |
|---|
| 1452 | } |
|---|
| 1453 | int GB_read_security_delete(GBDATA *gbd) { |
|---|
| 1454 | GB_test_transaction(gbd); |
|---|
| 1455 | return GB_GET_SECURITY_DELETE(gbd); |
|---|
| 1456 | } |
|---|
| 1457 | GB_ERROR GB_write_security_write(GBDATA *gbd, unsigned long level) |
|---|
| 1458 | { |
|---|
| 1459 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 1460 | GB_test_transaction(Main); |
|---|
| 1461 | |
|---|
| 1462 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
|---|
| 1463 | return gb_security_error(gbd); |
|---|
| 1464 | if (GB_GET_SECURITY_WRITE(gbd) == level) return 0; |
|---|
| 1465 | GB_PUT_SECURITY_WRITE(gbd, level); |
|---|
| 1466 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
|---|
| 1467 | GB_DO_CALLBACKS(gbd); |
|---|
| 1468 | return 0; |
|---|
| 1469 | } |
|---|
| 1470 | GB_ERROR GB_write_security_read(GBDATA *gbd, unsigned long level) // @@@ unused |
|---|
| 1471 | { |
|---|
| 1472 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 1473 | GB_test_transaction(Main); |
|---|
| 1474 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
|---|
| 1475 | return gb_security_error(gbd); |
|---|
| 1476 | if (GB_GET_SECURITY_READ(gbd) == level) return 0; |
|---|
| 1477 | GB_PUT_SECURITY_READ(gbd, level); |
|---|
| 1478 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
|---|
| 1479 | GB_DO_CALLBACKS(gbd); |
|---|
| 1480 | return 0; |
|---|
| 1481 | } |
|---|
| 1482 | GB_ERROR GB_write_security_delete(GBDATA *gbd, unsigned long level) |
|---|
| 1483 | { |
|---|
| 1484 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 1485 | GB_test_transaction(Main); |
|---|
| 1486 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
|---|
| 1487 | return gb_security_error(gbd); |
|---|
| 1488 | if (GB_GET_SECURITY_DELETE(gbd) == level) return 0; |
|---|
| 1489 | GB_PUT_SECURITY_DELETE(gbd, level); |
|---|
| 1490 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
|---|
| 1491 | GB_DO_CALLBACKS(gbd); |
|---|
| 1492 | return 0; |
|---|
| 1493 | } |
|---|
| 1494 | GB_ERROR GB_write_security_levels(GBDATA *gbd, unsigned long readlevel, unsigned long writelevel, unsigned long deletelevel) |
|---|
| 1495 | { |
|---|
| 1496 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 1497 | GB_test_transaction(Main); |
|---|
| 1498 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
|---|
| 1499 | return gb_security_error(gbd); |
|---|
| 1500 | GB_PUT_SECURITY_WRITE(gbd, writelevel); |
|---|
| 1501 | GB_PUT_SECURITY_READ(gbd, readlevel); |
|---|
| 1502 | GB_PUT_SECURITY_DELETE(gbd, deletelevel); |
|---|
| 1503 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
|---|
| 1504 | GB_DO_CALLBACKS(gbd); |
|---|
| 1505 | return 0; |
|---|
| 1506 | } |
|---|
| 1507 | |
|---|
| 1508 | void GB_change_my_security(GBDATA *gbd, int level) { |
|---|
| 1509 | GB_MAIN(gbd)->security_level = level<0 ? 0 : (level>7 ? 7 : level); |
|---|
| 1510 | } |
|---|
| 1511 | |
|---|
| 1512 | // For internal use only |
|---|
| 1513 | void GB_push_my_security(GBDATA *gbd) |
|---|
| 1514 | { |
|---|
| 1515 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 1516 | Main->pushed_security_level++; |
|---|
| 1517 | if (Main->pushed_security_level <= 1) { |
|---|
| 1518 | Main->old_security_level = Main->security_level; |
|---|
| 1519 | Main->security_level = 7; |
|---|
| 1520 | } |
|---|
| 1521 | } |
|---|
| 1522 | |
|---|
| 1523 | void GB_pop_my_security(GBDATA *gbd) { |
|---|
| 1524 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 1525 | Main->pushed_security_level--; |
|---|
| 1526 | if (Main->pushed_security_level <= 0) { |
|---|
| 1527 | Main->security_level = Main->old_security_level; |
|---|
| 1528 | } |
|---|
| 1529 | } |
|---|
| 1530 | |
|---|
| 1531 | |
|---|
| 1532 | // ------------------------ |
|---|
| 1533 | // Key information |
|---|
| 1534 | |
|---|
| 1535 | GB_TYPES GB_read_type(GBDATA *gbd) { |
|---|
| 1536 | GB_test_transaction(gbd); |
|---|
| 1537 | return gbd->type(); |
|---|
| 1538 | } |
|---|
| 1539 | |
|---|
| 1540 | bool GB_is_container(GBDATA *gbd) { |
|---|
| 1541 | return gbd && gbd->is_container(); |
|---|
| 1542 | } |
|---|
| 1543 | |
|---|
| 1544 | char *GB_read_key(GBDATA *gbd) { |
|---|
| 1545 | return strdup(GB_read_key_pntr(gbd)); |
|---|
| 1546 | } |
|---|
| 1547 | |
|---|
| 1548 | GB_CSTR GB_read_key_pntr(GBDATA *gbd) { |
|---|
| 1549 | GB_CSTR k; |
|---|
| 1550 | GB_test_transaction(gbd); |
|---|
| 1551 | k = GB_KEY(gbd); |
|---|
| 1552 | if (!k) k = GBS_global_string("<invalid key (quark=%i)>", GB_KEY_QUARK(gbd)); |
|---|
| 1553 | return k; |
|---|
| 1554 | } |
|---|
| 1555 | |
|---|
| 1556 | GB_CSTR gb_read_key_pntr(GBDATA *gbd) { |
|---|
| 1557 | return GB_KEY(gbd); |
|---|
| 1558 | } |
|---|
| 1559 | |
|---|
| 1560 | |
|---|
| 1561 | GBQUARK gb_find_existing_quark(GB_MAIN_TYPE *Main, const char *key) { |
|---|
| 1562 | //! @return existing quark for 'key' (-1 if key == NULL, 0 if key is unknown) |
|---|
| 1563 | return key ? GBS_read_hash(Main->key_2_index_hash, key) : -1; |
|---|
| 1564 | } |
|---|
| 1565 | |
|---|
| 1566 | GBQUARK gb_find_or_create_quark(GB_MAIN_TYPE *Main, const char *key) { |
|---|
| 1567 | //! @return existing or newly created quark for 'key' |
|---|
| 1568 | GBQUARK quark = gb_find_existing_quark(Main, key); |
|---|
| 1569 | if (!quark) quark = gb_create_key(Main, key, true); |
|---|
| 1570 | return quark; |
|---|
| 1571 | } |
|---|
| 1572 | |
|---|
| 1573 | GBQUARK gb_find_or_create_NULL_quark(GB_MAIN_TYPE *Main, const char *key) { |
|---|
| 1574 | // similar to gb_find_or_create_quark, |
|---|
| 1575 | // but if 'key' is NULL, quark 0 will be returned. |
|---|
| 1576 | // |
|---|
| 1577 | // Use this function with care. |
|---|
| 1578 | // |
|---|
| 1579 | // Known good use: |
|---|
| 1580 | // - create main entry and its dummy father via gb_make_container() |
|---|
| 1581 | |
|---|
| 1582 | return key ? gb_find_or_create_quark(Main, key) : 0; |
|---|
| 1583 | } |
|---|
| 1584 | |
|---|
| 1585 | GBQUARK GB_find_existing_quark(GBDATA *gbd, const char *key) { |
|---|
| 1586 | //! @return existing quark for 'key' (-1 if key == NULL, 0 if key is unknown) |
|---|
| 1587 | return gb_find_existing_quark(GB_MAIN(gbd), key); |
|---|
| 1588 | } |
|---|
| 1589 | |
|---|
| 1590 | GBQUARK GB_find_or_create_quark(GBDATA *gbd, const char *key) { |
|---|
| 1591 | //! @return existing or newly created quark for 'key' |
|---|
| 1592 | return gb_find_or_create_quark(GB_MAIN(gbd), key); |
|---|
| 1593 | } |
|---|
| 1594 | |
|---|
| 1595 | |
|---|
| 1596 | // --------------------------------------------- |
|---|
| 1597 | |
|---|
| 1598 | GBQUARK GB_get_quark(GBDATA *gbd) { |
|---|
| 1599 | return GB_KEY_QUARK(gbd); |
|---|
| 1600 | } |
|---|
| 1601 | |
|---|
| 1602 | bool GB_has_key(GBDATA *gbd, const char *key) { |
|---|
| 1603 | GBQUARK quark = GB_find_existing_quark(gbd, key); |
|---|
| 1604 | return quark && (quark == GB_get_quark(gbd)); |
|---|
| 1605 | } |
|---|
| 1606 | |
|---|
| 1607 | // --------------------------------------------- |
|---|
| 1608 | |
|---|
| 1609 | long GB_read_clock(GBDATA *gbd) { |
|---|
| 1610 | if (GB_ARRAY_FLAGS(gbd).changed) return GB_MAIN(gbd)->clock; |
|---|
| 1611 | return gbd->update_date(); |
|---|
| 1612 | } |
|---|
| 1613 | |
|---|
| 1614 | // --------------------------------------------- |
|---|
| 1615 | // Get and check the database hierarchy |
|---|
| 1616 | |
|---|
| 1617 | GBDATA *GB_get_father(GBDATA *gbd) { |
|---|
| 1618 | // Get the father of an entry |
|---|
| 1619 | GB_test_transaction(gbd); |
|---|
| 1620 | return gbd->get_father(); |
|---|
| 1621 | } |
|---|
| 1622 | |
|---|
| 1623 | GBDATA *GB_get_grandfather(GBDATA *gbd) { |
|---|
| 1624 | GB_test_transaction(gbd); |
|---|
| 1625 | |
|---|
| 1626 | GBDATA *gb_grandpa = GB_FATHER(gbd); |
|---|
| 1627 | if (gb_grandpa) { |
|---|
| 1628 | gb_grandpa = GB_FATHER(gb_grandpa); |
|---|
| 1629 | if (gb_grandpa && !GB_FATHER(gb_grandpa)) gb_grandpa = NULL; // never return dummy_father of root container |
|---|
| 1630 | } |
|---|
| 1631 | return gb_grandpa; |
|---|
| 1632 | } |
|---|
| 1633 | |
|---|
| 1634 | // Get the root entry (gb_main) |
|---|
| 1635 | GBDATA *GB_get_root(GBDATA *gbd) { return GB_MAIN(gbd)->gb_main(); } |
|---|
| 1636 | GBCONTAINER *gb_get_root(GBENTRY *gbe) { return GB_MAIN(gbe)->root_container; } |
|---|
| 1637 | GBCONTAINER *gb_get_root(GBCONTAINER *gbc) { return GB_MAIN(gbc)->root_container; } |
|---|
| 1638 | |
|---|
| 1639 | bool GB_check_father(GBDATA *gbd, GBDATA *gb_maybefather) { |
|---|
| 1640 | // Test whether an entry is a subentry of another |
|---|
| 1641 | GBDATA *gbfather; |
|---|
| 1642 | for (gbfather = GB_get_father(gbd); |
|---|
| 1643 | gbfather; |
|---|
| 1644 | gbfather = GB_get_father(gbfather)) |
|---|
| 1645 | { |
|---|
| 1646 | if (gbfather == gb_maybefather) return true; |
|---|
| 1647 | } |
|---|
| 1648 | return false; |
|---|
| 1649 | } |
|---|
| 1650 | |
|---|
| 1651 | // -------------------------- |
|---|
| 1652 | // create and rename |
|---|
| 1653 | |
|---|
| 1654 | GBENTRY *gb_create(GBCONTAINER *father, const char *key, GB_TYPES type) { |
|---|
| 1655 | GBENTRY *gbe = gb_make_entry(father, key, -1, 0, type); |
|---|
| 1656 | gb_touch_header(GB_FATHER(gbe)); |
|---|
| 1657 | gb_touch_entry(gbe, GB_CREATED); |
|---|
| 1658 | |
|---|
| 1659 | gb_assert(GB_ARRAY_FLAGS(gbe).changed < GB_DELETED); // happens sometimes -> needs debugging |
|---|
| 1660 | |
|---|
| 1661 | return gbe; |
|---|
| 1662 | } |
|---|
| 1663 | |
|---|
| 1664 | GBCONTAINER *gb_create_container(GBCONTAINER *father, const char *key) { |
|---|
| 1665 | // Create a container, do not check anything |
|---|
| 1666 | GBCONTAINER *gbc = gb_make_container(father, key, -1, 0); |
|---|
| 1667 | gb_touch_header(GB_FATHER(gbc)); |
|---|
| 1668 | gb_touch_entry(gbc, GB_CREATED); |
|---|
| 1669 | return gbc; |
|---|
| 1670 | } |
|---|
| 1671 | |
|---|
| 1672 | GBDATA *GB_create(GBDATA *father, const char *key, GB_TYPES type) { |
|---|
| 1673 | /*! Create a DB entry |
|---|
| 1674 | * |
|---|
| 1675 | * @param father container to create DB field in |
|---|
| 1676 | * @param key name of field |
|---|
| 1677 | * @param type field type |
|---|
| 1678 | * |
|---|
| 1679 | * @return |
|---|
| 1680 | * - created DB entry |
|---|
| 1681 | * - NULL on failure (error is exported then) |
|---|
| 1682 | * |
|---|
| 1683 | * @see GB_create_container() |
|---|
| 1684 | */ |
|---|
| 1685 | |
|---|
| 1686 | if (GB_check_key(key)) { |
|---|
| 1687 | GB_print_error(); |
|---|
| 1688 | return NULL; |
|---|
| 1689 | } |
|---|
| 1690 | |
|---|
| 1691 | if (type == GB_DB) { |
|---|
| 1692 | gb_assert(type != GB_DB); // you like to use GB_create_container! |
|---|
| 1693 | GB_export_error("GB_create error: can't create containers"); |
|---|
| 1694 | return NULL; |
|---|
| 1695 | } |
|---|
| 1696 | |
|---|
| 1697 | if (!father) { |
|---|
| 1698 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
|---|
| 1699 | return NULL; |
|---|
| 1700 | } |
|---|
| 1701 | GB_test_transaction(father); |
|---|
| 1702 | if (father->is_entry()) { |
|---|
| 1703 | GB_export_errorf("GB_create: father (%s) is not of GB_DB type (%i) (creating '%s')", |
|---|
| 1704 | GB_read_key_pntr(father), father->type(), key); |
|---|
| 1705 | return NULL; |
|---|
| 1706 | } |
|---|
| 1707 | |
|---|
| 1708 | if (type == GB_POINTER) { |
|---|
| 1709 | if (!GB_in_temporary_branch(father)) { |
|---|
| 1710 | GB_export_error("GB_create: pointers only allowed in temporary branches"); |
|---|
| 1711 | return NULL; |
|---|
| 1712 | } |
|---|
| 1713 | } |
|---|
| 1714 | |
|---|
| 1715 | return gb_create(father->expect_container(), key, type); |
|---|
| 1716 | } |
|---|
| 1717 | |
|---|
| 1718 | GBDATA *GB_create_container(GBDATA *father, const char *key) { |
|---|
| 1719 | /*! Create a new DB container |
|---|
| 1720 | * |
|---|
| 1721 | * @param father parent container |
|---|
| 1722 | * @param key name of created container |
|---|
| 1723 | * |
|---|
| 1724 | * @return |
|---|
| 1725 | * - created container |
|---|
| 1726 | * - NULL on failure (error is exported then) |
|---|
| 1727 | * |
|---|
| 1728 | * @see GB_create() |
|---|
| 1729 | */ |
|---|
| 1730 | |
|---|
| 1731 | if (GB_check_key(key)) { |
|---|
| 1732 | GB_print_error(); |
|---|
| 1733 | return NULL; |
|---|
| 1734 | } |
|---|
| 1735 | |
|---|
| 1736 | if ((*key == '\0')) { |
|---|
| 1737 | GB_export_error("GB_create error: empty key"); |
|---|
| 1738 | return NULL; |
|---|
| 1739 | } |
|---|
| 1740 | if (!father) { |
|---|
| 1741 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
|---|
| 1742 | return NULL; |
|---|
| 1743 | } |
|---|
| 1744 | |
|---|
| 1745 | GB_test_transaction(father); |
|---|
| 1746 | return gb_create_container(father->expect_container(), key); |
|---|
| 1747 | } |
|---|
| 1748 | |
|---|
| 1749 | // ---------------------- |
|---|
| 1750 | // recompression |
|---|
| 1751 | |
|---|
| 1752 | #if defined(WARN_TODO) |
|---|
| 1753 | #warning rename gb_set_compression into gb_recompress (misleading name) |
|---|
| 1754 | #endif |
|---|
| 1755 | |
|---|
| 1756 | static GB_ERROR gb_set_compression(GBDATA *source) { |
|---|
| 1757 | GB_ERROR error = 0; |
|---|
| 1758 | GB_test_transaction(source); |
|---|
| 1759 | |
|---|
| 1760 | switch (source->type()) { |
|---|
| 1761 | case GB_STRING: { |
|---|
| 1762 | char *str = GB_read_string(source); |
|---|
| 1763 | GB_write_string(source, ""); |
|---|
| 1764 | GB_write_string(source, str); |
|---|
| 1765 | free(str); |
|---|
| 1766 | break; |
|---|
| 1767 | } |
|---|
| 1768 | case GB_BITS: |
|---|
| 1769 | case GB_BYTES: |
|---|
| 1770 | case GB_INTS: |
|---|
| 1771 | case GB_FLOATS: |
|---|
| 1772 | break; |
|---|
| 1773 | case GB_DB: |
|---|
| 1774 | for (GBDATA *gb_p = GB_child(source); gb_p && !error; gb_p = GB_nextChild(gb_p)) { |
|---|
| 1775 | error = gb_set_compression(gb_p); |
|---|
| 1776 | } |
|---|
| 1777 | break; |
|---|
| 1778 | default: |
|---|
| 1779 | break; |
|---|
| 1780 | } |
|---|
| 1781 | return error; |
|---|
| 1782 | } |
|---|
| 1783 | |
|---|
| 1784 | bool GB_allow_compression(GBDATA *gb_main, bool allow_compression) { |
|---|
| 1785 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
|---|
| 1786 | int prev_mask = Main->compression_mask; |
|---|
| 1787 | Main->compression_mask = allow_compression ? -1 : 0; |
|---|
| 1788 | |
|---|
| 1789 | return prev_mask == 0 ? false : true; |
|---|
| 1790 | } |
|---|
| 1791 | |
|---|
| 1792 | |
|---|
| 1793 | GB_ERROR GB_delete(GBDATA*& source) { |
|---|
| 1794 | GBDATA *gb_main; |
|---|
| 1795 | |
|---|
| 1796 | GB_test_transaction(source); |
|---|
| 1797 | if (GB_GET_SECURITY_DELETE(source)>GB_MAIN(source)->security_level) { |
|---|
| 1798 | return GBS_global_string("Security error: deleting entry '%s' not permitted", GB_read_key_pntr(source)); |
|---|
| 1799 | } |
|---|
| 1800 | |
|---|
| 1801 | gb_main = GB_get_root(source); |
|---|
| 1802 | |
|---|
| 1803 | if (source->flags.compressed_data) { |
|---|
| 1804 | bool was_allowed = GB_allow_compression(gb_main, false); |
|---|
| 1805 | gb_set_compression(source); // write data w/o compression (otherwise GB_read_old_value... won't work) |
|---|
| 1806 | GB_allow_compression(gb_main, was_allowed); |
|---|
| 1807 | } |
|---|
| 1808 | |
|---|
| 1809 | { |
|---|
| 1810 | GB_MAIN_TYPE *Main = GB_MAIN(source); |
|---|
| 1811 | if (Main->get_transaction_level() < 0) { // no transaction mode |
|---|
| 1812 | gb_delete_entry(source); |
|---|
| 1813 | Main->call_pending_callbacks(); |
|---|
| 1814 | } |
|---|
| 1815 | else { |
|---|
| 1816 | gb_touch_entry(source, GB_DELETED); |
|---|
| 1817 | } |
|---|
| 1818 | } |
|---|
| 1819 | return 0; |
|---|
| 1820 | } |
|---|
| 1821 | |
|---|
| 1822 | GB_ERROR gb_delete_force(GBDATA *source) // delete always |
|---|
| 1823 | { |
|---|
| 1824 | gb_touch_entry(source, GB_DELETED); |
|---|
| 1825 | return 0; |
|---|
| 1826 | } |
|---|
| 1827 | |
|---|
| 1828 | |
|---|
| 1829 | // ------------------ |
|---|
| 1830 | // Copy data |
|---|
| 1831 | |
|---|
| 1832 | #if defined(WARN_TODO) |
|---|
| 1833 | #warning replace GB_copy with GB_copy_with_protection after release |
|---|
| 1834 | #endif |
|---|
| 1835 | |
|---|
| 1836 | GB_ERROR GB_copy(GBDATA *dest, GBDATA *source) { |
|---|
| 1837 | return GB_copy_with_protection(dest, source, false); |
|---|
| 1838 | } |
|---|
| 1839 | |
|---|
| 1840 | GB_ERROR GB_copy_with_protection(GBDATA *dest, GBDATA *source, bool copy_all_protections) { |
|---|
| 1841 | GB_ERROR error = 0; |
|---|
| 1842 | GB_test_transaction(source); |
|---|
| 1843 | |
|---|
| 1844 | GB_TYPES type = source->type(); |
|---|
| 1845 | if (dest->type() != type) { |
|---|
| 1846 | return GB_export_errorf("incompatible types in GB_copy (source %s:%u != %s:%u", |
|---|
| 1847 | GB_read_key_pntr(source), type, GB_read_key_pntr(dest), dest->type()); |
|---|
| 1848 | } |
|---|
| 1849 | |
|---|
| 1850 | switch (type) { |
|---|
| 1851 | case GB_INT: |
|---|
| 1852 | error = GB_write_int(dest, GB_read_int(source)); |
|---|
| 1853 | break; |
|---|
| 1854 | case GB_FLOAT: |
|---|
| 1855 | error = GB_write_float(dest, GB_read_float(source)); |
|---|
| 1856 | break; |
|---|
| 1857 | case GB_BYTE: |
|---|
| 1858 | error = GB_write_byte(dest, GB_read_byte(source)); |
|---|
| 1859 | break; |
|---|
| 1860 | case GB_STRING: // No local compression |
|---|
| 1861 | error = GB_write_string(dest, GB_read_char_pntr(source)); |
|---|
| 1862 | break; |
|---|
| 1863 | case GB_LINK: // No local compression |
|---|
| 1864 | error = GB_write_link(dest, GB_read_link_pntr(source)); |
|---|
| 1865 | break; |
|---|
| 1866 | case GB_BITS: // only local compressions for the following types |
|---|
| 1867 | case GB_BYTES: |
|---|
| 1868 | case GB_INTS: |
|---|
| 1869 | case GB_FLOATS: { |
|---|
| 1870 | GBENTRY *source_entry = source->as_entry(); |
|---|
| 1871 | GBENTRY *dest_entry = dest->as_entry(); |
|---|
| 1872 | |
|---|
| 1873 | gb_save_extern_data_in_ts(dest_entry); |
|---|
| 1874 | dest_entry->insert_data(source_entry->data(), source_entry->size(), source_entry->memsize()); |
|---|
| 1875 | |
|---|
| 1876 | dest->flags.compressed_data = source->flags.compressed_data; |
|---|
| 1877 | break; |
|---|
| 1878 | } |
|---|
| 1879 | case GB_DB: { |
|---|
| 1880 | if (!dest->is_container()) { |
|---|
| 1881 | GB_ERROR err = GB_export_errorf("GB_COPY Type conflict %s:%i != %s:%i", |
|---|
| 1882 | GB_read_key_pntr(dest), dest->type(), GB_read_key_pntr(source), GB_DB); |
|---|
| 1883 | GB_internal_error(err); |
|---|
| 1884 | return err; |
|---|
| 1885 | } |
|---|
| 1886 | |
|---|
| 1887 | GBCONTAINER *destc = dest->as_container(); |
|---|
| 1888 | GBCONTAINER *sourcec = source->as_container(); |
|---|
| 1889 | |
|---|
| 1890 | if (sourcec->flags2.folded_container) gb_unfold(sourcec, -1, -1); |
|---|
| 1891 | if (destc->flags2.folded_container) gb_unfold(destc, 0, -1); |
|---|
| 1892 | |
|---|
| 1893 | for (GBDATA *gb_p = GB_child(sourcec); gb_p; gb_p = GB_nextChild(gb_p)) { |
|---|
| 1894 | const char *key = GB_read_key_pntr(gb_p); |
|---|
| 1895 | GBDATA *gb_d; |
|---|
| 1896 | |
|---|
| 1897 | if (gb_p->is_container()) { |
|---|
| 1898 | gb_d = GB_create_container(destc, key); |
|---|
| 1899 | gb_create_header_array(gb_d->as_container(), gb_p->as_container()->d.size); |
|---|
| 1900 | } |
|---|
| 1901 | else { |
|---|
| 1902 | gb_d = GB_create(destc, key, gb_p->type()); |
|---|
| 1903 | } |
|---|
| 1904 | |
|---|
| 1905 | if (!gb_d) error = GB_await_error(); |
|---|
| 1906 | else error = GB_copy_with_protection(gb_d, gb_p, copy_all_protections); |
|---|
| 1907 | |
|---|
| 1908 | if (error) break; |
|---|
| 1909 | } |
|---|
| 1910 | |
|---|
| 1911 | destc->flags3 = sourcec->flags3; |
|---|
| 1912 | break; |
|---|
| 1913 | } |
|---|
| 1914 | default: |
|---|
| 1915 | error = GB_export_error("GB_copy-error: unhandled type"); |
|---|
| 1916 | } |
|---|
| 1917 | if (error) return error; |
|---|
| 1918 | |
|---|
| 1919 | gb_touch_entry(dest, GB_NORMAL_CHANGE); |
|---|
| 1920 | |
|---|
| 1921 | dest->flags.security_read = source->flags.security_read; |
|---|
| 1922 | if (copy_all_protections) { |
|---|
| 1923 | dest->flags.security_write = source->flags.security_write; |
|---|
| 1924 | dest->flags.security_delete = source->flags.security_delete; |
|---|
| 1925 | } |
|---|
| 1926 | |
|---|
| 1927 | return 0; |
|---|
| 1928 | } |
|---|
| 1929 | |
|---|
| 1930 | |
|---|
| 1931 | static char *gb_stpcpy(char *dest, const char *source) |
|---|
| 1932 | { |
|---|
| 1933 | while ((*dest++=*source++)) ; |
|---|
| 1934 | return dest-1; // return pointer to last copied character (which is \0) |
|---|
| 1935 | } |
|---|
| 1936 | |
|---|
| 1937 | char* GB_get_subfields(GBDATA *gbd) { |
|---|
| 1938 | /*! Get all subfield names |
|---|
| 1939 | * |
|---|
| 1940 | * @return all subfields of 'gbd' as ';'-separated heap-copy |
|---|
| 1941 | * (first and last char of result is a ';') |
|---|
| 1942 | */ |
|---|
| 1943 | GB_test_transaction(gbd); |
|---|
| 1944 | |
|---|
| 1945 | char *result = 0; |
|---|
| 1946 | if (gbd->is_container()) { |
|---|
| 1947 | GBCONTAINER *gbc = gbd->as_container(); |
|---|
| 1948 | int result_length = 0; |
|---|
| 1949 | |
|---|
| 1950 | if (gbc->flags2.folded_container) { |
|---|
| 1951 | gb_unfold(gbc, -1, -1); |
|---|
| 1952 | } |
|---|
| 1953 | |
|---|
| 1954 | for (GBDATA *gbp = GB_child(gbd); gbp; gbp = GB_nextChild(gbp)) { |
|---|
| 1955 | const char *key = GB_read_key_pntr(gbp); |
|---|
| 1956 | int keylen = strlen(key); |
|---|
| 1957 | |
|---|
| 1958 | if (result) { |
|---|
| 1959 | char *neu_result = (char*)malloc(result_length+keylen+1+1); |
|---|
| 1960 | |
|---|
| 1961 | if (neu_result) { |
|---|
| 1962 | char *p = gb_stpcpy(neu_result, result); |
|---|
| 1963 | p = gb_stpcpy(p, key); |
|---|
| 1964 | *p++ = ';'; |
|---|
| 1965 | p[0] = 0; |
|---|
| 1966 | |
|---|
| 1967 | freeset(result, neu_result); |
|---|
| 1968 | result_length += keylen+1; |
|---|
| 1969 | } |
|---|
| 1970 | else { |
|---|
| 1971 | gb_assert(0); |
|---|
| 1972 | } |
|---|
| 1973 | } |
|---|
| 1974 | else { |
|---|
| 1975 | result = (char*)malloc(1+keylen+1+1); |
|---|
| 1976 | result[0] = ';'; |
|---|
| 1977 | strcpy(result+1, key); |
|---|
| 1978 | result[keylen+1] = ';'; |
|---|
| 1979 | result[keylen+2] = 0; |
|---|
| 1980 | result_length = keylen+2; |
|---|
| 1981 | } |
|---|
| 1982 | } |
|---|
| 1983 | } |
|---|
| 1984 | else { |
|---|
| 1985 | result = strdup(";"); |
|---|
| 1986 | } |
|---|
| 1987 | |
|---|
| 1988 | return result; |
|---|
| 1989 | } |
|---|
| 1990 | |
|---|
| 1991 | // -------------------------- |
|---|
| 1992 | // temporary entries |
|---|
| 1993 | |
|---|
| 1994 | GB_ERROR GB_set_temporary(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
|---|
| 1995 | /*! if the temporary flag is set, then that entry (including all subentries) will not be saved |
|---|
| 1996 | * @see GB_clear_temporary() and GB_is_temporary() |
|---|
| 1997 | */ |
|---|
| 1998 | |
|---|
| 1999 | GB_ERROR error = NULL; |
|---|
| 2000 | GB_test_transaction(gbd); |
|---|
| 2001 | |
|---|
| 2002 | if (GB_GET_SECURITY_DELETE(gbd)>GB_MAIN(gbd)->security_level) { |
|---|
| 2003 | error = GBS_global_string("Security error in GB_set_temporary: %s", GB_read_key_pntr(gbd)); |
|---|
| 2004 | } |
|---|
| 2005 | else { |
|---|
| 2006 | gbd->flags.temporary = 1; |
|---|
| 2007 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
|---|
| 2008 | } |
|---|
| 2009 | RETURN_ERROR(error); |
|---|
| 2010 | } |
|---|
| 2011 | |
|---|
| 2012 | GB_ERROR GB_clear_temporary(GBDATA *gbd) { // @@@ used in ptpan branch - do not remove |
|---|
| 2013 | //! undo effect of GB_set_temporary() |
|---|
| 2014 | |
|---|
| 2015 | GB_test_transaction(gbd); |
|---|
| 2016 | gbd->flags.temporary = 0; |
|---|
| 2017 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
|---|
| 2018 | return 0; |
|---|
| 2019 | } |
|---|
| 2020 | |
|---|
| 2021 | bool GB_is_temporary(GBDATA *gbd) { |
|---|
| 2022 | //! @see GB_set_temporary() and GB_in_temporary_branch() |
|---|
| 2023 | GB_test_transaction(gbd); |
|---|
| 2024 | return (long)gbd->flags.temporary; |
|---|
| 2025 | } |
|---|
| 2026 | |
|---|
| 2027 | bool GB_in_temporary_branch(GBDATA *gbd) { |
|---|
| 2028 | /*! @return true, if 'gbd' is member of a temporary subtree, |
|---|
| 2029 | * i.e. if GB_is_temporary(itself or any parent) |
|---|
| 2030 | */ |
|---|
| 2031 | |
|---|
| 2032 | if (GB_is_temporary(gbd)) return true; |
|---|
| 2033 | |
|---|
| 2034 | GBDATA *gb_parent = GB_get_father(gbd); |
|---|
| 2035 | if (!gb_parent) return false; |
|---|
| 2036 | |
|---|
| 2037 | return GB_in_temporary_branch(gb_parent); |
|---|
| 2038 | } |
|---|
| 2039 | |
|---|
| 2040 | // --------------------- |
|---|
| 2041 | // transactions |
|---|
| 2042 | |
|---|
| 2043 | GB_ERROR GB_MAIN_TYPE::initial_client_transaction() { |
|---|
| 2044 | // the first client transaction ever |
|---|
| 2045 | transaction_level = 1; |
|---|
| 2046 | GB_ERROR error = gbcmc_init_transaction(root_container); |
|---|
| 2047 | if (!error) ++clock; |
|---|
| 2048 | return error; |
|---|
| 2049 | } |
|---|
| 2050 | |
|---|
| 2051 | inline GB_ERROR GB_MAIN_TYPE::start_transaction() { |
|---|
| 2052 | gb_assert(transaction_level == 0); |
|---|
| 2053 | |
|---|
| 2054 | transaction_level = 1; |
|---|
| 2055 | aborted_transaction = 0; |
|---|
| 2056 | |
|---|
| 2057 | GB_ERROR error = NULL; |
|---|
| 2058 | if (is_client()) { |
|---|
| 2059 | error = gbcmc_begin_transaction(gb_main()); |
|---|
| 2060 | if (!error) { |
|---|
| 2061 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, 0); // init structures |
|---|
| 2062 | gb_untouch_children_and_me(root_container); |
|---|
| 2063 | } |
|---|
| 2064 | } |
|---|
| 2065 | |
|---|
| 2066 | if (!error) { |
|---|
| 2067 | /* do all callbacks |
|---|
| 2068 | * cb that change the db are no problem, because it's the beginning of a ta |
|---|
| 2069 | */ |
|---|
| 2070 | call_pending_callbacks(); |
|---|
| 2071 | ++clock; |
|---|
| 2072 | } |
|---|
| 2073 | return error; |
|---|
| 2074 | } |
|---|
| 2075 | |
|---|
| 2076 | inline GB_ERROR GB_MAIN_TYPE::begin_transaction() { |
|---|
| 2077 | if (transaction_level>0) return GBS_global_string("attempt to start a NEW transaction (at transaction level %i)", transaction_level); |
|---|
| 2078 | if (transaction_level == 0) return start_transaction(); |
|---|
| 2079 | return NULL; // NO_TRANSACTION_MODE |
|---|
| 2080 | } |
|---|
| 2081 | |
|---|
| 2082 | inline GB_ERROR GB_MAIN_TYPE::abort_transaction() { |
|---|
| 2083 | if (transaction_level<=0) { |
|---|
| 2084 | if (transaction_level<0) return "GB_abort_transaction: Attempt to abort transaction in no-transaction-mode"; |
|---|
| 2085 | return "GB_abort_transaction: No transaction running"; |
|---|
| 2086 | } |
|---|
| 2087 | if (transaction_level>1) { |
|---|
| 2088 | aborted_transaction = 1; |
|---|
| 2089 | return pop_transaction(); |
|---|
| 2090 | } |
|---|
| 2091 | |
|---|
| 2092 | gb_abort_transaction_local_rek(gb_main_ref()); |
|---|
| 2093 | if (is_client()) { |
|---|
| 2094 | GB_ERROR error = gbcmc_abort_transaction(gb_main()); |
|---|
| 2095 | if (error) return error; |
|---|
| 2096 | } |
|---|
| 2097 | clock--; |
|---|
| 2098 | call_pending_callbacks(); |
|---|
| 2099 | transaction_level = 0; |
|---|
| 2100 | gb_untouch_children_and_me(root_container); |
|---|
| 2101 | return 0; |
|---|
| 2102 | } |
|---|
| 2103 | |
|---|
| 2104 | inline GB_ERROR GB_MAIN_TYPE::commit_transaction() { |
|---|
| 2105 | GB_ERROR error = 0; |
|---|
| 2106 | GB_CHANGE flag; |
|---|
| 2107 | |
|---|
| 2108 | if (!transaction_level) { |
|---|
| 2109 | return "commit_transaction: No transaction running"; |
|---|
| 2110 | } |
|---|
| 2111 | if (transaction_level>1) { |
|---|
| 2112 | return GBS_global_string("attempt to commit at transaction level %i", transaction_level); |
|---|
| 2113 | } |
|---|
| 2114 | if (aborted_transaction) { |
|---|
| 2115 | aborted_transaction = 0; |
|---|
| 2116 | return abort_transaction(); |
|---|
| 2117 | } |
|---|
| 2118 | if (is_server()) { |
|---|
| 2119 | char *error1 = gb_set_undo_sync(gb_main()); |
|---|
| 2120 | while (1) { |
|---|
| 2121 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
|---|
| 2122 | if (!flag) break; // nothing to do |
|---|
| 2123 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, 0); |
|---|
| 2124 | gb_untouch_children_and_me(root_container); |
|---|
| 2125 | if (error) break; |
|---|
| 2126 | call_pending_callbacks(); |
|---|
| 2127 | } |
|---|
| 2128 | gb_disable_undo(gb_main()); |
|---|
| 2129 | if (error1) { |
|---|
| 2130 | transaction_level = 0; |
|---|
| 2131 | gb_assert(error); // maybe return error1? |
|---|
| 2132 | return error; // @@@ huh? why not return error1 |
|---|
| 2133 | } |
|---|
| 2134 | } |
|---|
| 2135 | else { |
|---|
| 2136 | gb_disable_undo(gb_main()); |
|---|
| 2137 | while (1) { |
|---|
| 2138 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
|---|
| 2139 | if (!flag) break; // nothing to do |
|---|
| 2140 | |
|---|
| 2141 | error = gbcmc_begin_sendupdate(gb_main()); if (error) break; |
|---|
| 2142 | error = gb_commit_transaction_local_rek(gb_main_ref(), 1, 0); if (error) break; |
|---|
| 2143 | error = gbcmc_end_sendupdate(gb_main()); if (error) break; |
|---|
| 2144 | |
|---|
| 2145 | gb_untouch_children_and_me(root_container); |
|---|
| 2146 | call_pending_callbacks(); |
|---|
| 2147 | } |
|---|
| 2148 | if (!error) error = gbcmc_commit_transaction(gb_main()); |
|---|
| 2149 | |
|---|
| 2150 | } |
|---|
| 2151 | transaction_level = 0; |
|---|
| 2152 | return error; |
|---|
| 2153 | } |
|---|
| 2154 | |
|---|
| 2155 | inline GB_ERROR GB_MAIN_TYPE::push_transaction() { |
|---|
| 2156 | if (transaction_level == 0) return start_transaction(); |
|---|
| 2157 | if (transaction_level>0) ++transaction_level; |
|---|
| 2158 | // transaction<0 is NO_TRANSACTION_MODE |
|---|
| 2159 | return NULL; |
|---|
| 2160 | } |
|---|
| 2161 | |
|---|
| 2162 | inline GB_ERROR GB_MAIN_TYPE::pop_transaction() { |
|---|
| 2163 | if (transaction_level==0) return "attempt to pop nested transaction while none running"; |
|---|
| 2164 | if (transaction_level<0) return 0; // NO_TRANSACTION_MODE |
|---|
| 2165 | if (transaction_level==1) return commit_transaction(); |
|---|
| 2166 | transaction_level--; |
|---|
| 2167 | return NULL; |
|---|
| 2168 | } |
|---|
| 2169 | |
|---|
| 2170 | inline GB_ERROR GB_MAIN_TYPE::no_transaction() { |
|---|
| 2171 | if (is_client()) return "Tried to disable transactions in a client"; |
|---|
| 2172 | transaction_level = -1; |
|---|
| 2173 | return NULL; |
|---|
| 2174 | } |
|---|
| 2175 | |
|---|
| 2176 | GB_ERROR GB_MAIN_TYPE::send_update_to_server(GBDATA *gbd) { |
|---|
| 2177 | GB_ERROR error = NULL; |
|---|
| 2178 | |
|---|
| 2179 | if (!transaction_level) error = "send_update_to_server: no transaction running"; |
|---|
| 2180 | else if (is_server()) error = "send_update_to_server: only possible from clients (not from server itself)"; |
|---|
| 2181 | else { |
|---|
| 2182 | const gb_triggered_callback *chg_cbl_old = changeCBs.pending.get_tail(); |
|---|
| 2183 | const gb_triggered_callback *del_cbl_old = deleteCBs.pending.get_tail(); |
|---|
| 2184 | |
|---|
| 2185 | error = gbcmc_begin_sendupdate(gb_main()); |
|---|
| 2186 | if (!error) error = gb_commit_transaction_local_rek(gbd, 2, 0); |
|---|
| 2187 | if (!error) error = gbcmc_end_sendupdate(gb_main()); |
|---|
| 2188 | |
|---|
| 2189 | if (!error && |
|---|
| 2190 | (chg_cbl_old != changeCBs.pending.get_tail() || |
|---|
| 2191 | del_cbl_old != deleteCBs.pending.get_tail())) |
|---|
| 2192 | { |
|---|
| 2193 | error = "send_update_to_server triggered a callback (this is not allowed)"; |
|---|
| 2194 | } |
|---|
| 2195 | } |
|---|
| 2196 | return error; |
|---|
| 2197 | } |
|---|
| 2198 | |
|---|
| 2199 | // -------------------------------------- |
|---|
| 2200 | // client transaction interface |
|---|
| 2201 | |
|---|
| 2202 | GB_ERROR GB_push_transaction(GBDATA *gbd) { |
|---|
| 2203 | /*! start a transaction if no transaction is running. |
|---|
| 2204 | * (otherwise only trace nested transactions) |
|---|
| 2205 | * |
|---|
| 2206 | * recommended transaction usage: |
|---|
| 2207 | * |
|---|
| 2208 | * \code |
|---|
| 2209 | * GB_ERROR myFunc() { |
|---|
| 2210 | * GB_ERROR error = GB_push_transaction(gbd); |
|---|
| 2211 | * if (!error) { |
|---|
| 2212 | * error = ...; |
|---|
| 2213 | * } |
|---|
| 2214 | * return GB_end_transaction(gbd, error); |
|---|
| 2215 | * } |
|---|
| 2216 | * |
|---|
| 2217 | * void myFunc() { |
|---|
| 2218 | * GB_ERROR error = GB_push_transaction(gbd); |
|---|
| 2219 | * if (!error) { |
|---|
| 2220 | * error = ...; |
|---|
| 2221 | * } |
|---|
| 2222 | * GB_end_transaction_show_error(gbd, error, aw_message); |
|---|
| 2223 | * } |
|---|
| 2224 | * \endcode |
|---|
| 2225 | * |
|---|
| 2226 | * @see GB_pop_transaction(), GB_end_transaction(), GB_begin_transaction() |
|---|
| 2227 | */ |
|---|
| 2228 | |
|---|
| 2229 | return GB_MAIN(gbd)->push_transaction(); |
|---|
| 2230 | } |
|---|
| 2231 | |
|---|
| 2232 | GB_ERROR GB_pop_transaction(GBDATA *gbd) { |
|---|
| 2233 | //! commit a transaction started with GB_push_transaction() |
|---|
| 2234 | return GB_MAIN(gbd)->pop_transaction(); |
|---|
| 2235 | } |
|---|
| 2236 | GB_ERROR GB_begin_transaction(GBDATA *gbd) { |
|---|
| 2237 | /*! like GB_push_transaction(), |
|---|
| 2238 | * but fails if there is already an transaction running. |
|---|
| 2239 | * @see GB_commit_transaction() and GB_abort_transaction() |
|---|
| 2240 | */ |
|---|
| 2241 | return GB_MAIN(gbd)->begin_transaction(); |
|---|
| 2242 | } |
|---|
| 2243 | GB_ERROR GB_no_transaction(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
|---|
| 2244 | return GB_MAIN(gbd)->no_transaction(); |
|---|
| 2245 | } |
|---|
| 2246 | |
|---|
| 2247 | GB_ERROR GB_abort_transaction(GBDATA *gbd) { |
|---|
| 2248 | /*! abort a running transaction, |
|---|
| 2249 | * i.e. forget all changes made to DB inside the current transaction. |
|---|
| 2250 | * |
|---|
| 2251 | * May be called instead of GB_pop_transaction() or GB_commit_transaction() |
|---|
| 2252 | * |
|---|
| 2253 | * If a nested transactions got aborted, |
|---|
| 2254 | * committing a surrounding transaction will silently abort it as well. |
|---|
| 2255 | */ |
|---|
| 2256 | return GB_MAIN(gbd)->abort_transaction(); |
|---|
| 2257 | } |
|---|
| 2258 | |
|---|
| 2259 | GB_ERROR GB_commit_transaction(GBDATA *gbd) { |
|---|
| 2260 | /*! commit a transaction started with GB_begin_transaction() |
|---|
| 2261 | * |
|---|
| 2262 | * commit changes made to DB. |
|---|
| 2263 | * |
|---|
| 2264 | * in case of nested transactions, this is equal to GB_pop_transaction() |
|---|
| 2265 | */ |
|---|
| 2266 | return GB_MAIN(gbd)->commit_transaction(); |
|---|
| 2267 | } |
|---|
| 2268 | |
|---|
| 2269 | GB_ERROR GB_end_transaction(GBDATA *gbd, GB_ERROR error) { |
|---|
| 2270 | /*! abort or commit transaction |
|---|
| 2271 | * |
|---|
| 2272 | * @ param error |
|---|
| 2273 | * - if NULL commit transaction |
|---|
| 2274 | * - else abort transaction |
|---|
| 2275 | * |
|---|
| 2276 | * always commits in no-transaction-mode |
|---|
| 2277 | * |
|---|
| 2278 | * @return error or transaction error |
|---|
| 2279 | * @see GB_push_transaction() for example |
|---|
| 2280 | */ |
|---|
| 2281 | |
|---|
| 2282 | if (GB_get_transaction_level(gbd)<0) { |
|---|
| 2283 | ASSERT_RESULT(GB_ERROR, NULL, GB_pop_transaction(gbd)); |
|---|
| 2284 | } |
|---|
| 2285 | else { |
|---|
| 2286 | if (error) GB_abort_transaction(gbd); |
|---|
| 2287 | else error = GB_pop_transaction(gbd); |
|---|
| 2288 | } |
|---|
| 2289 | return error; |
|---|
| 2290 | } |
|---|
| 2291 | |
|---|
| 2292 | void GB_end_transaction_show_error(GBDATA *gbd, GB_ERROR error, void (*error_handler)(GB_ERROR)) { |
|---|
| 2293 | //! like GB_end_transaction(), but show error using 'error_handler' |
|---|
| 2294 | error = GB_end_transaction(gbd, error); |
|---|
| 2295 | if (error) error_handler(error); |
|---|
| 2296 | } |
|---|
| 2297 | |
|---|
| 2298 | int GB_get_transaction_level(GBDATA *gbd) { |
|---|
| 2299 | /*! @return transaction level |
|---|
| 2300 | * <0 -> in no-transaction-mode (abort is impossible) |
|---|
| 2301 | * 0 -> not in transaction |
|---|
| 2302 | * 1 -> one single transaction |
|---|
| 2303 | * 2, ... -> nested transactions |
|---|
| 2304 | */ |
|---|
| 2305 | return GB_MAIN(gbd)->get_transaction_level(); |
|---|
| 2306 | } |
|---|
| 2307 | |
|---|
| 2308 | // ------------------ |
|---|
| 2309 | // callbacks |
|---|
| 2310 | |
|---|
| 2311 | static void dummy_db_cb(GBDATA*, GB_CB_TYPE) { gb_assert(0); } // used as marker for deleted callbacks |
|---|
| 2312 | DatabaseCallback TypedDatabaseCallback::MARKED_DELETED = makeDatabaseCallback(dummy_db_cb); |
|---|
| 2313 | |
|---|
| 2314 | GB_MAIN_TYPE *gb_get_main_during_cb() { |
|---|
| 2315 | /* if inside a callback, return the DB root of the DB element, the callback was called for. |
|---|
| 2316 | * if not inside a callback, return NULL. |
|---|
| 2317 | */ |
|---|
| 2318 | return inside_callback_main; |
|---|
| 2319 | } |
|---|
| 2320 | |
|---|
| 2321 | NOT4PERL bool GB_inside_callback(GBDATA *of_gbd, GB_CB_TYPE cbtype) { |
|---|
| 2322 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
|---|
| 2323 | bool inside = false; |
|---|
| 2324 | |
|---|
| 2325 | if (Main) { // inside a callback |
|---|
| 2326 | gb_assert(currently_called_back); |
|---|
| 2327 | if (currently_called_back->gbd == of_gbd) { |
|---|
| 2328 | GB_CB_TYPE curr_cbtype; |
|---|
| 2329 | if (Main->has_pending_delete_callback()) { // delete callbacks were not all performed yet |
|---|
| 2330 | // => current callback is a delete callback |
|---|
| 2331 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & GB_CB_DELETE); |
|---|
| 2332 | } |
|---|
| 2333 | else { |
|---|
| 2334 | gb_assert(Main->has_pending_change_callback()); |
|---|
| 2335 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & (GB_CB_ALL-GB_CB_DELETE)); |
|---|
| 2336 | } |
|---|
| 2337 | gb_assert(curr_cbtype != GB_CB_NONE); // wtf!? are we inside callback or not? |
|---|
| 2338 | |
|---|
| 2339 | if ((cbtype&curr_cbtype) != GB_CB_NONE) { |
|---|
| 2340 | inside = true; |
|---|
| 2341 | } |
|---|
| 2342 | } |
|---|
| 2343 | } |
|---|
| 2344 | |
|---|
| 2345 | return inside; |
|---|
| 2346 | } |
|---|
| 2347 | |
|---|
| 2348 | GBDATA *GB_get_gb_main_during_cb() { |
|---|
| 2349 | GBDATA *gb_main = NULL; |
|---|
| 2350 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
|---|
| 2351 | |
|---|
| 2352 | if (Main) { // inside callback |
|---|
| 2353 | if (!GB_inside_callback(Main->gb_main(), GB_CB_DELETE)) { // main is not deleted |
|---|
| 2354 | gb_main = Main->gb_main(); |
|---|
| 2355 | } |
|---|
| 2356 | } |
|---|
| 2357 | return gb_main; |
|---|
| 2358 | } |
|---|
| 2359 | |
|---|
| 2360 | |
|---|
| 2361 | |
|---|
| 2362 | static GB_CSTR gb_read_pntr_ts(GBDATA *gbd, gb_transaction_save *ts) { |
|---|
| 2363 | int type = GB_TYPE_TS(ts); |
|---|
| 2364 | const char *data = GB_GETDATA_TS(ts); |
|---|
| 2365 | if (data) { |
|---|
| 2366 | if (ts->flags.compressed_data) { // uncompressed data return pntr to database entry |
|---|
| 2367 | long size = GB_GETSIZE_TS(ts) * gb_convert_type_2_sizeof[type] + gb_convert_type_2_appendix_size[type]; |
|---|
| 2368 | data = gb_uncompress_data(gbd, data, size); |
|---|
| 2369 | } |
|---|
| 2370 | } |
|---|
| 2371 | return data; |
|---|
| 2372 | } |
|---|
| 2373 | |
|---|
| 2374 | // get last array value in callbacks |
|---|
| 2375 | NOT4PERL const void *GB_read_old_value() { |
|---|
| 2376 | char *data; |
|---|
| 2377 | |
|---|
| 2378 | if (!currently_called_back) { |
|---|
| 2379 | GB_export_error("You cannot call GB_read_old_value outside a ARBDB callback"); |
|---|
| 2380 | return NULL; |
|---|
| 2381 | } |
|---|
| 2382 | if (!currently_called_back->old) { |
|---|
| 2383 | GB_export_error("No old value available in GB_read_old_value"); |
|---|
| 2384 | return NULL; |
|---|
| 2385 | } |
|---|
| 2386 | data = GB_GETDATA_TS(currently_called_back->old); |
|---|
| 2387 | if (!data) return NULL; |
|---|
| 2388 | |
|---|
| 2389 | return gb_read_pntr_ts(currently_called_back->gbd, currently_called_back->old); |
|---|
| 2390 | } |
|---|
| 2391 | // same for size |
|---|
| 2392 | long GB_read_old_size() { |
|---|
| 2393 | if (!currently_called_back) { |
|---|
| 2394 | GB_export_error("You cannot call GB_read_old_size outside a ARBDB callback"); |
|---|
| 2395 | return -1; |
|---|
| 2396 | } |
|---|
| 2397 | if (!currently_called_back->old) { |
|---|
| 2398 | GB_export_error("No old value available in GB_read_old_size"); |
|---|
| 2399 | return -1; |
|---|
| 2400 | } |
|---|
| 2401 | return GB_GETSIZE_TS(currently_called_back->old); |
|---|
| 2402 | } |
|---|
| 2403 | |
|---|
| 2404 | inline char *cbtype2readable(GB_CB_TYPE type) { |
|---|
| 2405 | ConstStrArray septype; |
|---|
| 2406 | |
|---|
| 2407 | #define appendcbtype(cbt) do { \ |
|---|
| 2408 | if (type&cbt) { \ |
|---|
| 2409 | type = GB_CB_TYPE(type-cbt); \ |
|---|
| 2410 | septype.put(#cbt); \ |
|---|
| 2411 | } \ |
|---|
| 2412 | } while(0) |
|---|
| 2413 | |
|---|
| 2414 | appendcbtype(GB_CB_DELETE); |
|---|
| 2415 | appendcbtype(GB_CB_CHANGED); |
|---|
| 2416 | appendcbtype(GB_CB_SON_CREATED); |
|---|
| 2417 | |
|---|
| 2418 | gb_assert(type == GB_CB_NONE); |
|---|
| 2419 | |
|---|
| 2420 | return GBT_join_names(septype, '|'); |
|---|
| 2421 | } |
|---|
| 2422 | |
|---|
| 2423 | char *TypedDatabaseCallback::get_info() const { |
|---|
| 2424 | const char *readable_fun = GBS_funptr2readable((void*)dbcb.callee(), true); |
|---|
| 2425 | char *readable_cbtype = cbtype2readable((GB_CB_TYPE)dbcb.inspect_CD2()); |
|---|
| 2426 | char *result = GBS_global_string_copy("func='%s' type=%s clientdata=%p", |
|---|
| 2427 | readable_fun, readable_cbtype, (void*)dbcb.inspect_CD1()); |
|---|
| 2428 | |
|---|
| 2429 | free(readable_cbtype); |
|---|
| 2430 | |
|---|
| 2431 | return result; |
|---|
| 2432 | } |
|---|
| 2433 | |
|---|
| 2434 | char *GB_get_callback_info(GBDATA *gbd) { |
|---|
| 2435 | // returns human-readable information about callbacks of 'gbd' or 0 |
|---|
| 2436 | char *result = 0; |
|---|
| 2437 | if (gbd->ext) { |
|---|
| 2438 | gb_callback_list *cbl = gbd->get_callbacks(); |
|---|
| 2439 | if (cbl) { |
|---|
| 2440 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
|---|
| 2441 | char *cb_info = cb->spec.get_info(); |
|---|
| 2442 | if (result) { |
|---|
| 2443 | char *new_result = GBS_global_string_copy("%s\n%s", result, cb_info); |
|---|
| 2444 | free(result); |
|---|
| 2445 | free(cb_info); |
|---|
| 2446 | result = new_result; |
|---|
| 2447 | } |
|---|
| 2448 | else { |
|---|
| 2449 | result = cb_info; |
|---|
| 2450 | } |
|---|
| 2451 | } |
|---|
| 2452 | } |
|---|
| 2453 | } |
|---|
| 2454 | |
|---|
| 2455 | return result; |
|---|
| 2456 | } |
|---|
| 2457 | |
|---|
| 2458 | #if defined(ASSERTION_USED) |
|---|
| 2459 | template<typename CB> |
|---|
| 2460 | bool CallbackList<CB>::contains_unremoved_callback(const CB& like) const { |
|---|
| 2461 | for (const_itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
|---|
| 2462 | if (cb->spec.is_equal_to(like.spec) && !cb->spec.is_marked_for_removal()) { |
|---|
| 2463 | return true; |
|---|
| 2464 | } |
|---|
| 2465 | } |
|---|
| 2466 | return false; |
|---|
| 2467 | } |
|---|
| 2468 | #endif |
|---|
| 2469 | |
|---|
| 2470 | inline void add_to_callback_chain(gb_callback_list*& head, const TypedDatabaseCallback& cbs) { |
|---|
| 2471 | if (!head) head = new gb_callback_list; |
|---|
| 2472 | head->add(gb_callback(cbs)); |
|---|
| 2473 | } |
|---|
| 2474 | inline void add_to_callback_chain(gb_hierarchy_callback_list*& head, const TypedDatabaseCallback& cbs, GBDATA *gb_representative) { |
|---|
| 2475 | if (!head) head = new gb_hierarchy_callback_list; |
|---|
| 2476 | head->add(gb_hierarchy_callback(cbs, gb_representative)); |
|---|
| 2477 | } |
|---|
| 2478 | |
|---|
| 2479 | inline GB_ERROR gb_add_callback(GBDATA *gbd, const TypedDatabaseCallback& cbs) { |
|---|
| 2480 | /* Adds a callback to a DB entry. |
|---|
| 2481 | * |
|---|
| 2482 | * Be careful when writing GB_CB_DELETE callbacks, there is a severe restriction: |
|---|
| 2483 | * |
|---|
| 2484 | * - the DB element may already be freed. The pointer is still pointing to the original |
|---|
| 2485 | * location, so you can use it to identify the DB element, but you cannot dereference |
|---|
| 2486 | * it under all circumstances. |
|---|
| 2487 | * |
|---|
| 2488 | * ARBDB internal delete-callbacks may use gb_get_main_during_cb() to access the DB root. |
|---|
| 2489 | * See also: GB_get_gb_main_during_cb() |
|---|
| 2490 | */ |
|---|
| 2491 | |
|---|
| 2492 | #if defined(DEBUG) |
|---|
| 2493 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
|---|
| 2494 | printf("Warning: add_priority_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
|---|
| 2495 | #if defined(DEVEL_RALF) |
|---|
| 2496 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
|---|
| 2497 | #endif // DEVEL_RALF |
|---|
| 2498 | } |
|---|
| 2499 | #endif // DEBUG |
|---|
| 2500 | |
|---|
| 2501 | GB_test_transaction(gbd); // may return error |
|---|
| 2502 | gbd->create_extended(); |
|---|
| 2503 | add_to_callback_chain(gbd->ext->callback, cbs); |
|---|
| 2504 | return 0; |
|---|
| 2505 | } |
|---|
| 2506 | |
|---|
| 2507 | GB_ERROR GB_add_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
|---|
| 2508 | return gb_add_callback(gbd, TypedDatabaseCallback(dbcb, type)); |
|---|
| 2509 | } |
|---|
| 2510 | |
|---|
| 2511 | inline void GB_MAIN_TYPE::callback_group::add_hcb(GBDATA *gb_representative, const TypedDatabaseCallback& dbcb) { |
|---|
| 2512 | add_to_callback_chain(hierarchy_cbs, dbcb, gb_representative); |
|---|
| 2513 | } |
|---|
| 2514 | |
|---|
| 2515 | GB_ERROR GB_MAIN_TYPE::add_hierarchy_cb(GBDATA *gb_representative, const TypedDatabaseCallback& dbcb) { |
|---|
| 2516 | GB_CB_TYPE type = dbcb.get_type(); |
|---|
| 2517 | if (type & GB_CB_DELETE) { |
|---|
| 2518 | deleteCBs.add_hcb(gb_representative, dbcb.with_type_changed_to(GB_CB_DELETE)); |
|---|
| 2519 | } |
|---|
| 2520 | if (type & GB_CB_ALL_BUT_DELETE) { |
|---|
| 2521 | changeCBs.add_hcb(gb_representative, dbcb.with_type_changed_to(GB_CB_TYPE(type&GB_CB_ALL_BUT_DELETE))); |
|---|
| 2522 | } |
|---|
| 2523 | return NULL; |
|---|
| 2524 | } |
|---|
| 2525 | |
|---|
| 2526 | GB_ERROR GB_add_hierarchy_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { // @@@ needed to impl #418 |
|---|
| 2527 | /*! bind callback to ALL entries which are at the same DB-hierarchy as 'gbd'. |
|---|
| 2528 | * |
|---|
| 2529 | * Hierarchy callbacks are triggered before normal callbacks (added by GB_add_callback or GB_ensure_callback). |
|---|
| 2530 | * Nevertheless delete callbacks take precedence over change callbacks |
|---|
| 2531 | * (i.e. a normal delete callback is triggered before a hierarchical change callback). |
|---|
| 2532 | * |
|---|
| 2533 | * Hierarchy callbacks are cannot be installed and will NOT be triggered in NO_TRANSACTION_MODE |
|---|
| 2534 | * (i.e. it will not work in ARBs property DBs) |
|---|
| 2535 | */ |
|---|
| 2536 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
|---|
| 2537 | gb_assert(Main->get_transaction_level()>=0); // hierarchical callbacks are not supported in NO_TRANSACTION_MODE |
|---|
| 2538 | return Main->add_hierarchy_cb(gbd, TypedDatabaseCallback(dbcb, type)); |
|---|
| 2539 | } |
|---|
| 2540 | |
|---|
| 2541 | template <typename PRED> |
|---|
| 2542 | inline void gb_remove_callbacks_that(GBDATA *gbd, PRED shallRemove) { |
|---|
| 2543 | #if defined(ASSERTION_USED) |
|---|
| 2544 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
|---|
| 2545 | printf("Warning: gb_remove_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
|---|
| 2546 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
|---|
| 2547 | return; |
|---|
| 2548 | } |
|---|
| 2549 | #endif // DEBUG |
|---|
| 2550 | |
|---|
| 2551 | if (gbd->ext) { |
|---|
| 2552 | gb_callback_list *cbl = gbd->get_callbacks(); |
|---|
| 2553 | if (cbl) { |
|---|
| 2554 | bool prev_running = false; |
|---|
| 2555 | |
|---|
| 2556 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ) { |
|---|
| 2557 | bool this_running = cb->running; |
|---|
| 2558 | |
|---|
| 2559 | if (shallRemove(*cb)) { |
|---|
| 2560 | if (prev_running || this_running) { |
|---|
| 2561 | cb->spec.mark_for_removal(); |
|---|
| 2562 | ++cb; |
|---|
| 2563 | } |
|---|
| 2564 | else { |
|---|
| 2565 | cb = cbl->callbacks.erase(cb); |
|---|
| 2566 | } |
|---|
| 2567 | } |
|---|
| 2568 | else { |
|---|
| 2569 | ++cb; |
|---|
| 2570 | } |
|---|
| 2571 | prev_running = this_running; |
|---|
| 2572 | } |
|---|
| 2573 | } |
|---|
| 2574 | } |
|---|
| 2575 | } |
|---|
| 2576 | |
|---|
| 2577 | struct ShallBeDeleted { |
|---|
| 2578 | bool operator()(const gb_callback& cb) const { return cb.spec.is_marked_for_removal(); } |
|---|
| 2579 | }; |
|---|
| 2580 | static void gb_remove_callbacks_marked_for_deletion(GBDATA *gbd) { |
|---|
| 2581 | gb_remove_callbacks_that(gbd, ShallBeDeleted()); |
|---|
| 2582 | } |
|---|
| 2583 | |
|---|
| 2584 | struct IsCallback : private TypedDatabaseCallback { |
|---|
| 2585 | IsCallback(GB_CB func_, GB_CB_TYPE type_) : TypedDatabaseCallback(makeDatabaseCallback((GB_CB)func_, (int*)NULL), type_) {} |
|---|
| 2586 | bool operator()(const gb_callback& cb) const { return sig_is_equal_to(cb.spec); } |
|---|
| 2587 | }; |
|---|
| 2588 | struct IsSpecificCallback : private TypedDatabaseCallback { |
|---|
| 2589 | IsSpecificCallback(const TypedDatabaseCallback& cb) : TypedDatabaseCallback(cb) {} |
|---|
| 2590 | bool operator()(const gb_callback& cb) const { return is_equal_to(cb.spec); } |
|---|
| 2591 | }; |
|---|
| 2592 | |
|---|
| 2593 | void GB_remove_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
|---|
| 2594 | // remove specific callback; 'type' and 'dbcb' have to match |
|---|
| 2595 | gb_remove_callbacks_that(gbd, IsSpecificCallback(TypedDatabaseCallback(dbcb, type))); |
|---|
| 2596 | } |
|---|
| 2597 | void GB_remove_all_callbacks_to(GBDATA *gbd, GB_CB_TYPE type, GB_CB func) { |
|---|
| 2598 | // removes all callbacks 'func' bound to 'gbd' with 'type' |
|---|
| 2599 | gb_remove_callbacks_that(gbd, IsCallback(func, type)); |
|---|
| 2600 | } |
|---|
| 2601 | |
|---|
| 2602 | GB_ERROR GB_ensure_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
|---|
| 2603 | TypedDatabaseCallback newcb(dbcb, type); |
|---|
| 2604 | gb_callback_list *cbl = gbd->get_callbacks(); |
|---|
| 2605 | if (cbl) { |
|---|
| 2606 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
|---|
| 2607 | if (cb->spec.is_equal_to(newcb) && !cb->spec.is_marked_for_removal()) { |
|---|
| 2608 | return NULL; // already in cb list |
|---|
| 2609 | } |
|---|
| 2610 | } |
|---|
| 2611 | } |
|---|
| 2612 | return gb_add_callback(gbd, newcb); |
|---|
| 2613 | } |
|---|
| 2614 | |
|---|
| 2615 | int GB_nsons(GBDATA *gbd) { |
|---|
| 2616 | /*! return number of child entries |
|---|
| 2617 | * |
|---|
| 2618 | * @@@ does this work in clients ? |
|---|
| 2619 | */ |
|---|
| 2620 | |
|---|
| 2621 | return gbd->is_container() |
|---|
| 2622 | ? gbd->as_container()->d.size |
|---|
| 2623 | : 0; |
|---|
| 2624 | } |
|---|
| 2625 | |
|---|
| 2626 | void GB_disable_quicksave(GBDATA *gbd, const char *reason) { |
|---|
| 2627 | /*! Disable quicksaving database |
|---|
| 2628 | * @param gbd any DB node |
|---|
| 2629 | * @param reason why quicksaving is not allowed |
|---|
| 2630 | */ |
|---|
| 2631 | freedup(GB_MAIN(gbd)->qs.quick_save_disabled, reason); |
|---|
| 2632 | } |
|---|
| 2633 | |
|---|
| 2634 | GB_ERROR GB_resort_data_base(GBDATA *gb_main, GBDATA **new_order_list, long listsize) { |
|---|
| 2635 | { |
|---|
| 2636 | long client_count = GB_read_clients(gb_main); |
|---|
| 2637 | if (client_count<0) |
|---|
| 2638 | return "Sorry: this program is not the arbdb server, you cannot resort your data"; |
|---|
| 2639 | |
|---|
| 2640 | if (client_count>0) |
|---|
| 2641 | return GBS_global_string("There are %li clients (editors, tree programs) connected to this server.\n" |
|---|
| 2642 | "You need to these close clients before you can run this operation.", |
|---|
| 2643 | client_count); |
|---|
| 2644 | } |
|---|
| 2645 | |
|---|
| 2646 | if (listsize <= 0) return 0; |
|---|
| 2647 | |
|---|
| 2648 | GBCONTAINER *father = GB_FATHER(new_order_list[0]); |
|---|
| 2649 | GB_disable_quicksave(gb_main, "some entries in the database got a new order"); |
|---|
| 2650 | |
|---|
| 2651 | gb_header_list *hl = GB_DATA_LIST_HEADER(father->d); |
|---|
| 2652 | for (long new_index = 0; new_index< listsize; new_index++) { |
|---|
| 2653 | long old_index = new_order_list[new_index]->index; |
|---|
| 2654 | |
|---|
| 2655 | if (old_index < new_index) { |
|---|
| 2656 | GB_warningf("Warning at resort database: entry exists twice: %li and %li", |
|---|
| 2657 | old_index, new_index); |
|---|
| 2658 | } |
|---|
| 2659 | else { |
|---|
| 2660 | GBDATA *ogb = GB_HEADER_LIST_GBD(hl[old_index]); |
|---|
| 2661 | GBDATA *ngb = GB_HEADER_LIST_GBD(hl[new_index]); |
|---|
| 2662 | |
|---|
| 2663 | gb_header_list h = hl[new_index]; |
|---|
| 2664 | hl[new_index] = hl[old_index]; |
|---|
| 2665 | hl[old_index] = h; // Warning: Relative Pointers are incorrect !!! |
|---|
| 2666 | |
|---|
| 2667 | SET_GB_HEADER_LIST_GBD(hl[old_index], ngb); |
|---|
| 2668 | SET_GB_HEADER_LIST_GBD(hl[new_index], ogb); |
|---|
| 2669 | |
|---|
| 2670 | if (ngb) ngb->index = old_index; |
|---|
| 2671 | if (ogb) ogb->index = new_index; |
|---|
| 2672 | } |
|---|
| 2673 | } |
|---|
| 2674 | |
|---|
| 2675 | gb_touch_entry(father, GB_NORMAL_CHANGE); |
|---|
| 2676 | return 0; |
|---|
| 2677 | } |
|---|
| 2678 | |
|---|
| 2679 | GB_ERROR gb_resort_system_folder_to_top(GBCONTAINER *gb_main) { |
|---|
| 2680 | GBDATA *gb_system = GB_entry(gb_main, GB_SYSTEM_FOLDER); |
|---|
| 2681 | GBDATA *gb_first = GB_child(gb_main); |
|---|
| 2682 | GBDATA **new_order_list; |
|---|
| 2683 | GB_ERROR error = 0; |
|---|
| 2684 | int i, len; |
|---|
| 2685 | if (GB_read_clients(gb_main)<0) return 0; // we are not server |
|---|
| 2686 | if (!gb_system) { |
|---|
| 2687 | return GB_export_error("System databaseentry does not exist"); |
|---|
| 2688 | } |
|---|
| 2689 | if (gb_first == gb_system) return 0; |
|---|
| 2690 | len = GB_number_of_subentries(gb_main); |
|---|
| 2691 | new_order_list = (GBDATA **)GB_calloc(sizeof(GBDATA *), len); |
|---|
| 2692 | new_order_list[0] = gb_system; |
|---|
| 2693 | for (i=1; i<len; i++) { |
|---|
| 2694 | new_order_list[i] = gb_first; |
|---|
| 2695 | do { |
|---|
| 2696 | gb_first = GB_nextChild(gb_first); |
|---|
| 2697 | } while (gb_first == gb_system); |
|---|
| 2698 | } |
|---|
| 2699 | error = GB_resort_data_base(gb_main, new_order_list, len); |
|---|
| 2700 | free(new_order_list); |
|---|
| 2701 | return error; |
|---|
| 2702 | } |
|---|
| 2703 | |
|---|
| 2704 | // ------------------------------ |
|---|
| 2705 | // private(?) user flags |
|---|
| 2706 | |
|---|
| 2707 | STATIC_ASSERT_ANNOTATED(((GB_USERFLAG_ANY+1)&GB_USERFLAG_ANY) == 0, "not all bits set in GB_USERFLAG_ANY"); |
|---|
| 2708 | |
|---|
| 2709 | #if defined(ASSERTION_USED) |
|---|
| 2710 | inline bool legal_user_bitmask(unsigned char bitmask) { |
|---|
| 2711 | return bitmask>0 && bitmask<=GB_USERFLAG_ANY; |
|---|
| 2712 | } |
|---|
| 2713 | #endif |
|---|
| 2714 | |
|---|
| 2715 | inline gb_flag_types2& get_user_flags(GBDATA *gbd) { |
|---|
| 2716 | return gbd->expect_container()->flags2; |
|---|
| 2717 | } |
|---|
| 2718 | |
|---|
| 2719 | bool GB_user_flag(GBDATA *gbd, unsigned char user_bit) { |
|---|
| 2720 | gb_assert(legal_user_bitmask(user_bit)); |
|---|
| 2721 | return get_user_flags(gbd).user_bits & user_bit; |
|---|
| 2722 | } |
|---|
| 2723 | void GB_set_user_flag(GBDATA *gbd, unsigned char user_bit) { |
|---|
| 2724 | gb_assert(legal_user_bitmask(user_bit)); |
|---|
| 2725 | gb_flag_types2& flags = get_user_flags(gbd); |
|---|
| 2726 | flags.user_bits |= user_bit; |
|---|
| 2727 | } |
|---|
| 2728 | |
|---|
| 2729 | void GB_clear_user_flag(GBDATA *gbd, unsigned char user_bit) { |
|---|
| 2730 | gb_assert(legal_user_bitmask(user_bit)); |
|---|
| 2731 | gb_flag_types2& flags = get_user_flags(gbd); |
|---|
| 2732 | flags.user_bits &= (user_bit^GB_USERFLAG_ANY); |
|---|
| 2733 | } |
|---|
| 2734 | |
|---|
| 2735 | // ------------------------ |
|---|
| 2736 | // mark DB entries |
|---|
| 2737 | |
|---|
| 2738 | void GB_write_flag(GBDATA *gbd, long flag) { |
|---|
| 2739 | GBCONTAINER *gbc = gbd->expect_container(); |
|---|
| 2740 | GB_MAIN_TYPE *Main = GB_MAIN(gbc); |
|---|
| 2741 | |
|---|
| 2742 | GB_test_transaction(Main); |
|---|
| 2743 | |
|---|
| 2744 | int ubit = Main->users[0]->userbit; |
|---|
| 2745 | int prev = GB_ARRAY_FLAGS(gbc).flags; |
|---|
| 2746 | gbc->flags.saved_flags = prev; |
|---|
| 2747 | |
|---|
| 2748 | if (flag) { |
|---|
| 2749 | GB_ARRAY_FLAGS(gbc).flags |= ubit; |
|---|
| 2750 | } |
|---|
| 2751 | else { |
|---|
| 2752 | GB_ARRAY_FLAGS(gbc).flags &= ~ubit; |
|---|
| 2753 | } |
|---|
| 2754 | if (prev != (int)GB_ARRAY_FLAGS(gbc).flags) { |
|---|
| 2755 | gb_touch_entry(gbc, GB_NORMAL_CHANGE); |
|---|
| 2756 | gb_touch_header(GB_FATHER(gbc)); |
|---|
| 2757 | GB_DO_CALLBACKS(gbc); |
|---|
| 2758 | } |
|---|
| 2759 | } |
|---|
| 2760 | |
|---|
| 2761 | int GB_read_flag(GBDATA *gbd) { |
|---|
| 2762 | GB_test_transaction(gbd); |
|---|
| 2763 | if (GB_ARRAY_FLAGS(gbd).flags & GB_MAIN(gbd)->users[0]->userbit) return 1; |
|---|
| 2764 | else return 0; |
|---|
| 2765 | } |
|---|
| 2766 | |
|---|
| 2767 | void GB_touch(GBDATA *gbd) { |
|---|
| 2768 | GB_test_transaction(gbd); |
|---|
| 2769 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
|---|
| 2770 | GB_DO_CALLBACKS(gbd); |
|---|
| 2771 | } |
|---|
| 2772 | |
|---|
| 2773 | |
|---|
| 2774 | char GB_type_2_char(GB_TYPES type) { |
|---|
| 2775 | const char *type2char = "-bcif-B-CIFlSS-%"; |
|---|
| 2776 | return type2char[type]; |
|---|
| 2777 | } |
|---|
| 2778 | |
|---|
| 2779 | GB_ERROR GB_print_debug_information(void */*dummy_AW_root*/, GBDATA *gb_main) { |
|---|
| 2780 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
|---|
| 2781 | GB_push_transaction(gb_main); |
|---|
| 2782 | for (int i=0; i<Main->keycnt; i++) { |
|---|
| 2783 | if (Main->keys[i].key) { |
|---|
| 2784 | printf("%3i %20s nref %i\n", i, Main->keys[i].key, (int)Main->keys[i].nref); |
|---|
| 2785 | } |
|---|
| 2786 | else { |
|---|
| 2787 | printf(" %3i unused key, next free key = %li\n", i, Main->keys[i].next_free_key); |
|---|
| 2788 | } |
|---|
| 2789 | } |
|---|
| 2790 | gbm_debug_mem(); |
|---|
| 2791 | GB_pop_transaction(gb_main); |
|---|
| 2792 | return 0; |
|---|
| 2793 | } |
|---|
| 2794 | |
|---|
| 2795 | static int GB_info_deep = 15; |
|---|
| 2796 | |
|---|
| 2797 | |
|---|
| 2798 | static int gb_info(GBDATA *gbd, int deep) { |
|---|
| 2799 | if (gbd==NULL) { printf("NULL\n"); return -1; } |
|---|
| 2800 | GB_push_transaction(gbd); |
|---|
| 2801 | |
|---|
| 2802 | GB_TYPES type = gbd->type(); |
|---|
| 2803 | |
|---|
| 2804 | if (deep) { |
|---|
| 2805 | printf(" "); |
|---|
| 2806 | } |
|---|
| 2807 | |
|---|
| 2808 | printf("(GBDATA*)0x%lx (GBCONTAINER*)0x%lx ", (long)gbd, (long)gbd); |
|---|
| 2809 | |
|---|
| 2810 | if (gbd->rel_father==0) { printf("father=NULL\n"); return -1; } |
|---|
| 2811 | |
|---|
| 2812 | GBCONTAINER *gbc; |
|---|
| 2813 | GB_MAIN_TYPE *Main; |
|---|
| 2814 | if (type==GB_DB) { gbc = gbd->as_container(); Main = GBCONTAINER_MAIN(gbc); } |
|---|
| 2815 | else { gbc = NULL; Main = GB_MAIN(gbd); } |
|---|
| 2816 | |
|---|
| 2817 | if (!Main) { printf("Oops - I have no main entry!!!\n"); return -1; } |
|---|
| 2818 | if (gbd==Main->dummy_father) { printf("dummy_father!\n"); return -1; } |
|---|
| 2819 | |
|---|
| 2820 | printf("%10s Type '%c' ", GB_read_key_pntr(gbd), GB_type_2_char(type)); |
|---|
| 2821 | |
|---|
| 2822 | switch (type) { |
|---|
| 2823 | case GB_DB: { |
|---|
| 2824 | int size = gbc->d.size; |
|---|
| 2825 | printf("Size %i nheader %i hmemsize %i", gbc->d.size, gbc->d.nheader, gbc->d.headermemsize); |
|---|
| 2826 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
|---|
| 2827 | if (size < GB_info_deep) { |
|---|
| 2828 | int index; |
|---|
| 2829 | gb_header_list *header; |
|---|
| 2830 | |
|---|
| 2831 | header = GB_DATA_LIST_HEADER(gbc->d); |
|---|
| 2832 | for (index = 0; index < gbc->d.nheader; index++) { |
|---|
| 2833 | GBDATA *gb_sub = GB_HEADER_LIST_GBD(header[index]); |
|---|
| 2834 | printf("\t\t%10s (GBDATA*)0x%lx (GBCONTAINER*)0x%lx\n", Main->keys[header[index].flags.key_quark].key, (long)gb_sub, (long)gb_sub); |
|---|
| 2835 | } |
|---|
| 2836 | } |
|---|
| 2837 | break; |
|---|
| 2838 | } |
|---|
| 2839 | default: { |
|---|
| 2840 | char *data = GB_read_as_string(gbd); |
|---|
| 2841 | if (data) { printf("%s", data); free(data); } |
|---|
| 2842 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
|---|
| 2843 | } |
|---|
| 2844 | } |
|---|
| 2845 | |
|---|
| 2846 | |
|---|
| 2847 | GB_pop_transaction(gbd); |
|---|
| 2848 | |
|---|
| 2849 | return 0; |
|---|
| 2850 | } |
|---|
| 2851 | |
|---|
| 2852 | int GB_info(GBDATA *gbd) { // unused - intended to be used in debugger |
|---|
| 2853 | return gb_info(gbd, 0); |
|---|
| 2854 | } |
|---|
| 2855 | |
|---|
| 2856 | long GB_number_of_subentries(GBDATA *gbd) { |
|---|
| 2857 | GBCONTAINER *gbc = gbd->expect_container(); |
|---|
| 2858 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
|---|
| 2859 | |
|---|
| 2860 | long subentries = 0; |
|---|
| 2861 | int end = gbc->d.nheader; |
|---|
| 2862 | |
|---|
| 2863 | for (int index = 0; index<end; index++) { |
|---|
| 2864 | if (header[index].flags.changed < GB_DELETED) subentries++; |
|---|
| 2865 | } |
|---|
| 2866 | return subentries; |
|---|
| 2867 | } |
|---|
| 2868 | |
|---|
| 2869 | // -------------------------------------------------------------------------------- |
|---|
| 2870 | |
|---|
| 2871 | #ifdef UNIT_TESTS |
|---|
| 2872 | |
|---|
| 2873 | #include <test_unit.h> |
|---|
| 2874 | #include <locale.h> |
|---|
| 2875 | |
|---|
| 2876 | void TEST_GB_atof() { |
|---|
| 2877 | // startup of ARB (gtk_only@11651) is failing on ubuntu 13.10 (in GBT_read_tree) |
|---|
| 2878 | // (failed with "Error: 'GB_safe_atof("0.0810811", ..) returns error: cannot convert '0.0810811' to double'") |
|---|
| 2879 | // Reason: LANG[UAGE] or LC_NUMERIC set to "de_DE..." |
|---|
| 2880 | // |
|---|
| 2881 | // Notes: |
|---|
| 2882 | // * gtk apparently calls 'setlocale(LC_ALL, "");', motif doesnt |
|---|
| 2883 | |
|---|
| 2884 | TEST_EXPECT_SIMILAR(GB_atof("0.031"), 0.031, 0.0001); // @@@ make this fail, then fix it |
|---|
| 2885 | } |
|---|
| 2886 | |
|---|
| 2887 | void TEST_999_strtod_replacement() { |
|---|
| 2888 | // caution: if it fails -> locale is not reset (therefore call with low priority 999) |
|---|
| 2889 | const char *old = setlocale(LC_NUMERIC, "de_DE.UTF-8"); |
|---|
| 2890 | { |
|---|
| 2891 | // TEST_EXPECT_SIMILAR__BROKEN(strtod("0.031", NULL), 0.031, 0.0001); |
|---|
| 2892 | TEST_EXPECT_SIMILAR(g_ascii_strtod("0.031", NULL), 0.031, 0.0001); |
|---|
| 2893 | } |
|---|
| 2894 | setlocale(LC_NUMERIC, old); |
|---|
| 2895 | } |
|---|
| 2896 | |
|---|
| 2897 | static void test_another_shell() { delete new GB_shell; } |
|---|
| 2898 | static void test_opendb() { GB_close(GB_open("no.arb", "c")); } |
|---|
| 2899 | |
|---|
| 2900 | void TEST_GB_shell() { |
|---|
| 2901 | { |
|---|
| 2902 | GB_shell *shell = new GB_shell; |
|---|
| 2903 | TEST_EXPECT_SEGFAULT(test_another_shell); |
|---|
| 2904 | test_opendb(); // no SEGV here |
|---|
| 2905 | delete shell; |
|---|
| 2906 | } |
|---|
| 2907 | |
|---|
| 2908 | TEST_EXPECT_SEGFAULT(test_opendb); // should be impossible to open db w/o shell |
|---|
| 2909 | } |
|---|
| 2910 | |
|---|
| 2911 | void TEST_GB_number_of_subentries() { |
|---|
| 2912 | GB_shell shell; |
|---|
| 2913 | GBDATA *gb_main = GB_open("no.arb", "c"); |
|---|
| 2914 | |
|---|
| 2915 | { |
|---|
| 2916 | GB_transaction ta(gb_main); |
|---|
| 2917 | |
|---|
| 2918 | GBDATA *gb_cont = GB_create_container(gb_main, "container"); |
|---|
| 2919 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 0); |
|---|
| 2920 | |
|---|
| 2921 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
|---|
| 2922 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
|---|
| 2923 | |
|---|
| 2924 | { |
|---|
| 2925 | GBDATA *gb_entry; |
|---|
| 2926 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_entry = GB_create(gb_cont, "entry", GB_STRING)); |
|---|
| 2927 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
|---|
| 2928 | |
|---|
| 2929 | TEST_EXPECT_NO_ERROR(GB_delete(gb_entry)); |
|---|
| 2930 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
|---|
| 2931 | } |
|---|
| 2932 | |
|---|
| 2933 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
|---|
| 2934 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
|---|
| 2935 | } |
|---|
| 2936 | |
|---|
| 2937 | // TEST_REJECT(true); // @@@ fail while GB_shell open |
|---|
| 2938 | |
|---|
| 2939 | GB_close(gb_main); |
|---|
| 2940 | } |
|---|
| 2941 | |
|---|
| 2942 | static void test_count_cb(GBDATA *, int *counter) { |
|---|
| 2943 | fprintf(stderr, "test_count_cb: var.add=%p old.val=%i ", counter, *counter); |
|---|
| 2944 | (*counter)++; |
|---|
| 2945 | fprintf(stderr, "new.val=%i\n", *counter); |
|---|
| 2946 | fflush(stderr); |
|---|
| 2947 | } |
|---|
| 2948 | |
|---|
| 2949 | static void remove_self_cb(GBDATA *gbe, GB_CB_TYPE cbtype) { |
|---|
| 2950 | GB_remove_callback(gbe, cbtype, makeDatabaseCallback(remove_self_cb)); |
|---|
| 2951 | } |
|---|
| 2952 | |
|---|
| 2953 | static void re_add_self_cb(GBDATA *gbe, int *calledCounter, GB_CB_TYPE cbtype) { |
|---|
| 2954 | ++(*calledCounter); |
|---|
| 2955 | |
|---|
| 2956 | DatabaseCallback dbcb = makeDatabaseCallback(re_add_self_cb, calledCounter); |
|---|
| 2957 | GB_remove_callback(gbe, cbtype, dbcb); |
|---|
| 2958 | |
|---|
| 2959 | GB_ERROR error = GB_add_callback(gbe, cbtype, dbcb); |
|---|
| 2960 | gb_assert(!error); |
|---|
| 2961 | } |
|---|
| 2962 | |
|---|
| 2963 | void TEST_db_callbacks_ta_nota() { |
|---|
| 2964 | GB_shell shell; |
|---|
| 2965 | |
|---|
| 2966 | enum TAmode { |
|---|
| 2967 | NO_TA = 1, // no transaction mode |
|---|
| 2968 | WITH_TA = 2, // transaction mode |
|---|
| 2969 | |
|---|
| 2970 | BOTH_TA_MODES = (NO_TA|WITH_TA) |
|---|
| 2971 | }; |
|---|
| 2972 | |
|---|
| 2973 | for (TAmode ta_mode = NO_TA; ta_mode <= WITH_TA; ta_mode = TAmode(ta_mode+1)) { |
|---|
| 2974 | GBDATA *gb_main = GB_open("no.arb", "c"); |
|---|
| 2975 | GB_ERROR error; |
|---|
| 2976 | |
|---|
| 2977 | TEST_ANNOTATE(ta_mode == NO_TA ? "NO_TA" : "WITH_TA"); |
|---|
| 2978 | if (ta_mode == NO_TA) { |
|---|
| 2979 | error = GB_no_transaction(gb_main); TEST_EXPECT_NO_ERROR(error); |
|---|
| 2980 | } |
|---|
| 2981 | |
|---|
| 2982 | // create some DB entries |
|---|
| 2983 | GBDATA *gbc; |
|---|
| 2984 | GBDATA *gbe1; |
|---|
| 2985 | GBDATA *gbe2; |
|---|
| 2986 | GBDATA *gbe3; |
|---|
| 2987 | { |
|---|
| 2988 | GB_transaction ta(gb_main); |
|---|
| 2989 | |
|---|
| 2990 | gbc = GB_create_container(gb_main, "cont"); |
|---|
| 2991 | gbe1 = GB_create(gbc, "entry", GB_STRING); |
|---|
| 2992 | gbe2 = GB_create(gb_main, "entry", GB_INT); |
|---|
| 2993 | } |
|---|
| 2994 | |
|---|
| 2995 | // counters to detect called callbacks |
|---|
| 2996 | int e1_changed = 0; |
|---|
| 2997 | int e2_changed = 0; |
|---|
| 2998 | int c_changed = 0; |
|---|
| 2999 | int c_son_created = 0; |
|---|
| 3000 | |
|---|
| 3001 | int e1_deleted = 0; |
|---|
| 3002 | int e2_deleted = 0; |
|---|
| 3003 | int e3_deleted = 0; |
|---|
| 3004 | int c_deleted = 0; |
|---|
| 3005 | |
|---|
| 3006 | #define CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc) \ |
|---|
| 3007 | that(e1_changed).is_equal_to(e1c), \ |
|---|
| 3008 | that(e2_changed).is_equal_to(e2c), \ |
|---|
| 3009 | that(c_changed).is_equal_to(cc), \ |
|---|
| 3010 | that(c_son_created).is_equal_to(csc) |
|---|
| 3011 | |
|---|
| 3012 | #define DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd) \ |
|---|
| 3013 | that(e1_deleted).is_equal_to(e1d), \ |
|---|
| 3014 | that(e2_deleted).is_equal_to(e2d), \ |
|---|
| 3015 | that(e3_deleted).is_equal_to(e3d), \ |
|---|
| 3016 | that(c_deleted).is_equal_to(cd) |
|---|
| 3017 | |
|---|
| 3018 | #define TEST_EXPECT_CHCB_COUNTERS(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
|---|
| 3019 | #define TEST_EXPECT_CHCB___WANTED(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
|---|
| 3020 | |
|---|
| 3021 | #define TEST_EXPECT_DLCB_COUNTERS(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
|---|
| 3022 | #define TEST_EXPECT_DLCB___WANTED(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
|---|
| 3023 | |
|---|
| 3024 | #define TEST_EXPECT_COUNTER(tam,cnt,expected) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL(cnt, expected); }while(0) |
|---|
| 3025 | #define TEST_EXPECT_COUNTER__BROKEN(tam,cnt,expected,got) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL__BROKEN(cnt, expected, got); }while(0) |
|---|
| 3026 | |
|---|
| 3027 | #define RESET_CHCB_COUNTERS() do{ e1_changed = e2_changed = c_changed = c_son_created = 0; }while(0) |
|---|
| 3028 | #define RESET_DLCB_COUNTERS() do{ e1_deleted = e2_deleted = e3_deleted = c_deleted = 0; }while(0) |
|---|
| 3029 | #define RESET_ALL_CB_COUNTERS() do{ RESET_CHCB_COUNTERS(); RESET_DLCB_COUNTERS(); }while(0) |
|---|
| 3030 | |
|---|
| 3031 | // install some DB callbacks |
|---|
| 3032 | { |
|---|
| 3033 | GB_transaction ta(gb_main); |
|---|
| 3034 | GB_add_callback(gbe1, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e1_changed)); |
|---|
| 3035 | GB_add_callback(gbe2, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e2_changed)); |
|---|
| 3036 | GB_add_callback(gbc, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &c_changed)); |
|---|
| 3037 | GB_add_callback(gbc, GB_CB_SON_CREATED, makeDatabaseCallback(test_count_cb, &c_son_created)); |
|---|
| 3038 | } |
|---|
| 3039 | |
|---|
| 3040 | // check callbacks were not called yet |
|---|
| 3041 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); |
|---|
| 3042 | |
|---|
| 3043 | // trigger callbacks |
|---|
| 3044 | { |
|---|
| 3045 | GB_transaction ta(gb_main); |
|---|
| 3046 | |
|---|
| 3047 | error = GB_write_string(gbe1, "hi"); TEST_EXPECT_NO_ERROR(error); |
|---|
| 3048 | error = GB_write_int(gbe2, 666); TEST_EXPECT_NO_ERROR(error); |
|---|
| 3049 | |
|---|
| 3050 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); // callbacks triggered instantly in NO_TA mode |
|---|
| 3051 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, WITH_TA); // callbacks delayed until transaction is committed |
|---|
| 3052 | |
|---|
| 3053 | } // [Note: callbacks happen here in ta_mode] |
|---|
| 3054 | |
|---|
| 3055 | // test that GB_CB_SON_CREATED is not triggered here: |
|---|
| 3056 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); |
|---|
| 3057 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, WITH_TA); |
|---|
| 3058 | |
|---|
| 3059 | // really create a son |
|---|
| 3060 | RESET_CHCB_COUNTERS(); |
|---|
| 3061 | { |
|---|
| 3062 | GB_transaction ta(gb_main); |
|---|
| 3063 | gbe3 = GB_create(gbc, "e3", GB_STRING); |
|---|
| 3064 | } |
|---|
| 3065 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // broken |
|---|
| 3066 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
|---|
| 3067 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 1, WITH_TA); |
|---|
| 3068 | |
|---|
| 3069 | // change that son |
|---|
| 3070 | RESET_CHCB_COUNTERS(); |
|---|
| 3071 | { |
|---|
| 3072 | GB_transaction ta(gb_main); |
|---|
| 3073 | error = GB_write_string(gbe3, "bla"); TEST_EXPECT_NO_ERROR(error); |
|---|
| 3074 | } |
|---|
| 3075 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, BOTH_TA_MODES); |
|---|
| 3076 | |
|---|
| 3077 | |
|---|
| 3078 | // test delete callbacks |
|---|
| 3079 | RESET_CHCB_COUNTERS(); |
|---|
| 3080 | { |
|---|
| 3081 | GB_transaction ta(gb_main); |
|---|
| 3082 | |
|---|
| 3083 | GB_add_callback(gbe1, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e1_deleted)); |
|---|
| 3084 | GB_add_callback(gbe2, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e2_deleted)); |
|---|
| 3085 | GB_add_callback(gbe3, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e3_deleted)); |
|---|
| 3086 | GB_add_callback(gbc, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &c_deleted)); |
|---|
| 3087 | } |
|---|
| 3088 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // adding callbacks does not trigger existing change-callbacks |
|---|
| 3089 | { |
|---|
| 3090 | GB_transaction ta(gb_main); |
|---|
| 3091 | |
|---|
| 3092 | error = GB_delete(gbe3); TEST_EXPECT_NO_ERROR(error); |
|---|
| 3093 | error = GB_delete(gbe2); TEST_EXPECT_NO_ERROR(error); |
|---|
| 3094 | |
|---|
| 3095 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, NO_TA); |
|---|
| 3096 | TEST_EXPECT_DLCB_COUNTERS(0, 0, 0, 0, WITH_TA); |
|---|
| 3097 | } |
|---|
| 3098 | |
|---|
| 3099 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, WITH_TA); // container changed by deleting a son (gbe3); no longer triggers via GB_SON_CHANGED |
|---|
| 3100 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // change is not triggered in NO_TA mode (error?) |
|---|
| 3101 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
|---|
| 3102 | |
|---|
| 3103 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, BOTH_TA_MODES); |
|---|
| 3104 | |
|---|
| 3105 | RESET_ALL_CB_COUNTERS(); |
|---|
| 3106 | { |
|---|
| 3107 | GB_transaction ta(gb_main); |
|---|
| 3108 | error = GB_delete(gbc); TEST_EXPECT_NO_ERROR(error); // delete the container containing gbe1 and gbe3 (gbe3 alreay deleted) |
|---|
| 3109 | } |
|---|
| 3110 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // deleting the container does not trigger any change callbacks |
|---|
| 3111 | TEST_EXPECT_DLCB_COUNTERS(1, 0, 0, 1, BOTH_TA_MODES); // deleting the container does also trigger the delete callback for gbe1 |
|---|
| 3112 | |
|---|
| 3113 | // -------------------------------------------------------------------------------- |
|---|
| 3114 | // document that a callback now can be removed while it is running |
|---|
| 3115 | // (in NO_TA mode; always worked in WITH_TA mode) |
|---|
| 3116 | { |
|---|
| 3117 | GBDATA *gbe; |
|---|
| 3118 | { |
|---|
| 3119 | GB_transaction ta(gb_main); |
|---|
| 3120 | gbe = GB_create(gb_main, "new_e1", GB_INT); // recreate |
|---|
| 3121 | GB_add_callback(gbe, GB_CB_CHANGED, makeDatabaseCallback(remove_self_cb)); |
|---|
| 3122 | } |
|---|
| 3123 | { GB_transaction ta(gb_main); GB_touch(gbe); } |
|---|
| 3124 | } |
|---|
| 3125 | |
|---|
| 3126 | // test that a callback may remove and re-add itself |
|---|
| 3127 | { |
|---|
| 3128 | GBDATA *gbn1; |
|---|
| 3129 | GBDATA *gbn2; |
|---|
| 3130 | |
|---|
| 3131 | int counter1 = 0; |
|---|
| 3132 | int counter2 = 0; |
|---|
| 3133 | |
|---|
| 3134 | { |
|---|
| 3135 | GB_transaction ta(gb_main); |
|---|
| 3136 | gbn1 = GB_create(gb_main, "new_e1", GB_INT); |
|---|
| 3137 | gbn2 = GB_create(gb_main, "new_e2", GB_INT); |
|---|
| 3138 | GB_add_callback(gbn1, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter1)); |
|---|
| 3139 | } |
|---|
| 3140 | |
|---|
| 3141 | TEST_EXPECT_COUNTER(NO_TA, counter1, 0); // no callback triggered (trigger happens BEFORE call to GB_add_callback in NO_TA mode!) |
|---|
| 3142 | TEST_EXPECT_COUNTER(WITH_TA, counter1, 1); // callback gets triggered by GB_create |
|---|
| 3143 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
|---|
| 3144 | |
|---|
| 3145 | counter1 = 0; counter2 = 0; |
|---|
| 3146 | |
|---|
| 3147 | // test no callback is triggered by just adding a callback |
|---|
| 3148 | { |
|---|
| 3149 | GB_transaction ta(gb_main); |
|---|
| 3150 | GB_add_callback(gbn2, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter2)); |
|---|
| 3151 | } |
|---|
| 3152 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 0); |
|---|
| 3153 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
|---|
| 3154 | |
|---|
| 3155 | { GB_transaction ta(gb_main); GB_touch(gbn1); } |
|---|
| 3156 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
|---|
| 3157 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
|---|
| 3158 | |
|---|
| 3159 | { GB_transaction ta(gb_main); GB_touch(gbn2); } |
|---|
| 3160 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
|---|
| 3161 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
|---|
| 3162 | |
|---|
| 3163 | { GB_transaction ta(gb_main); } |
|---|
| 3164 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
|---|
| 3165 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
|---|
| 3166 | } |
|---|
| 3167 | |
|---|
| 3168 | GB_close(gb_main); |
|---|
| 3169 | } |
|---|
| 3170 | } |
|---|
| 3171 | |
|---|
| 3172 | // ----------------------- |
|---|
| 3173 | // test callbacks |
|---|
| 3174 | |
|---|
| 3175 | struct calledWith { |
|---|
| 3176 | GBDATA *gbd; |
|---|
| 3177 | GB_CB_TYPE type; |
|---|
| 3178 | int time_called; |
|---|
| 3179 | |
|---|
| 3180 | static int timer; |
|---|
| 3181 | |
|---|
| 3182 | calledWith(GBDATA *gbd_, GB_CB_TYPE type_) : gbd(gbd_), type(type_), time_called(++timer) {} |
|---|
| 3183 | calledWith(const calledWith& other) : gbd(other.gbd), type(other.type), time_called(other.time_called) {} |
|---|
| 3184 | DECLARE_ASSIGNMENT_OPERATOR(calledWith); |
|---|
| 3185 | }; |
|---|
| 3186 | |
|---|
| 3187 | int calledWith::timer = 0; |
|---|
| 3188 | |
|---|
| 3189 | class callback_trace { |
|---|
| 3190 | typedef std::list<calledWith> calledList; |
|---|
| 3191 | typedef calledList::iterator calledIter; |
|---|
| 3192 | |
|---|
| 3193 | calledList called; |
|---|
| 3194 | |
|---|
| 3195 | calledIter find(GBDATA *gbd) { |
|---|
| 3196 | calledIter c = called.begin(); |
|---|
| 3197 | while (c != called.end()) { |
|---|
| 3198 | if (c->gbd == gbd) break; |
|---|
| 3199 | ++c; |
|---|
| 3200 | } |
|---|
| 3201 | return c; |
|---|
| 3202 | } |
|---|
| 3203 | calledIter find(GB_CB_TYPE exp_type) { |
|---|
| 3204 | calledIter c = called.begin(); |
|---|
| 3205 | while (c != called.end()) { |
|---|
| 3206 | if (c->type&exp_type) break; |
|---|
| 3207 | ++c; |
|---|
| 3208 | } |
|---|
| 3209 | return c; |
|---|
| 3210 | } |
|---|
| 3211 | calledIter find(GBDATA *gbd, GB_CB_TYPE exp_type) { |
|---|
| 3212 | calledIter c = called.begin(); |
|---|
| 3213 | while (c != called.end()) { |
|---|
| 3214 | if (c->gbd == gbd && (c->type&exp_type)) break; |
|---|
| 3215 | ++c; |
|---|
| 3216 | } |
|---|
| 3217 | return c; |
|---|
| 3218 | } |
|---|
| 3219 | |
|---|
| 3220 | bool removed(calledIter c) { |
|---|
| 3221 | if (c == called.end()) return false; |
|---|
| 3222 | called.erase(c); |
|---|
| 3223 | return true; |
|---|
| 3224 | } |
|---|
| 3225 | |
|---|
| 3226 | public: |
|---|
| 3227 | callback_trace() { called.clear(); } |
|---|
| 3228 | |
|---|
| 3229 | void set_called_by(GBDATA *gbd, GB_CB_TYPE type) { called.push_back(calledWith(gbd, type)); } |
|---|
| 3230 | |
|---|
| 3231 | bool was_called_by(GBDATA *gbd) { return removed(find(gbd)); } |
|---|
| 3232 | bool was_called_by(GB_CB_TYPE exp_type) { return removed(find(exp_type)); } |
|---|
| 3233 | bool was_called_by(GBDATA *gbd, GB_CB_TYPE exp_type) { return removed(find(gbd, exp_type)); } |
|---|
| 3234 | |
|---|
| 3235 | int call_time(GBDATA *gbd, GB_CB_TYPE exp_type) { |
|---|
| 3236 | calledIter found = find(gbd, exp_type); |
|---|
| 3237 | if (found == called.end()) return -1; |
|---|
| 3238 | |
|---|
| 3239 | int t = found->time_called; |
|---|
| 3240 | removed(found); |
|---|
| 3241 | return t; |
|---|
| 3242 | } |
|---|
| 3243 | |
|---|
| 3244 | bool was_not_called() const { return called.empty(); } |
|---|
| 3245 | bool was_called() const { return !was_not_called(); } |
|---|
| 3246 | }; |
|---|
| 3247 | |
|---|
| 3248 | static void some_cb(GBDATA *gbd, callback_trace *trace, GB_CB_TYPE cbtype) { |
|---|
| 3249 | trace->set_called_by(gbd, cbtype); |
|---|
| 3250 | } |
|---|
| 3251 | |
|---|
| 3252 | #define TRACESTRUCT(ELEM,FLAVOR) trace_##ELEM##_##FLAVOR |
|---|
| 3253 | #define HIERARCHY_TRACESTRUCT(ELEM,FLAVOR) traceHier_##ELEM##_##FLAVOR |
|---|
| 3254 | |
|---|
| 3255 | #define ADD_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
|---|
| 3256 | #define ADD_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
|---|
| 3257 | #define ADD_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
|---|
| 3258 | |
|---|
| 3259 | #define ADD_CHANGED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,changed)))) |
|---|
| 3260 | #define ADD_DELETED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,deleted)))) |
|---|
| 3261 | #define ADD_NWCHILD_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,newchild)))) |
|---|
| 3262 | |
|---|
| 3263 | #define ENSURE_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
|---|
| 3264 | #define ENSURE_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
|---|
| 3265 | #define ENSURE_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
|---|
| 3266 | |
|---|
| 3267 | #define REMOVE_CHANGED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed))) |
|---|
| 3268 | #define REMOVE_DELETED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted))) |
|---|
| 3269 | #define REMOVE_NWCHILD_CALLBACK(elem) GB_remove_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild))) |
|---|
| 3270 | |
|---|
| 3271 | #define INIT_CHANGED_CALLBACK(elem) callback_trace TRACESTRUCT(elem,changed); ADD_CHANGED_CALLBACK(elem) |
|---|
| 3272 | #define INIT_DELETED_CALLBACK(elem) callback_trace TRACESTRUCT(elem,deleted); ADD_DELETED_CALLBACK(elem) |
|---|
| 3273 | #define INIT_NWCHILD_CALLBACK(elem) callback_trace TRACESTRUCT(elem,newchild); ADD_NWCHILD_CALLBACK(elem) |
|---|
| 3274 | |
|---|
| 3275 | #define INIT_CHANGED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,changed); ADD_CHANGED_HIERARCHY_CALLBACK(elem) |
|---|
| 3276 | #define INIT_DELETED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,deleted); ADD_DELETED_HIERARCHY_CALLBACK(elem) |
|---|
| 3277 | #define INIT_NWCHILD_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,newchild); ADD_NWCHILD_HIERARCHY_CALLBACK(elem) |
|---|
| 3278 | |
|---|
| 3279 | #define ENSURE_ENTRY_CALLBACKS(entry) ENSURE_CHANGED_CALLBACK(entry); ENSURE_DELETED_CALLBACK(entry) |
|---|
| 3280 | #define ENSURE_CONTAINER_CALLBACKS(cont) ENSURE_CHANGED_CALLBACK(cont); ENSURE_NWCHILD_CALLBACK(cont); ENSURE_DELETED_CALLBACK(cont) |
|---|
| 3281 | |
|---|
| 3282 | #define REMOVE_ENTRY_CALLBACKS(entry) REMOVE_CHANGED_CALLBACK(entry); REMOVE_DELETED_CALLBACK(entry) |
|---|
| 3283 | #define REMOVE_CONTAINER_CALLBACKS(cont) REMOVE_CHANGED_CALLBACK(cont); REMOVE_NWCHILD_CALLBACK(cont); REMOVE_DELETED_CALLBACK(cont) |
|---|
| 3284 | |
|---|
| 3285 | #define INIT_ENTRY_CALLBACKS(entry) INIT_CHANGED_CALLBACK(entry); INIT_DELETED_CALLBACK(entry) |
|---|
| 3286 | #define INIT_CONTAINER_CALLBACKS(cont) INIT_CHANGED_CALLBACK(cont); INIT_NWCHILD_CALLBACK(cont); INIT_DELETED_CALLBACK(cont) |
|---|
| 3287 | |
|---|
| 3288 | #define TRIGGER_CHANGE(gbd) do { \ |
|---|
| 3289 | GB_initial_transaction ta(gb_main); \ |
|---|
| 3290 | if (ta.ok()) GB_touch(gbd); \ |
|---|
| 3291 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); \ |
|---|
| 3292 | } while(0) |
|---|
| 3293 | |
|---|
| 3294 | #define TRIGGER_2_CHANGES(gbd1, gbd2) do { \ |
|---|
| 3295 | GB_initial_transaction ta(gb_main); \ |
|---|
| 3296 | if (ta.ok()) { \ |
|---|
| 3297 | GB_touch(gbd1); \ |
|---|
| 3298 | GB_touch(gbd2); \ |
|---|
| 3299 | } \ |
|---|
| 3300 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); \ |
|---|
| 3301 | } while(0) |
|---|
| 3302 | |
|---|
| 3303 | #define TRIGGER_DELETE(gbd) do { \ |
|---|
| 3304 | GB_initial_transaction ta(gb_main); \ |
|---|
| 3305 | GB_ERROR error = NULL; \ |
|---|
| 3306 | if (ta.ok()) error = GB_delete(gbd); \ |
|---|
| 3307 | TEST_EXPECT_NO_ERROR(ta.close(error)); \ |
|---|
| 3308 | } while(0) |
|---|
| 3309 | |
|---|
| 3310 | #define TEST_EXPECT_NO_CALLBACK_TRIGGERED() \ |
|---|
| 3311 | TEST_EXPECT(trace_top_deleted.was_not_called()); \ |
|---|
| 3312 | TEST_EXPECT(trace_son_deleted.was_not_called()); \ |
|---|
| 3313 | TEST_EXPECT(trace_grandson_deleted.was_not_called()); \ |
|---|
| 3314 | TEST_EXPECT(trace_ograndson_deleted.was_not_called()); \ |
|---|
| 3315 | TEST_EXPECT(trace_cont_top_deleted.was_not_called()); \ |
|---|
| 3316 | TEST_EXPECT(trace_cont_son_deleted.was_not_called()); \ |
|---|
| 3317 | TEST_EXPECT(trace_top_changed.was_not_called()); \ |
|---|
| 3318 | TEST_EXPECT(trace_son_changed.was_not_called()); \ |
|---|
| 3319 | TEST_EXPECT(trace_grandson_changed.was_not_called()); \ |
|---|
| 3320 | TEST_EXPECT(trace_ograndson_changed.was_not_called()); \ |
|---|
| 3321 | TEST_EXPECT(trace_cont_top_changed.was_not_called()); \ |
|---|
| 3322 | TEST_EXPECT(trace_cont_son_changed.was_not_called()); \ |
|---|
| 3323 | TEST_EXPECT(trace_cont_top_newchild.was_not_called()); \ |
|---|
| 3324 | TEST_EXPECT(trace_cont_son_newchild.was_not_called()); \ |
|---|
| 3325 | |
|---|
| 3326 | |
|---|
| 3327 | #define TEST_EXPECT_CB_TRIGGERED(TRACE,GBD,TYPE) TEST_EXPECT(TRACE.was_called_by(GBD, TYPE)) |
|---|
| 3328 | #define TEST_EXPECT_CB_TRIGGERED_AT(TRACE,GBD,TYPE,TIME) TEST_EXPECT_EQUAL(TRACE.call_time(GBD, TYPE), TIME) |
|---|
| 3329 | |
|---|
| 3330 | #define TEST_EXPECT_CHANGE_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_CHANGED) |
|---|
| 3331 | #define TEST_EXPECT_DELETE_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_DELETE) |
|---|
| 3332 | #define TEST_EXPECT_NCHILD_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_SON_CREATED) |
|---|
| 3333 | |
|---|
| 3334 | #define TEST_EXPECT_CHANGE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_CHANGED, TIME) |
|---|
| 3335 | #define TEST_EXPECT_DELETE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_DELETE, TIME) |
|---|
| 3336 | |
|---|
| 3337 | void TEST_db_callbacks() { |
|---|
| 3338 | GB_shell shell; |
|---|
| 3339 | GBDATA *gb_main = GB_open("new.arb", "c"); |
|---|
| 3340 | |
|---|
| 3341 | // create some data |
|---|
| 3342 | GB_begin_transaction(gb_main); |
|---|
| 3343 | |
|---|
| 3344 | GBDATA *cont_top = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top); |
|---|
| 3345 | GBDATA *cont_son = GB_create_container(cont_top, "cont_son"); TEST_REJECT_NULL(cont_son); |
|---|
| 3346 | |
|---|
| 3347 | GBDATA *top = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top); |
|---|
| 3348 | GBDATA *son = GB_create(cont_top, "son", GB_INT); TEST_REJECT_NULL(son); |
|---|
| 3349 | GBDATA *grandson = GB_create(cont_son, "grandson", GB_STRING); TEST_REJECT_NULL(grandson); |
|---|
| 3350 | GBDATA *ograndson = GB_create(cont_son, "ograndson", GB_STRING); TEST_REJECT_NULL(ograndson); |
|---|
| 3351 | |
|---|
| 3352 | GB_commit_transaction(gb_main); |
|---|
| 3353 | |
|---|
| 3354 | // install callbacks |
|---|
| 3355 | GB_begin_transaction(gb_main); |
|---|
| 3356 | INIT_CONTAINER_CALLBACKS(cont_top); |
|---|
| 3357 | INIT_CONTAINER_CALLBACKS(cont_son); |
|---|
| 3358 | INIT_ENTRY_CALLBACKS(top); |
|---|
| 3359 | INIT_ENTRY_CALLBACKS(son); |
|---|
| 3360 | INIT_ENTRY_CALLBACKS(grandson); |
|---|
| 3361 | INIT_ENTRY_CALLBACKS(ograndson); |
|---|
| 3362 | GB_commit_transaction(gb_main); |
|---|
| 3363 | |
|---|
| 3364 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3365 | |
|---|
| 3366 | // trigger change callbacks via change |
|---|
| 3367 | GB_begin_transaction(gb_main); |
|---|
| 3368 | GB_write_string(top, "hello world"); |
|---|
| 3369 | GB_commit_transaction(gb_main); |
|---|
| 3370 | TEST_EXPECT_CHANGE_TRIGGERED(trace_top_changed, top); |
|---|
| 3371 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3372 | |
|---|
| 3373 | GB_begin_transaction(gb_main); |
|---|
| 3374 | GB_write_string(top, "hello world"); // no change |
|---|
| 3375 | GB_commit_transaction(gb_main); |
|---|
| 3376 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3377 | |
|---|
| 3378 | #if 0 |
|---|
| 3379 | // code is wrong (cannot set terminal entry to "marked") |
|---|
| 3380 | GB_begin_transaction(gb_main); |
|---|
| 3381 | GB_write_flag(son, 1); // only change "mark" |
|---|
| 3382 | GB_commit_transaction(gb_main); |
|---|
| 3383 | TEST_EXPECT_CHANGE_TRIGGERED(trace_son_changed, son); |
|---|
| 3384 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3385 | TEST_EXPECT_TRIGGER__UNWANTED(trace_cont_top_newchild); // @@@ modifying son should not trigger newchild callback |
|---|
| 3386 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3387 | #else |
|---|
| 3388 | // @@@ add test code similar to wrong section above |
|---|
| 3389 | #endif |
|---|
| 3390 | |
|---|
| 3391 | GB_begin_transaction(gb_main); |
|---|
| 3392 | GB_touch(grandson); |
|---|
| 3393 | GB_commit_transaction(gb_main); |
|---|
| 3394 | TEST_EXPECT_CHANGE_TRIGGERED(trace_grandson_changed, grandson); |
|---|
| 3395 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
|---|
| 3396 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3397 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3398 | |
|---|
| 3399 | // trigger change- and soncreate-callbacks via create |
|---|
| 3400 | |
|---|
| 3401 | GB_begin_transaction(gb_main); |
|---|
| 3402 | GBDATA *son2 = GB_create(cont_top, "son2", GB_INT); TEST_REJECT_NULL(son2); |
|---|
| 3403 | GB_commit_transaction(gb_main); |
|---|
| 3404 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3405 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
|---|
| 3406 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3407 | |
|---|
| 3408 | GB_begin_transaction(gb_main); |
|---|
| 3409 | GBDATA *grandson2 = GB_create(cont_son, "grandson2", GB_STRING); TEST_REJECT_NULL(grandson2); |
|---|
| 3410 | GB_commit_transaction(gb_main); |
|---|
| 3411 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
|---|
| 3412 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_son_newchild, cont_son); |
|---|
| 3413 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3414 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3415 | |
|---|
| 3416 | // trigger callbacks via delete |
|---|
| 3417 | |
|---|
| 3418 | TRIGGER_DELETE(son2); |
|---|
| 3419 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3420 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3421 | |
|---|
| 3422 | TRIGGER_DELETE(grandson2); |
|---|
| 3423 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3424 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
|---|
| 3425 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3426 | |
|---|
| 3427 | TEST_EXPECT_NO_ERROR(GB_request_undo_type(gb_main, GB_UNDO_UNDO)); |
|---|
| 3428 | |
|---|
| 3429 | TRIGGER_DELETE(top); |
|---|
| 3430 | TEST_EXPECT_DELETE_TRIGGERED(trace_top_deleted, top); |
|---|
| 3431 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3432 | |
|---|
| 3433 | TRIGGER_DELETE(grandson); |
|---|
| 3434 | TEST_EXPECT_DELETE_TRIGGERED(trace_grandson_deleted, grandson); |
|---|
| 3435 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3436 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
|---|
| 3437 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3438 | |
|---|
| 3439 | TRIGGER_DELETE(cont_son); |
|---|
| 3440 | TEST_EXPECT_DELETE_TRIGGERED(trace_ograndson_deleted, ograndson); |
|---|
| 3441 | TEST_EXPECT_DELETE_TRIGGERED(trace_cont_son_deleted, cont_son); |
|---|
| 3442 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3443 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3444 | |
|---|
| 3445 | // trigger callbacks by undoing last 3 delete transactions |
|---|
| 3446 | |
|---|
| 3447 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of cont_son |
|---|
| 3448 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3449 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
|---|
| 3450 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3451 | |
|---|
| 3452 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of grandson |
|---|
| 3453 | // cont_son callbacks are not triggered (they are not restored by undo) |
|---|
| 3454 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3455 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3456 | |
|---|
| 3457 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of top |
|---|
| 3458 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3459 | |
|---|
| 3460 | // reinstall callbacks that were removed by deletes |
|---|
| 3461 | |
|---|
| 3462 | GB_begin_transaction(gb_main); |
|---|
| 3463 | ENSURE_CONTAINER_CALLBACKS(cont_top); |
|---|
| 3464 | ENSURE_CONTAINER_CALLBACKS(cont_son); |
|---|
| 3465 | ENSURE_ENTRY_CALLBACKS(top); |
|---|
| 3466 | ENSURE_ENTRY_CALLBACKS(son); |
|---|
| 3467 | ENSURE_ENTRY_CALLBACKS(grandson); |
|---|
| 3468 | GB_commit_transaction(gb_main); |
|---|
| 3469 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3470 | |
|---|
| 3471 | // trigger callbacks which will be removed |
|---|
| 3472 | |
|---|
| 3473 | TRIGGER_CHANGE(son); |
|---|
| 3474 | TEST_EXPECT_CHANGE_TRIGGERED(trace_son_changed, son); |
|---|
| 3475 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3476 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3477 | |
|---|
| 3478 | GB_begin_transaction(gb_main); |
|---|
| 3479 | GBDATA *son3 = GB_create(cont_top, "son3", GB_INT); TEST_REJECT_NULL(son3); |
|---|
| 3480 | GB_commit_transaction(gb_main); |
|---|
| 3481 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
|---|
| 3482 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
|---|
| 3483 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3484 | |
|---|
| 3485 | // test remove callback |
|---|
| 3486 | |
|---|
| 3487 | GB_begin_transaction(gb_main); |
|---|
| 3488 | REMOVE_ENTRY_CALLBACKS(son); |
|---|
| 3489 | REMOVE_CONTAINER_CALLBACKS(cont_top); |
|---|
| 3490 | GB_commit_transaction(gb_main); |
|---|
| 3491 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3492 | |
|---|
| 3493 | // "trigger" removed callbacks |
|---|
| 3494 | |
|---|
| 3495 | TRIGGER_CHANGE(son); |
|---|
| 3496 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3497 | |
|---|
| 3498 | GB_begin_transaction(gb_main); |
|---|
| 3499 | GBDATA *son4 = GB_create(cont_top, "son4", GB_INT); TEST_REJECT_NULL(son4); |
|---|
| 3500 | GB_commit_transaction(gb_main); |
|---|
| 3501 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3502 | |
|---|
| 3503 | GB_close(gb_main); |
|---|
| 3504 | } |
|---|
| 3505 | |
|---|
| 3506 | void TEST_POSTCOND_arbdb() { |
|---|
| 3507 | GB_ERROR error = NULL; |
|---|
| 3508 | if (GB_have_error()) { |
|---|
| 3509 | error = GB_await_error(); // clears the error (to make further tests succeed) |
|---|
| 3510 | } |
|---|
| 3511 | |
|---|
| 3512 | bool unclosed_GB_shell = closed_open_shell_for_unit_tests(); |
|---|
| 3513 | |
|---|
| 3514 | TEST_REJECT(error); // your test finished with an exported error |
|---|
| 3515 | TEST_REJECT(unclosed_GB_shell); // your test finished w/o destroying GB_shell |
|---|
| 3516 | } |
|---|
| 3517 | |
|---|
| 3518 | #define TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED() \ |
|---|
| 3519 | TEST_EXPECT(traceHier_anyGrandson_changed.was_not_called()); \ |
|---|
| 3520 | TEST_EXPECT(traceHier_anyGrandson_deleted.was_not_called()); \ |
|---|
| 3521 | TEST_EXPECT(traceHier_anySonContainer_newchild.was_not_called()) |
|---|
| 3522 | |
|---|
| 3523 | #undef TEST_EXPECT_NO_CALLBACK_TRIGGERED |
|---|
| 3524 | |
|---|
| 3525 | #define TEST_EXPECT_NO_CALLBACK_TRIGGERED() \ |
|---|
| 3526 | TEST_EXPECT(trace_anotherGrandson_changed.was_not_called()); \ |
|---|
| 3527 | TEST_EXPECT(trace_elimGrandson2_deleted.was_not_called()) |
|---|
| 3528 | |
|---|
| 3529 | void TEST_hierarchy_callbacks() { |
|---|
| 3530 | GB_shell shell; |
|---|
| 3531 | GBDATA *gb_main = GB_open("new.arb", "c"); |
|---|
| 3532 | |
|---|
| 3533 | // create some data |
|---|
| 3534 | GB_begin_transaction(gb_main); |
|---|
| 3535 | |
|---|
| 3536 | GBDATA *cont_top1 = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top1); |
|---|
| 3537 | GBDATA *cont_top2 = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top2); |
|---|
| 3538 | |
|---|
| 3539 | GBDATA *cont_son11 = GB_create_container(cont_top1, "cont_son"); TEST_REJECT_NULL(cont_son11); |
|---|
| 3540 | GBDATA *cont_son21 = GB_create_container(cont_top2, "cont_son"); TEST_REJECT_NULL(cont_son21); |
|---|
| 3541 | GBDATA *cont_son22 = GB_create_container(cont_top2, "cont_son"); TEST_REJECT_NULL(cont_son22); |
|---|
| 3542 | |
|---|
| 3543 | GBDATA *top1 = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top1); |
|---|
| 3544 | GBDATA *top2 = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top2); |
|---|
| 3545 | |
|---|
| 3546 | GBDATA *son11 = GB_create(cont_top1, "son", GB_INT); TEST_REJECT_NULL(son11); |
|---|
| 3547 | GBDATA *son12 = GB_create(cont_top1, "son", GB_INT); TEST_REJECT_NULL(son12); |
|---|
| 3548 | GBDATA *son21 = GB_create(cont_top2, "son", GB_INT); TEST_REJECT_NULL(son21); |
|---|
| 3549 | |
|---|
| 3550 | GBDATA *grandson111 = GB_create(cont_son11, "grandson", GB_STRING); TEST_REJECT_NULL(grandson111); |
|---|
| 3551 | GBDATA *grandson112 = GB_create(cont_son11, "grandson", GB_STRING); TEST_REJECT_NULL(grandson112); |
|---|
| 3552 | GBDATA *grandson211 = GB_create(cont_son21, "grandson", GB_STRING); TEST_REJECT_NULL(grandson211); |
|---|
| 3553 | GBDATA *grandson221 = GB_create(cont_son22, "grandson", GB_STRING); TEST_REJECT_NULL(grandson221); |
|---|
| 3554 | GBDATA *grandson222 = GB_create(cont_son22, "grandson", GB_STRING); TEST_REJECT_NULL(grandson222); |
|---|
| 3555 | |
|---|
| 3556 | // create some entries at uncommon locations (compared to entries created above) |
|---|
| 3557 | GBDATA *ctop_top = GB_create (cont_top2, "top", GB_STRING); TEST_REJECT_NULL(ctop_top); |
|---|
| 3558 | GBDATA *top_son = GB_create (gb_main, "son", GB_INT); TEST_REJECT_NULL(top_son); |
|---|
| 3559 | GBDATA *cson = GB_create_container(gb_main, "cont_son"); TEST_REJECT_NULL(cson); |
|---|
| 3560 | GBDATA *cson_gs = GB_create (cson, "grandson", GB_STRING); TEST_REJECT_NULL(cson_gs); |
|---|
| 3561 | |
|---|
| 3562 | GB_commit_transaction(gb_main); |
|---|
| 3563 | |
|---|
| 3564 | // test gb_hierarchy_location |
|---|
| 3565 | { |
|---|
| 3566 | gb_hierarchy_location loc_top(top1); |
|---|
| 3567 | gb_hierarchy_location loc_son(son11); |
|---|
| 3568 | gb_hierarchy_location loc_grandson(grandson222); |
|---|
| 3569 | |
|---|
| 3570 | TEST_EXPECT(loc_top.matches(top1)); |
|---|
| 3571 | TEST_EXPECT(loc_top.matches(top2)); |
|---|
| 3572 | TEST_EXPECT(!loc_top.matches(cont_top1)); |
|---|
| 3573 | TEST_EXPECT(!loc_top.matches(son12)); |
|---|
| 3574 | TEST_EXPECT(!loc_top.matches(cont_son22)); |
|---|
| 3575 | TEST_EXPECT(!loc_top.matches(ctop_top)); |
|---|
| 3576 | |
|---|
| 3577 | TEST_EXPECT(loc_son.matches(son11)); |
|---|
| 3578 | TEST_EXPECT(loc_son.matches(son21)); |
|---|
| 3579 | TEST_EXPECT(!loc_son.matches(top1)); |
|---|
| 3580 | TEST_EXPECT(!loc_son.matches(grandson111)); |
|---|
| 3581 | TEST_EXPECT(!loc_son.matches(cont_son22)); |
|---|
| 3582 | TEST_EXPECT(!loc_son.matches(top_son)); |
|---|
| 3583 | |
|---|
| 3584 | TEST_EXPECT(loc_grandson.matches(grandson222)); |
|---|
| 3585 | TEST_EXPECT(loc_grandson.matches(grandson111)); |
|---|
| 3586 | TEST_EXPECT(!loc_grandson.matches(son11)); |
|---|
| 3587 | TEST_EXPECT(!loc_grandson.matches(top1)); |
|---|
| 3588 | TEST_EXPECT(!loc_grandson.matches(cont_son22)); |
|---|
| 3589 | TEST_EXPECT(!loc_grandson.matches(cson_gs)); |
|---|
| 3590 | |
|---|
| 3591 | gb_hierarchy_location loc_ctop_top(ctop_top); |
|---|
| 3592 | TEST_EXPECT(loc_ctop_top.matches(ctop_top)); |
|---|
| 3593 | TEST_EXPECT(!loc_ctop_top.matches(top1)); |
|---|
| 3594 | |
|---|
| 3595 | gb_hierarchy_location loc_top_son(top_son); |
|---|
| 3596 | TEST_EXPECT(loc_top_son.matches(top_son)); |
|---|
| 3597 | TEST_EXPECT(!loc_top_son.matches(son11)); |
|---|
| 3598 | |
|---|
| 3599 | gb_hierarchy_location loc_gs(cson_gs); |
|---|
| 3600 | TEST_EXPECT(loc_gs.matches(cson_gs)); |
|---|
| 3601 | TEST_EXPECT(!loc_gs.matches(grandson211)); |
|---|
| 3602 | |
|---|
| 3603 | gb_hierarchy_location loc_root(gb_main); |
|---|
| 3604 | TEST_EXPECT(loc_root.matches(gb_main)); |
|---|
| 3605 | TEST_EXPECT(!loc_root.matches(cont_top1)); |
|---|
| 3606 | TEST_EXPECT(!loc_root.matches(cont_son11)); |
|---|
| 3607 | TEST_EXPECT(!loc_root.matches(top1)); |
|---|
| 3608 | TEST_EXPECT(!loc_root.matches(son11)); |
|---|
| 3609 | TEST_EXPECT(!loc_root.matches(grandson211)); |
|---|
| 3610 | } |
|---|
| 3611 | |
|---|
| 3612 | |
|---|
| 3613 | // instanciate callback_trace data and install hierarchy callbacks |
|---|
| 3614 | GBDATA *anySon = son11; |
|---|
| 3615 | |
|---|
| 3616 | GBDATA *anySonContainer = cont_son11; |
|---|
| 3617 | GBDATA *anotherSonContainer = cont_son22; |
|---|
| 3618 | |
|---|
| 3619 | GBDATA *anyGrandson = grandson221; |
|---|
| 3620 | GBDATA *anotherGrandson = grandson112; |
|---|
| 3621 | GBDATA *elimGrandson = grandson222; |
|---|
| 3622 | GBDATA *elimGrandson2 = grandson111; |
|---|
| 3623 | GBDATA *newGrandson = NULL; |
|---|
| 3624 | |
|---|
| 3625 | INIT_CHANGED_HIERARCHY_CALLBACK(anyGrandson); |
|---|
| 3626 | INIT_DELETED_HIERARCHY_CALLBACK(anyGrandson); |
|---|
| 3627 | INIT_NWCHILD_HIERARCHY_CALLBACK(anySonContainer); |
|---|
| 3628 | |
|---|
| 3629 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3630 | |
|---|
| 3631 | // trigger change-callback using same DB entry |
|---|
| 3632 | TRIGGER_CHANGE(anyGrandson); |
|---|
| 3633 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
|---|
| 3634 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3635 | |
|---|
| 3636 | // trigger change-callback using another DB entry (same hierarchy) |
|---|
| 3637 | TRIGGER_CHANGE(anotherGrandson); |
|---|
| 3638 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
|---|
| 3639 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3640 | |
|---|
| 3641 | // check nothing is triggered by an element at different hierarchy |
|---|
| 3642 | TRIGGER_CHANGE(anySon); |
|---|
| 3643 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3644 | |
|---|
| 3645 | // trigger change-callback using both DB entries (in two TAs) |
|---|
| 3646 | TRIGGER_CHANGE(anyGrandson); |
|---|
| 3647 | TRIGGER_CHANGE(anotherGrandson); |
|---|
| 3648 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
|---|
| 3649 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
|---|
| 3650 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3651 | |
|---|
| 3652 | // trigger change-callback using both DB entries (in one TA) |
|---|
| 3653 | TRIGGER_2_CHANGES(anyGrandson, anotherGrandson); |
|---|
| 3654 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
|---|
| 3655 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
|---|
| 3656 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3657 | |
|---|
| 3658 | // trigger son-created-callback |
|---|
| 3659 | { |
|---|
| 3660 | GB_initial_transaction ta(gb_main); |
|---|
| 3661 | if (ta.ok()) { |
|---|
| 3662 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
|---|
| 3663 | } |
|---|
| 3664 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
|---|
| 3665 | } |
|---|
| 3666 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anySonContainer); |
|---|
| 3667 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3668 | |
|---|
| 3669 | // trigger 2 son-created-callbacks (for 2 containers) and one change-callback (for a newly created son) |
|---|
| 3670 | { |
|---|
| 3671 | GB_initial_transaction ta(gb_main); |
|---|
| 3672 | if (ta.ok()) { |
|---|
| 3673 | newGrandson = GB_create(anotherSonContainer, "grandson", GB_STRING); TEST_REJECT_NULL(newGrandson); |
|---|
| 3674 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
|---|
| 3675 | } |
|---|
| 3676 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
|---|
| 3677 | } |
|---|
| 3678 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, newGrandson); |
|---|
| 3679 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anotherSonContainer); |
|---|
| 3680 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anySonContainer); |
|---|
| 3681 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3682 | |
|---|
| 3683 | // trigger delete-callback |
|---|
| 3684 | { |
|---|
| 3685 | GB_initial_transaction ta(gb_main); |
|---|
| 3686 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson)); |
|---|
| 3687 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
|---|
| 3688 | } |
|---|
| 3689 | TEST_EXPECT_DELETE_TRIGGERED(traceHier_anyGrandson_deleted, elimGrandson); |
|---|
| 3690 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3691 | |
|---|
| 3692 | // bind normal (non-hierarchical) callbacks to entries which trigger hierarchical callbacks and .. |
|---|
| 3693 | calledWith::timer = 0; |
|---|
| 3694 | GB_begin_transaction(gb_main); |
|---|
| 3695 | INIT_CHANGED_CALLBACK(anotherGrandson); |
|---|
| 3696 | INIT_DELETED_CALLBACK(elimGrandson2); |
|---|
| 3697 | GB_commit_transaction(gb_main); |
|---|
| 3698 | |
|---|
| 3699 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3700 | |
|---|
| 3701 | { |
|---|
| 3702 | GB_initial_transaction ta(gb_main); |
|---|
| 3703 | if (ta.ok()) { |
|---|
| 3704 | GB_touch(anotherGrandson); |
|---|
| 3705 | GB_touch(elimGrandson2); |
|---|
| 3706 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson2)); |
|---|
| 3707 | } |
|---|
| 3708 | } |
|---|
| 3709 | |
|---|
| 3710 | // .. test call-order (delete before change, hierarchical before normal): |
|---|
| 3711 | TEST_EXPECT_DELETE_TRIGGERED_AT(traceHier_anyGrandson_deleted, elimGrandson2, 1); |
|---|
| 3712 | TEST_EXPECT_DELETE_TRIGGERED_AT(trace_elimGrandson2_deleted, elimGrandson2, 2); |
|---|
| 3713 | TEST_EXPECT_CHANGE_TRIGGERED_AT(traceHier_anyGrandson_changed, anotherGrandson, 3); |
|---|
| 3714 | TEST_EXPECT_CHANGE_TRIGGERED_AT(trace_anotherGrandson_changed, anotherGrandson, 4); |
|---|
| 3715 | |
|---|
| 3716 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
|---|
| 3717 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
|---|
| 3718 | |
|---|
| 3719 | // cleanup |
|---|
| 3720 | GB_close(gb_main); |
|---|
| 3721 | } |
|---|
| 3722 | |
|---|
| 3723 | #endif // UNIT_TESTS |
|---|
| 3724 | |
|---|
| 3725 | |
|---|