2013-04-17 00:00:59 -04:00
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
2013-04-16 23:57:48 -04:00
# ident "$Id$"
2013-04-17 00:00:59 -04:00
# ident "Copyright (c) 2007-2012 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
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_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
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 ) {
2013-04-17 00:01:02 -04:00
r * = 2 ;
assert ( r > 0 ) ;
2008-05-28 01:22:51 +00:00
}
return r ;
2008-01-11 14:03:33 +00:00
}
2013-04-17 00:01:02 -04:00
int toku_fifo_enq ( FIFO fifo , const void * key , unsigned int keylen , const void * data , unsigned int datalen , enum ft_msg_type type , MSN msn , XIDS xids , bool is_fresh , int32_t * 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-17 00:00:58 -04:00
XMALLOC_N ( fifo - > memory_size , fifo - > memory ) ;
2008-05-28 01:22:51 +00:00
}
2013-04-17 00:00:24 -04:00
if ( 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-17 00:00:58 -04:00
char * XMALLOC_N ( next_2 , newmem ) ;
2013-04-16 23:59:47 -04:00
char * oldmem = fifo - > memory ;
if ( newmem = = 0 ) return ENOMEM ;
2013-04-17 00:00:24 -04:00
memcpy ( newmem , oldmem , fifo - > memory_used ) ;
2013-04-16 23:59:47 -04:00
fifo - > memory_size = next_2 ;
fifo - > memory = newmem ;
toku_free ( oldmem ) ;
}
2008-05-28 01:22:51 +00:00
}
2013-04-17 00:00:24 -04:00
struct fifo_entry * entry = ( struct fifo_entry * ) ( fifo - > memory + 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 ) {
* 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-17 00:00:24 -04:00
int toku_fifo_iterate_internal_start ( FIFO UU ( fifo ) ) { return 0 ; }
int toku_fifo_iterate_internal_has_more ( FIFO fifo , int off ) { return off < fifo - > memory_used ; }
2008-05-28 01:22:51 +00:00
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:35 -04:00
void toku_fifo_iterate ( FIFO fifo , void ( * f ) ( bytevec key , ITEMLEN keylen , bytevec data , ITEMLEN datalen , enum ft_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-17 00:00:24 -04:00
unsigned int toku_fifo_buffer_size_in_use ( FIFO fifo ) {
return fifo - > memory_used ;
}
2013-04-16 23:59:55 -04:00
unsigned long toku_fifo_memory_size_in_use ( FIFO fifo ) {
2013-04-17 00:00:24 -04:00
return sizeof ( * fifo ) + fifo - > memory_used ;
2013-04-16 23:59:55 -04:00
}
2013-04-16 23:59:58 -04:00
unsigned long toku_fifo_memory_footprint ( FIFO fifo ) {
2013-04-17 00:00:24 -04:00
size_t size_used = toku_memory_footprint ( fifo - > memory , fifo - > memory_used ) ;
2013-04-16 23:59:58 -04:00
long rval = sizeof ( * fifo ) + size_used ;
return rval ;
}
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 ) ;
}
2013-04-17 00:01:02 -04:00
struct fifo_entry * toku_fifo_get_entry ( FIFO fifo , int off ) {
2013-04-16 23:59:47 -04:00
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 ;
2013-04-17 00:00:24 -04:00
new_fifo - > memory_used = orig_fifo - > memory_used ;
2013-04-17 00:00:13 -04:00
new_fifo - > memory_size = new_fifo - > memory_used ;
2013-04-17 00:00:58 -04:00
XMALLOC_N ( new_fifo - > memory_size , new_fifo - > memory ) ;
2013-04-17 00:00:13 -04:00
memcpy (
new_fifo - > memory ,
2013-04-17 00:00:24 -04:00
orig_fifo - > memory ,
2013-04-17 00:00:13 -04:00
new_fifo - > memory_size
) ;
* cloned_fifo = new_fifo ;
}
2013-04-17 00:00:24 -04:00
2013-04-17 00:01:01 -04:00
bool toku_are_fifos_same ( FIFO fifo1 , FIFO fifo2 ) {
2013-04-17 00:00:24 -04:00
return (
fifo1 - > memory_used = = fifo2 - > memory_used & &
memcmp ( fifo1 - > memory , fifo2 - > memory , fifo1 - > memory_used ) = = 0
) ;
}