2007-11-23 17:41:02 +00:00
# ifndef BRT_INTERNAL_H
# define BRT_INTERNAL_H
2007-11-29 14:27:42 +00:00
2008-01-24 15:10:32 +00:00
# ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
2007-11-29 14:18:54 +00:00
2008-04-02 23:40:36 +00:00
# include "toku_assert.h"
2007-07-13 19:37:47 +00:00
# include "cachetable.h"
2008-01-11 14:03:33 +00:00
# include "fifo.h"
2008-04-02 23:40:36 +00:00
# include "yerror.h"
2007-07-13 19:37:47 +00:00
# include "brt.h"
2008-05-04 16:56:15 +00:00
# include "crc.h"
2008-01-25 15:43:37 +00:00
# include "list.h"
2008-04-02 23:40:36 +00:00
# include "mempool.h"
# include "kv-pair.h"
2008-04-04 18:03:03 +00:00
# include "leafentry.h"
2007-07-13 19:37:47 +00:00
2008-04-25 13:46:17 +00:00
typedef void * OMTVALUE ;
2008-04-22 20:39:50 +00:00
# include "omt.h"
2007-08-25 21:58:25 +00:00
# ifndef BRT_FANOUT
# define BRT_FANOUT 16
# endif
2007-11-14 17:58:38 +00:00
enum { TREE_FANOUT = BRT_FANOUT } ;
2007-07-13 19:37:47 +00:00
enum { KEY_VALUE_OVERHEAD = 8 } ; /* Must store the two lengths. */
2008-04-22 20:39:50 +00:00
enum { OMT_ITEM_OVERHEAD = 0 } ; /* No overhead for the OMT item. The PMA needed to know the idx, but the OMT doesn't. */
2008-02-05 18:25:23 +00:00
enum { BRT_CMD_OVERHEAD = ( 1 // the type
+ 8 ) // the xid
} ;
2008-04-22 20:39:50 +00:00
enum { LE_OVERHEAD_BOUND = 9 } ; // the type and xid
2007-11-14 17:58:38 +00:00
enum { BRT_DEFAULT_NODE_SIZE = 1 < < 20 } ;
2007-09-06 21:36:45 +00:00
2007-07-13 19:37:47 +00:00
struct nodeheader_in_file {
int n_in_buffer ;
} ;
enum { BUFFER_HEADER_SIZE = ( 4 // height//
+ 4 // n_children
+ TREE_FANOUT * 8 // children
) } ;
2007-12-06 12:00:24 +00:00
struct brtnode_nonleaf_childinfo {
u_int32_t subtree_fingerprint ;
2008-04-30 13:23:04 +00:00
u_int64_t leafentry_estimate ; // estimate how many leafentries are below us.
2007-12-06 12:00:24 +00:00
DISKOFF diskoff ;
2008-01-31 22:05:43 +00:00
FIFO buffer ;
2008-01-11 14:03:33 +00:00
unsigned int n_bytes_in_buffer ; /* How many bytes are in each buffer (including overheads for the disk-representation) */
2007-12-06 12:00:24 +00:00
} ;
2007-07-13 19:37:47 +00:00
typedef struct brtnode * BRTNODE ;
/* Internal nodes. */
struct brtnode {
enum typ_tag tag ;
unsigned int nodesize ;
2007-11-27 15:22:56 +00:00
unsigned int flags ;
2007-11-14 17:58:38 +00:00
DISKOFF thisnodename ; // The size of the node allocated on disk. Not all is necessarily in use.
2008-01-23 18:06:23 +00:00
// These two LSNs are used to decide when to make a copy of a node instead of overwriting it.
// In the TOKULOGGER is a field called checkpoint_lsn which is the lsn of the most recent checkpoint
LSN disk_lsn ; // The LSN as of the most recent version on disk. (Updated by brt-serialize) This lsn is saved in the node.
LSN log_lsn ; // The LSN of the youngest log entry that affects the current in-memory state. The log write may not have actually made it to disk. This lsn is not saved in disk (since the two lsns are the same for any node not in main memory.)
// The checkpointing works as follows:
// When we unpin a node: if it is dirty and disk_lsn<checkpoint_lsn then we need to make a new copy.
// When we checkpoint: Create a checkpoint record, and cause every dirty node to be written to disk. The new checkpoint record is *not* incorporated into the disk_lsn of the written nodes.
// While we are checkpointing, someone may modify a dirty node that has not yet been written. In that case, when we unpin the node, we make the new copy (because the disk_lsn<checkpoint_lsn), just as we would usually.
//
2008-04-22 20:39:50 +00:00
int layout_version ; // What version of the data structure? (version 2 adds the xid to the brt cmds)
2007-07-13 19:37:47 +00:00
int height ; /* height is always >= 0. 0 for leaf, >0 for nonleaf. */
2007-11-14 17:58:38 +00:00
u_int32_t rand4fingerprint ;
2008-01-11 14:03:33 +00:00
u_int32_t local_fingerprint ; /* For leaves this is everything in the buffer. For nonleaves, this is everything in the buffers, but does not include child subtree fingerprints. */
2007-11-14 17:58:38 +00:00
int dirty ;
2007-07-13 19:37:47 +00:00
union node {
struct nonleaf {
2007-11-14 17:58:38 +00:00
// Don't actually store the subree fingerprint in the in-memory data structure.
2007-07-13 19:37:47 +00:00
int n_children ; /* if n_children==TREE_FANOUT+1 then the tree needs to be rebalanced. */
2007-12-06 12:00:24 +00:00
unsigned int totalchildkeylens ;
2008-01-11 14:03:33 +00:00
unsigned int n_bytes_in_buffers ;
2007-12-06 12:00:24 +00:00
2008-03-06 21:46:57 +00:00
struct brtnode_nonleaf_childinfo * childinfos ; /* One extra so we can grow */
2007-12-06 19:16:18 +00:00
2008-01-31 22:05:43 +00:00
# define BNC_SUBTREE_FINGERPRINT(node,i) ((node)->u.n.childinfos[i].subtree_fingerprint)
2008-04-30 13:23:04 +00:00
# define BNC_SUBTREE_LEAFENTRY_ESTIMATE(node,i) ((node)->u.n.childinfos[i].leafentry_estimate)
2008-01-31 22:05:43 +00:00
# define BNC_DISKOFF(node,i) ((node)->u.n.childinfos[i].diskoff)
# define BNC_BUFFER(node,i) ((node)->u.n.childinfos[i].buffer)
# define BNC_NBYTESINBUF(node,i) ((node)->u.n.childinfos[i].n_bytes_in_buffer)
2007-12-06 19:16:18 +00:00
2008-03-06 21:46:57 +00:00
struct kv_pair * * childkeys ; /* Pivot keys. Child 0's keys are <= childkeys[0]. Child 1's keys are <= childkeys[1].
2007-07-13 19:37:47 +00:00
Note : It is possible that Child 1 ' s keys are = = to child 0 ' s key ' s , so it is
not necessarily true that child 1 ' s keys are > childkeys [ 0 ] .
However , in the absense of duplicate keys , child 1 ' s keys * are * > childkeys [ 0 ] . */
2007-08-23 18:07:18 +00:00
} n ;
2007-07-13 19:37:47 +00:00
struct leaf {
2008-04-22 20:39:50 +00:00
OMT buffer ;
2007-12-04 22:18:21 +00:00
unsigned int n_bytes_in_buffer ; /* How many bytes to represent the PMA (including the per-key overheads, but not including the overheads for the node. */
2008-04-02 23:40:36 +00:00
struct mempool buffer_mempool ;
2007-07-13 19:37:47 +00:00
} l ;
} u ;
} ;
2007-11-19 20:22:56 +00:00
/* pivot flags (must fit in 8 bits) */
2007-11-15 14:44:05 +00:00
enum {
BRT_PIVOT_TRUNC = 4 ,
BRT_PIVOT_FRONT_COMPRESS = 8 ,
} ;
2007-07-13 19:37:47 +00:00
struct brt_header {
int dirty ;
unsigned int nodesize ;
2007-11-14 17:58:38 +00:00
DISKOFF freelist ;
DISKOFF unused_memory ;
DISKOFF unnamed_root ;
2007-07-13 19:37:47 +00:00
int n_named_roots ; /* -1 if the only one is unnamed */
char * * names ;
2007-11-14 17:58:38 +00:00
DISKOFF * roots ;
unsigned int flags ;
2008-04-09 02:45:27 +00:00
FIFO fifo ; // all the abort and commit commands. If the header gets flushed to disk, we write the fifo contents beyond the unused_memory.
2007-07-13 19:37:47 +00:00
} ;
struct brt {
CACHEFILE cf ;
2008-04-17 03:11:55 +00:00
char * fname ; // the filename
2007-07-13 19:37:47 +00:00
char * database_name ;
// The header is shared. It is also ephemeral.
struct brt_header * h ;
2008-01-25 15:43:37 +00:00
struct list cursors ;
2007-07-24 01:32:03 +00:00
2007-11-14 17:58:38 +00:00
unsigned int nodesize ;
unsigned int flags ;
2007-07-24 14:34:05 +00:00
int ( * compare_fun ) ( DB * , const DBT * , const DBT * ) ;
2007-11-14 17:58:38 +00:00
int ( * dup_compare ) ( DB * , const DBT * , const DBT * ) ;
2007-11-26 21:51:36 +00:00
DB * db ; // To pass to the compare fun
2007-07-24 01:32:03 +00:00
void * skey , * sval ; /* Used for DBT return values. */
2008-04-09 02:45:27 +00:00
2008-04-25 14:22:05 +00:00
OMT txns ; // transactions that are using this OMT (note that the transaction checks the cf also)
2007-07-13 19:37:47 +00:00
} ;
/* serialization code */
2008-04-17 03:11:55 +00:00
void toku_serialize_brtnode_to ( int fd , DISKOFF off , BRTNODE node ) ;
int toku_deserialize_brtnode_from ( int fd , DISKOFF off , BRTNODE * brtnode ) ;
2007-11-19 23:54:17 +00:00
unsigned int toku_serialize_brtnode_size ( BRTNODE node ) ; /* How much space will it take? */
2007-11-20 00:02:51 +00:00
int toku_keycompare ( bytevec key1 , ITEMLEN key1len , bytevec key2 , ITEMLEN key2len ) ;
2007-07-13 19:37:47 +00:00
2007-11-19 23:54:17 +00:00
void toku_verify_counts ( BRTNODE ) ;
2007-07-13 19:37:47 +00:00
2007-11-21 13:07:49 +00:00
int toku_serialize_brt_header_size ( struct brt_header * h ) ;
2007-11-19 23:54:17 +00:00
int toku_serialize_brt_header_to ( int fd , struct brt_header * h ) ;
2007-11-21 13:07:49 +00:00
int toku_serialize_brt_header_to_wbuf ( struct wbuf * , struct brt_header * h ) ;
2007-11-19 23:54:17 +00:00
int toku_deserialize_brtheader_from ( int fd , DISKOFF off , struct brt_header * * brth ) ;
2007-07-13 19:37:47 +00:00
2008-04-09 02:45:27 +00:00
int toku_serialize_fifo_at ( int fd , off_t freeoff , FIFO fifo ) ; // Write a fifo into a disk, without worrying about fitting it into a block. This write is done at the end of the file.
int toku_deserialize_fifo_at ( int fd , off_t at , FIFO * fifo ) ;
2007-11-29 15:09:14 +00:00
void toku_brtnode_free ( BRTNODE * node ) ;
2007-08-01 02:37:21 +00:00
2007-07-13 19:37:47 +00:00
# if 1
# define DEADBEEF ((void*)0xDEADBEEF)
# else
# define DEADBEEF ((void*)0xDEADBEEFDEADBEEF)
# endif
2007-11-14 17:58:38 +00:00
struct brtenv {
CACHETABLE ct ;
TOKULOGGER logger ;
long long checksum_number ;
// SPINLOCK checkpointing;
} ;
2007-11-29 15:09:14 +00:00
extern cachetable_flush_func_t toku_brtnode_flush_callback , toku_brtheader_flush_callback ;
extern cachetable_fetch_func_t toku_brtnode_fetch_callback , toku_brtheader_fetch_callback ;
2007-11-14 17:58:38 +00:00
extern int toku_read_and_pin_brt_header ( CACHEFILE cf , struct brt_header * * header ) ;
extern int toku_unpin_brt_header ( BRT brt ) ;
extern CACHEKEY * toku_calculate_root_offset_pointer ( BRT brt ) ;
static const BRTNODE null_brtnode = 0 ;
2008-04-07 01:30:25 +00:00
//extern u_int32_t toku_calccrc32_kvpair (const void *key, int keylen, const void *val, int vallen);
//extern u_int32_t toku_calccrc32_kvpair_struct (const struct kv_pair *kvp);
2008-04-04 18:22:01 +00:00
extern u_int32_t toku_calccrc32_cmd ( u_int32_t type , TXNID xid , const void * key , u_int32_t keylen , const void * val , u_int32_t vallen ) ;
2008-02-05 18:25:23 +00:00
extern u_int32_t toku_calccrc32_cmdstruct ( BRT_CMD cmd ) ;
2007-11-26 21:51:36 +00:00
2007-12-06 13:52:52 +00:00
// How long is the pivot key?
2007-12-06 14:20:47 +00:00
unsigned int toku_brt_pivot_key_len ( BRT , struct kv_pair * ) ; // Given the tree
unsigned int toku_brtnode_pivot_key_len ( BRTNODE , struct kv_pair * ) ; // Given the node
2007-12-06 13:52:52 +00:00
2008-01-25 15:43:37 +00:00
/* a brt cursor is represented as a kv pair in a tree */
struct brt_cursor {
struct list cursors_link ;
BRT brt ;
DBT key ;
DBT val ;
2008-03-15 19:06:39 +00:00
int is_temporary_cursor ; // If it is a temporary cursor then use the following skey and sval to return tokudb-managed values in dbts. Otherwise use the brt's skey and skval.
void * skey , * sval ;
2008-01-25 15:43:37 +00:00
} ;
2008-04-22 20:39:50 +00:00
// logs the memory allocation, but not the creation of the new node
2008-03-06 21:46:57 +00:00
int toku_create_new_brtnode ( BRT t , BRTNODE * result , int height , TOKULOGGER logger ) ;
2008-03-05 18:34:32 +00:00
int toku_unpin_brtnode ( BRT brt , BRTNODE node ) ;
unsigned int toku_brtnode_which_child ( BRTNODE node , DBT * k , DBT * d , BRT t ) ;
2008-02-25 22:46:48 +00:00
/* Stuff for testing */
int toku_testsetup_leaf ( BRT brt , DISKOFF * diskoff ) ;
int toku_testsetup_nonleaf ( BRT brt , int height , DISKOFF * diskoff , int n_children , DISKOFF * children , u_int32_t * subtree_fingerprints , char * * keys , int * keylens ) ;
int toku_testsetup_root ( BRT brt , DISKOFF diskoff ) ;
int toku_testsetup_get_sersize ( BRT brt , DISKOFF diskoff ) ; // Return the size on disk.
int toku_testsetup_insert_to_leaf ( BRT brt , DISKOFF diskoff , char * key , int keylen , char * val , int vallen , u_int32_t * leaf_fingerprint ) ;
int toku_testsetup_insert_to_nonleaf ( BRT brt , DISKOFF diskoff , enum brt_cmd_type , char * key , int keylen , char * val , int vallen , u_int32_t * subtree_fingerprint ) ;
2008-03-09 02:39:37 +00:00
int toku_set_func_fsync ( int ( * fsync_function ) ( int ) ) ;
2008-04-07 01:30:25 +00:00
// These two go together to do lookups in a brtnode using the keys in a command.
struct cmd_leafval_bessel_extra {
2008-04-02 23:40:36 +00:00
BRT t ;
2008-04-07 01:30:25 +00:00
BRT_CMD cmd ;
int compare_both_keys ; // Set to 1 for DUPSORT databases that are not doing a DELETE_BOTH
2008-04-02 23:40:36 +00:00
} ;
2008-04-25 13:46:17 +00:00
int toku_cmd_leafval_bessel ( OMTVALUE leafentry , void * extra ) ;
2008-04-07 01:30:25 +00:00
int toku_brt_root_put_cmd ( BRT brt , BRT_CMD cmd , TOKULOGGER logger ) ;
2008-04-09 02:45:27 +00:00
int toku_cachefile_root_put_cmd ( CACHEFILE cf , BRT_CMD cmd , TOKULOGGER logger ) ;
2008-04-07 01:30:25 +00:00
2008-04-22 20:39:50 +00:00
int toku_omt_compress_kvspace ( OMT omt , struct mempool * memp ) ;
void * mempool_malloc_from_omt ( OMT omt , struct mempool * mp , size_t size ) ;
2008-03-19 19:23:45 +00:00
2008-04-23 04:17:28 +00:00
void toku_verify_all_in_mempool ( BRTNODE node ) ;
2008-05-04 16:56:15 +00:00
int toku_verify_brtnode ( BRT brt , DISKOFF off , bytevec lorange , ITEMLEN lolen , bytevec hirange , ITEMLEN hilen , int recurse ) ;
2008-04-30 13:23:04 +00:00
// Diff from 5 to 6: Added leafentry_estimate
# define BRT_LAYOUT_VERSION 6
2008-04-23 04:17:28 +00:00
2007-11-23 17:41:02 +00:00
# endif