ccn_btree.c File Reference

Go to the source code of this file.

Defines

#define MYFETCH(p, f)   ccn_btree_fetchval(&((p)->f[0]), sizeof((p)->f))
 BTree implementation.
#define MYSTORE(p, f, v)   ccn_btree_storeval(&((p)->f[0]), sizeof((p)->f), (v))
#define MIN_NODE_BYTES   (sizeof(struct ccn_btree_node_header) + sizeof(struct ccn_btree_entry_trailer))
 Minimum size of a non-empty node.
#define MSG(fmt,...)   ((void)0)
#define CCN_BTREE_MAGIC   0x53ade78
#define CCN_BTREE_VERSION   1
#define CCN_BTREE_MAX_NODE_BYTES   (8U<<20)
#define MSG(fmt,...)   if (outfp != NULL) fprintf(outfp, fmt "\n", __VA_ARGS__)

Functions

unsigned ccn_btree_fetchval (const unsigned char *p, int size)
void ccn_btree_storeval (unsigned char *p, int size, unsigned v)
static struct
ccn_btree_entry_trailer
seek_trailer (struct ccn_btree_node *node, int i)
 Find the entry trailer associated with entry i of the btree node.
void * ccn_btree_node_getentry (size_t payload_bytes, struct ccn_btree_node *node, int i)
 Get the address of the indexed entry within the node.
static struct
ccn_btree_internal_payload
seek_internal (struct ccn_btree_node *node, int i)
 Get the address of entry within an internal (non-leaf) node.
int ccn_btree_node_nent (struct ccn_btree_node *node)
 Number of entries within the btree node.
int ccn_btree_node_getentrysize (struct ccn_btree_node *node)
 Size, in bytes, of entries within the node.
int ccn_btree_node_payloadsize (struct ccn_btree_node *node)
 Size, in bytes, of payloads within the node.
int ccn_btree_node_level (struct ccn_btree_node *node)
 Node level (leaves are at level 0).
int ccn_btree_key_fetch (struct ccn_charbuf *dst, struct ccn_btree_node *node, int i)
 Fetch the key within the indexed entry of node.
int ccn_btree_key_append (struct ccn_charbuf *dst, struct ccn_btree_node *node, int i)
 Append the key within the indexed entry of node to dst.
int ccn_btree_compare (const unsigned char *key, size_t size, struct ccn_btree_node *node, int i)
 Compare given key with the key in the indexed entry of the node.
int ccn_btree_searchnode (const unsigned char *key, size_t size, struct ccn_btree_node *node)
 Search the node for the given key.
int ccn_btree_lookup (struct ccn_btree *btree, const unsigned char *key, size_t size, struct ccn_btree_node **leafp)
 Do a btree lookup, starting from the default root.
int ccn_btree_lookup_internal (struct ccn_btree *btree, struct ccn_btree_node *root, int stoplevel, const unsigned char *key, size_t size, struct ccn_btree_node **ansp)
 Do a btree lookup, starting from the provided root and stopping at stoplevel.
static void scan_reusable (const unsigned char *key, size_t keysize, struct ccn_btree_node *node, int ndx, unsigned reuse[2])
int ccn_btree_insert_entry (struct ccn_btree_node *node, int i, const unsigned char *key, size_t keysize, void *payload, size_t payload_bytes)
 Insert a new entry into a node.
static struct ccn_btree_nodeccn_btree_grow_a_level (struct ccn_btree *btree, struct ccn_btree_node *node)
 Given an old root, add a level to the tree to prepare for a split.
int ccn_btree_oversize (struct ccn_btree *btree, struct ccn_btree_node *node)
 Test for an oversize node.
int ccn_btree_split (struct ccn_btree *btree, struct ccn_btree_node *node)
int ccn_btree_next_leaf (struct ccn_btree *btree, struct ccn_btree_node *node, struct ccn_btree_node **ansp)
 Find the leaf that comes after the given node.
int ccn_btree_prev_leaf (struct ccn_btree *btree, struct ccn_btree_node *node, struct ccn_btree_node **ansp)
 Find the leaf that comes before the given node.
int ccn_btree_close_node (struct ccn_btree *btree, struct ccn_btree_node *node)
 Write out any pending changes, mark the node clean, and release node iodata.
