/* -*- mode: C; c-basic-offset: 4 -*- */ #ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved." #include "includes.h" #include "test.h" static TOKUTXN const null_txn = 0; static DB * const null_db = 0; static char fname[] = __FILE__ ".brt"; static void test_dump_empty_db (void) { BRT t; CACHETABLE ct; int r; toku_memory_check=1; r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); unlink(fname); r = toku_open_brt(fname, 1, &t, 1024, ct, null_txn, toku_builtin_compare_fun, null_db); assert(r==0); if (verbose) toku_dump_brt(stdout, t); r = toku_close_brt(t, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); toku_memory_check_all_free(); } /* Test running multiple trees in different files */ static void test_multiple_files_of_size (int size) { const char *n0 = __FILE__ "test0.brt"; const char *n1 = __FILE__ "test1.brt"; CACHETABLE ct; BRT t0,t1; int r,i; if (verbose) printf("test_multiple_files_of_size(%d)\n", size); unlink(n0); unlink(n1); toku_memory_check_all_free(); r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); r = toku_open_brt(n0, 1, &t0, size, ct, null_txn, toku_builtin_compare_fun, null_db); assert(r==0); r = toku_open_brt(n1, 1, &t1, size, ct, null_txn, toku_builtin_compare_fun, null_db); assert(r==0); for (i=0; i<10000; i++) { char key[100],val[100]; DBT k,v; snprintf(key, 100, "key%d", i); snprintf(val, 100, "val%d", i); toku_brt_insert(t0, toku_fill_dbt(&k, key, 1+strlen(key)), toku_fill_dbt(&v, val, 1+strlen(val)), null_txn); snprintf(val, 100, "Val%d", i); toku_brt_insert(t1, toku_fill_dbt(&k, key, 1+strlen(key)), toku_fill_dbt(&v, val, 1+strlen(val)), null_txn); } //toku_verify_brt(t0); //dump_brt(t0); //dump_brt(t1); toku_verify_brt(t0); toku_verify_brt(t1); r = toku_close_brt(t0, 0); assert(r==0); r = toku_close_brt(t1, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); toku_memory_check_all_free(); /* Now see if the data is all there. */ r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); r = toku_open_brt(n0, 0, &t0, 1<<12, ct, null_txn, toku_builtin_compare_fun, null_db); if (verbose) printf("%s:%d r=%d\n", __FILE__, __LINE__,r); assert(r==0); r = toku_open_brt(n1, 0, &t1, 1<<12, ct, null_txn, toku_builtin_compare_fun, null_db); assert(r==0); for (i=0; i<10000; i++) { char key[100],val[100]; snprintf(key, 100, "key%d", i); snprintf(val, 100, "val%d", i); brt_lookup_and_check_nodup(t0, key, val); snprintf(val, 100, "Val%d", i); brt_lookup_and_check_nodup(t1, key, val); } r = toku_close_brt(t0, 0); assert(r==0); r = toku_close_brt(t1, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); toku_memory_check_all_free(); } static void test_multiple_files (void) { test_multiple_files_of_size (1<<12); test_multiple_files_of_size (1<<20); } /* Test to see that a single db can be opened many times. */ static void test_multiple_brts_one_db_one_file (void) { enum { MANYN = 2 }; int i, r; CACHETABLE ct; BRT trees[MANYN]; if (verbose) printf("test_multiple_brts_one_db_one_file:"); toku_memory_check_all_free(); unlink(fname); r = toku_brt_create_cachetable(&ct, 32, ZERO_LSN, NULL_LOGGER); assert(r==0); for (i=0; idata; unsigned char *bd=b->data; unsigned int siz=a->size; assert(a->size==b->size); assert(db==&nonce_db); // make sure the db was passed down correctly for (i=0; ibd[siz-1-i]) return +1; } return 0; } static void test_wrongendian_compare (int wrong_p, unsigned int N) { CACHETABLE ct; BRT brt; int r; unsigned int i; unlink(fname); toku_memory_check_all_free(); { char a[4]={0,1,0,0}; char b[4]={1,0,0,0}; DBT at, bt; assert(wrong_compare_fun(&nonce_db, toku_fill_dbt(&at, a, 4), toku_fill_dbt(&bt, b, 4))>0); assert(wrong_compare_fun(&nonce_db, toku_fill_dbt(&at, b, 4), toku_fill_dbt(&bt, a, 4))<0); } r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); //printf("%s:%d WRONG=%d\n", __FILE__, __LINE__, wrong_p); if (0) { // ???? Why is this commented out? r = toku_open_brt(fname, 1, &brt, 1<<20, ct, null_txn, wrong_p ? wrong_compare_fun : toku_builtin_compare_fun, &nonce_db); assert(r==0); for (i=1; i<257; i+=255) { unsigned char a[4],b[4]; b[3] = a[0] = (unsigned char)(i&255); b[2] = a[1] = (unsigned char)((i>>8)&255); b[1] = a[2] = (unsigned char)((i>>16)&255); b[0] = a[3] = (unsigned char)((i>>24)&255); DBT kbt = {.size=sizeof(a), .data=a}; DBT vbt = {.size=sizeof(b), .data=b}; if (verbose) printf("%s:%d insert: %02x%02x%02x%02x -> %02x%02x%02x%02x\n", __FILE__, __LINE__, ((char*)kbt.data)[0], ((char*)kbt.data)[1], ((char*)kbt.data)[2], ((char*)kbt.data)[3], ((char*)vbt.data)[0], ((char*)vbt.data)[1], ((char*)vbt.data)[2], ((char*)vbt.data)[3]); r = toku_brt_insert(brt, &kbt, &vbt, null_txn); assert(r==0); } { BRT_CURSOR cursor=0; r = toku_brt_cursor(brt, &cursor, NULL); assert(r==0); for (i=0; i<2; i++) { unsigned char a[4],b[4]; struct check_pair pair = {4, &a, 4, &b, 0}; b[3] = a[0] = (unsigned char)(i&255); b[2] = a[1] = (unsigned char)((i>>8)&255); b[1] = a[2] = (unsigned char)((i>>16)&255); b[0] = a[3] = (unsigned char)((i>>24)&255); r = toku_brt_cursor_get(cursor, NULL, NULL, lookup_checkf, &pair, DB_NEXT); assert(r==0); assert(pair.call_count==1); } r = toku_close_brt(brt, 0); } } { toku_cachetable_verify(ct); r = toku_open_brt(fname, 1, &brt, 1<<20, ct, null_txn, wrong_p ? wrong_compare_fun : toku_builtin_compare_fun, &nonce_db); assert(r==0); toku_cachetable_verify(ct); for (i=0; i>8)&255); b[1] = a[2] = (unsigned char)((i>>16)&255); b[0] = a[3] = (unsigned char)((i>>24)&255); DBT kbt = {.size=sizeof(a), .data=a}; DBT vbt = {.size=sizeof(b), .data=b}; if (0) printf("%s:%d insert: %02x%02x%02x%02x -> %02x%02x%02x%02x\n", __FILE__, __LINE__, ((unsigned char*)kbt.data)[0], ((unsigned char*)kbt.data)[1], ((unsigned char*)kbt.data)[2], ((unsigned char*)kbt.data)[3], ((unsigned char*)vbt.data)[0], ((unsigned char*)vbt.data)[1], ((unsigned char*)vbt.data)[2], ((unsigned char*)vbt.data)[3]); r = toku_brt_insert(brt, &kbt, &vbt, null_txn); assert(r==0); toku_cachetable_verify(ct); } BRT_CURSOR cursor=0; r = toku_brt_cursor(brt, &cursor, NULL); assert(r==0); for (i=0; i>8)&255); b[1] = a[2] = (unsigned char)((i>>16)&255); b[0] = a[3] = (unsigned char)((i>>24)&255); r = toku_brt_cursor_get(cursor, NULL, NULL, lookup_checkf, &pair, DB_NEXT); assert(r==0); assert(pair.call_count==1); toku_cachetable_verify(ct); } r = toku_close_brt(brt, 0); assert(r==0); } r = toku_cachetable_close(&ct); assert(r==0); toku_memory_check_all_free(); } static int test_brt_cursor_keycompare(DB *db __attribute__((unused)), const DBT *a, const DBT *b) { return toku_keycompare(a->data, a->size, b->data, b->size); } static void test_large_kv(int bsize, int ksize, int vsize) { BRT t; int r; CACHETABLE ct; if (verbose) printf("test_large_kv: %d %d %d\n", bsize, ksize, vsize); r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); unlink(fname); r = toku_open_brt(fname, 1, &t, bsize, ct, null_txn, toku_builtin_compare_fun, null_db); assert(r==0); DBT key, val; char *k, *v; k = toku_malloc(ksize); assert(k); memset(k, 0, ksize); v = toku_malloc(vsize); assert(v); memset(v, 0, vsize); toku_fill_dbt(&key, k, ksize); toku_fill_dbt(&val, v, vsize); r = toku_brt_insert(t, &key, &val, 0); assert(r == 0); toku_free(k); toku_free(v); r = toku_close_brt(t, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); } /* * test the key and value limits * the current implementation crashes when kvsize == bsize/2 rather than fails */ static void test_brt_limits(void) { int bsize = 1024; int kvsize = 4; while (kvsize < bsize/2) { test_large_kv(bsize, kvsize, kvsize); toku_memory_check_all_free(); kvsize *= 2; } } /* * verify that a delete on an empty tree fails */ static void test_brt_delete_empty(void) { if (verbose) printf("test_brt_delete_empty\n"); BRT t; int r; CACHETABLE ct; r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); unlink(fname); r = toku_open_brt(fname, 1, &t, 4096, ct, null_txn, toku_builtin_compare_fun, null_db); assert(r==0); DBT key; int k = toku_htonl(1); toku_fill_dbt(&key, &k, sizeof k); r = toku_brt_delete(t, &key, null_txn); assert(r == 0); r = toku_close_brt(t, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); } /* * insert n keys, delete all n keys, verify that lookups for all the keys fail, * verify that a cursor walk of the tree finds nothing */ static void test_brt_delete_present(int n) { if (verbose) printf("test_brt_delete_present:%d\n", n); BRT t; int r; CACHETABLE ct; int i; r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); unlink(fname); r = toku_open_brt(fname, 1, &t, 4096, ct, null_txn, toku_builtin_compare_fun, null_db); assert(r==0); /* insert 0 .. n-1 */ for (i=0; i 0) { { int k = toku_htonl(n-1); DBT key = {.size=sizeof k, .data=&k}; r = toku_brt_delete(t, &key, null_txn); assert(r == 0); } { int k = toku_htonl(n-1); DBT key = {.size=sizeof k, .data=&k}; struct check_pair pair = {0,0,0,0,0}; r = toku_brt_lookup(t, &key, NULL, lookup_checkf, &pair); assert(r == DB_NOTFOUND); assert(pair.call_count==0); } } r = toku_close_brt(t, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); } /* insert <0,0>, <0,1>, .. <0,n> delete_both <0,i> for all even i verify <0,i> exists for all odd i */ static void test_brt_delete_both(int n) { if (verbose) printf("test_brt_delete_both:%d\n", n); BRT t=0; int r; CACHETABLE ct; int i; r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); unlink(fname); r = toku_brt_create(&t); assert(r == 0); r = toku_brt_set_flags(t, TOKU_DB_DUP + TOKU_DB_DUPSORT); assert(r == 0); r = toku_brt_set_nodesize(t, 4096); assert(r == 0); r = toku_brt_open(t, fname, 1, 1, ct, null_txn, (DB*)0); assert(r==0); DBT key, val; int k, v; for (i=0; i= v */ for (i=0; i max_key) { /* there is no smallest key if v > the max key */ assert(r == DB_NOTFOUND); assert(pair.call_count==0); } else { assert(r == 0); assert(pair.call_count==1); } } r = toku_brt_cursor_close(cursor); assert(r==0); r = toku_close_brt(brt, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); } static void test_new_brt_cursor_set(int n, int cursor_op, DB *db) { if (verbose) printf("test_brt_cursor_set:%d %d %p\n", n, cursor_op, db); int r; CACHETABLE ct; BRT brt; BRT_CURSOR cursor=0; unlink(fname); r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0); r = toku_open_brt(fname, 1, &brt, 1<<12, ct, null_txn, test_brt_cursor_keycompare, db); assert(r==0); int i; /* insert keys 0, 10, 20 .. 10*(n-1) */ for (i=0; i