mirror of https://github.com/etcd-io/bbolt.git
tests pass
parent
bd8c09e895
commit
846cb9db03
12
c/cursor.go
12
c/cursor.go
|
@ -120,9 +120,11 @@ void bolt_cursor_first(bolt_cursor *c, bolt_val *key, bolt_val *value, uint32_t
|
|||
|
||||
// Positions the cursor to the next leaf element and returns the key/value pair.
|
||||
void bolt_cursor_next(bolt_cursor *c, bolt_val *key, bolt_val *value, uint32_t *flags) {
|
||||
int i;
|
||||
|
||||
// Attempt to move over one element until we're successful.
|
||||
// Move up the stack as we hit the end of each page in our stack.
|
||||
for (int i = c->top; i >= 0; i--) {
|
||||
for (i = c->top; i >= 0; i--) {
|
||||
elem_ref *elem = &c->stack[i];
|
||||
if (elem->index < elem->page->count - 1) {
|
||||
elem->index++;
|
||||
|
@ -175,7 +177,7 @@ page *cursor_page(bolt_cursor *c, pgid id) {
|
|||
return (page *)(c->data + (c->pgsz * id));
|
||||
}
|
||||
|
||||
// Returns the leaf element at a given index on a given page.
|
||||
// Returns the branch element at a given index on a given page.
|
||||
branch_element *branch_page_element(page *p, uint16_t index) {
|
||||
branch_element *elements = (branch_element*)((void*)(p) + sizeof(page));
|
||||
return &elements[index];
|
||||
|
@ -237,10 +239,11 @@ void cursor_search(bolt_cursor *c, bolt_val key, pgid id) {
|
|||
// Recursively search over a leaf page for a key.
|
||||
void cursor_search_leaf(bolt_cursor *c, bolt_val key) {
|
||||
elem_ref *ref = &c->stack[c->top];
|
||||
int i;
|
||||
|
||||
// HACK: Simply loop over elements to find the right one. Replace with a binary search.
|
||||
leaf_element *elems = (leaf_element*)((void*)(ref->page) + sizeof(page));
|
||||
for (int i=0; i<ref->page->count; i++) {
|
||||
for (i=0; i<ref->page->count; i++) {
|
||||
leaf_element *elem = &elems[i];
|
||||
int rc = memcmp(key.data, ((void*)elem) + elem->pos, (elem->ksize < key.size ? elem->ksize : key.size));
|
||||
|
||||
|
@ -259,10 +262,11 @@ void cursor_search_leaf(bolt_cursor *c, bolt_val key) {
|
|||
// Recursively search over a branch page for a key.
|
||||
void cursor_search_branch(bolt_cursor *c, bolt_val key) {
|
||||
elem_ref *ref = &c->stack[c->top];
|
||||
int i;
|
||||
|
||||
// HACK: Simply loop over elements to find the right one. Replace with a binary search.
|
||||
branch_element *elems = (branch_element*)((void*)(ref->page) + sizeof(page));
|
||||
for (int i=0; i<ref->page->count; i++) {
|
||||
for (i=0; i<ref->page->count; i++) {
|
||||
branch_element *elem = &elems[i];
|
||||
int rc = memcmp(key.data, ((void*)elem) + elem->pos, (elem->ksize < key.size ? elem->ksize : key.size));
|
||||
|
||||
|
|
|
@ -122,7 +122,7 @@ func TestCursor_Iterate_Large(t *testing.T) {
|
|||
db.Update(func(tx *bolt.Tx) error {
|
||||
b, _ := tx.CreateBucket([]byte("widgets"))
|
||||
for i := 0; i < 1000; i++ {
|
||||
b.Put([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("%020d", i)))
|
||||
b.Put([]byte(fmt.Sprintf("%05d", i)), []byte(fmt.Sprintf("%020d", i)))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
@ -130,7 +130,7 @@ func TestCursor_Iterate_Large(t *testing.T) {
|
|||
var index int
|
||||
c := NewCursor(tx.Bucket([]byte("widgets")))
|
||||
for k, v := c.First(); len(k) > 0; k, v = c.Next() {
|
||||
assert.Equal(t, fmt.Sprintf("%d", index), string(k))
|
||||
assert.Equal(t, fmt.Sprintf("%05d", index), string(k))
|
||||
assert.Equal(t, fmt.Sprintf("%020d", index), string(v))
|
||||
index++
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue