Commit 6a74f0aa authored by Yoni Fogel's avatar Yoni Fogel

Refs Tokutek/ft-engine/#40 Add c_restrict_to_range function to force cursor to...

Refs Tokutek/ft-engine/#40 Add c_restrict_to_range function to force cursor to keep track of prelocked range
parent bcf33e44
......@@ -205,6 +205,7 @@ enum {
TOKUDB_CURSOR_CONTINUE = -100014,
TOKUDB_BAD_CHECKSUM = -100015,
TOKUDB_HUGE_PAGES_ENABLED = -100016,
TOKUDB_OUT_OF_RANGE = -100017,
DONTUSE_I_JUST_PUT_THIS_HERE_SO_I_COULD_HAVE_A_COMMA_AFTER_EACH_ITEM
};
......@@ -356,6 +357,7 @@ static void print_defines (void) {
dodefine(TOKUDB_CURSOR_CONTINUE);
dodefine(TOKUDB_BAD_CHECKSUM);
dodefine(TOKUDB_HUGE_PAGES_ENABLED);
dodefine(TOKUDB_OUT_OF_RANGE);
/* LOADER flags */
printf("/* LOADER flags */\n");
......@@ -591,6 +593,8 @@ static void print_dbc_struct (void) {
"int (*c_getf_set_range)(DBC *, uint32_t, DBT *, YDB_CALLBACK_FUNCTION, void *)",
"int (*c_getf_set_range_reverse)(DBC *, uint32_t, DBT *, YDB_CALLBACK_FUNCTION, void *)",
"int (*c_pre_acquire_range_lock)(DBC*, const DBT*, const DBT*)",
"int (*c_restrict_to_range)(DBC*, const DBT*, const DBT*, bool pre_acquire, int out_of_range_error)",
"void (*c_remove_restriction)(DBC*)",
NULL};
sort_and_dump_fields("dbc", false, extra);
}
......
......@@ -795,14 +795,15 @@ struct ft_cursor_leaf_info {
struct ft_cursor {
struct toku_list cursors_link;
FT_HANDLE ft_handle;
bool prefetching;
DBT key, val; // The key-value pair that the cursor currently points to
DBT range_lock_left_key, range_lock_right_key;
bool prefetching;
bool left_is_neg_infty, right_is_pos_infty;
bool is_snapshot_read; // true if query is read_committed, false otherwise
bool is_leaf_mode;
bool disable_prefetching;
bool is_temporary;
int out_of_range_error;
TOKUTXN ttxn;
struct ft_cursor_leaf_info leaf_info;
};
......
......@@ -4100,6 +4100,10 @@ int toku_ft_cursor (
return 0;
}
void toku_ft_cursor_remove_restriction(FT_CURSOR ftcursor) {
ftcursor->out_of_range_error = 0;
}
void
toku_ft_cursor_set_temporary(FT_CURSOR ftcursor) {
ftcursor->is_temporary = true;
......@@ -4117,7 +4121,8 @@ toku_ft_cursor_is_leaf_mode(FT_CURSOR ftcursor) {
void
toku_ft_cursor_set_range_lock(FT_CURSOR cursor, const DBT *left, const DBT *right,
bool left_is_neg_infty, bool right_is_pos_infty)
bool left_is_neg_infty, bool right_is_pos_infty,
int out_of_range_error)
{
// Destroy any existing keys and then clone the given left, right keys
toku_destroy_dbt(&cursor->range_lock_left_key);
......@@ -4133,6 +4138,8 @@ toku_ft_cursor_set_range_lock(FT_CURSOR cursor, const DBT *left, const DBT *righ
} else {
toku_clone_dbt(&cursor->range_lock_right_key, *right);
}
cursor->out_of_range_error = out_of_range_error;
}
void toku_ft_cursor_close(FT_CURSOR cursor) {
......@@ -4690,6 +4697,21 @@ toku_move_ftnode_messages_to_stale(FT ft, FTNODE node) {
}
}
static int cursor_check_restricted_range(FT_CURSOR c, bytevec key, ITEMLEN keylen) {
if (c->out_of_range_error) {
FT ft = c->ft_handle->ft;
FAKE_DB(db, &ft->cmp_descriptor);
DBT found_key;
toku_fill_dbt(&found_key, key, keylen);
if ((!c->left_is_neg_infty && ft->compare_fun(&db, &found_key, &c->range_lock_left_key) < 0) ||
(!c->right_is_pos_infty && ft->compare_fun(&db, &found_key, &c->range_lock_right_key) > 0)) {
invariant(c->out_of_range_error);
return c->out_of_range_error;
}
}
return 0;
}
static int
ft_cursor_shortcut (
FT_CURSOR cursor,
......@@ -4773,8 +4795,10 @@ got_a_good_value:
&vallen,
&val
);
r = getf(keylen, key, vallen, val, getf_v, false);
r = cursor_check_restricted_range(ftcursor, key, keylen);
if (r==0) {
r = getf(keylen, key, vallen, val, getf_v, false);
}
if (r==0 || r == TOKUDB_CURSOR_CONTINUE) {
ftcursor->leaf_info.to_be.omt = bn->buffer;
ftcursor->leaf_info.to_be.index = idx;
......@@ -5396,7 +5420,6 @@ static int ft_cursor_compare_next(ft_search_t *search, DBT *x) {
return compare_k_x(brt, search->k, x) < 0; /* return min xy: kv < xy */
}
static int
ft_cursor_shortcut (
FT_CURSOR cursor,
......@@ -5435,7 +5458,10 @@ ft_cursor_shortcut (
val
);
r = getf(*keylen, *key, *vallen, *val, getf_v, false);
r = cursor_check_restricted_range(cursor, *key, *keylen);
if (r==0) {
r = getf(*keylen, *key, *vallen, *val, getf_v, false);
}
if (r == 0 || r == TOKUDB_CURSOR_CONTINUE) {
//Update cursor.
cursor->leaf_info.to_be.index = index;
......
......@@ -256,8 +256,9 @@ void toku_ft_cursor_set_leaf_mode(FT_CURSOR);
// Sets a boolean on the brt cursor that prevents uncessary copying of
// the cursor duing a one query.
void toku_ft_cursor_set_temporary(FT_CURSOR);
void toku_ft_cursor_remove_restriction(FT_CURSOR);
int toku_ft_cursor_is_leaf_mode(FT_CURSOR);
void toku_ft_cursor_set_range_lock(FT_CURSOR, const DBT *, const DBT *, bool, bool);
void toku_ft_cursor_set_range_lock(FT_CURSOR, const DBT *, const DBT *, bool, bool, int);
// get is deprecated in favor of the individual functions below
int toku_ft_cursor_get (FT_CURSOR cursor, DBT *key, FT_GET_CALLBACK_FUNCTION getf, void *getf_v, int get_flags) __attribute__ ((warn_unused_result));
......
......@@ -149,7 +149,7 @@ test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute_
snprintf(rkey, 100, "hello%d", i + 100);
toku_ft_cursor_set_range_lock(c, toku_fill_dbt(&lk, lkey, 1+strlen(lkey)),
toku_fill_dbt(&rk, rkey, 1+strlen(rkey)),
false, false);
false, false, 0);
r = toku_ft_cursor_set(c, &lk, found, NULL); assert(r == 0);
for (int j = 0; j < 100; ++j) {
r = toku_ft_cursor_next(c, found, NULL); assert(r == 0);
......
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*
COPYING CONDITIONS NOTICE:
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation, and provided that the
following conditions are met:
* Redistributions of source code must retain this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below).
* Redistributions in binary form must reproduce this COPYING
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
GRANT (below) in the documentation and/or other materials
provided with the distribution.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
COPYRIGHT NOTICE:
TokuDB, Tokutek Fractal Tree Indexing Library.
Copyright (C) 2007-2013 Tokutek, Inc.
DISCLAIMER:
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
UNIVERSITY PATENT NOTICE:
The technology is licensed by the Massachusetts Institute of
Technology, Rutgers State University of New Jersey, and the Research
Foundation of State University of New York at Stony Brook under
United States of America Serial No. 11/760379 and to the patents
and/or patent applications resulting from it.
PATENT MARKING NOTICE:
This software is covered by US Patent No. 8,185,551.
PATENT RIGHTS GRANT:
"THIS IMPLEMENTATION" means the copyrightable works distributed by
Tokutek as part of the Fractal Tree project.
"PATENT CLAIMS" means the claims of patents that are owned or
licensable by Tokutek, both currently or in the future; and that in
the absence of this license would be infringed by THIS
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
"PATENT CHALLENGE" shall mean a challenge to the validity,
patentability, enforceability and/or non-infringement of any of the
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
Tokutek hereby grants to you, for the term and geographical scope of
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to
make, have made, use, offer to sell, sell, import, transfer, and
otherwise run, modify, and propagate the contents of THIS
IMPLEMENTATION, where such license applies only to the PATENT
CLAIMS. This grant does not include claims that would be infringed
only as a consequence of further modifications of THIS
IMPLEMENTATION. If you or your agent or licensee institute or order
or agree to the institution of patent litigation against any entity
(including a cross-claim or counterclaim in a lawsuit) alleging that
THIS IMPLEMENTATION constitutes direct or contributory patent
infringement, or inducement of patent infringement, then any rights
granted to you under this License shall terminate as of the date
such litigation is filed. If you or your agent or exclusive
licensee institute or order or agree to the institution of a PATENT
CHALLENGE, then Tokutek may terminate any rights granted to you
under this License.
*/
#ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <sys/stat.h>
#include <db.h>
static void
test_restrict (int64_t n, int error_to_expect) {
assert(n > 30);
DB_TXN * const null_txn = 0;
int r;
toku_os_recursive_delete(TOKU_TEST_FILENAME);
r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); assert(r==0);
/* create the dup database file */
DB_ENV *env;
r = db_env_create(&env, 0); assert(r == 0);
r=env->set_default_bt_compare(env, int64_dbt_cmp); CKERR(r);
r = env->open(env, TOKU_TEST_FILENAME, DB_CREATE+DB_PRIVATE+DB_INIT_MPOOL, 0); assert(r == 0);
DB *db;
r = db_create(&db, env, 0);
assert(r == 0);
r = db->set_flags(db, 0);
assert(r == 0);
r = db->open(db, null_txn, "restrict.db", NULL, DB_BTREE, DB_CREATE, 0666);
assert(r == 0);
int64_t keys[n];
int64_t i;
for (i=0; i<n; i++) {
keys[i] = i;
}
DBT key, val;
for (i=0; i<n; i++) {
r = db->put(db, null_txn, dbt_init(&key, &keys[i], sizeof keys[i]), dbt_init(&val, &i, sizeof i), 0);
assert(r == 0);
}
DBC* cursor;
r = db->cursor(db, NULL, &cursor, 0);
CKERR(r);
DBT dbt_left, dbt_right;
int64_t int_left, int_right;
int_left = n / 3;
int_right = 2 * n / 3;
dbt_init(&dbt_left, &keys[int_left], sizeof keys[int_left]);
dbt_init(&dbt_right, &keys[int_right], sizeof keys[int_right]);
r = cursor->c_restrict_to_range(
cursor,
&dbt_left,
&dbt_right,
true,
error_to_expect);
CKERR(r);
for (i=0; i<n; i++) {
r = cursor->c_get(cursor, dbt_init(&key, &keys[i], sizeof keys[i]), dbt_init(&val, NULL, 0), DB_SET);
if (i < int_left || i > int_right) {
CKERR2(r, error_to_expect);
} else {
CKERR(r);
assert(val.size == 8);
assert(*(int64_t*)val.data == i);
}
}
// Forwards
r = cursor->c_get(cursor, dbt_init(&key, &keys[int_left], sizeof keys[int_left]), dbt_init(&val, NULL, 0), DB_SET);
CKERR(r);
assert(val.size == 8);
assert(*(int64_t*)val.data == int_left);
for (i=int_left+1; i < n; i++) {
r = cursor->c_get(cursor, dbt_init(&key, &keys[i], sizeof keys[i]), dbt_init(&val, NULL, 0), DB_NEXT);
if (i >= int_left && i <= int_right) {
CKERR(r);
assert(val.size == 8);
assert(*(int64_t*)val.data == i);
} else {
CKERR2(r, error_to_expect);
break;
}
}
r = cursor->c_get(cursor, dbt_init(&key, &keys[int_right], sizeof keys[int_right]), dbt_init(&val, NULL, 0), DB_SET);
CKERR(r);
assert(val.size == 8);
assert(*(int64_t*)val.data == int_right);
for (i=int_right-1; i >= 0; i--) {
r = cursor->c_get(cursor, dbt_init(&key, &keys[i], sizeof keys[i]), dbt_init(&val, NULL, 0), DB_PREV);
if (i >= int_left && i <= int_right) {
CKERR(r);
assert(val.size == 8);
assert(*(int64_t*)val.data == i);
} else {
CKERR2(r, error_to_expect);
break;
}
}
r = cursor->c_close(cursor); CKERR(r);
r = db->close(db, 0); CKERR(r);
r = env->close(env, 0); CKERR(r);
}
int
test_main(int argc, char *const argv[]) {
parse_args(argc, argv);
for (int i = 3*64; i < 3*1024; i *= 2) {
test_restrict(i, DB_NOTFOUND);
test_restrict(i, TOKUDB_OUT_OF_RANGE);
test_restrict(i, 0);
}
return 0;
}
......@@ -700,14 +700,28 @@ toku_c_close(DBC * c) {
}
static int
c_pre_acquire_range_lock(DBC *dbc, const DBT *left_key, const DBT *right_key) {
c_restrict_to_range(DBC *dbc, const DBT *left_key, const DBT *right_key, bool pre_acquire, int out_of_range_error) {
if (out_of_range_error != DB_NOTFOUND &&
out_of_range_error != TOKUDB_OUT_OF_RANGE &&
out_of_range_error != 0) {
return toku_ydb_do_error(
dbc->dbp->dbenv,
EINVAL,
"Invalid out_of_range_error [%d] for c_restrict_to_range\n",
out_of_range_error
);
}
if (left_key == toku_dbt_negative_infinity() && right_key == toku_dbt_positive_infinity()) {
out_of_range_error = 0;
}
DB *db = dbc->dbp;
DB_TXN *txn = dbc_struct_i(dbc)->txn;
HANDLE_PANICKED_DB(db);
toku_ft_cursor_set_range_lock(dbc_struct_i(dbc)->c, left_key, right_key,
(left_key == toku_dbt_negative_infinity()),
(right_key == toku_dbt_positive_infinity()));
if (!db->i->lt || !txn)
(right_key == toku_dbt_positive_infinity()),
out_of_range_error);
if (!db->i->lt || !txn || !pre_acquire)
return 0;
//READ_UNCOMMITTED and READ_COMMITTED transactions do not need read locks.
if (!dbc_struct_i(dbc)->rmw && dbc_struct_i(dbc)->iso != TOKU_ISO_SERIALIZABLE)
......@@ -719,6 +733,16 @@ c_pre_acquire_range_lock(DBC *dbc, const DBT *left_key, const DBT *right_key) {
return r;
}
static int
c_pre_acquire_range_lock(DBC *dbc, const DBT *left_key, const DBT *right_key) {
return c_restrict_to_range(dbc, left_key, right_key, true, 0);
}
static void
c_remove_restriction(DBC *dbc) {
toku_ft_cursor_remove_restriction(dbc_struct_i(dbc)->c);
}
int
toku_c_get(DBC* c, DBT* key, DBT* val, uint32_t flag) {
//This function exists for legacy (test compatibility) purposes/parity with bdb.
......@@ -811,6 +835,8 @@ toku_db_cursor_internal(DB * db, DB_TXN * txn, DBC ** c, uint32_t flags, int is_
SCRS(c_getf_set_range);
SCRS(c_getf_set_range_reverse);
SCRS(c_pre_acquire_range_lock);
SCRS(c_restrict_to_range);
SCRS(c_remove_restriction);
#undef SCRS
result->c_get = toku_c_get;
......
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