aboutsummaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
authorMartin Kobetic <mkobetic@gmail.com>2014-04-23 15:36:41 +0000
committerMartin Kobetic <mkobetic@gmail.com>2014-04-23 15:36:41 +0000
commit181eb96dc707c1bb4e375b55520d1ad037b2a365 (patch)
tree4d1139b2b7a65d8b796b33549dafa6ee5bfea0f1 /c
parentanother test case and minor cleanup (diff)
downloaddedo-181eb96dc707c1bb4e375b55520d1ad037b2a365.tar.gz
dedo-181eb96dc707c1bb4e375b55520d1ad037b2a365.tar.xz
add test for empty bucket handling
Diffstat (limited to 'c')
-rw-r--r--c/cursor.go18
-rw-r--r--c/cursor_test.go26
2 files changed, 36 insertions, 8 deletions
diff --git a/c/cursor.go b/c/cursor.go
index 21f1576..4c00767 100644
--- a/c/cursor.go
+++ b/c/cursor.go
@@ -196,8 +196,8 @@ 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) {
elem_ref *ref = cursor_current(c);
- // If stack is empty return null.
- if (ref == NULL) {
+ // If stack or current page is empty return null.
+ if (ref == NULL || ref->page->count == 0) {
key->size = value->size = 0;
key->data = value->data = NULL;
*flags = 0;
@@ -337,7 +337,9 @@ func (c *Cursor) First() (key, value []byte) {
var k, v C.bolt_val
var flags C.uint32_t
C.bolt_cursor_first(c.C, &k, &v, &flags)
- if k.data == nil {
+ fmt.Printf("key: %#v, value: %#v\n", k, v)
+ fmt.Printf("k.data: %#v, k.size: %#v\n", uintptr(k.data), int(k.size))
+ if uintptr(k.data) == uintptr(0) {
return nil, nil
}
return C.GoBytes(k.data, C.int(k.size)), C.GoBytes(v.data, C.int(v.size))
@@ -349,7 +351,8 @@ func (c *Cursor) Next() (key, value []byte) {
var k, v C.bolt_val
var flags C.uint32_t
C.bolt_cursor_next(c.C, &k, &v, &flags)
- if k.data == nil {
+ fmt.Printf("key: %#v, value: %#v\n", k, v)
+ if k.size == 0 {
return nil, nil
}
return C.GoBytes(k.data, C.int(k.size)), C.GoBytes(v.data, C.int(v.size))
@@ -366,9 +369,10 @@ func (c *Cursor) Seek(seek []byte) (key, value []byte, flags int) {
_seek.data = unsafe.Pointer(&seek[0])
}
C.bolt_cursor_seek(c.C, _seek, &k, &v, &_flags)
- //fmt.Printf("Key %v [%v]\n", k.data, k.size)
- //fmt.Printf("Value %v [%v]\n", k.data, k.size)
- if k.data == nil {
+ fmt.Printf("Key %#v [%#v]\n", k.data, k.size)
+ fmt.Printf("Value %#v [%#v]\n", k.data, k.size)
+ fmt.Printf("key: %#v, value: %#v\n", k, v)
+ if k.size == 0 {
return nil, nil, 0
}
diff --git a/c/cursor_test.go b/c/cursor_test.go
index fa1e6bb..5a8f3e0 100644
--- a/c/cursor_test.go
+++ b/c/cursor_test.go
@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)
-// Ensure that the C cursor can
+// Ensure that the C cursor can seek to first element.
func TestCursor_First(t *testing.T) {
withDB(func(db *bolt.DB) {
db.Update(func(tx *bolt.Tx) error {
@@ -28,6 +28,30 @@ func TestCursor_First(t *testing.T) {
})
}
+// Ensure that a C cursor handles empty bucket properly
+func TestCursor_Empty(t *testing.T) {
+ withDB(func(db *bolt.DB) {
+ db.Update(func(tx *bolt.Tx) error {
+ tx.CreateBucket([]byte("widgets"))
+ return nil
+ })
+ db.View(func(tx *bolt.Tx) error {
+ c := NewCursor(tx.Bucket([]byte("widgets")))
+ key, value := c.First()
+ assert.Equal(t, nil, key)
+ assert.Equal(t, nil, value)
+ key, value = c.Next()
+ assert.Equal(t, nil, key)
+ assert.Equal(t, nil, value)
+ key, value, flags := c.Seek([]byte("bar"))
+ assert.Equal(t, nil, key)
+ assert.Equal(t, nil, value)
+ assert.Equal(t, 0, flags)
+ return nil
+ })
+ })
+}
+
// Ensure that a C cursor can seek to the appropriate keys.
func TestCursor_Seek(t *testing.T) {
withDB(func(db *bolt.DB) {