Commit d9842830 authored by Rich Prohaska's avatar Rich Prohaska

implement and test cursor->get_both_range. addresses #187

git-svn-id: file:///svn/tokudb@1152 c7de825b-a66e-492c-adef-691d508d4ae1
parent 9290db6f
......@@ -76,6 +76,7 @@ typedef enum {
#define DB_DELETE_ANY 65536
#define DB_FIRST 10
#define DB_GET_BOTH 11
#define DB_GET_BOTH_RANGE 13
#define DB_LAST 18
#define DB_CURRENT 8
#define DB_NEXT 19
......
......@@ -77,6 +77,7 @@ typedef enum {
#define DB_DELETE_ANY 65536
#define DB_FIRST 9
#define DB_GET_BOTH 10
#define DB_GET_BOTH_RANGE 12
#define DB_LAST 17
#define DB_CURRENT 7
#define DB_NEXT 18
......
......@@ -77,6 +77,7 @@ typedef enum {
#define DB_DELETE_ANY 65536
#define DB_FIRST 9
#define DB_GET_BOTH 10
#define DB_GET_BOTH_RANGE 12
#define DB_LAST 17
#define DB_CURRENT 7
#define DB_NEXT 18
......
......@@ -77,6 +77,7 @@ typedef enum {
#define DB_DELETE_ANY 65536
#define DB_FIRST 7
#define DB_GET_BOTH 8
#define DB_GET_BOTH_RANGE 10
#define DB_LAST 15
#define DB_CURRENT 6
#define DB_NEXT 16
......
......@@ -77,6 +77,7 @@ typedef enum {
#define DB_DELETE_ANY 65536
#define DB_FIRST 7
#define DB_GET_BOTH 8
#define DB_GET_BOTH_RANGE 10
#define DB_LAST 15
#define DB_CURRENT 6
#define DB_NEXT 16
......
......@@ -86,6 +86,7 @@ void print_defines (void) {
dodefine(DB_FIRST);
dodefine(DB_GET_BOTH);
dodefine(DB_GET_BOTH_RANGE);
dodefine(DB_LAST);
dodefine(DB_CURRENT);
dodefine(DB_NEXT);
......
......@@ -77,6 +77,7 @@ typedef enum {
#define DB_DELETE_ANY 65536
#define DB_FIRST 9
#define DB_GET_BOTH 10
#define DB_GET_BOTH_RANGE 12
#define DB_LAST 17
#define DB_CURRENT 7
#define DB_NEXT 18
......
......@@ -2722,7 +2722,7 @@ static int brtcurs_set_search(BRT_CURSOR cursor, DISKOFF off, int op, DBT *key,
if (r == 0) {
if (op == DB_SET || op == DB_GET_BOTH)
r = toku_pma_cursor_set_both(cursor->pmacurs, key, val);
else if (op == DB_SET_RANGE /* || op == DB_GET_BOTH_RANGE */ )
else if (op == DB_SET_RANGE || op == DB_GET_BOTH_RANGE)
r = toku_pma_cursor_set_range_both(cursor->pmacurs, key, val);
else
assert(0);
......@@ -2899,6 +2899,13 @@ int toku_brt_cursor_get (BRT_CURSOR cursor, DBT *kbt, DBT *vbt, int flags, TOKUT
r = toku_pma_cursor_get_current(cursor->pmacurs, kbt, vbt, 0);
if (r != 0) goto died0;
break;
case DB_GET_BOTH_RANGE:
r = unpin_cursor(cursor); assert(r == 0);
r = brtcurs_set_search(cursor, *rootp, DB_GET_BOTH_RANGE, kbt, vbt, txn, null_brtnode);
if (r != 0) goto died0;
r = toku_pma_cursor_get_current(cursor->pmacurs, kbt, vbt, 0);
if (r != 0) goto died0;
break;
default:
fprintf(stderr, "%s:%d c_get(...,%d) not ready\n", __FILE__, __LINE__, flags);
abort();
......
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <db.h>
#include "test.h"
void db_put(DB *db, int k, int v) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->put(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
assert(r == 0);
}
void db_get(DB *db, int k) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
assert(r == 0);
int vv;
assert(val.size == sizeof vv);
memcpy(&vv, val.data, val.size);
printf("do_search %d\n", htonl(vv));
free(val.data);
}
void db_del(DB *db, int k) {
DB_TXN * const null_txn = 0;
DBT key;
int r = db->del(db, null_txn, dbt_init(&key, &k, sizeof k), 0);
assert(r == 0);
}
void expect_db_get(DB *db, int k, int v) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
assert(r == 0);
int vv;
assert(val.size == sizeof vv);
memcpy(&vv, val.data, val.size);
assert(vv == v);
free(val.data);
}
void expect_cursor_get(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT);
assert(r == 0);
assert(key.size == sizeof k);
int kk;
memcpy(&kk, key.data, key.size);
assert(val.size == sizeof v);
int vv;
memcpy(&vv, val.data, val.size);
if (kk != k || vv != v) printf("expect key %d got %d - %d %d\n", htonl(k), htonl(kk), htonl(v), htonl(vv));
assert(kk == k);
assert(vv == v);
free(key.data);
free(val.data);
}
void expect_cursor_set(DBC *cursor, int k) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), DB_SET);
assert(r == 0);
free(val.data);
}
void expect_cursor_get_both_range(DBC *cursor, int k, int v, int expectr) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), DB_GET_BOTH_RANGE);
assert(r == expectr);
}
void expect_cursor_get_current(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_CURRENT);
assert(r == 0);
int kk, vv;
assert(key.size == sizeof kk); memcpy(&kk, key.data, key.size); assert(kk == k);
assert(val.size == sizeof vv); memcpy(&vv, val.data, val.size); assert(vv == v);
free(key.data); free(val.data);
}
/* insert, close, delete, insert, search */
void test_icdi_search(int n, int dup_mode) {
if (verbose) printf("test_icdi_search:%d %d\n", n, dup_mode);
DB_ENV * const null_env = 0;
DB *db;
DB_TXN * const null_txn = 0;
const char * const fname = DIR "/" "test_icdi_search.brt";
int r;
unlink(fname);
/* create the dup database file */
r = db_create(&db, null_env, 0); assert(r == 0);
r = db->set_flags(db, dup_mode); assert(r == 0);
r = db->set_pagesize(db, 4096); assert(r == 0);
r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666); assert(r == 0);
/* insert n duplicates */
int i;
for (i=0; i<n; i++) {
int k = htonl(n/2);
int v = htonl(i);
db_put(db, k, v);
expect_db_get(db, k, htonl(0));
}
/* reopen the database to force nonleaf buffering */
r = db->close(db, 0); assert(r == 0);
r = db_create(&db, null_env, 0); assert(r == 0);
r = db->set_flags(db, dup_mode); assert(r == 0);
r = db->set_pagesize(db, 4096); assert(r == 0);
r = db->open(db, null_txn, fname, "main", DB_BTREE, 0, 0666); assert(r == 0);
db_del(db, htonl(n/2));
/* insert n duplicates */
for (i=n-1; i>=0; i--) {
int k = htonl(n/2);
int v = htonl(n+i);
db_put(db, k, v);
DBC *cursor;
r = db->cursor(db, null_txn, &cursor, 0); assert(r == 0);
expect_cursor_get_both_range(cursor, k, v, 0);
expect_cursor_get_current(cursor, k, v);
expect_cursor_get_both_range(cursor, 0, 0, 0);
expect_cursor_get_current(cursor, k, v);
r = cursor->c_close(cursor); assert(r == 0);
}
DBC *cursor;
r = db->cursor(db, null_txn, &cursor, 0); assert(r == 0);
expect_cursor_get_both_range(cursor, 0, 0, 0);
expect_cursor_get_both_range(cursor, htonl(n), 0, DB_NOTFOUND);
for (i=0; i<n; i++) {
expect_cursor_get(cursor, htonl(n/2), htonl(n+i));
}
r = cursor->c_close(cursor); assert(r == 0);
r = db->close(db, 0); assert(r == 0);
}
int main(int argc, const char *argv[]) {
int i;
parse_args(argc, argv);
system("rm -rf " DIR);
mkdir(DIR, 0777);
for (i=1; i<65537; i *= 2) {
test_icdi_search(i, DB_DUP + DB_DUPSORT);
}
return 0;
}
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <db.h>
#include "test.h"
void db_put(DB *db, int k, int v) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->put(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
assert(r == 0);
}
void db_get(DB *db, int k) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
assert(r == 0);
int vv;
assert(val.size == sizeof vv);
memcpy(&vv, val.data, val.size);
printf("do_search %d\n", htonl(vv));
free(val.data);
}
void db_del(DB *db, int k) {
DB_TXN * const null_txn = 0;
DBT key;
int r = db->del(db, null_txn, dbt_init(&key, &k, sizeof k), 0);
assert(r == 0);
}
void expect_db_get(DB *db, int k, int v) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
assert(r == 0);
int vv;
assert(val.size == sizeof vv);
memcpy(&vv, val.data, val.size);
assert(vv == v);
free(val.data);
}
void expect_cursor_get(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT);
assert(r == 0);
assert(key.size == sizeof k);
int kk;
memcpy(&kk, key.data, key.size);
assert(val.size == sizeof v);
int vv;
memcpy(&vv, val.data, val.size);
if (kk != k || vv != v) printf("expect key %d got %d - %d %d\n", htonl(k), htonl(kk), htonl(v), htonl(vv));
assert(kk == k);
assert(vv == v);
free(key.data);
free(val.data);
}
void expect_cursor_set(DBC *cursor, int k) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), DB_SET);
assert(r == 0);
free(val.data);
}
void expect_cursor_get_both(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), DB_GET_BOTH);
assert(r == 0);
}
void expect_cursor_get_current(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_CURRENT);
assert(r == 0);
int kk, vv;
assert(key.size == sizeof kk); memcpy(&kk, key.data, key.size); assert(kk == k);
assert(val.size == sizeof vv); memcpy(&vv, val.data, val.size); assert(vv == v);
free(key.data); free(val.data);
}
/* insert, close, delete, insert, search */
void test_icdi_search(int n, int dup_mode) {
if (verbose) printf("test_icdi_search:%d %d\n", n, dup_mode);
DB_ENV * const null_env = 0;
DB *db;
DB_TXN * const null_txn = 0;
const char * const fname = DIR "/" "test_icdi_search.brt";
int r;
unlink(fname);
/* create the dup database file */
r = db_create(&db, null_env, 0); assert(r == 0);
r = db->set_flags(db, dup_mode); assert(r == 0);
r = db->set_pagesize(db, 4096); assert(r == 0);
r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666); assert(r == 0);
/* insert n duplicates */
int i;
for (i=0; i<n; i++) {
int k = htonl(n/2);
int v = htonl(i);
db_put(db, k, v);
expect_db_get(db, k, htonl(0));
}
/* reopen the database to force nonleaf buffering */
r = db->close(db, 0); assert(r == 0);
r = db_create(&db, null_env, 0); assert(r == 0);
r = db->set_flags(db, dup_mode); assert(r == 0);
r = db->set_pagesize(db, 4096); assert(r == 0);
r = db->open(db, null_txn, fname, "main", DB_BTREE, 0, 0666); assert(r == 0);
db_del(db, htonl(n/2));
/* insert n duplicates */
for (i=0; i<n; i++) {
int k = htonl(n/2);
int v = htonl(n+i);
db_put(db, k, v);
DBC *cursor;
r = db->cursor(db, 0, &cursor, 0); assert(r == 0);
expect_cursor_set(cursor, k);
expect_cursor_get_current(cursor, k, htonl(n+0));
r = cursor->c_close(cursor); assert(r == 0);
}
DBC *cursor;
r = db->cursor(db, null_txn, &cursor, 0); assert(r == 0);
for (i=0; i<n; i++) {
expect_cursor_get(cursor, htonl(n/2), htonl(n+i));
}
r = cursor->c_close(cursor); assert(r == 0);
r = db->close(db, 0); assert(r == 0);
}
int main(int argc, const char *argv[]) {
int i;
parse_args(argc, argv);
system("rm -rf " DIR);
mkdir(DIR, 0777);
for (i=1; i<65537; i *= 2) {
test_icdi_search(i, DB_DUP + DB_DUPSORT);
}
return 0;
}
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <db.h>
#include "test.h"
void db_put(DB *db, int k, int v) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->put(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
assert(r == 0);
}
void db_get(DB *db, int k) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
assert(r == 0);
int vv;
assert(val.size == sizeof vv);
memcpy(&vv, val.data, val.size);
printf("do_search %d\n", htonl(vv));
free(val.data);
}
void db_del(DB *db, int k) {
DB_TXN * const null_txn = 0;
DBT key;
int r = db->del(db, null_txn, dbt_init(&key, &k, sizeof k), 0);
assert(r == 0);
}
void expect_db_get(DB *db, int k, int v) {
DB_TXN * const null_txn = 0;
DBT key, val;
int r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
assert(r == 0);
int vv;
assert(val.size == sizeof vv);
memcpy(&vv, val.data, val.size);
assert(vv == v);
free(val.data);
}
void expect_cursor_get(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT);
assert(r == 0);
assert(key.size == sizeof k);
int kk;
memcpy(&kk, key.data, key.size);
assert(val.size == sizeof v);
int vv;
memcpy(&vv, val.data, val.size);
if (kk != k || vv != v) printf("expect key %d got %d - %d %d\n", htonl(k), htonl(kk), htonl(v), htonl(vv));
assert(kk == k);
assert(vv == v);
free(key.data);
free(val.data);
}
void expect_cursor_set(DBC *cursor, int k) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), DB_SET);
assert(r == 0);
free(val.data);
}
void expect_cursor_set_range(DBC *cursor, int k) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), DB_SET_RANGE);
assert(r == 0);
free(val.data);
}
void expect_cursor_get_both(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), DB_GET_BOTH);
assert(r == 0);
}
void expect_cursor_get_current(DBC *cursor, int k, int v) {
DBT key, val;
int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_CURRENT);
assert(r == 0);
int kk, vv;
assert(key.size == sizeof kk); memcpy(&kk, key.data, key.size); assert(kk == k);
assert(val.size == sizeof vv); memcpy(&vv, val.data, val.size); assert(vv == v);
free(key.data); free(val.data);
}
/* insert, close, delete, insert, search */
void test_icdi_search(int n, int dup_mode) {
if (verbose) printf("test_icdi_search:%d %d\n", n, dup_mode);
DB_ENV * const null_env = 0;
DB *db;
DB_TXN * const null_txn = 0;
const char * const fname = DIR "/" "test_icdi_search.brt";
int r;
unlink(fname);
/* create the dup database file */
r = db_create(&db, null_env, 0); assert(r == 0);
r = db->set_flags(db, dup_mode); assert(r == 0);
r = db->set_pagesize(db, 4096); assert(r == 0);
r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666); assert(r == 0);
/* insert n duplicates */
int i;
for (i=0; i<n; i++) {
int k = htonl(n/2);
int v = htonl(i);
db_put(db, k, v);
expect_db_get(db, k, htonl(0));
}
/* reopen the database to force nonleaf buffering */
r = db->close(db, 0); assert(r == 0);
r = db_create(&db, null_env, 0); assert(r == 0);
r = db->set_flags(db, dup_mode); assert(r == 0);
r = db->set_pagesize(db, 4096); assert(r == 0);
r = db->open(db, null_txn, fname, "main", DB_BTREE, 0, 0666); assert(r == 0);
db_del(db, htonl(n/2));
/* insert n duplicates */
for (i=n-1; i >= 0; i--) {
int k = htonl(n/2);
int v = htonl(n+i);
db_put(db, k, v);
DBC *cursor;
r = db->cursor(db, 0, &cursor, 0); assert(r == 0);
expect_cursor_set_range(cursor, k);
expect_cursor_get_current(cursor, k, htonl(n+i));
r = cursor->c_close(cursor); assert(r == 0);
}
DBC *cursor;
r = db->cursor(db, null_txn, &cursor, 0); assert(r == 0);
for (i=0; i<n; i++) {
expect_cursor_get(cursor, htonl(n/2), htonl(n+i));
}
r = cursor->c_close(cursor); assert(r == 0);
r = db->close(db, 0); assert(r == 0);
}
int main(int argc, const char *argv[]) {
int i;
parse_args(argc, argv);
system("rm -rf " DIR);
mkdir(DIR, 0777);
for (i=1; i<65537; i *= 2) {
test_icdi_search(i, DB_DUP + DB_DUPSORT);
}
return 0;
}
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