Skip to content

Commit

Permalink
bplus: bp_update, bp_bulk_update
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Jan 28, 2012
1 parent 33c24f5 commit 910278a
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 32 deletions.
46 changes: 41 additions & 5 deletions include/bplus.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ typedef struct bp_key_s bp_key_t;
typedef struct bp_key_s bp_value_t;

typedef int (*bp_compare_cb)(const bp_key_t* a, const bp_key_t* b);
typedef int (*bp_update_cb)(void* arg,
const bp_value_t* previous,
const bp_value_t* value);
typedef void (*bp_range_cb)(void* arg,
const bp_key_t* key,
const bp_value_t* value);
Expand Down Expand Up @@ -47,14 +50,31 @@ int bp_get_previous(bp_db_t* tree,
bp_value_t* previous);

/*
* Set one value by key
* Set one value by key (without solving conflicts, overwrite)
*/
int bp_set(bp_db_t* tree,
const bp_key_t* key,
const bp_value_t* value);
int bp_sets(bp_db_t* tree,
const char* key,
const char* value);

/*
* Update or create value by key (with solving conflicts)
*/
int bp_set(bp_db_t* tree, const bp_key_t* key, const bp_value_t* value);
int bp_sets(bp_db_t* tree, const char* key, const char* value);
int bp_update(bp_db_t* tree,
const bp_key_t* key,
const bp_value_t* value,
bp_update_cb update_cb,
void* arg);
int bp_updates(bp_db_t* tree,
const char* key,
const char* value,
bp_update_cb update_cb,
void* arg);

/*
* Set multiple values by key
*
* Set multiple values by keys
*/
int bp_bulk_set(bp_db_t* tree,
const uint64_t count,
Expand All @@ -65,6 +85,22 @@ int bp_bulk_sets(bp_db_t* tree,
const char** keys,
const char** values);

/*
* Update multiple values by keys
*/
int bp_bulk_update(bp_db_t* tree,
const uint64_t count,
const bp_key_t** keys,
const bp_value_t** values,
bp_update_cb update_cb,
void* arg);
int bp_bulk_updates(bp_db_t* tree,
const uint64_t count,
const char** keys,
const char** values,
bp_update_cb update_cb,
void* arg);

/*
* Remove one value by key
*/
Expand Down
12 changes: 9 additions & 3 deletions include/private/pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ int bp__page_save_value(bp_db_t* t,
const uint64_t index,
const int cmp,
const bp_key_t* key,
const bp_value_t* value);
const bp_value_t* value,
bp_update_cb cb,
void* arg);

int bp__page_search(bp_db_t* t,
bp__page_t* page,
Expand All @@ -66,13 +68,17 @@ int bp__page_get_range(bp_db_t* t,
int bp__page_insert(bp_db_t* t,
bp__page_t* page,
const bp_key_t* key,
const bp_value_t* value);
const bp_value_t* value,
bp_update_cb update_cb,
void* arg);
int bp__page_bulk_insert(bp_db_t* t,
bp__page_t* page,
const bp_key_t* limit,
uint64_t* count,
bp_key_t** keys,
bp_value_t** values);
bp_value_t** values,
bp_update_cb update_cb,
void* arg);
int bp__page_remove(bp_db_t* t, bp__page_t* page, const bp_key_t* key);
int bp__page_copy(bp_db_t* source, bp_db_t* target, bp__page_t* page);

Expand Down
76 changes: 59 additions & 17 deletions src/bplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ int bp_get_previous(bp_db_t* tree,
}


