Commit 0b8355de authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul

pma_cursor_next almost works

git-svn-id: file:///svn/tokudb@20 c7de825b-a66e-492c-adef-691d508d4ae1
parent 44c89c28
...@@ -634,7 +634,56 @@ void test_cursor_last_empty(void) { ...@@ -634,7 +634,56 @@ void test_cursor_last_empty(void) {
memory_check_all_free(); memory_check_all_free();
} }
void test_cursor_next (void) {
const char *n="testbrt.brt";
CACHETABLE ct;
BRT brt;
BRT_CURSOR cursor;
int r;
DBT kbt, vbt;
unlink(n);
memory_check_all_free();
r = brt_create_cachetable(&ct, 0); assert(r==0);
//printf("%s:%d %d alloced\n", __FILE__, __LINE__, get_n_items_malloced()); print_malloced_items();
r = open_brt(n, 0, 1, &brt, 1<<12, ct); assert(r==0);
//printf("%s:%d %d alloced\n", __FILE__, __LINE__, get_n_items_malloced()); print_malloced_items();
r = brt_insert(brt, "hello", 6, "there", 6);
r = brt_insert(brt, "byebye", 7, "byenow", 7);
r = brt_cursor(brt, &cursor); assert(r==0);
r = ybt_init(&kbt); assert(r==0);
//printf("%s:%d %d alloced\n", __FILE__, __LINE__, get_n_items_malloced()); print_malloced_items();
r = ybt_init(&vbt); assert(r==0);
//printf("%s:%d %d alloced\n", __FILE__, __LINE__, get_n_items_malloced()); print_malloced_items();
r = brt_c_get(cursor, &kbt, &vbt, DB_NEXT);
//printf("%s:%d %d alloced\n", __FILE__, __LINE__, get_n_items_malloced()); print_malloced_items();
assert(r==0);
assert(kbt.size==7);
assert(memcmp(kbt.data, "byebye", 7)==0);
assert(vbt.size==7);
assert(memcmp(vbt.data, "byenow", 7)==0);
r = brt_c_get(cursor, &kbt, &vbt, DB_NEXT);
assert(r==0);
assert(kbt.size==6);
assert(memcmp(kbt.data, "hello", 6)==0);
assert(vbt.size==6);
assert(memcmp(vbt.data, "there", 6)==0);
r = brt_c_get(cursor, &kbt, &vbt, DB_NEXT);
assert(r==DB_NOTFOUND);
r = close_brt(brt);
//printf("%s:%d %d alloced\n", __FILE__, __LINE__, get_n_items_malloced()); print_malloced_items();
r = cachetable_close(ct); assert(r==0);
//printf("%s:%d %d alloced\n", __FILE__, __LINE__, get_n_items_malloced()); print_malloced_items();
memory_check_all_free();
}
static void brt_blackbox_test (void) { static void brt_blackbox_test (void) {
test_cursor_next(); memory_check_all_free();
test_multiple_dbs_many(); memory_check_all_free(); test_multiple_dbs_many(); memory_check_all_free();
test_cursor_last_empty(); memory_check_all_free(); test_cursor_last_empty(); memory_check_all_free();
test_multiple_brts_one_db_one_file(); memory_check_all_free(); test_multiple_brts_one_db_one_file(); memory_check_all_free();
......
...@@ -1496,6 +1496,17 @@ int brtcurs_set_position_first (BRT_CURSOR cursor, diskoff off) { ...@@ -1496,6 +1496,17 @@ int brtcurs_set_position_first (BRT_CURSOR cursor, diskoff off) {
} }
} }
/* reuqires that the cursor is initialized. */
int brtcurs_set_position_next (BRT_CURSOR cursor) {
int r = pma_cursor_set_position_next(cursor->pmacurs);
if (r==DB_NOTFOUND) {
/* We fell off the end of the pma. */
fprintf(stderr, "Need to deal with falling off the end of the pma in a cursor\n");
abort();
}
return 0;
}
static int unpin_cursor (BRT_CURSOR cursor) { static int unpin_cursor (BRT_CURSOR cursor) {
BRT brt=cursor->brt; BRT brt=cursor->brt;
int i; int i;
...@@ -1535,6 +1546,12 @@ int brt_c_get (BRT_CURSOR cursor, DBT *kbt, DBT *vbt, int flags) { ...@@ -1535,6 +1546,12 @@ int brt_c_get (BRT_CURSOR cursor, DBT *kbt, DBT *vbt, int flags) {
r=brtcurs_set_position_first(cursor, *rootp); if (r!=0) goto died0; r=brtcurs_set_position_first(cursor, *rootp); if (r!=0) goto died0;
r=pma_cget_current(cursor->pmacurs, kbt, vbt); r=pma_cget_current(cursor->pmacurs, kbt, vbt);
break; break;
case DB_NEXT:
if (cursor->path_len<0) return brt_c_get(cursor, kbt, vbt, (flags&(~DB_NEXT))|DB_FIRST);
assert(cursor->path_len>0);
r=brtcurs_set_position_next(cursor); if (r!=0) goto died0;
r=pma_cget_current(cursor->pmacurs, kbt, vbt);
break;
default: default:
fprintf(stderr, "%s:%d c_get(...,%d) not ready\n", __FILE__, __LINE__, flags); fprintf(stderr, "%s:%d c_get(...,%d) not ready\n", __FILE__, __LINE__, flags);
abort(); abort();
......
...@@ -417,10 +417,54 @@ void test_pma_cursor_2 (void) { ...@@ -417,10 +417,54 @@ void test_pma_cursor_2 (void) {
r=pma_free(&pma); assert(r==0); r=pma_free(&pma); assert(r==0);
} }
void test_pma_cursor_3 (void) {
PMA pma;
PMA_CURSOR c=0;
int r;
DBT key,val;
r=pma_create(&pma); assert(r==0);
r=pma_insert(pma, "x", 2, "xx", 3); assert(r==BRT_OK);
r=pma_insert(pma, "m", 2, "mm", 3); assert(r==BRT_OK);
r=pma_insert(pma, "aa", 3, "a", 2); assert(r==BRT_OK);
ybt_init(&key); key.flags=DB_DBT_REALLOC;
ybt_init(&val); val.flags=DB_DBT_REALLOC;
r=pma_cursor(pma, &c); assert(r==0); assert(c!=0);
r=pma_cursor_set_position_first(c); assert(r==0);
r=pma_cget_current(c, &key, &val); assert(r==0);
assert(key.size=3); assert(memcmp(key.data,"aa",3)==0);
assert(val.size=2); assert(memcmp(val.data,"a",2)==0);
r=pma_cursor_set_position_next(c); assert(r==0);
r=pma_cget_current(c, &key, &val); assert(r==0);
assert(key.size=2); assert(memcmp(key.data,"m",2)==0);
assert(val.size=3); assert(memcmp(val.data,"mm",3)==0);
r=pma_cursor_set_position_next(c); assert(r==0);
r=pma_cget_current(c, &key, &val); assert(r==0);
assert(key.size=2); assert(memcmp(key.data,"x",2)==0);
assert(val.size=3); assert(memcmp(val.data,"xx",3)==0);
r=pma_cursor_set_position_next(c); assert(r==DB_NOTFOUND);
/* After an error, the cursor should still point at the same thing. */
r=pma_cget_current(c, &key, &val); assert(r==0);
assert(key.size=2); assert(memcmp(key.data,"x",2)==0);
assert(val.size=3); assert(memcmp(val.data,"xx",3)==0);
r=pma_cursor_set_position_next(c); assert(r==DB_NOTFOUND);
r=pma_cursor_free(&c); assert(r==0);
r=pma_free(&pma); assert(r==0);
}
void test_pma_cursor (void) { void test_pma_cursor (void) {
test_pma_cursor_0(); test_pma_cursor_0();
test_pma_cursor_1(); test_pma_cursor_1();
test_pma_cursor_2(); test_pma_cursor_2();
test_pma_cursor_3();
} }
void pma_tests (void) { void pma_tests (void) {
......
...@@ -330,6 +330,19 @@ int pma_cursor_set_position_first (PMA_CURSOR c) ...@@ -330,6 +330,19 @@ int pma_cursor_set_position_first (PMA_CURSOR c)
return 0; return 0;
} }
int pma_cursor_set_position_next (PMA_CURSOR c)
{
PMA pma = c->pma;
int old_position=c->position;
c->position++;
while (c->position<pma->N) {
if (c->pma->pairs[c->position].key!=0) return 0;
c->position++;
}
c->position=old_position;
return DB_NOTFOUND;
}
int pma_cget_current (PMA_CURSOR c, DBT *key, DBT *val) { int pma_cget_current (PMA_CURSOR c, DBT *key, DBT *val) {
PMA pma = c->pma; PMA pma = c->pma;
if (pma->pairs[c->position].key==0) return BRT_KEYEMPTY; if (pma->pairs[c->position].key==0) return BRT_KEYEMPTY;
......
...@@ -40,6 +40,7 @@ int pma_cursor_free (PMA_CURSOR*); ...@@ -40,6 +40,7 @@ int pma_cursor_free (PMA_CURSOR*);
int pma_cursor_set_position_last (PMA_CURSOR c); int pma_cursor_set_position_last (PMA_CURSOR c);
int pma_cursor_set_position_first (PMA_CURSOR c); int pma_cursor_set_position_first (PMA_CURSOR c);
int pma_cursor_set_position_next (PMA_CURSOR c); /* Requires the cursor is init'd. Returns DB_NOTFOUND if we fall off the end. */
int pma_cget_current (PMA_CURSOR c, DBT *key, DBT *val); int pma_cget_current (PMA_CURSOR c, DBT *key, DBT *val);
/* Return PMA_NOTFOUND if the pma is empty. */ /* Return PMA_NOTFOUND if the pma is empty. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment