/* -*- mode: C; c-basic-offset: 4 -*- */ #ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved." #include "includes.h" #include "test.h" static const char fname[]= __FILE__ ".brt"; static TOKUTXN const null_txn = 0; static DB * const null_db = 0; static int test_cursor_debug = 0; 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 assert_cursor_notfound(BRT brt, int position) { BRT_CURSOR cursor=0; int r; r = toku_brt_cursor(brt, &cursor); assert(r==0); struct check_pair pair = {0,0,0,0,0}; r = toku_brt_cursor_get(cursor, NULL, NULL, lookup_checkf, &pair, position, null_txn); assert(r == DB_NOTFOUND); assert(pair.call_count==0); r = toku_brt_cursor_close(cursor); assert(r==0); } static void assert_cursor_value(BRT brt, int position, long long value) { BRT_CURSOR cursor=0; int r; r = toku_brt_cursor(brt, &cursor); assert(r==0); if (test_cursor_debug && verbose) printf("key: "); struct check_pair pair = {len_ignore, 0, sizeof(value), &value, 0}; r = toku_brt_cursor_get(cursor, NULL, NULL, lookup_checkf, &pair, position, null_txn); assert(r == 0); assert(pair.call_count==1); r = toku_brt_cursor_close(cursor); assert(r==0); } static void assert_cursor_first_last(BRT brt, long long firstv, long long lastv) { BRT_CURSOR cursor=0; int r; r = toku_brt_cursor(brt, &cursor); assert(r==0); if (test_cursor_debug && verbose) printf("first key: "); { struct check_pair pair = {len_ignore, 0, sizeof(firstv), &firstv, 0}; r = toku_brt_cursor_get(cursor, NULL, NULL, lookup_checkf, &pair, DB_FIRST, null_txn); assert(r == 0); assert(pair.call_count==1); } if (test_cursor_debug && verbose) printf("last key:"); { struct check_pair pair = {len_ignore, 0, sizeof(lastv), &lastv, 0}; r = toku_brt_cursor_get(cursor, NULL, NULL, lookup_checkf, &pair, DB_LAST, null_txn); assert(r == 0); assert(pair.call_count==1); } if (test_cursor_debug && verbose) printf("\n"); r = toku_brt_cursor_close(cursor); assert(r==0); } static void test_brt_cursor_first(int n, DB *db) { CACHETABLE ct; BRT brt; int r; int i; if (verbose) printf("test_brt_cursor_first:%d %p\n", n, db); 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); /* insert a bunch of kv pairs */ for (i=0; i=0; i--) { char key[8]; long long v; DBT kbt, vbt; snprintf(key, sizeof key, "%4.4d", i); toku_fill_dbt(&kbt, key, strlen(key)+1); v = i; toku_fill_dbt(&vbt, &v, sizeof v); r = toku_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 = toku_close_brt(brt, 0, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); } static void assert_cursor_walk(BRT brt, int n) { BRT_CURSOR cursor=0; int i; int r; r = toku_brt_cursor(brt, &cursor); assert(r==0); if (test_cursor_debug && verbose) printf("key: "); for (i=0; ; i++) { long long v = i; struct check_pair pair = {len_ignore, 0, sizeof(v), &v, 0}; r = toku_brt_cursor_get(cursor, NULL, NULL, lookup_checkf, &pair, DB_NEXT, null_txn); if (r != 0) { assert(pair.call_count==0); break; } assert(pair.call_count==1); } if (test_cursor_debug && verbose) printf("\n"); assert(i == n); r = toku_brt_cursor_close(cursor); assert(r==0); } static void test_brt_cursor_walk(int n, DB *db) { CACHETABLE ct; BRT brt; int r; int i; if (verbose) printf("test_brt_cursor_walk:%d %p\n", n, db); 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); /* 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); 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, 0); assert(r==0); r = toku_cachetable_close(&ct); assert(r==0); } static void test_brt_cursor_delete(int n, DB *db) { if (verbose) printf("test_brt_cursor_delete:%d %p\n", n, db); int error; CACHETABLE ct; BRT brt; BRT_CURSOR cursor=0; unlink(fname); error = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(error == 0); error = toku_open_brt(fname, 1, &brt, 1<<12, ct, null_txn, test_brt_cursor_keycompare, db); assert(error == 0); error = toku_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