another test case and minor cleanup

This commit is contained in:
Martin Kobetic 2014-04-23 14:25:25 +00:00
parent 9d095430b4
commit ded351df92
2 changed files with 17 additions and 10 deletions

View File

@ -196,7 +196,7 @@ leaf_element *page_leaf_element(page *p, uint16_t index) {
void cursor_key_value(bolt_cursor *c, bolt_val *key, bolt_val *value, uint32_t *flags) { void cursor_key_value(bolt_cursor *c, bolt_val *key, bolt_val *value, uint32_t *flags) {
elem_ref *ref = cursor_current(c); elem_ref *ref = cursor_current(c);
// if stack is empty return null // If stack is empty return null.
if (ref == NULL) { if (ref == NULL) {
key->size = value->size = 0; key->size = value->size = 0;
key->data = value->data = NULL; key->data = value->data = NULL;
@ -204,7 +204,7 @@ void cursor_key_value(bolt_cursor *c, bolt_val *key, bolt_val *value, uint32_t *
return; return;
}; };
// Descend to the current leaf page if we're on branch page // Descend to the current leaf page if we're on branch page.
while (ref->page->flags & PAGE_BRANCH) { while (ref->page->flags & PAGE_BRANCH) {
branch_element *elem = page_branch_element(ref->page,ref->index); branch_element *elem = page_branch_element(ref->page,ref->index);
ref = cursor_push(c, elem->pgid); ref = cursor_push(c, elem->pgid);
@ -254,7 +254,7 @@ void cursor_search_leaf(bolt_cursor *c, bolt_val key) {
int rc = memcmp(key.data, ((void*)elem) + elem->pos, (elem->ksize < key.size ? elem->ksize : key.size)); int rc = memcmp(key.data, ((void*)elem) + elem->pos, (elem->ksize < key.size ? elem->ksize : key.size));
// int len = key.size > 10 ? 10 : key.size; // int len = key.size > 10 ? 10 : key.size;
// printf("\n?L rc=%d; elem=%.*s[%d]", rc, len, ((char*)elem) + elem->pos + elem->ksize - len, elem->ksize); // printf("\n?L rc=%d; elem=...%.*s[%d]", rc, len, ((char*)elem) + elem->pos + elem->ksize - len, elem->ksize);
if ((rc == 0 && key.size <= elem->ksize) || rc < 0) { if ((rc == 0 && key.size <= elem->ksize) || rc < 0) {
ref->index = i; ref->index = i;
return; return;
@ -277,17 +277,18 @@ void cursor_search_branch(bolt_cursor *c, bolt_val key) {
int rc = memcmp(key.data, ((void*)elem) + elem->pos, (elem->ksize < key.size ? elem->ksize : key.size)); int rc = memcmp(key.data, ((void*)elem) + elem->pos, (elem->ksize < key.size ? elem->ksize : key.size));
// int len = key.size > 10 ? 10 : key.size; // int len = key.size > 10 ? 10 : key.size;
// printf("\n?B rc=%d; elem=%.*s[%d]", rc, len, ((char*)elem) + elem->pos + elem->ksize - len, elem->ksize); // printf("\n?B rc=%d; elem=...%.*s[%d]", rc, len, ((char*)elem) + elem->pos + elem->ksize - len, elem->ksize);
if (rc == 0 && key.size == elem->ksize) { if (rc == 0 && key.size == elem->ksize) {
// exact match, done // Exact match, done.
ref->index = i; ref->index = i;
return; return;
} else if ((rc == 0 && key.size < elem->ksize) || rc < 0) { } else if ((rc == 0 && key.size < elem->ksize) || rc < 0) {
// if key is less than anything in this subtree we are done // If key is less than anything in this subtree we are done.
// This should really only happen for key that's less than anything in the tree.
if (i == 0) return; if (i == 0) return;
// otherwise search the previous subtree // Otherwise search the previous subtree.
cursor_search(c, key, elems[i-1].pgid); cursor_search(c, key, elems[i-1].pgid);
// didn't find anything greater than key? // Didn't find anything greater than key?
if (cursor_current(c) == ref) if (cursor_current(c) == ref)
ref->index = i; ref->index = i;
else else
@ -296,9 +297,9 @@ void cursor_search_branch(bolt_cursor *c, bolt_val key) {
} }
} }
// If nothing was greater than the key then search the last child // If nothing was greater than the key then search the last child.
cursor_search(c, key, elems[ref->page->count-1].pgid); cursor_search(c, key, elems[ref->page->count-1].pgid);
// Still didn't find anything greater than key? // If still didn't find anything greater than key, then pop the page off the stack.
if (cursor_current(c) == ref) if (cursor_current(c) == ref)
cursor_pop(c); cursor_pop(c);
else else

View File

@ -270,6 +270,12 @@ func TestCursor_Seek_Deep(t *testing.T) {
assert.Equal(t, "", string(k)) assert.Equal(t, "", string(k))
assert.Equal(t, "", string(v)) assert.Equal(t, "", string(v))
// Exact match in the middle of a branch page.
seek = fmt.Sprintf("%0*d", pgsz, 4170)
k, v, _ = c.Seek([]byte(seek))
assert.Equal(t, seek, string(k))
assert.Equal(t, seek, string(v))
return nil return nil
}) })
}) })