static void finalize_node (struct hashtb_enumerator *e)
void ccn_btree_note_error (struct ccn_btree *bt, int info)
 Keep count of noticed errors.
struct ccn_btreeccn_btree_create (void)
 Create a new btree handle, not attached to any external files.
int ccn_btree_destroy (struct ccn_btree **pbt)
 Destroys a btree handle, shutting things down cleanly.
int ccn_btree_init_node (struct ccn_btree_node *node, int level, unsigned char nodetype, unsigned char extsz)
 Initialize the btree node.
struct ccn_btree_nodeccn_btree_getnode (struct ccn_btree *bt, ccn_btnodeid nodeid, ccn_btnodeid parentid)
 Access a btree node, creating or reading it if necessary.
struct ccn_btree_nodeccn_btree_rnode (struct ccn_btree *bt, ccn_btnodeid nodeid)
 Access a btree node that is already resident.
int ccn_btree_chknode (struct ccn_btree_node *node)
 Check a node for internal consistency.
int ccn_btree_prepare_for_update (struct ccn_btree *bt, struct ccn_btree_node *node)
 Get ready to update a btree node.
static int compare_lexical (struct ccn_charbuf *a, struct ccn_charbuf *b)
static void ccn_charbuf_append_escaped (struct ccn_charbuf *dst, struct ccn_charbuf *src)
int ccn_btree_check (struct ccn_btree *btree, FILE *outfp)
 Check the structure of the btree for consistency.


Define Documentation

#define CCN_BTREE_MAGIC   0x53ade78

#define CCN_BTREE_MAX_NODE_BYTES   (8U<<20)

Definition at line 1051 of file ccn_btree.c.

Referenced by ccn_btree_getnode().

#define CCN_BTREE_VERSION   1

Definition at line 911 of file ccn_btree.c.

Referenced by ccn_btree_chknode(), and ccn_btree_init_node().

#define MIN_NODE_BYTES   (sizeof(struct ccn_btree_node_header) + sizeof(struct ccn_btree_entry_trailer))

Minimum size of a non-empty node.

Definition at line 60 of file ccn_btree.c.

Referenced by ccn_btree_node_getentrysize(), ccn_btree_node_nent(), and seek_trailer().

#define MSG ( fmt,
...   )     if (outfp != NULL) fprintf(outfp, fmt "\n", __VA_ARGS__)

Definition at line 1283 of file ccn_btree.c.

#define MSG ( fmt,
...   )     ((void)0)

Definition at line 1283 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_grow_a_level(), and ccn_btree_split().

#define MYFETCH ( p,
 )     ccn_btree_fetchval(&((p)->f[0]), sizeof((p)->f))

#define MYSTORE ( p,
f,
 )     ccn_btree_storeval(&((p)->f[0]), sizeof((p)->f), (v))


Function Documentation

int ccn_btree_check ( struct ccn_btree btree,
FILE *  outfp 
)

Check the structure of the btree for consistency.

If outfp is not NULL, information about structure will be written.

Returns:
-1 if an error was found.

Definition at line 1292 of file ccn_btree.c.

Referenced by r_store_init(), test_btree_inserts_from_stdin(), and test_btree_lookup().

int ccn_btree_chknode ( struct ccn_btree_node node  ) 

Check a node for internal consistency.

Sets or clears node->corrupt as appropriate. In case of success, sets the correct value for node->freelow

Returns:
old value of node->corrupt if the node looks OK, otherwise -1

Definition at line 1145 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_getnode(), ccn_btree_insert_entry(), ccn_btree_prepare_for_update(), ccn_btree_split(), example_btree_small(), r_store_index_cleaner(), test_basic_btree_insert_entry(), test_btree_chknode(), test_btree_inserts_from_stdin(), and test_insert_content().

int ccn_btree_close_node ( struct ccn_btree btree,
struct ccn_btree_node node 
)

Write out any pending changes, mark the node clean, and release node iodata.

Retains the cached node data in memory.

Returns:
0 for success or -1 for error.

Definition at line 921 of file ccn_btree.c.

Referenced by ccn_btree_check(), finalize_node(), and r_store_index_cleaner().

int ccn_btree_compare ( const unsigned char *  key,
size_t  size,
struct ccn_btree_node node,
int  i 
)

Compare given key with the key in the indexed entry of the node.

