Commit c3f89d45 authored by Zardosht Kasheff's avatar Zardosht Kasheff

addressed #523

added functions for range tree iteration

git-svn-id: file:///svn/tokudb@2790 c7de825b-a66e-492c-adef-691d508d4ae1
parent 8e3c0bd5
......@@ -57,6 +57,12 @@ int toku_rt_create(toku_range_tree** ptree,
r = toku_rbt_init(end_cmp, &temptree->i.rbt, user_malloc, user_free, user_realloc);
if (r!=0) { goto cleanup; }
/*
* Start range tree in invalid iteration state, toku_rt_start_scan must
* be called to start iteration
*/
temptree->iter_at_beginning = FALSE;
temptree->successor_finger = NULL;
*ptree = temptree;
r = 0;
cleanup:
......@@ -181,8 +187,12 @@ int toku_rt_insert(toku_range_tree* tree, toku_range* range) {
r = toku_rbt_finger_insert(range, tree->i.rbt, insert_finger);
if (r!=0) { goto cleanup; }
r = 0;
tree->numelements++;
/*
* invalidate iteration, because we have inserted node
*/
tree->successor_finger = NULL;
r = 0;
cleanup:
return r;
}
......@@ -219,8 +229,12 @@ int toku_rt_delete(toku_range_tree* tree, toku_range* range) {
r = toku_rbt_finger_delete(delete_finger, tree->i.rbt);
if (r!=0) { goto cleanup; }
r = 0;
/*
* invalidate iteration, because we have deleted node
*/
tree->successor_finger = NULL;
tree->numelements--;
r = 0;
cleanup:
return r;
}
......@@ -327,3 +341,43 @@ int toku_rt_get_size(toku_range_tree* tree, u_int32_t* size) {
*size = tree->numelements;
return 0;
}
void toku_rt_start_scan (toku_range_tree* range_tree) {
range_tree->iter_at_beginning = TRUE;
range_tree->successor_finger = NULL;
return;
}
int toku_rt_next (toku_range_tree* range_tree, toku_range* out_range) {
int r = ENOSYS;
toku_range* ret_range = NULL;
struct toku_rbt_node* ignore_insert = NULL;
if (!range_tree || !out_range) { r = EINVAL; goto cleanup; }
/* Check to see if range tree is in invalid iteation state */
if (!range_tree->iter_at_beginning && !range_tree->successor_finger)
{ r = EDOM; goto cleanup; }
if (range_tree->iter_at_beginning) {
r = toku_rbt_lookup(RB_LUFIRST, NULL, range_tree->i.rbt,
&ignore_insert, &range_tree->successor_finger, &ret_range);
if (r != 0) { goto cleanup; }
}
else {
/*
* If there is no successor because we have arrived at the end of the iteration,
* or because of some unexpected error, ret_range will have the value of NULL,
* which we want to return to the user in such cases
*/
r = toku_rbt_finger_successor(&range_tree->successor_finger, &ret_range);
if (r != 0) { goto cleanup; }
}
out_range->left = ret_range->left;
out_range->right = ret_range->right;
out_range->data = ret_range->data;
r = 0;
cleanup:
return r;
}
......@@ -35,6 +35,25 @@ struct __toku_range_tree {
/** The user realloc function */
void* (*realloc)(void*, size_t);
/*
* if iter_at_beginning is TRUE, then iteration is at beginning,
* if iter_at_beginning is FALSE and successor_finger is NOT NULL,
* iteration is ongoing,
* if iter_at_beginning is FALSE and successor_finger is NULL,
* iteration has ended or in invalid state.
*/
/*
* BOOL that says if iteration is at beginning.
* If at beginning, toku_rt_next returns first element
* in the range_tree, otherwise returns successor of curr_range
*/
BOOL iter_at_beginning;
/**
* The range we are currently at in the iterator, if it is set to NULL, that means
* the iteration is no longer valid and/or the iteration is done
* */
struct toku_rbt_node* successor_finger;
toku_range_tree_local i;
};
......
......@@ -207,7 +207,7 @@ int toku_rt_predecessor(toku_range_tree* tree, toku_point* point,
- 0: Success.
- EINVAL: If any pointer argument is NULL.
If tree allows overlaps.
- Other exit codes may be forwarded from underlying system calls.
- Other exit codes may be forwarded from underlying system calls.
*/
int toku_rt_successor(toku_range_tree* tree, toku_point* point,
toku_range* succ, BOOL* wasfound);
......@@ -223,5 +223,9 @@ int toku_rt_successor(toku_range_tree* tree, toku_point* point,
- EINVAL: If any pointer argument is NULL.
*/
int toku_rt_get_size(toku_range_tree* tree, u_int32_t* size);
void toku_rt_start_scan (toku_range_tree* range_tree);
int toku_rt_next (toku_range_tree* range_tree, toku_range* out_range);
#endif /* #if !defined(TOKU_RANGE_TREE_H) */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment