Skip to content

Commit

Permalink
Fixed a bug with determining the required node count.
Browse files Browse the repository at this point in the history
The algorithm used the best possible case for the calculation, instead of the
worst possible case.
  • Loading branch information
derickr committed Jan 30, 2012
1 parent 0b5afaf commit 236c108
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions btree/btree.c
Expand Up @@ -9,6 +9,8 @@
#include <unistd.h>
#include <errno.h>

#define NODE_COUNT(nr_of_items, order) (((2 * nr_of_items) / order) + 1)

/* Forwards declarations */
static void btree_dump_node(btree_tree *t, btree_node *node);
static btree_node* btree_find_branch(btree_tree *t, btree_node *node, uint64_t key, uint32_t *i);
Expand All @@ -32,7 +34,7 @@ static int btree_allocate(char *path, uint32_t order, uint32_t nr_of_items, size
* Nodes: 4096 * (nr_of_items / order)
* Data: nr_of_items * (length-marker + ts + data_size + '\0' delimiter)
*/
node_count = (nr_of_items / order) + 1;
node_count = NODE_COUNT(nr_of_items, order);
bytes = BTREE_HEADER_SIZE + (node_count * 4096) + (nr_of_items * (sizeof(size_t) + sizeof(time_t) + data_size + 1));

fd = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
Expand Down Expand Up @@ -102,7 +104,7 @@ static void btree_init(btree_tree *tree)
tree->header->version = 1;
tree->header->next_node_idx = 0;
tree->header->next_data_idx = 0;
tree->header->node_count = (tree->header->max_items / tree->header->order) + 1;
tree->header->node_count = NODE_COUNT(tree->header->max_items, tree->header->order);
tree->data = tree->mmap + BTREE_HEADER_SIZE + (tree->header->node_count * 4096);

tmp_node = btree_allocate_node(tree);
Expand Down
2 changes: 1 addition & 1 deletion tests/getinfo-01.phpt
Expand Up @@ -18,5 +18,5 @@ Array
[max_items] => 64
[item_count] => 0
[item_size] => 128
[node_count] => 22
[node_count] => 43
)
2 changes: 1 addition & 1 deletion tests/stumblecache-ctor-01.phpt
Expand Up @@ -42,7 +42,7 @@ array(6) {
["item_size"]=>
int(32)
["node_count"]=>
int(342)
int(683)
}
StumbleCache::__construct(): The order should be in between 3 and 102, 2 was requested.
StumbleCache::__construct(): The max_items should setting be in between 1 and 1073741824, 3729846630 was requested.
Expand Down

0 comments on commit 236c108

Please sign in to comment.