The comparison is a standard lexicographic one on unsigned bytes; that is, there is no assumption of what the bytes actually encode.

The special return value -9999 indicates the key is a strict prefix. This does not matter to the btree lookup, but is useful for higher levels.

Returns:
negative, zero, or positive to indicate less, equal, or greater

Definition at line 266 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_searchnode(), test_btree_compare(), and testhelp_count_matches().

struct ccn_btree* ccn_btree_create ( void   )  [read]

Create a new btree handle, not attached to any external files.

Returns:
new handle, or NULL in case of error.

Definition at line 973 of file ccn_btree.c.

Referenced by example_btree_small(), r_store_init(), test_btree_init(), test_btree_inserts_from_stdin(), and test_insert_content().

int ccn_btree_destroy ( struct ccn_btree **  pbt  ) 

Destroys a btree handle, shutting things down cleanly.

Returns:
a negative value in case of error.

Definition at line 1001 of file ccn_btree.c.

Referenced by r_store_final(), r_store_init(), test_basic_btree_insert_entry(), test_btree_init(), test_btree_inserts_from_stdin(), and test_btree_lookup().

unsigned ccn_btree_fetchval ( const unsigned char *  p,
int  size 
)

Definition at line 35 of file ccn_btree.c.

struct ccn_btree_node* ccn_btree_getnode ( struct ccn_btree bt,
ccn_btnodeid  nodeid,
ccn_btnodeid  parentid 
) [read]

Access a btree node, creating or reading it if necessary.

Care should be taken to not store the node handle in data structures, since it will become invalid when the node gets flushed from the resident cache.

Returns:
node handle

Definition at line 1063 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_grow_a_level(), ccn_btree_lookup(), ccn_btree_lookup_internal(), ccn_btree_next_leaf(), ccn_btree_prev_leaf(), ccn_btree_split(), example_btree_small(), r_store_content_btree_insert(), r_store_init(), test_btree_init(), test_btree_inserts_from_stdin(), and test_insert_content().

static struct ccn_btree_node* ccn_btree_grow_a_level ( struct ccn_btree btree,
struct ccn_btree_node node 
) [static, read]

Given an old root, add a level to the tree to prepare for a split.

Returns:
node with a new nodeid, new singleton root, and the old contents.

Definition at line 575 of file ccn_btree.c.

Referenced by ccn_btree_split().

int ccn_btree_init_node ( struct ccn_btree_node node,
int  level,
unsigned char  nodetype,
unsigned char  extsz 
)

Initialize the btree node.

It is the caller's responsibility to be sure that the node does not contain any useful information.

Returns:
-1 for error, 0 for success

Definition at line 1029 of file ccn_btree.c.

Referenced by ccn_btree_grow_a_level(), ccn_btree_split(), r_store_init(), test_btree_inserts_from_stdin(), and test_insert_content().

int ccn_btree_insert_entry ( struct ccn_btree_node node,
int  i,
const unsigned char *  key,
size_t  keysize,
void *  payload,
size_t  payload_bytes 
)

Insert a new entry into a node.

The caller is responsible for providing the correct index i, which will become the index of the new entry.

The caller is also responsible for triggering a split.

Returns:
the new entry count, or -1 in case of error.

Definition at line 464 of file ccn_btree.c.

Referenced by ccn_btree_grow_a_level(), ccn_btree_insert_content(), ccn_btree_split(), test_basic_btree_insert_entry(), and test_btree_inserts_from_stdin().

int ccn_btree_key_append ( struct ccn_charbuf dst,
struct ccn_btree_node node,
int  i 
)

Append the key within the indexed entry of node to dst.

Returns:
-1 in case of error

Definition at line 226 of file ccn_btree.c.

Referenced by ccn_btree_key_fetch(), and test_btree_key_fetch().

int ccn_btree_key_fetch ( struct ccn_charbuf dst,
struct ccn_btree_node node,
int  i 
)

Fetch the key within the indexed entry of node.

Returns:
-1 in case of error

Definition at line 213 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_match_interest(), ccn_btree_next_leaf(), ccn_btree_split(), r_store_look(), and test_btree_key_fetch().

int ccn_btree_lookup ( struct ccn_btree btree,
const unsigned char *  key,
size_t  size,
struct ccn_btree_node **  leafp 
)

Do a btree lookup, starting from the default root.

