Go to the source code of this file.
Data Structures | |
struct | ccn_btree_io |
Holds the methods and the associated common data. More... | |
struct | ccn_btree_node |
State associated with a btree node. More... | |
struct | ccn_btree |
State associated with a btree as a whole. More... | |
struct | ccn_btree_node_header |
Structure of a node. More... | |
struct | ccn_btree_entry_trailer |
Structure of a node entry trailer. More... | |
struct | ccn_btree_internal_payload |
Structure of the entry payload within an internal (non-leaf) node. More... | |
struct | ccn_btree_internal_entry |
Logical structure of the entry within an internal (non-leaf) node. More... | |
Defines | |
#define | CCN_BT_ACTIVITY_REFERENCE_BUMP 1 |
Increment to node->activity when node is referenced but not changed. | |
#define | CCN_BT_ACTIVITY_READ_BUMP 8 |
Increment to node->activity when node is read from disk. | |
#define | CCN_BT_ACTIVITY_UPDATE_BUMP 16 |
Increment to node->activity when node is modified. | |
#define | CCN_BT_OPEN_NODES_IDLE 5 |
Limit to the number of btree nodes kept open when idle. | |
#define | CCN_BT_OPEN_NODES_LIMIT 13 |
Limit to the number of file descriptors the btree should use at a time. | |
#define | CCN_BT_SIZE_UNITS 8 |
#define | CCN_BT_MAX_KEY_SIZE 65535 |
Maximum key size, dictated by size of above size fields. | |
#define | CCN_BT_INTERNAL_MAGIC 0xCC |
#define | CCN_BT_ENCRES(ndx, success) (2 * (ndx) + ((success) || 0)) |
#define | CCN_BT_SRCH_FOUND(res) ((res) & 1) |
#define | CCN_BT_SRCH_INDEX(res) ((res) >> 1) |
Typedefs | |
typedef int(* | ccn_btree_io_openfn )(struct ccn_btree_io *, struct ccn_btree_node *) |
Methods for external I/O of btree nodes. | |
typedef int(* | ccn_btree_io_readfn )(struct ccn_btree_io *, struct ccn_btree_node *, unsigned) |
typedef int(* | ccn_btree_io_writefn )(struct ccn_btree_io *, struct ccn_btree_node *) |
typedef int(* | ccn_btree_io_closefn )(struct ccn_btree_io *, struct ccn_btree_node *) |
typedef int(* | ccn_btree_io_destroyfn )(struct ccn_btree_io **) |
typedef unsigned | ccn_btnodeid |
Functions | |
int | ccn_btree_node_nent (struct ccn_btree_node *node) |
Number of entries within the btree node. | |
int | ccn_btree_node_level (struct ccn_btree_node *node) |
Node level (leaves are at level 0). | |
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. | |
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. | |
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_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. | |
int | ccn_btree_init_node (struct ccn_btree_node *node, int level, unsigned char nodetype, unsigned char extsz) |
Initialize the btree node. | |
int | ccn_btree_oversize (struct ccn_btree *btree, struct ccn_btree_node *node) |
Test for an oversize node. | |
int | ccn_btree_chknode (struct ccn_btree_node *node) |
Check a node for internal consistency. | |
struct ccn_btree * | ccn_btree_create (void) |
Create a new btree handle, not attached to any external files. | |
int | ccn_btree_destroy (struct ccn_btree **) |
Destroys a btree handle, shutting things down cleanly. | |
void | ccn_btree_note_error (struct ccn_btree *bt, int info) |
Keep count of noticed errors. | |
struct ccn_btree_node * | ccn_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_node * | ccn_btree_rnode (struct ccn_btree *bt, ccn_btnodeid nodeid) |
Access a btree node that is already resident. | |
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. | |
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. | |
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_split (struct ccn_btree *btree, struct ccn_btree_node *node) |
int | ccn_btree_prepare_for_update (struct ccn_btree *bt, struct ccn_btree_node *node) |
Get ready to update a btree node. | |
int | ccn_btree_check (struct ccn_btree *btree, FILE *outfp) |
Check the structure of the btree for consistency. | |
struct ccn_btree_io * | ccn_btree_io_from_directory (const char *path, struct ccn_charbuf *msgs) |
Create a btree storage layer from a directory. | |
unsigned | ccn_btree_fetchval (const unsigned char *p, int size) |
void | ccn_btree_storeval (unsigned char *p, int size, unsigned v) |
Definition in file btree.h.
#define CCN_BT_ACTIVITY_READ_BUMP 8 |
Increment to node->activity when node is read from disk.
Definition at line 111 of file btree.h.
Referenced by ccn_btree_getnode().
#define CCN_BT_ACTIVITY_REFERENCE_BUMP 1 |
Increment to node->activity when node is referenced but not changed.
Definition at line 109 of file btree.h.
Referenced by ccn_btree_getnode().
#define CCN_BT_ACTIVITY_UPDATE_BUMP 16 |
Increment to node->activity when node is modified.
Definition at line 113 of file btree.h.
Referenced by ccn_btree_prepare_for_update().
#define CCN_BT_ENCRES | ( | ndx, | |||
success | ) | (2 * (ndx) + ((success) || 0)) |
Definition at line 244 of file btree.h.
Referenced by ccn_btree_searchnode(), test_btree_lookup(), and test_btree_searchnode().
#define CCN_BT_INTERNAL_MAGIC 0xCC |
Definition at line 205 of file btree.h.
Referenced by ccn_btree_grow_a_level(), ccn_btree_split(), and seek_internal().
#define CCN_BT_MAX_KEY_SIZE 65535 |
Maximum key size, dictated by size of above size fields.
Definition at line 195 of file btree.h.
Referenced by ccn_btree_insert_entry().
#define CCN_BT_OPEN_NODES_IDLE 5 |
Limit to the number of btree nodes kept open when idle.
Definition at line 116 of file btree.h.
Referenced by r_store_index_cleaner().
#define CCN_BT_OPEN_NODES_LIMIT 13 |
Limit to the number of file descriptors the btree should use at a time.
Definition at line 118 of file btree.h.
Referenced by ccn_btree_getnode(), and r_store_index_needs_cleaning().
#define CCN_BT_SIZE_UNITS 8 |
Definition at line 193 of file btree.h.
Referenced by ccn_btree_chknode(), ccn_btree_init_node(), ccn_btree_insert_entry(), ccn_btree_node_getentry(), ccn_btree_node_getentrysize(), check_structure_size(), and seek_trailer().
#define CCN_BT_SRCH_FOUND | ( | res | ) | ((res) & 1) |
Definition at line 245 of file btree.h.
Referenced by ccn_btree_lookup_internal(), ccn_btree_next_leaf(), ccn_btree_split(), r_store_content_btree_insert(), r_store_lookup(), r_store_set_accession_from_offset(), r_sync_enumerate_action(), test_basic_btree_insert_entry(), test_btree_inserts_from_stdin(), and test_insert_content().
#define CCN_BT_SRCH_INDEX | ( | res | ) | ((res) >> 1) |
Definition at line 246 of file btree.h.
Referenced by ccn_btree_lookup_internal(), ccn_btree_next_leaf(), ccn_btree_split(), 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_insert_content(), and testhelp_count_matches().
typedef unsigned ccn_btnodeid |
typedef int(* ccn_btree_io_closefn)(struct ccn_btree_io *, struct ccn_btree_node *) |
typedef int(* ccn_btree_io_destroyfn)(struct ccn_btree_io **) |
typedef int(* ccn_btree_io_openfn)(struct ccn_btree_io *, struct ccn_btree_node *) |
Methods for external I/O of btree nodes.
These are supplied by the client, and provide an abstraction to hold the persistent representation of the btree.
Each node has a nodeid that serves as its filename. These start as 1 and are assigned consecutively. The node may correspond to a file in a file system, or to some other abstraction as appropriate.
Open should prepare for I/O to a node. It may use the iodata slot to keep track of its state, and should set iodata to a non-NULL value. It should update the count of openfds as appropriate.
Read gets bytes from the file and places it into the buffer at the corresponding position. The parameter is a limit for the max buffer size. Bytes prior to the clean mark do not need to be read. The buffer should be extended, if necessary, to hold the data. Read is not responsible for updating the clean mark.
Write puts bytes from the buffer into the file, and truncates the file according to the buffer length. Bytes prior to the clean mork do not need to be written, since they should be the same in the buffer and the file. Write is not responsible for updating the clean mark.
Close is called at the obvious time. It should free any node io state and set iodata to NULL, updating openfds as appropriate. It should not change the other parts of the node.
Negative return values indicate errors.
typedef int(* ccn_btree_io_readfn)(struct ccn_btree_io *, struct ccn_btree_node *, unsigned) |
typedef int(* ccn_btree_io_writefn)(struct ccn_btree_io *, struct ccn_btree_node *) |
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.
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
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.
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.
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.
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.
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.
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().
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.
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.
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().
struct ccn_btree_io* ccn_btree_io_from_directory | ( | const char * | path, | |
struct ccn_charbuf * | msgs | |||
) | [read] |
Create a btree storage layer from a directory.
In this implementation of the storage layer, each btree block is stored as a separate file. The files are named using the decimal representation of the nodeid.
If msgs is not NULL, diagnostics may be recorded there.
path | is the name of the directory, which must exist. |
Definition at line 57 of file ccn_btree_store.c.
Referenced by r_store_init(), test_btree_io(), and test_btree_lockfile().
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.
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.
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().
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().
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
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.
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.
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).
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 | ) |
Number of entries within the btree node.
Definition at line 139 of file ccn_btree.c.
Referenced by ccn_btree_check(), ccn_btree_chknode(), ccn_btree_insert_entry(), ccn_btree_next_leaf(), ccn_btree_oversize(), ccn_btree_prev_leaf(), ccn_btree_searchnode(), ccn_btree_split(), r_store_look(), test_btree_searchnode(), and testhelp_count_matches().
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.
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.
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.
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
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.
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.
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 | |||
) |
Definition at line 637 of file ccn_btree.c.
Referenced by r_store_content_btree_insert(), test_basic_btree_insert_entry(), and test_btree_inserts_from_stdin().
void ccn_btree_storeval | ( | unsigned char * | p, | |
int | size, | |||
unsigned | v | |||
) |
Definition at line 49 of file ccn_btree.c.