Commit 2a04aefc authored by Yoni Fogel's avatar Yoni Fogel Committed by Zardosht Kasheff

refs Tokutek/mongo#886

Add db->get_last_key() and test.
Add test
parent e1a82f88
...@@ -551,6 +551,7 @@ static void print_db_struct (void) { ...@@ -551,6 +551,7 @@ static void print_db_struct (void) {
"int (*get_fractal_tree_info64)(DB*,uint64_t*,uint64_t*,uint64_t*,uint64_t*)", "int (*get_fractal_tree_info64)(DB*,uint64_t*,uint64_t*,uint64_t*,uint64_t*)",
"int (*iterate_fractal_tree_block_map)(DB*,int(*)(uint64_t,int64_t,int64_t,int64_t,int64_t,void*),void*)", "int (*iterate_fractal_tree_block_map)(DB*,int(*)(uint64_t,int64_t,int64_t,int64_t,int64_t,void*),void*)",
"const char *(*get_dname)(DB *db)", "const char *(*get_dname)(DB *db)",
"int (*get_last_key)(DB *db, DBT*, uint32_t)",
NULL}; NULL};
sort_and_dump_fields("db", true, extra); sort_and_dump_fields("db", true, extra);
} }
......
...@@ -72,6 +72,7 @@ if(BUILD_TESTING OR BUILD_SRC_TESTS) ...@@ -72,6 +72,7 @@ if(BUILD_TESTING OR BUILD_SRC_TESTS)
execute-updates execute-updates
filesize filesize
get_key_after_bytes_unit get_key_after_bytes_unit
get_last_key
helgrind1 helgrind1
helgrind2 helgrind2
helgrind3 helgrind3
......
/* -*- 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.
This software is covered by US Patent No. 8,489,638.
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."
#ident "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."
/**
* Test that various queries behave correctly
*
* Zardosht says:
*
* write a test that inserts a bunch of elements into the tree,
* and then verify that the following types of queries work:
* - db->get
* - next
* - prev
* - set_range
* - set_range_reverse
* - first
* - last
* - current
*
* do it on a table with:
* - just a leaf node
* - has internal nodes (make node size 4K and bn size 1K)
* - big cachetable such that everything fits
* - small cachetable such that not a lot fits
*
* make sure APIs are the callback APIs (getf_XXX)
* make sure your callbacks all return TOKUDB_CURSOR_CONTINUE,
* so we ensure that returning TOKUDB_CURSOR_CONTINUE does not
* mess anything up.
*/
#include "test.h"
/**
* Calculate or verify that a value for a given key is correct
* Returns 0 if the value is correct, nonzero otherwise.
*/
static void get_value_by_key(DBT * key, DBT * value)
{
// keys/values are always stored in the DBT in net order
int * CAST_FROM_VOIDP(k, key->data);
int v = toku_ntohl(*k) * 2 + 1;
memcpy(value->data, &v, sizeof(int));
}
static void init_env(DB_ENV ** env, size_t ct_size)
{
int r;
const int envflags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD |
DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_PRIVATE;
printf("initializing environment\n");
toku_os_recursive_delete(TOKU_TEST_FILENAME);
r = toku_os_mkdir(TOKU_TEST_FILENAME, 0755); { int chk_r = r; CKERR(chk_r); }
r = db_env_create(env, 0); { int chk_r = r; CKERR(chk_r); }
assert(ct_size < 1024 * 1024 * 1024L);
r = (*env)->set_cachesize(*env, 0, ct_size, 1); { int chk_r = r; CKERR(chk_r); }
r = (*env)->open(*env, TOKU_TEST_FILENAME, envflags, 0755); { int chk_r = r; CKERR(chk_r); }
}
static void init_db(DB_ENV * env, DB ** db)
{
int r;
const int node_size = 4096;
const int bn_size = 1024;
printf("initializing db\n");
DB_TXN * txn;
r = db_create(db, env, 0); { int chk_r = r; CKERR(chk_r); }
r = (*db)->set_readpagesize(*db, bn_size); { int chk_r = r; CKERR(chk_r); }
r = (*db)->set_pagesize(*db, node_size); { int chk_r = r; CKERR(chk_r); }
r = env->txn_begin(env, nullptr, &txn, 0); { int chk_r = r; CKERR(chk_r); }
r = (*db)->open(*db, txn, "db", nullptr, DB_BTREE, DB_CREATE, 0644); { int chk_r = r; CKERR(chk_r); }
r = txn->commit(txn, 0); { int chk_r = r; CKERR(chk_r); }
}
static void cleanup_env_and_db(DB_ENV * env, DB * db)
{
int r;
printf("cleaning up environment and db\n");
r = db->close(db, 0); { int chk_r = r; CKERR(chk_r); }
r = env->close(env, 0); { int chk_r = r; CKERR(chk_r); }
}
static void check_dbt_matches(DB *db, int expect_r, int key, DBT *kdbt) {
int r = db->get_last_key(db, kdbt, 0);
CKERR2(r, expect_r);
if (r==0) {
invariant(kdbt->size == sizeof(int));
int found_key = *(int*)kdbt->data;
invariant(key == (int)ntohl(found_key));
}
}
static void check_last_key_matches(DB *db, int expect_r, int key) {
DBT kdbt;
dbt_init(&kdbt, nullptr, 0);
check_dbt_matches(db, expect_r, key, &kdbt);
dbt_init_malloc(&kdbt);
check_dbt_matches(db, expect_r, key, &kdbt);
if (kdbt.data) {
toku_free(kdbt.data);
}
dbt_init_realloc(&kdbt);
check_dbt_matches(db, expect_r, key, &kdbt);
if (kdbt.data) {
toku_free(kdbt.data);
}
}
static void do_test(size_t ct_size, int num_keys)
{
int i, r;
DB * db;
DB_ENV * env;
DB_TXN *txn = nullptr;
DB_TXN *txn2 = nullptr;
uint64_t loops_run = 0;
(void)txn2;
printf("doing tests for ct_size %lu, num_keys %d\n",
ct_size, num_keys);
// initialize everything and insert data
init_env(&env, ct_size);
assert(env != nullptr);
init_db(env, &db);
assert(db != nullptr);
r = env->txn_begin(env, nullptr, &txn, 0);
CKERR(r);
DBT key, value;
for (i = 0; i < num_keys; i++) {
int v, k = toku_htonl(i);
dbt_init(&key, &k, sizeof(int));
dbt_init(&value, &v, sizeof(int));
get_value_by_key(&key, &value);
r = db->put(db, txn, &key, &value, 0);
CKERR(r);
}
CKERR(r);
int expect_r = num_keys == 0 ? DB_NOTFOUND : 0;
check_last_key_matches(db, expect_r, num_keys - 1);
r = txn->commit(txn, 0);
check_last_key_matches(db, expect_r, num_keys - 1);
if (num_keys == 0) {
goto cleanup;
}
// r = env->txn_begin(env, nullptr, &txn2, 0);
// CKERR(r);
r = env->txn_begin(env, nullptr, &txn, 0);
CKERR(r);
r = db->del(db, txn, &key, 0);
check_last_key_matches(db, 0, num_keys - 1);
r = txn->commit(txn, 0);
check_last_key_matches(db, 0, num_keys - 1);
// r = txn2->commit(txn2, 0);
check_last_key_matches(db, 0, num_keys - 1);
//Run Garbage collection (NOTE does not work when everything fits in root??? WHY)
r = db->hot_optimize(db, nullptr, nullptr, nullptr, nullptr, &loops_run);
CKERR(r);
r = env->txn_checkpoint(env, 0, 0, 0);
CKERR(r);
//Run Garbage collection (NOTE does not work when everything fits in root??? WHY)
r = db->hot_optimize(db, nullptr, nullptr, nullptr, nullptr, &loops_run);
CKERR(r);
r = env->txn_checkpoint(env, 0, 0, 0);
CKERR(r);
//NOTE: tried overkill (double optimize, double checkpoint.. gc still doesn't happen for everything in root in single basement
if (num_keys < 128) {
//TODO: HACK Fix this hack.
// Fits in root (in one basement).. no garbage collection so original still found
check_last_key_matches(db, 0, num_keys - 1);
} else {
if (num_keys >= 2) {
check_last_key_matches(db, 0, num_keys - 2);
} else {
//Useless case until/unless I can get garbage collection in root working
check_last_key_matches(db, DB_NOTFOUND, num_keys - 1);
}
}
cleanup:
cleanup_env_and_db(env, db);
}
int test_main(int argc, char * const argv[])
{
default_parse_args(argc, argv);
for (int i = 0; i <= 2; i++) {
do_test(1024*1024, i);
}
for (int i = 4; i <= 1024; i*=i) {
do_test(1024*1024, i);
}
return 0;
}
...@@ -107,6 +107,7 @@ PATENT RIGHTS GRANT: ...@@ -107,6 +107,7 @@ PATENT RIGHTS GRANT:
#include "indexer.h" #include "indexer.h"
#include <portability/toku_atomic.h> #include <portability/toku_atomic.h>
#include <util/status.h> #include <util/status.h>
#include <ft/le-cursor.h>
static YDB_DB_LAYER_STATUS_S ydb_db_layer_status; static YDB_DB_LAYER_STATUS_S ydb_db_layer_status;
#ifdef STATUS_VALUE #ifdef STATUS_VALUE
...@@ -942,6 +943,53 @@ locked_db_optimize(DB *db) { ...@@ -942,6 +943,53 @@ locked_db_optimize(DB *db) {
return r; return r;
} }
struct last_key_extra {
DBT * const key_dbt;
DB * db;
};
static int
db_get_last_key_callback(ITEMLEN keylen, bytevec key, ITEMLEN vallen UU(), bytevec val UU(), void *extra, bool lock_only) {
if (!lock_only) {
struct last_key_extra * CAST_FROM_VOIDP(info, extra);
toku_dbt_set(keylen, key, info->key_dbt, &info->db->i->skey);
}
return 0;
}
static int
toku_db_get_last_key(DB * db, DB_TXN *txn, DBT * key, uint32_t flags UU()) {
int r;
LE_CURSOR cursor = nullptr;
struct last_key_extra extra = { .key_dbt = key, .db = db };
r = toku_le_cursor_create(&cursor, db->i->ft_handle, db_txn_struct_i(txn)->tokutxn);
if (r != 0) { goto cleanup; }
// Goes in reverse order. First key returned is last in dictionary.
r = toku_le_cursor_next(cursor, db_get_last_key_callback, &extra);
if (r != 0) { goto cleanup; }
cleanup:
if (cursor) {
toku_le_cursor_close(cursor);
}
return r;
}
static int
autotxn_db_get_last_key(DB* db, DBT* key, uint32_t flags) {
bool changed; int r;
DB_TXN *txn = nullptr;
// Cursors inside require transactions, but this is _not_ a transactional function.
// Create transaction in a wrapper and then later close it.
r = toku_db_construct_autotxn(db, &txn, &changed, false);
if (r!=0) return r;
r = toku_db_get_last_key(db, txn, key, flags);
return toku_db_destruct_autotxn(txn, r, changed);
}
static int static int
toku_db_get_fragmentation(DB * db, TOKU_DB_FRAGMENTATION report) { toku_db_get_fragmentation(DB * db, TOKU_DB_FRAGMENTATION report) {
HANDLE_PANICKED_DB(db); HANDLE_PANICKED_DB(db);
...@@ -1087,6 +1135,7 @@ toku_db_create(DB ** db, DB_ENV * env, uint32_t flags) { ...@@ -1087,6 +1135,7 @@ toku_db_create(DB ** db, DB_ENV * env, uint32_t flags) {
result->update = autotxn_db_update; result->update = autotxn_db_update;
result->update_broadcast = autotxn_db_update_broadcast; result->update_broadcast = autotxn_db_update_broadcast;
result->change_descriptor = autotxn_db_change_descriptor; result->change_descriptor = autotxn_db_change_descriptor;
result->get_last_key = autotxn_db_get_last_key;
// unlocked methods // unlocked methods
result->get = autotxn_db_get; result->get = autotxn_db_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