In the absence of errors, if *leafp is not NULL the handle for the appropriate leaf node will be stored. See ccn_btree_getnode() for warning about lifetime of the resulting pointer.

The return value is encoded as for ccn_btree_searchnode().

Returns:
CCN_BT_ENCRES(index, success) indication, or -1 for an error.

Definition at line 366 of file ccn_btree.c.

Referenced by r_store_content_btree_insert(), r_store_look(), r_store_lookup(), r_store_set_accession_from_offset(), r_sync_enumerate_action(), test_basic_btree_insert_entry(), test_btree_inserts_from_stdin(), test_btree_lookup(), test_insert_content(), and testhelp_count_matches().

int ccn_btree_lookup_internal ( struct ccn_btree btree,
struct ccn_btree_node root,
int  stoplevel,
const unsigned char *  key,
size_t  size,
struct ccn_btree_node **  ansp 
)

Do a btree lookup, starting from the provided root and stopping at stoplevel.

In the absence of errors, if *ansp is not NULL the handle for the appropriate node will be stored. See ccn_btree_getnode() for warning about lifetime of the resulting pointer.

The return value is encoded as for ccn_btree_searchnode().

Returns:
CCN_BT_ENCRES(index, success) indication, or -1 for an error.

Definition at line 390 of file ccn_btree.c.

Referenced by ccn_btree_lookup(), and ccn_btree_next_leaf().

int ccn_btree_next_leaf ( struct ccn_btree btree,
struct ccn_btree_node node,
struct ccn_btree_node **  ansp 
)

Find the leaf that comes after the given node.

This may be used to walk though the leaf nodes in order. If success, sets *ansp to a leaf pointer or NULL

Returns:
0 if at end, 1 if *ansp is not NULL, -1 if error.

Definition at line 793 of file ccn_btree.c.

Referenced by r_store_look(), and test_btree_inserts_from_stdin().

void* ccn_btree_node_getentry ( size_t  payload_bytes,
struct ccn_btree_node node,
int  i 
)

Get the address of the indexed entry within the node.

payload_bytes must be divisible by CCN_BT_SIZE_UNITS.

Returns:
NULL in case of error.

Definition at line 103 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_content_cobid(), ccn_btree_content_cobsz(), ccn_btree_content_set_cobid(), ccn_btree_insert_entry(), ccn_btree_match_interest(), ccn_btree_next_leaf(), ccn_btree_prev_leaf(), ccn_btree_split(), and seek_internal().

int ccn_btree_node_getentrysize ( struct ccn_btree_node node  ) 

Size, in bytes, of entries within the node.

If there are no entries, returns 0. This size includes the entry trailer.

Returns:
size, or -1 for error

Definition at line 161 of file ccn_btree.c.

Referenced by ccn_btree_insert_entry(), and ccn_btree_node_payloadsize().

int ccn_btree_node_level ( struct ccn_btree_node node  ) 

Node level (leaves are at level 0).

Returns:
the node level, or -1 for error

Definition at line 198 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_grow_a_level(), ccn_btree_insert_entry(), ccn_btree_lookup_internal(), ccn_btree_oversize(), ccn_btree_prev_leaf(), ccn_btree_split(), scan_reusable(), and test_btree_lookup().

int ccn_btree_node_nent ( struct ccn_btree_node node  ) 

int ccn_btree_node_payloadsize ( struct ccn_btree_node node  ) 

Size, in bytes, of payloads within the node.

If there are no entries, returns 0. This does not include the entry trailer, but will include padding to a multiple of CCN_BT_SIZE_UNITS.

Returns:
size, or -1 for error

Definition at line 184 of file ccn_btree.c.

Referenced by ccn_btree_split().

void ccn_btree_note_error ( struct ccn_btree bt,
int  info 
)

Keep count of noticed errors.

Do this in one place so it is easy to set a breakpoint.

Definition at line 963 of file ccn_btree.c.

Referenced by ccn_btree_check(), ccn_btree_close_node(), ccn_btree_getnode(), ccn_btree_grow_a_level(), ccn_btree_lookup_internal(), ccn_btree_prepare_for_update(), and ccn_btree_split().

int ccn_btree_oversize ( struct ccn_btree btree,
struct ccn_btree_node node 
)

Test for an oversize node.

This takes into account both the size of a node and the count of entries.

