all tests pass

master
Martin Kobetic 2014-04-18 00:14:56 +00:00
parent 9a3d0db1b2
commit 1bead4401c
2 changed files with 22 additions and 14 deletions

View File

@ -152,7 +152,7 @@ void bolt_cursor_next(bolt_cursor *c, bolt_val *key, bolt_val *value, uint32_t *
void bolt_cursor_seek(bolt_cursor *c, bolt_val seek, bolt_val *key, bolt_val *value, uint32_t *flags) { void bolt_cursor_seek(bolt_cursor *c, bolt_val seek, bolt_val *key, bolt_val *value, uint32_t *flags) {
// Start from root page/node and traverse to correct page. // Start from root page/node and traverse to correct page.
cursor_push(c, c->root); cursor_push(c, c->root);
cursor_search(c, seek, c->root); if (seek.size > 0) cursor_search(c, seek, c->root);
elem_ref *ref = cursor_current(c); elem_ref *ref = cursor_current(c);
// If the cursor is pointing to the end of page then return nil. // If the cursor is pointing to the end of page then return nil.
@ -244,7 +244,6 @@ void cursor_search(bolt_cursor *c, bolt_val key, pgid id) {
// Push page onto the cursor stack. // Push page onto the cursor stack.
elem_ref *ref = cursor_push(c, id); elem_ref *ref = cursor_push(c, id);
printf("search page id=%d depth=%d\n", (int)id, c->top);
// If we're on a leaf page/node then find the specific node. // If we're on a leaf page/node then find the specific node.
if (ref->page->flags & PAGE_LEAF) { if (ref->page->flags & PAGE_LEAF) {
cursor_search_leaf(c, key); cursor_search_leaf(c, key);
@ -268,13 +267,13 @@ void cursor_search_leaf(bolt_cursor *c, bolt_val key) {
// printf("? %.*s | %.*s\n", key.size, key.data, elem->ksize, ((void*)elem) + elem->pos); // printf("? %.*s | %.*s\n", key.size, key.data, elem->ksize, ((void*)elem) + elem->pos);
// printf("rc=%d; key.size(%d) >= elem->ksize(%d)\n", rc, key.size, elem->ksize); // printf("rc=%d; key.size(%d) >= elem->ksize(%d)\n", rc, key.size, elem->ksize);
if (key.size == 0 || (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;
} }
} }
// If nothing was matched then pop the current page off the stack. // If nothing was greater than the key then pop the current page off the stack.
cursor_pop(c); cursor_pop(c);
} }
@ -289,16 +288,25 @@ void cursor_search_branch(bolt_cursor *c, bolt_val key) {
branch_element *elem = &elems[i]; branch_element *elem = &elems[i];
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));
if (key.size == 0 || (rc == 0 && key.size >= elem->ksize) || rc < 0) { if (rc == 0 && key.size == elem->ksize) {
// exact match, done
ref->index = i; ref->index = i;
cursor_search(c, key, elem->pgid); return;
if (cursor_current(c) == ref) ref->index++; } else if ((rc == 0 && key.size < elem->ksize) || rc < 0) {
if (ref->index < ref->page->count) return; // if key is less than anything in this subtree we are done
break; if (i == 0) return;
// otherwise search the previous subtree
cursor_search(c, key, elems[i-1].pgid);
// didn't find anything greater than key?
if (cursor_current(c) == ref)
ref->index = i;
else
ref->index = i-1;
return;
} }
} }
// If nothing was matched then pop the current page off the stack. // If nothing was greater than the key then pop the current page off the stack.
cursor_pop(c); cursor_pop(c);
} }

View File

@ -153,22 +153,22 @@ func TestCursor_Seek_Large(t *testing.T) {
db.View(func(tx *bolt.Tx) error { db.View(func(tx *bolt.Tx) error {
c := NewCursor(tx.Bucket([]byte("widgets"))) c := NewCursor(tx.Bucket([]byte("widgets")))
fmt.Println("// Exact match should go to the key.") // Exact match should go to the key.
k, v, _ := c.Seek([]byte("05000\000")) k, v, _ := c.Seek([]byte("05000\000"))
assert.Equal(t, "05000\000", string(k)) assert.Equal(t, "05000\000", string(k))
assert.Equal(t, fmt.Sprintf("%020d", 5000), string(v)) assert.Equal(t, fmt.Sprintf("%020d", 5000), string(v))
fmt.Println("// Inexact match should go to the next key.") // Inexact match should go to the next key.
k, v, _ = c.Seek([]byte("07495\000")) k, v, _ = c.Seek([]byte("07495\000"))
assert.Equal(t, "07500\000", string(k)) assert.Equal(t, "07500\000", string(k))
assert.Equal(t, fmt.Sprintf("%020d", 7500), string(v)) assert.Equal(t, fmt.Sprintf("%020d", 7500), string(v))
fmt.Println("// Low key should go to the first key.") // Low key should go to the first key.
k, v, _ = c.Seek([]byte("00000\000")) k, v, _ = c.Seek([]byte("00000\000"))
assert.Equal(t, "00010\000", string(k)) assert.Equal(t, "00010\000", string(k))
assert.Equal(t, fmt.Sprintf("%020d", 10), string(v)) assert.Equal(t, fmt.Sprintf("%020d", 10), string(v))
fmt.Println("// High key should return no key.") // High key should return no key.
k, v, _ = c.Seek([]byte("40000\000")) k, v, _ = c.Seek([]byte("40000\000"))
assert.Equal(t, "", string(k)) assert.Equal(t, "", string(k))
assert.Equal(t, "", string(v)) assert.Equal(t, "", string(v))