2013-04-16 23:59:58 -04:00
/* -*- mode: C; c-basic-offset: 4 -*- */
2013-04-16 23:57:48 -04:00
# ident "$Id$"
2013-04-16 23:59:09 -04:00
# ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved."
2013-04-16 23:57:48 -04:00
# 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-16 23:57:20 -04:00
# include "includes.h"
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
# include "xids.h"
2008-01-11 14:03:33 +00:00
2008-05-28 01:22:51 +00:00
struct fifo {
int n_items_in_fifo ;
char * memory ; // An array of bytes into which fifo_entries are embedded.
int memory_size ; // How big is fifo_memory
int memory_start ; // Where is the first used byte?
2013-04-16 23:57:41 -04:00
int memory_used ; // How many bytes are in use?
2008-05-28 01:22:51 +00:00
} ;
const int fifo_initial_size = 4096 ;
2008-01-11 14:03:33 +00:00
static void fifo_init ( struct fifo * fifo ) {
2008-05-28 01:22:51 +00:00
fifo - > n_items_in_fifo = 0 ;
fifo - > memory = 0 ;
fifo - > memory_size = 0 ;
fifo - > memory_start = 0 ;
fifo - > memory_used = 0 ;
2008-01-11 14:03:33 +00:00
}
2008-01-11 22:18:39 +00:00
static int fifo_entry_size ( struct fifo_entry * entry ) {
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
return sizeof ( struct fifo_entry ) + entry - > keylen + entry - > vallen
+ xids_get_size ( & entry - > xids_s )
- sizeof ( XIDS_S ) ; //Prevent double counting from fifo_entry+xids_get_size
2008-01-11 22:18:39 +00:00
}
2008-01-11 14:03:33 +00:00
static struct fifo_entry * fifo_peek ( struct fifo * fifo ) {
2008-05-28 01:22:51 +00:00
if ( fifo - > n_items_in_fifo = = 0 ) return NULL ;
else return ( struct fifo_entry * ) ( fifo - > memory + fifo - > memory_start ) ;
2008-01-11 14:03:33 +00:00
}
int toku_fifo_create ( FIFO * ptr ) {
2013-04-16 23:59:50 -04:00
struct fifo * XMALLOC ( fifo ) ;
2008-01-11 14:03:33 +00:00
if ( fifo = = 0 ) return ENOMEM ;
fifo_init ( fifo ) ;
* ptr = fifo ;
return 0 ;
}
void toku_fifo_free ( FIFO * ptr ) {
2008-05-28 01:22:51 +00:00
FIFO fifo = * ptr ;
if ( fifo - > memory ) toku_free ( fifo - > memory ) ;
fifo - > memory = 0 ;
toku_free ( fifo ) ;
* ptr = 0 ;
2008-01-11 14:03:33 +00:00
}
int toku_fifo_n_entries ( FIFO fifo ) {
2008-05-28 01:22:51 +00:00
return fifo - > n_items_in_fifo ;
}
static int next_power_of_two ( int n ) {
int r = 4096 ;
while ( r < n ) {
r * = 2 ;
assert ( r > 0 ) ;
}
return r ;
2008-01-11 14:03:33 +00:00
}
2013-04-16 23:59:07 -04:00
void toku_fifo_size_hint ( FIFO fifo , size_t size ) {
if ( fifo - > memory = = NULL ) {
fifo - > memory_size = next_power_of_two ( size ) ;
2013-04-16 23:59:50 -04:00
fifo - > memory = toku_xmalloc ( fifo - > memory_size ) ;
2013-04-16 23:59:07 -04:00
}
}
2013-04-17 00:00:07 -04:00
int toku_fifo_enq ( FIFO fifo , const void * key , unsigned int keylen , const void * data , unsigned int datalen , enum brt_msg_type type , MSN msn , XIDS xids , bool is_fresh , long * dest ) {
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
int need_space_here = sizeof ( struct fifo_entry )
+ keylen + datalen
+ xids_get_size ( xids )
- sizeof ( XIDS_S ) ; //Prevent double counting
2008-05-28 01:22:51 +00:00
int need_space_total = fifo - > memory_used + need_space_here ;
if ( fifo - > memory = = NULL ) {
2013-04-16 23:59:07 -04:00
fifo - > memory_size = next_power_of_two ( need_space_total ) ;
2013-04-16 23:59:50 -04:00
fifo - > memory = toku_xmalloc ( fifo - > memory_size ) ;
2008-05-28 01:22:51 +00:00
}
if ( fifo - > memory_start + need_space_total > fifo - > memory_size ) {
2013-04-16 23:59:47 -04:00
// Out of memory at the end.
int next_2 = next_power_of_two ( need_space_total ) ;
if ( ( 2 * next_2 > fifo - > memory_size )
| | ( 8 * next_2 < fifo - > memory_size ) ) {
// resize the fifo
2013-04-16 23:59:50 -04:00
char * newmem = toku_xmalloc ( next_2 ) ;
2013-04-16 23:59:47 -04:00
char * oldmem = fifo - > memory ;
if ( newmem = = 0 ) return ENOMEM ;
memcpy ( newmem , oldmem + fifo - > memory_start , fifo - > memory_used ) ;
fifo - > memory_size = next_2 ;
assert ( fifo - > memory_start = = 0 ) ;
fifo - > memory_start = 0 ;
fifo - > memory = newmem ;
toku_free ( oldmem ) ;
} else {
// slide things over
memmove ( fifo - > memory , fifo - > memory + fifo - > memory_start , fifo - > memory_used ) ;
assert ( fifo - > memory_start = = 0 ) ;
fifo - > memory_start = 0 ;
}
2008-05-28 01:22:51 +00:00
}
struct fifo_entry * entry = ( struct fifo_entry * ) ( fifo - > memory + fifo - > memory_start + fifo - > memory_used ) ;
2013-04-17 00:00:07 -04:00
fifo_entry_set_msg_type ( entry , type ) ;
2013-04-16 23:59:48 -04:00
entry - > msn = msn ;
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
xids_cpy ( & entry - > xids_s , xids ) ;
2013-04-16 23:59:48 -04:00
entry - > is_fresh = is_fresh ;
2008-01-11 14:03:33 +00:00
entry - > keylen = keylen ;
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
unsigned char * e_key = xids_get_end_of_array ( & entry - > xids_s ) ;
memcpy ( e_key , key , keylen ) ;
2008-01-11 14:03:33 +00:00
entry - > vallen = datalen ;
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
memcpy ( e_key + keylen , data , datalen ) ;
2013-04-16 23:59:47 -04:00
if ( dest ) {
assert ( fifo - > memory_start = = 0 ) ;
* dest = fifo - > memory_used ;
}
2008-05-28 01:22:51 +00:00
fifo - > n_items_in_fifo + + ;
fifo - > memory_used + = need_space_here ;
2008-01-11 14:03:33 +00:00
return 0 ;
}
2013-04-16 23:59:48 -04:00
int toku_fifo_enq_cmdstruct ( FIFO fifo , const BRT_MSG cmd , bool is_fresh , long * dest ) {
return toku_fifo_enq ( fifo , cmd - > u . id . key - > data , cmd - > u . id . key - > size , cmd - > u . id . val - > data , cmd - > u . id . val - > size , cmd - > type , cmd - > msn , cmd - > xids , is_fresh , dest ) ;
2008-02-29 20:47:11 +00:00
}
2008-01-29 21:43:08 +00:00
/* peek at the head (the oldest entry) of the fifo */
2013-04-17 00:00:07 -04:00
int toku_fifo_peek ( FIFO fifo , bytevec * key , unsigned int * keylen , bytevec * data , unsigned int * datalen , enum brt_msg_type * type , MSN * msn , XIDS * xids , bool * is_fresh ) {
2008-01-11 14:03:33 +00:00
struct fifo_entry * entry = fifo_peek ( fifo ) ;
if ( entry = = 0 ) return - 1 ;
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
unsigned char * e_key = xids_get_end_of_array ( & entry - > xids_s ) ;
* key = e_key ;
2008-01-11 14:03:33 +00:00
* keylen = entry - > keylen ;
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
* data = e_key + entry - > keylen ;
2008-01-11 14:03:33 +00:00
* datalen = entry - > vallen ;
2013-04-17 00:00:07 -04:00
* type = fifo_entry_get_msg_type ( entry ) ;
2013-04-16 23:59:40 -04:00
* msn = entry - > msn ;
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
* xids = & entry - > xids_s ;
2013-04-16 23:59:48 -04:00
* is_fresh = entry - > is_fresh ;
2008-01-11 14:03:33 +00:00
return 0 ;
}
2013-04-16 23:58:54 -04:00
#if 0
2013-04-16 23:58:00 -04:00
// fill in the BRT_MSG, using the two DBTs for the DBT part.
int toku_fifo_peek_cmdstruct ( FIFO fifo , BRT_MSG cmd , DBT * key , DBT * data ) {
2008-03-17 18:56:12 +00:00
u_int32_t type ;
2008-02-29 20:47:11 +00:00
bytevec keyb , datab ;
unsigned int keylen , datalen ;
Addresses #1125 Merged nested transactions from temporary merge branch into main.
Current tests fail (not regressions, they fail as of 13461)
* {{{x1.tdbrun}}}
* {{{test_log(2,3,4,5,6,7,8,9,10).recover}}}
* {{{test-recover(1,2,3).tdbrun}}}
* {{{test1324.tdbrun}}}
ULE_DEBUG disabled (defined to 0) Can be re-enabled for test purposes (set to 1).
refs [t:1125]
Merging into the temp branch (tokudb.main_13461+1125)
{{{svn merge --accept=postpone -r 12527:13461 ../tokudb.1125 ./}}}
Merging into main
{{{svn merge --accept=postpone -r13462:13463 ../tokudb.main_13461+1125/ ./}}}
git-svn-id: file:///svn/toku/tokudb@13464 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-16 23:57:56 -04:00
int r = toku_fifo_peek ( fifo , & keyb , & keylen , & datab , & datalen , & type , & cmd - > xids ) ;
2008-02-29 20:47:11 +00:00
if ( r ! = 0 ) return r ;
2013-04-16 23:58:00 -04:00
cmd - > type = ( enum brt_msg_type ) type ;
2008-02-29 20:47:11 +00:00
toku_fill_dbt ( key , keyb , keylen ) ;
toku_fill_dbt ( data , datab , datalen ) ;
cmd - > u . id . key = key ;
cmd - > u . id . val = data ;
return 0 ;
}
2013-04-16 23:58:54 -04:00
# endif
2008-02-29 20:47:11 +00:00
2008-01-11 14:03:33 +00:00
int toku_fifo_deq ( FIFO fifo ) {
2008-05-28 01:22:51 +00:00
if ( fifo - > n_items_in_fifo = = 0 ) return - 1 ;
struct fifo_entry * e = fifo_peek ( fifo ) ;
assert ( e ) ;
int used_here = fifo_entry_size ( e ) ;
fifo - > n_items_in_fifo - - ;
fifo - > memory_start + = used_here ;
fifo - > memory_used - = used_here ;
2008-01-11 14:03:33 +00:00
return 0 ;
}
2013-04-16 23:59:48 -04:00
int toku_fifo_empty ( FIFO fifo ) {
assert ( fifo - > memory_start = = 0 ) ;
fifo - > memory_used = 0 ;
fifo - > n_items_in_fifo = 0 ;
return 0 ;
}
2008-05-28 01:22:51 +00:00
int toku_fifo_iterate_internal_start ( FIFO fifo ) { return fifo - > memory_start ; }
int toku_fifo_iterate_internal_has_more ( FIFO fifo , int off ) { return off < fifo - > memory_start + fifo - > memory_used ; }
int toku_fifo_iterate_internal_next ( FIFO fifo , int off ) {
struct fifo_entry * e = ( struct fifo_entry * ) ( fifo - > memory + off ) ;
return off + fifo_entry_size ( e ) ;
}
struct fifo_entry * toku_fifo_iterate_internal_get_entry ( FIFO fifo , int off ) {
return ( struct fifo_entry * ) ( fifo - > memory + off ) ;
}
2008-03-17 18:56:12 +00:00
2013-04-17 00:00:07 -04:00
void toku_fifo_iterate ( FIFO fifo , void ( * f ) ( bytevec key , ITEMLEN keylen , bytevec data , ITEMLEN datalen , enum brt_msg_type type , MSN msn , XIDS xids , bool is_fresh , void * ) , void * arg ) {
2008-05-28 01:22:51 +00:00
FIFO_ITERATE ( fifo ,
2013-04-16 23:59:48 -04:00
key , keylen , data , datalen , type , msn , xids , is_fresh ,
f ( key , keylen , data , datalen , type , msn , xids , is_fresh , arg ) ) ;
2008-01-11 14:03:33 +00:00
}
2013-04-16 23:59:44 -04:00
void toku_fifo_size_is_stabilized ( FIFO fifo ) {
if ( fifo - > memory_used < fifo - > memory_size / 2 ) {
char * old_memory = fifo - > memory ;
int new_memory_size = fifo - > memory_used * 2 ;
char * new_memory = toku_xmalloc ( new_memory_size ) ;
memcpy ( new_memory , old_memory + fifo - > memory_start , fifo - > memory_used ) ;
fifo - > memory = new_memory ;
fifo - > memory_start = 0 ;
fifo - > memory_size = new_memory_size ;
toku_free ( old_memory ) ;
}
}
2013-04-16 23:59:55 -04:00
unsigned long toku_fifo_memory_size_in_use ( FIFO fifo ) {
return sizeof ( * fifo ) + fifo - > memory_start + fifo - > memory_used ;
}
2013-04-16 23:59:58 -04:00
unsigned long toku_fifo_memory_footprint ( FIFO fifo ) {
size_t size_used = toku_memory_footprint ( fifo - > memory , fifo - > memory_start + fifo - > memory_used ) ;
long rval = sizeof ( * fifo ) + size_used ;
return rval ;
}
2008-05-28 01:22:51 +00:00
unsigned long toku_fifo_memory_size ( FIFO fifo ) {
return sizeof ( * fifo ) + fifo - > memory_size ;
}
2013-04-16 23:59:44 -04:00
2013-04-16 23:59:47 -04:00
DBT * fill_dbt_for_fifo_entry ( DBT * dbt , const struct fifo_entry * entry ) {
return toku_fill_dbt ( dbt , xids_get_end_of_array ( ( XIDS ) & entry - > xids_s ) , entry - > keylen ) ;
}
const struct fifo_entry * toku_fifo_get_entry ( FIFO fifo , long off ) {
return toku_fifo_iterate_internal_get_entry ( fifo , off ) ;
}
2013-04-17 00:00:13 -04:00
void toku_fifo_clone ( FIFO orig_fifo , FIFO * cloned_fifo ) {
struct fifo * XMALLOC ( new_fifo ) ;
assert ( new_fifo ) ;
new_fifo - > n_items_in_fifo = orig_fifo - > n_items_in_fifo ;
new_fifo - > memory_start = 0 ;
new_fifo - > memory_used = orig_fifo - > memory_used - orig_fifo - > memory_start ;
new_fifo - > memory_size = new_fifo - > memory_used ;
new_fifo - > memory = toku_xmalloc ( new_fifo - > memory_size ) ;
memcpy (
new_fifo - > memory ,
orig_fifo - > memory + orig_fifo - > memory_start ,
new_fifo - > memory_size
) ;
* cloned_fifo = new_fifo ;
}