Returns:
a boolean result.

Definition at line 623 of file ccn_btree.c.

Referenced by ccn_btree_split(), and r_store_content_btree_insert().

int ccn_btree_prepare_for_update ( struct ccn_btree bt,
struct ccn_btree_node node 
)

Get ready to update a btree node.

If applicable, open the node so that it will be in a good state to write later on.

Returns:
0 if OK, -1 for error.

Definition at line 1232 of file ccn_btree.c.

Referenced by ccn_btree_grow_a_level(), ccn_btree_split(), r_store_content_btree_insert(), and r_store_set_accession_from_offset().

int ccn_btree_prev_leaf ( struct ccn_btree btree,
struct ccn_btree_node node,
struct ccn_btree_node **  ansp 
)

Find the leaf that comes before the given node.

This may be used to walk though the leaf nodes in reverse order. If success, sets *ansp to a leaf pointer or NULL

Returns:
0 if at beginning, 1 if *ansp is not NULL, -1 if error.

Definition at line 860 of file ccn_btree.c.

Referenced by test_btree_inserts_from_stdin().

struct ccn_btree_node* ccn_btree_rnode ( struct ccn_btree bt,
ccn_btnodeid  nodeid 
) [read]

Access a btree node that is already resident.

Care should be taken to not store the node handle in data structures, since it will become invalid when the node gets flushed from the resident cache.

This call does not bump the activity counter.

Returns:
node handle, or NULL if the node is not currently resident.

Definition at line 1131 of file ccn_btree.c.

Referenced by ccn_btree_split(), r_store_index_cleaner(), test_btree_init(), and test_btree_inserts_from_stdin().

int ccn_btree_searchnode ( const unsigned char *  key,
size_t  size,
struct ccn_btree_node node 
)

Search the node for the given key.

The return value is encoded as 2 * index + (found ? 1 : 0); that is, a successful search returns an odd number and an unsuccessful search returns an even number. In the case of an unsuccessful search, the index indicates where the item would go if it were to be inserted.

Uses a binary search, so the keys in the node must be sorted and unique.

Returns:
CCN_BT_ENCRES(index, success) indication, or -1 for an error.

Definition at line 327 of file ccn_btree.c.

Referenced by ccn_btree_lookup_internal(), ccn_btree_next_leaf(), ccn_btree_split(), and test_btree_searchnode().

int ccn_btree_split ( struct ccn_btree btree,
struct ccn_btree_node node 
)

void ccn_btree_storeval ( unsigned char *  p,
int  size,
unsigned  v 
)

Definition at line 49 of file ccn_btree.c.

static void ccn_charbuf_append_escaped ( struct ccn_charbuf dst,
struct ccn_charbuf src 
) [static]

Definition at line 1267 of file ccn_btree.c.

Referenced by ccn_btree_check().

static int compare_lexical ( struct ccn_charbuf a,
struct ccn_charbuf b 
) [static]

Definition at line 1253 of file ccn_btree.c.

Referenced by ccn_btree_check().

static void finalize_node ( struct hashtb_enumerator e  )  [static]

Definition at line 946 of file ccn_btree.c.

Referenced by ccn_btree_create().

static void scan_reusable ( const unsigned char *  key,
size_t  keysize,
struct ccn_btree_node node,
int  ndx,
unsigned  reuse[2] 
) [static]

Definition at line 443 of file ccn_btree.c.

Referenced by ccn_btree_insert_entry().

static struct ccn_btree_internal_payload* seek_internal ( struct ccn_btree_node node,
int  i 
) [static, read]

Get the address of entry within an internal (non-leaf) node.

Definition at line 121 of file ccn_btree.c.

Referenced by ccn_btree_lookup_internal().

static struct ccn_btree_entry_trailer* seek_trailer ( struct ccn_btree_node node,
int  i 
) [static, read]

Find the entry trailer associated with entry i of the btree node.

Sets node->corrupt if a problem with the node's structure is discovered.

Returns:
entry trailer pointer, or NULL if there is a problem.

Definition at line 69 of file ccn_btree.c.

Referenced by ccn_btree_chknode(), ccn_btree_compare(), ccn_btree_key_append(), and ccn_btree_node_getentry().


Generated on Thu Feb 16 00:44:21 2012 for Content-Centric Networking in C by  doxygen 1.5.6