static tdb *make_tdb(const char *root, const uint64_t *tstamps, uint32_t num, int should_fail) { static uint8_t uuid[16]; const char *fields[] = {}; tdb_cons* c = tdb_cons_init(); test_cons_settings(c); uint64_t zero = 0; uint32_t i; assert(tdb_cons_open(c, root, fields, 0) == 0); for (i = 0; i < num; i++) assert(tdb_cons_add(c, uuid, tstamps[i], fields, &zero) == 0); assert(tdb_cons_finalize(c) == (should_fail ? TDB_ERR_TIMESTAMP_TOO_LARGE: 0)); tdb_cons_close(c); if (!should_fail){ tdb* t = tdb_init(); assert(tdb_open(t, root) == 0); return t; }else return NULL; }
static tdb *create_db(const char *root, uint32_t id, uint32_t len) { char path[TDB_MAX_PATH_SIZE]; const char *fields[] = {"id"}; const char *values[] = {(const char*)&id}; uint64_t lengths[] = {4}; uint32_t i, j; static uint8_t uuid[16]; uint64_t timestamp = 100; tdb_path(path, "%s.%u", root, id); tdb_cons* cons = tdb_cons_init(); test_cons_settings(cons); assert(tdb_cons_open(cons, path, fields, 1) == 0); for (i = 1000; i < 1000 + NUM_COMMON_TRAILS; i++){ memcpy(uuid, &i, 4); for (timestamp = 1000, j = 0; j < len; j++){ assert(tdb_cons_add(cons, uuid, timestamp, values, lengths) == 0); timestamp += id; } } assert(tdb_cons_finalize(cons) == 0); tdb_cons_close(cons); tdb* db = tdb_init(); assert(tdb_open(db, path) == 0); return db; }
int main(int argc, char** argv) { static uint8_t uuid[16]; const char *fields[] = {"a", "b"}; const char *values[] = {"foo", "ba"}; const uint64_t lengths[] = {3, 2}; tdb_opt_value val = {.value = UINT64_MAX}; uint64_t i; char pkgname[4096]; strcpy(pkgname, getenv("TDB_TMP_DIR")); strcat(pkgname, "test.tdb"); tdb_cons* c = tdb_cons_init(); test_cons_settings(c); assert(tdb_cons_open(c, pkgname, fields, 2) == 0); assert(tdb_cons_set_opt(c, TDB_OPT_ONLY_DIFF_ITEMS, TDB_TRUE) == TDB_ERR_UNKNOWN_OPTION); assert(tdb_cons_set_opt(c, TDB_OPT_CONS_OUTPUT_FORMAT, val) == TDB_ERR_INVALID_OPTION_VALUE); assert(tdb_cons_set_opt(c, TDB_OPT_CONS_OUTPUT_FORMAT, opt_val(TDB_OPT_CONS_OUTPUT_FORMAT_PACKAGE)) == 0); assert(tdb_cons_get_opt(c, TDB_OPT_ONLY_DIFF_ITEMS, &val) == TDB_ERR_UNKNOWN_OPTION); assert(tdb_cons_get_opt(c, TDB_OPT_CONS_OUTPUT_FORMAT, &val) == 0); assert(val.value == TDB_OPT_CONS_OUTPUT_FORMAT_PACKAGE); for (i = 0; i < NUM_EVENTS; i++) assert(tdb_cons_add(c, uuid, i, values, lengths) == 0); assert(tdb_cons_finalize(c) == 0); tdb_cons_close(c); tdb* t = tdb_init(); assert(tdb_open(t, pkgname) == 0); tdb_cursor *cursor = tdb_cursor_new(t); const tdb_event *event; assert(tdb_get_trail(cursor, 0) == 0); for (i = 0; (event = tdb_cursor_next(cursor)); i++){ assert(event->timestamp == i); assert(event->num_items == 2); } assert(i == NUM_EVENTS); tdb_close(t); tdb_cursor_free(cursor); return 0; }
int main(int argc, char **argv) { static const char **fields; static uint64_t *lengths; dsfmt_t state; Pvoid_t uuids = NULL; tdb_cons* c = tdb_cons_init(); test_cons_settings(c); uint64_t i, j; __uint128_t prev_uuid = 0; Word_t key; int tst; assert(tdb_cons_open(c, argv[1], fields, 0) == 0); dsfmt_init_gen_rand(&state, 2489); for (i = 0; i < NUM_TRAILS; i++){ uint8_t uuid[16]; gen_random_uuid(uuid, &state); memcpy(&key, uuid, 8); J1S(tst, uuids, key); if (!tst){ printf("half-word collision! change random seed!\n"); return -1; } for (j = 0; j < NUM_EVENTS; j++) tdb_cons_add(c, uuid, i * 100 + j, fields, lengths); } J1C(key, uuids, 0, -1); assert(key == NUM_TRAILS); assert(tdb_cons_finalize(c) == 0); tdb_cons_close(c); tdb* t = tdb_init(); assert(tdb_open(t, argv[1]) == 0); assert(tdb_num_trails(t) == NUM_TRAILS); assert(tdb_num_events(t) == NUM_TRAILS * NUM_EVENTS); for (i = 0; i < NUM_TRAILS; i++){ __uint128_t this_uuid; /* uuids must be monotonically increasing */ memcpy(&this_uuid, tdb_get_uuid(t, i), 16); assert(this_uuid > prev_uuid); prev_uuid = this_uuid; /* remove this uuid from the uuid set and make sure it exists */ memcpy(&key, &this_uuid, 8); J1U(tst, uuids, key); assert(tst == 1); } /* make sure we retrieved all uuids */ J1C(key, uuids, 0, -1); assert(key == 0); return 0; }
int main(int argc, char** argv) { static uint8_t uuid[16]; const char *fields[] = {"a", "b"}; uint64_t lengths[] = {1, 1}; char val1 = 0; char val2 = 0; char *values[] = {&val1, &val2}; uint64_t i, j, num_events = 0; tdb_cons* c = tdb_cons_init(); test_cons_settings(c); assert(tdb_cons_open(c, getenv("TDB_TMP_DIR"), fields, 2) == 0); for (i = 1; i < NUM_TRAILS + 1; i++){ int x = (i % 5) + 1; memcpy(uuid, &i, 8); /* make sure at least some trails are longer than CURSOR_BUFFER_NUM_EVENTS */ for (j = 0; j < i * 10; j++){ /* let's add some complexity in edge encoding */ val1 = (j % 10) ? 1: 0; val2 = (j % x) ? 1: 0; assert(tdb_cons_add(c, uuid, j, (const char**)values, lengths) == 0); ++num_events; } } assert(tdb_cons_finalize(c) == 0); tdb_cons_close(c); tdb* t = tdb_init(); assert(tdb_open(t, getenv("TDB_TMP_DIR")) == 0); tdb_cursor *cursor = tdb_cursor_new(t); assert(tdb_num_events(t) == num_events); for (num_events = 0, i = 0; i < NUM_TRAILS; i++){ const tdb_event *event; int x = ((i + 1) % 5) + 1; assert(tdb_get_trail(cursor, i) == 0); /* test tdb_get_trail_length */ uint64_t n = tdb_get_trail_length(cursor); assert(n == (i + 1) * 10); num_events += n; assert(tdb_get_trail_length(cursor) == 0); /* test cursor */ assert(tdb_get_trail(cursor, i) == 0); j = 0; while ((event = tdb_cursor_next(cursor))){ const char *v; uint64_t len; assert(event->timestamp == j); assert(event->num_items == 2); val1 = (j % 10) ? 1: 0; val2 = (j % x) ? 1: 0; v = tdb_get_item_value(t, event->items[0], &len); assert(len == 1); assert(*v == val1); v = tdb_get_item_value(t, event->items[1], &len); assert(len == 1); assert(*v == val2); ++j; } } assert(tdb_num_events(t) == num_events); tdb_cursor_free(cursor); tdb_close(t); return 0; }