int bp_set(bp_db_t* tree, const bp_key_t* key, const bp_value_t* value) {
int bp_update(bp_db_t* tree,
const bp_key_t* key,
const bp_value_t* value,
bp_update_cb update_cb,
void* arg) {
int ret;

bp__rwlock_wrlock(&tree->rwlock);

ret = bp__page_insert(tree, tree->head.page, key, value);
ret = bp__page_insert(tree, tree->head.page, key, value, update_cb, arg);
if (ret == BP_OK) {
ret = bp__tree_write_head((bp__writer_t*) tree, NULL);
}
Expand All @@ -108,10 +112,12 @@ int bp_set(bp_db_t* tree, const bp_key_t* key, const bp_value_t* value) {
}


int bp_bulk_set(bp_db_t* tree,
const uint64_t count,
const bp_key_t** keys,
const bp_value_t** values) {
int bp_bulk_update(bp_db_t* tree,
const uint64_t count,
const bp_key_t** keys,
const bp_value_t** values,
bp_update_cb update_cb,
void* arg) {
int ret;
bp_key_t* keys_iter = (bp_key_t*) *keys;
bp_value_t* values_iter = (bp_value_t*) *values;
Expand All @@ -124,7 +130,9 @@ int bp_bulk_set(bp_db_t* tree,
NULL,
&left,
&keys_iter,
&values_iter);
&values_iter,
update_cb,
arg);
if (ret == BP_OK) {
ret = bp__tree_write_head((bp__writer_t*) tree, NULL);
}
Expand All @@ -135,6 +143,19 @@ int bp_bulk_set(bp_db_t* tree,
}


int bp_set(bp_db_t* tree, const bp_key_t* key, const bp_value_t* value) {
return bp_update(tree, key, value, NULL, NULL);
}


int bp_bulk_set(bp_db_t* tree,
const uint64_t count,
const bp_key_t** keys,
const bp_value_t** values) {
return bp_bulk_update(tree, count, keys, values, NULL, NULL);
}


int bp_remove(bp_db_t* tree, const bp_key_t* key) {
int ret;

Expand Down Expand Up @@ -249,21 +270,32 @@ int bp_gets(bp_db_t* tree, const char* key, char** value) {
}


int bp_sets(bp_db_t* tree, const char* key, const char* value) {
int bp_updates(bp_db_t* tree,
const char* key,
const char* value,
bp_update_cb update_cb,
void* arg) {
bp_key_t bkey;
bp_value_t bvalue;

BP__STOVAL(key, bkey);
BP__STOVAL(value, bvalue);

return bp_set(tree, &bkey, &bvalue);
return bp_update(tree, &bkey, &bvalue, update_cb, arg);
}


int bp_bulk_sets(bp_db_t* tree,
const uint64_t count,
const char** keys,
const char** values) {
int bp_sets(bp_db_t* tree, const char* key, const char* value) {
return bp_updates(tree, key, value, NULL, NULL);
}


int bp_bulk_updates(bp_db_t* tree,
const uint64_t count,
const char** keys,
const char** values,
bp_update_cb update_cb,
void* arg) {
int ret;
bp_key_t* bkeys;
bp_value_t* bvalues;
Expand All @@ -285,10 +317,12 @@ int bp_bulk_sets(bp_db_t* tree,
BP__STOVAL(values[i], bvalues[i]);
}

ret = bp_bulk_set(tree,
count,
(const bp_key_t**) &bkeys,
(const bp_value_t**) &bvalues);
ret = bp_bulk_update(tree,
count,
(const bp_key_t**) &bkeys,
(const bp_value_t**) &bvalues,
update_cb,
arg);

free(bkeys);
free(bvalues);
Expand All @@ -297,6 +331,14 @@ int bp_bulk_sets(bp_db_t* tree,
}


int bp_bulk_sets(bp_db_t* tree,
const uint64_t count,
const char** keys,
const char** values) {
return bp_bulk_updates(tree, count, keys, values, NULL, NULL);
}


int bp_removes(bp_db_t* tree, const char* key) {
bp_key_t bkey;

Expand Down
41 changes: 34 additions & 7 deletions src/pages.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ int bp__page_save_value(bp_db_t* t,
const uint64_t index,
const int cmp,
const bp_key_t* key,
const bp_value_t* value) {
const bp_value_t* value,
bp_update_cb update_cb,
void* arg) {
int ret;
bp__kv_t previous, tmp;

Expand Down Expand Up @@ -385,19 +387,28 @@ int bp__page_get_range(bp_db_t* t,
int bp__page_insert(bp_db_t* t,
bp__page_t* page,
const bp_key_t* key,
const bp_value_t* value) {
const bp_value_t* value,
bp_update_cb update_cb,
void* arg) {
int ret;
bp__page_search_res_t res;
ret = bp__page_search(t, page, key, kLoad, &res);
if (ret != BP_OK) return ret;

if (res.child == NULL) {
/* store value in db file to get offset and config */
ret = bp__page_save_value(t, page, res.index, res.cmp, key, value);
ret = bp__page_save_value(t,
page,
res.index,
res.cmp,
key,
value,
update_cb,
arg);
if (ret != BP_OK) return ret;
} else {
/* Insert kv in child page */
ret = bp__page_insert(t, res.child, key, value);
ret = bp__page_insert(t, res.child, key, value, update_cb, arg);

/* kv was inserted but page is full now */
if (ret == BP_ESPLITPAGE) {
Expand Down Expand Up @@ -440,7 +451,9 @@ int bp__page_bulk_insert(bp_db_t* t,
const bp_key_t* limit,
uint64_t* count,
bp_key_t** keys,
bp_value_t** values) {
bp_value_t** values,
bp_update_cb update_cb,
void* arg) {
int ret;
bp__page_search_res_t res;

Expand All @@ -452,7 +465,14 @@ int bp__page_bulk_insert(bp_db_t* t,

if (res.child == NULL) {
/* store value in db file to get offset and config */
ret = bp__page_save_value(t, page, res.index, res.cmp, *keys, *values);
ret = bp__page_save_value(t,
page,
res.index,
res.cmp,
*keys,
*values,
update_cb,
arg);
if (ret != BP_OK) return ret;

*keys = *keys + 1;
Expand All @@ -466,7 +486,14 @@ int bp__page_bulk_insert(bp_db_t* t,
new_limit = (bp_key_t*) &page->keys[res.index + 1];
}

ret = bp__page_bulk_insert(t, res.child, new_limit, count, keys, values);
ret = bp__page_bulk_insert(t,
res.child,
new_limit,
count,
keys,
values,
update_cb,
arg);
if (ret == BP_ESPLITPAGE) {
ret = bp__page_split(t, page, res.index, res.child);
} else if (ret == BP_OK) {
Expand Down

0 comments on commit 910278a

Please sign in to comment.