#include "brt.h" #include "key.h" #include "memory.h" #include #include #include #include #include #include #include #include extern long long n_items_malloced; static void test0 (void) { BRT t; int r; CACHETABLE ct; char fname[]="testbrt.brt"; printf("%s:%d test0\n", __FILE__, __LINE__); memory_check=1; memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); printf("%s:%d test0\n", __FILE__, __LINE__); unlink(fname); r = open_brt(fname, 0, 1, &t, 1024, ct, default_compare_fun); assert(r==0); printf("%s:%d test0\n", __FILE__, __LINE__); printf("%s:%d n_items_malloced=%lld\n", __FILE__, __LINE__, n_items_malloced); r = close_brt(t); assert(r==0); printf("%s:%d n_items_malloced=%lld\n", __FILE__, __LINE__, n_items_malloced); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); } static void test1 (void) { BRT t; int r; CACHETABLE ct; char fname[]="testbrt.brt"; DBT k,v; memory_check=1; memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); unlink(fname); r = open_brt(fname, 0, 1, &t, 1024, ct, default_compare_fun); assert(r==0); brt_insert(t, fill_dbt(&k, "hello", 6), fill_dbt(&v, "there", 6), 0); { r = brt_lookup(t, fill_dbt(&k, "hello", 6), init_dbt(&v), 0); assert(r==0); assert(strcmp(v.data, "there")==0); assert(v.size==6); } r = close_brt(t); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); printf("test1 ok\n"); } static void test2 (int memcheck) { BRT t; int r; int i; CACHETABLE ct; char fname[]="testbrt.brt"; memory_check=memcheck; printf("%s:%d checking\n", __FILE__, __LINE__); memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); unlink(fname); r = open_brt(fname, 0, 1, &t, 1024, ct, default_compare_fun); printf("%s:%d did setup\n", __FILE__, __LINE__); assert(r==0); for (i=0; i<2048; i++) { DBT k,v; char key[100],val[100]; snprintf(key,100,"hello%d",i); snprintf(val,100,"there%d",i); brt_insert(t, fill_dbt(&k, key, 1+strlen(key)), fill_dbt(&v, val, 1+strlen(val)), 0); //printf("%s:%d did insert %d\n", __FILE__, __LINE__, i); if (0) { brt_flush(t); { int n = get_n_items_malloced(); printf("%s:%d i=%d n_items_malloced=%d\n", __FILE__, __LINE__, i, n); if (n!=3) print_malloced_items(); assert(n==3); } } } printf("%s:%d inserted\n", __FILE__, __LINE__); r = close_brt(t); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); printf("test2 ok\n"); } static void test3 (int nodesize, int count, int memcheck) { BRT t; int r; struct timeval t0,t1; int i; CACHETABLE ct; char fname[]="testbrt.brt"; memory_check=memcheck; memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); gettimeofday(&t0, 0); unlink(fname); r = open_brt(fname, 0, 1, &t, nodesize, ct, default_compare_fun); assert(r==0); for (i=0; i=0) { char key[100], valexpected[100]; DBT k,v; if (i%1000==0) printf("r"); fflush(stdout); snprintf(key, 100, "key%d", rk); snprintf(valexpected, 100, "val%d", values[rk]); r = brt_lookup(t, fill_dbt(&k, key, 1+strlen(key)), init_dbt(&v), 0); assert(r==0); assert(v.size==(1+strlen(valexpected))); assert(memcmp(v.data,valexpected,v.size)==0); } } printf("\n"); toku_free(values); r = close_brt(t); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); } static void test_dump_empty_db (void) { BRT t; CACHETABLE ct; int r; char fname[]="testbrt.brt"; memory_check=1; r = brt_create_cachetable(&ct, 0); assert(r==0); unlink(fname); r = open_brt(fname, 0, 1, &t, 1024, ct, default_compare_fun); assert(r==0); dump_brt(t); r = close_brt(t); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); } /* Test running multiple trees in different files */ static void test_multiple_files_of_size (int size) { const char *n0 = "test0.brt"; const char *n1 = "test1.brt"; CACHETABLE ct; BRT t0,t1; int r,i; printf("test_multiple_files_of_size(%d)\n", size); unlink(n0); unlink(n1); memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); r = open_brt(n0, 0, 1, &t0, size, ct, default_compare_fun); assert(r==0); r = open_brt(n1, 0, 1, &t1, size, ct, default_compare_fun); 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); brt_insert(t0, fill_dbt(&k, key, 1+strlen(key)), fill_dbt(&v, val, 1+strlen(val)), 0); snprintf(val, 100, "Val%d", i); brt_insert(t1, fill_dbt(&k, key, 1+strlen(key)), fill_dbt(&v, val, 1+strlen(val)), 0); } //verify_brt(t0); //dump_brt(t0); //dump_brt(t1); verify_brt(t0); verify_brt(t1); r = close_brt(t0); assert(r==0); r = close_brt(t1); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); /* Now see if the data is all there. */ r = brt_create_cachetable(&ct, 0); assert(r==0); r = open_brt(n0, 0, 0, &t0, 1<<12, ct, default_compare_fun); printf("%s:%d r=%d\n", __FILE__, __LINE__,r); assert(r==0); r = open_brt(n1, 0, 0, &t1, 1<<12, ct, default_compare_fun); assert(r==0); for (i=0; i<10000; i++) { char key[100],val[100]; DBT k,actual; snprintf(key, 100, "key%d", i); snprintf(val, 100, "val%d", i); r=brt_lookup(t0, fill_dbt(&k, key, 1+strlen(key)), init_dbt(&actual), 0); assert(r==0); assert(strcmp(val,actual.data)==0); assert(actual.size==1+strlen(val)); snprintf(val, 100, "Val%d", i); r=brt_lookup(t1, fill_dbt(&k, key, 1+strlen(key)), init_dbt(&actual), 0); assert(r==0); assert(strcmp(val,actual.data)==0); assert(actual.size==1+strlen(val)); } r = close_brt(t0); assert(r==0); r = close_brt(t1); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); } static void test_multiple_files (void) { test_multiple_files_of_size (1<<12); test_multiple_files_of_size (1<<20); } static void test_named_db (void) { const char *n0 = "test0.brt"; const char *n1 = "test1.brt"; CACHETABLE ct; BRT t0; int r; DBT k,v; printf("test_named_db\n"); unlink(n0); unlink(n1); memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); r = open_brt(n0, "db1", 1, &t0, 1<<12, ct, default_compare_fun); assert(r==0); brt_insert(t0, fill_dbt(&k, "good", 5), fill_dbt(&v, "day", 4), 0); assert(r==0); r = close_brt(t0); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); r = open_brt(n0, "db1", 0, &t0, 1<<12, ct, default_compare_fun); assert(r==0); { r = brt_lookup(t0, fill_dbt(&k, "good", 5), init_dbt(&v), 0); assert(r==0); assert(v.size==4); assert(strcmp(v.data,"day")==0); } r = close_brt(t0); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); } static void test_multiple_dbs (void) { const char *n0 = "test0.brt"; const char *n1 = "test1.brt"; CACHETABLE ct; BRT t0,t1; int r; DBT k,v; printf("test_multiple_dbs: "); unlink(n0); unlink(n1); memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); r = open_brt(n0, "db1", 1, &t0, 1<<12, ct, default_compare_fun); assert(r==0); r = open_brt(n1, "db2", 1, &t1, 1<<12, ct, default_compare_fun); assert(r==0); brt_insert(t0, fill_dbt(&k, "good", 5), fill_dbt(&v, "grief", 6), 0); assert(r==0); brt_insert(t1, fill_dbt(&k, "bad", 4), fill_dbt(&v, "night", 6), 0); assert(r==0); r = close_brt(t0); assert(r==0); r = close_brt(t1); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); r = brt_create_cachetable(&ct, 0); assert(r==0); r = open_brt(n0, "db1", 0, &t0, 1<<12, ct, default_compare_fun); assert(r==0); r = open_brt(n1, "db2", 0, &t1, 1<<12, ct, default_compare_fun); assert(r==0); { r = brt_lookup(t0, fill_dbt(&k, "good", 5), init_dbt(&v), 0); assert(r==0); assert(v.size==6); assert(strcmp(v.data,"grief")==0); r = brt_lookup(t1, fill_dbt(&k, "good", 5), init_dbt(&v), 0); assert(r!=0); r = brt_lookup(t0, fill_dbt(&k, "bad", 4), init_dbt(&v), 0); assert(r!=0); r = brt_lookup(t1, fill_dbt(&k, "bad", 4), init_dbt(&v), 0); assert(r==0); assert(v.size==6); assert(strcmp(v.data,"night")==0); } r = close_brt(t0); assert(r==0); r = close_brt(t1); assert(r==0); r = cachetable_close(&ct); assert(r==0); memory_check_all_free(); printf("ok\n"); } /* Test to see a single file can contain many databases. */ static void test_multiple_dbs_many (void) { enum { MANYN = 16 }; int i, r; const char *name = "test.brt"; CACHETABLE ct; BRT trees[MANYN]; printf("test_multiple_dbs_many:\n"); memory_check_all_free(); unlink(name); r = brt_create_cachetable(&ct, MANYN+4); assert(r==0); for (i=0; iapp_private = &nonce; return x; } int wrong_compare_fun(DB *db, const DBT *a, const DBT *b) { unsigned int i; unsigned char *ad=a->data; unsigned char *bd=b->data; unsigned int siz=a->size; assert(a->size==b->size); assert(a->app_private == &nonce); // a must have the nonce in it, but I don't care if b does. 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) { const char *n="testbrt.brt"; CACHETABLE ct; BRT brt; BRT_CURSOR cursor; int r; DBT kbt, vbt; unsigned int i; unlink(n); 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, fill_dbt_ap(&at, a, 4, &nonce), fill_dbt(&bt, b, 4))>0); assert(wrong_compare_fun(&nonce_db, fill_dbt_ap(&at, b, 4, &nonce), fill_dbt(&bt, a, 4))<0); } r = brt_create_cachetable(&ct, 0); assert(r==0); printf("%s:%d WRONG=%d\n", __FILE__, __LINE__, wrong_p); if (0) { r = open_brt(n, 0, 1, &brt, 1<<20, ct, wrong_p ? wrong_compare_fun : default_compare_fun); assert(r==0); for (i=1; i<257; i+=255) { unsigned char a[4],b[4]; b[3] = a[0] = i&255; b[2] = a[1] = (i>>8)&255; b[1] = a[2] = (i>>16)&255; b[0] = a[3] = (i>>24)&255; fill_b(&kbt, a, sizeof(a)); fill_dbt(&vbt, b, sizeof(b)); 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 = brt_insert(brt, &kbt, &vbt, &nonce_db); assert(r==0); } r = brt_cursor(brt, &cursor); assert(r==0); for (i=0; i<2; i++) { init_dbt(&kbt); init_dbt(&vbt); r = brt_cursor_get(cursor, &kbt, &vbt, DB_NEXT, 0); assert(r==0); assert(kbt.size==4 && vbt.size==4); printf("%s:%d %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 = close_brt(brt); } if (1) { r = open_brt(n, 0, 1, &brt, 1<<20, ct, wrong_p ? wrong_compare_fun : default_compare_fun); assert(r==0); for (i=0; i>8)&255; b[1] = a[2] = (i>>16)&255; b[0] = a[3] = (i>>24)&255; fill_b(&kbt, a, sizeof(a)); fill_dbt(&vbt, b, sizeof(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 = brt_insert(brt, &kbt, &vbt, &nonce_db); assert(r==0); } r = brt_cursor(brt, &cursor); assert(r==0); int prev=-1; for (i=0; i %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]); this= ( (((unsigned char*)kbt.data)[3] << 24) + (((unsigned char*)kbt.data)[2] << 16) + (((unsigned char*)kbt.data)[1] << 8) + (((unsigned char*)kbt.data)[0] << 0)); assert(prev=0; i--) { char key[8]; long long v; DBT kbt, vbt; snprintf(key, sizeof key, "%4.4d", i); fill_dbt(&kbt, key, strlen(key)+1); v = i; fill_dbt(&vbt, &v, sizeof v); r = brt_insert(brt, &kbt, &vbt, 0); assert(r==0); } if (n == 0) assert_cursor_notfound(brt, DB_FIRST); else assert_cursor_value(brt, DB_FIRST, 0); r = close_brt(brt); assert(r==0); r = cachetable_close(&ct); assert(r==0); } void assert_cursor_walk(BRT brt, int n) { BRT_CURSOR cursor; int i; int r; r = brt_cursor(brt, &cursor); assert(r==0); if (test_cursor_debug) printf("key: "); for (i=0; ; i++) { DBT kbt, vbt; long long v; init_dbt(&kbt); kbt.flags = DB_DBT_MALLOC; init_dbt(&vbt); vbt.flags = DB_DBT_MALLOC; r = brt_cursor_get(cursor, &kbt, &vbt, DB_NEXT, 0); if (r != 0) break; if (test_cursor_debug) printf("%s ", (char*)kbt.data); assert(vbt.size == sizeof v); memcpy(&v, vbt.data, vbt.size); assert(v == i); toku_free(kbt.data); toku_free(vbt.data); } if (test_cursor_debug) printf("\n"); assert(i == n); r = brt_cursor_close(cursor); assert(r==0); } void test_brt_cursor_walk(int n) { const char *fname="testbrt.brt"; CACHETABLE ct; BRT brt; int r; int i; printf("test_brt_cursor_walk:%d\n", n); unlink(fname); r = brt_create_cachetable(&ct, 0); assert(r==0); r = open_brt(fname, 0, 1, &brt, 1<<12, ct, default_compare_fun); assert(r==0); /* insert a bunch of kv pairs */ 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); else { assert(r == 0); assert(val.size == sizeof vv); memcpy(&vv, val.data, val.size); assert(vv == (((v+9)/10)*10)); toku_free(val.data); } } r = brt_cursor_close(cursor); assert(r==0); r = close_brt(brt); assert(r==0); r = cachetable_close(&ct); assert(r==0); } void test_brt_cursor_delete(int n) { printf("test_brt_cursor_delete:%d\n", n); int error; char fname[]="testbrt.brt"; CACHETABLE ct; BRT brt; BRT_CURSOR cursor; unlink(fname); error = brt_create_cachetable(&ct, 0); assert(error == 0); error = open_brt(fname, 0, 1, &brt, 1<<12, ct, default_compare_fun); assert(error == 0); error = brt_cursor(brt, &cursor); assert(error == 0); DBT key, val; int k, v; int i; /* insert keys 0, 1, 2, .. (n-1) */ for (i=0; i