2013-04-17 00:00:36 -04:00
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: expandtab:ts=8:sw=4:softtabstop=4:
2013-04-17 00:00:03 -04:00
# ident "$Id$"
2013-04-16 23:59:23 -04:00
# ident "Copyright (c) 2010 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."
2013-04-17 00:00:53 -04:00
# include <db.h>
2013-04-16 23:59:23 -04:00
# include "includes.h"
# include "le-cursor.h"
2013-04-17 00:00:53 -04:00
// A LE_CURSOR is a special purpose FT_CURSOR that:
// - enables prefetching
// - does not perform snapshot reads. it reads everything, including uncommitted.
//
// A LE_CURSOR is good for scanning a FT from beginning to end. Useful for hot indexing.
//
2013-04-16 23:59:23 -04:00
// It caches the key that is was last positioned over to speed up key comparisions.
struct le_cursor {
2013-04-17 00:00:53 -04:00
// TODO: remove DBs from the ft layer comparison function
// so this is never necessary
// use a fake db for comparisons.
struct __toku_db fake_db ;
2013-04-17 00:00:35 -04:00
FT_CURSOR ft_cursor ;
2013-04-16 23:59:31 -04:00
DBT key ; // the key that the le cursor is positioned at
// TODO a better implementation would fetch the key from the brt cursor
2013-04-17 00:00:53 -04:00
bool neg_infinity ; // true when the le cursor is positioned at -infinity (initial setting)
bool pos_infinity ; // true when the le cursor is positioned at +infinity (when _next returns DB_NOTFOUND)
2013-04-16 23:59:23 -04:00
} ;
int
2013-04-17 00:00:53 -04:00
toku_le_cursor_create ( LE_CURSOR * le_cursor_result , FT_HANDLE brt , TOKUTXN txn ) {
2013-04-16 23:59:23 -04:00
int result = 0 ;
LE_CURSOR le_cursor = ( LE_CURSOR ) toku_malloc ( sizeof ( struct le_cursor ) ) ;
2013-04-17 00:00:53 -04:00
if ( le_cursor = = NULL ) {
2013-04-16 23:59:23 -04:00
result = errno ;
2013-04-17 00:00:53 -04:00
}
2013-04-16 23:59:23 -04:00
else {
2013-04-17 00:00:53 -04:00
result = toku_ft_cursor ( brt , & le_cursor - > ft_cursor , txn , false , false ) ;
2013-04-16 23:59:23 -04:00
if ( result = = 0 ) {
2013-04-16 23:59:31 -04:00
// TODO move the leaf mode to the brt cursor constructor
2013-04-17 00:00:35 -04:00
toku_ft_cursor_set_leaf_mode ( le_cursor - > ft_cursor ) ;
2013-04-16 23:59:31 -04:00
toku_init_dbt ( & le_cursor - > key ) ;
le_cursor - > key . flags = DB_DBT_REALLOC ;
2013-04-17 00:00:53 -04:00
le_cursor - > neg_infinity = true ;
le_cursor - > pos_infinity = false ;
// zero out the fake DB. this is a rare operation so it's not too slow.
memset ( & le_cursor - > fake_db , 0 , sizeof ( le_cursor - > fake_db ) ) ;
2013-04-16 23:59:23 -04:00
}
}
2013-04-17 00:00:53 -04:00
if ( result = = 0 ) {
2013-04-16 23:59:23 -04:00
* le_cursor_result = le_cursor ;
2013-04-17 00:00:53 -04:00
} else {
2013-04-16 23:59:23 -04:00
toku_free ( le_cursor ) ;
2013-04-17 00:00:53 -04:00
}
2013-04-16 23:59:23 -04:00
return result ;
}
int
2013-04-17 00:00:53 -04:00
toku_le_cursor_close ( LE_CURSOR le_cursor ) {
2013-04-17 00:00:35 -04:00
int result = toku_ft_cursor_close ( le_cursor - > ft_cursor ) ;
2013-04-16 23:59:23 -04:00
toku_destroy_dbt ( & le_cursor - > key ) ;
toku_free ( le_cursor ) ;
return result ;
}
2013-04-16 23:59:31 -04:00
// this implementation copies the key and leafentry into the supplied DBTs.
// this may be too slow. an alternative implementation could avoid copying the
// key by fetching the key from the brt cursor, and could avoid copying the leaf entry
// by processing the leaf entry in the brt cursor callback.
2013-04-16 23:59:23 -04:00
struct le_cursor_callback_arg {
DBT * key , * val ;
} ;
2013-04-16 23:59:31 -04:00
// copy the key and the leaf entry to the given DBTs
2013-04-16 23:59:23 -04:00
static int
2013-04-17 00:00:03 -04:00
le_cursor_callback ( ITEMLEN keylen , bytevec key , ITEMLEN vallen , bytevec val , void * v , bool lock_only ) {
if ( lock_only ) {
; // do nothing
} else {
struct le_cursor_callback_arg * arg = ( struct le_cursor_callback_arg * ) v ;
toku_dbt_set ( keylen , key , arg - > key , NULL ) ;
toku_dbt_set ( vallen , val , arg - > val , NULL ) ;
}
2013-04-16 23:59:23 -04:00
return 0 ;
}
int
2013-04-17 00:00:53 -04:00
toku_le_cursor_next ( LE_CURSOR le_cursor , DBT * le ) {
2013-04-16 23:59:31 -04:00
int result ;
if ( le_cursor - > pos_infinity )
result = DB_NOTFOUND ;
else {
2013-04-17 00:00:53 -04:00
le_cursor - > neg_infinity = false ;
2013-04-16 23:59:31 -04:00
struct le_cursor_callback_arg arg = { & le_cursor - > key , le } ;
// TODO replace this with a non deprecated function
2013-04-17 00:00:35 -04:00
result = toku_ft_cursor_get ( le_cursor - > ft_cursor , NULL , le_cursor_callback , & arg , DB_NEXT ) ;
2013-04-16 23:59:31 -04:00
if ( result = = DB_NOTFOUND )
2013-04-17 00:00:53 -04:00
le_cursor - > pos_infinity = true ;
2013-04-16 23:59:31 -04:00
}
2013-04-16 23:59:30 -04:00
return result ;
2013-04-16 23:59:23 -04:00
}
2013-04-17 00:00:53 -04:00
bool
toku_le_cursor_is_key_greater ( LE_CURSOR le_cursor , const DBT * key ) {
bool result ;
if ( le_cursor - > neg_infinity ) {
result = true ; // all keys are greater than -infinity
} else if ( le_cursor - > pos_infinity ) {
result = false ; // all keys are less than +infinity
} else {
// get the comparison function and descriptor from the cursor's ft
FT_HANDLE ft_handle = le_cursor - > ft_cursor - > ft_handle ;
ft_compare_func keycompare = toku_ft_get_bt_compare ( ft_handle ) ;
// store the descriptor in the fake DB to do a key comparison
le_cursor - > fake_db . cmp_descriptor = toku_ft_get_cmp_descriptor ( ft_handle ) ;
int r = keycompare ( & le_cursor - > fake_db , & le_cursor - > key , key ) ;
if ( r < 0 ) {
result = true ; // key is right of the cursor key
} else {
result = false ; // key is at or left of the cursor key
}
2013-04-16 23:59:23 -04:00
}
return result ;
}