diff options
Diffstat (limited to 'c/cursor_test.go')
-rw-r--r-- | c/cursor_test.go | 227 |
1 files changed, 140 insertions, 87 deletions
diff --git a/c/cursor_test.go b/c/cursor_test.go index 8d40d8d..560d03c 100644 --- a/c/cursor_test.go +++ b/c/cursor_test.go @@ -1,129 +1,182 @@ -package c +package c_test import ( "fmt" "io/ioutil" "os" - "sort" "testing" - "testing/quick" "github.com/boltdb/bolt" + . "github.com/boltdb/bolt/c" "github.com/stretchr/testify/assert" ) -// Test when cursor hits the end -// Implement seek; binary search within the page (branch page and element page) +// Ensure that the C cursor can +func TestCursor_First(t *testing.T) { + withDB(func(db *bolt.DB) { + db.Update(func(tx *bolt.Tx) error { + b, _ := tx.CreateBucket([]byte("widgets")) + return b.Put([]byte("foo"), []byte("barz")) + }) + db.View(func(tx *bolt.Tx) error { + c := NewCursor(tx.Bucket([]byte("widgets"))) + key, value := c.First() + assert.Equal(t, []byte("foo"), key) + assert.Equal(t, []byte("barz"), value) + return nil + }) + }) +} -// Ensure that a cursor can get the first element of a bucket. -func TestCursorFirst(t *testing.T) { - withOpenDB(func(db *bolt.DB, path string) { +// Ensure that a C cursor can seek to the appropriate keys. +func TestCursor_Seek(t *testing.T) { + withDB(func(db *bolt.DB) { + db.Update(func(tx *bolt.Tx) error { + b, err := tx.CreateBucket([]byte("widgets")) + assert.NoError(t, err) + assert.NoError(t, b.Put([]byte("foo"), []byte("0001"))) + assert.NoError(t, b.Put([]byte("bar"), []byte("0002"))) + assert.NoError(t, b.Put([]byte("baz"), []byte("0003"))) + _, err = b.CreateBucket([]byte("bkt")) + assert.NoError(t, err) + return nil + }) + db.View(func(tx *bolt.Tx) error { + c := NewCursor(tx.Bucket([]byte("widgets"))) + + // Exact match should go to the key. + k, v, flags := c.Seek([]byte("bar")) + assert.Equal(t, "bar", string(k)) + assert.Equal(t, "0002", string(v)) + assert.Equal(t, 0, flags) + + // Inexact match should go to the next key. + k, v, flags = c.Seek([]byte("bas")) + assert.Equal(t, "baz", string(k)) + assert.Equal(t, "0003", string(v)) + assert.Equal(t, 0, flags) + + // Low key should go to the first key. + k, v, flags = c.Seek([]byte("")) + assert.Equal(t, "bar", string(k)) + assert.Equal(t, "0002", string(v)) + assert.Equal(t, 0, flags) + + // High key should return no key. + k, v, flags = c.Seek([]byte("zzz")) + assert.Equal(t, "", string(k)) + assert.Equal(t, "", string(v)) + assert.Equal(t, 0, flags) + + // Buckets should return their key but no value. + k, v, flags = c.Seek([]byte("bkt")) + assert.Equal(t, []byte("bkt"), k) + assert.True(t, len(v) > 0) + assert.Equal(t, 1, flags) // bucketLeafFlag + + return nil + }) + }) +} + +// Ensure that a C cursor can iterate over a single root with a couple elements. +func TestCursor_Iterate_Leaf(t *testing.T) { + withDB(func(db *bolt.DB) { + db.Update(func(tx *bolt.Tx) error { + tx.CreateBucket([]byte("widgets")) + tx.Bucket([]byte("widgets")).Put([]byte("baz"), []byte{}) + tx.Bucket([]byte("widgets")).Put([]byte("foo"), []byte{0}) + tx.Bucket([]byte("widgets")).Put([]byte("bar"), []byte{1}) + return nil + }) + db.View(func(tx *bolt.Tx) error { + c := NewCursor(tx.Bucket([]byte("widgets"))) - // Bulk insert all values. - tx, _ := db.Begin(true) - b, _ := tx.CreateBucket(toBytes("widgets")) - assert.NoError(t, b.Put(toBytes("foo"), toBytes("barz"))) - assert.NoError(t, tx.Commit()) + k, v := c.First() + assert.Equal(t, string(k), "bar") + assert.Equal(t, []byte{1}, v) - // Get first and check consistency - tx, _ = db.Begin(false) - c := NewCursor(tx.Bucket(toBytes("widgets"))) - key, value := first(c) - assert.Equal(t, key, toBytes("foo")) - assert.Equal(t, value, toBytes("barz")) + k, v = c.Next() + assert.Equal(t, string(k), "baz") + assert.Equal(t, []byte{}, v) - tx.Rollback() + k, v = c.Next() + assert.Equal(t, string(k), "foo") + assert.Equal(t, []byte{0}, v) + + k, v = c.Next() + assert.Equal(t, []byte{}, k) + assert.Equal(t, []byte{}, v) + + k, v = c.Next() + assert.Equal(t, []byte{}, k) + assert.Equal(t, []byte{}, v) + return nil + }) }) } -// Ensure that a cursor can iterate over all elements in a bucket. -func TestIterate(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - - f := func(items testdata) bool { - withOpenDB(func(db *bolt.DB, path string) { - // Bulk insert all values. - tx, _ := db.Begin(true) - b, _ := tx.CreateBucket(toBytes("widgets")) - for _, item := range items { - assert.NoError(t, b.Put(item.Key, item.Value)) +// Ensure that a C cursor can iterate over a branches and leafs. +func TestCursor_Iterate_Large(t *testing.T) { + withDB(func(db *bolt.DB) { + 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))) } - assert.NoError(t, tx.Commit()) - - // Sort test data. - sort.Sort(items) - - // Iterate over all items and check consistency. - var index = 0 - tx, _ = db.Begin(false) - c := NewCursor(tx.Bucket(toBytes("widgets"))) - for key, value := first(c); key != nil && index < len(items); key, value = next(c) { - assert.Equal(t, key, items[index].Key) - assert.Equal(t, value, items[index].Value) + return nil + }) + db.View(func(tx *bolt.Tx) error { + 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("%020d", index), string(v)) index++ } - assert.Equal(t, len(items), index) - assert.Equal(t, len(items), index) - tx.Rollback() + assert.Equal(t, 1000, index) + return nil }) - return true - } - if err := quick.Check(f, qconfig()); err != nil { - t.Error(err) - } - fmt.Fprint(os.Stderr, "\n") -} - -// toBytes converts a string to an array of bytes. -func toBytes(s string) []byte { - return []byte(s) + }) } -// withTempPath executes a function with a database reference. -func withTempPath(fn func(string)) { - f, _ := ioutil.TempFile("", "bolt-") - path := f.Name() +// tempfile returns a temporary path. +func tempfile() string { + f, _ := ioutil.TempFile("", "bolt-c-") f.Close() - os.Remove(path) - defer os.RemoveAll(path) - - fn(path) + os.Remove(f.Name()) + return f.Name() } -// withOpenDB executes a function with an already opened database. -func withOpenDB(fn func(*bolt.DB, string)) { - withTempPath(func(path string) { - db, err := bolt.Open(path, 0666) - if err != nil { - panic("cannot open db: " + err.Error()) - } - defer db.Close() - fn(db, path) +// withDB executes a function with an already opened database. +func withDB(fn func(*bolt.DB)) { + path := tempfile() + db, err := bolt.Open(path, 0666) + if err != nil { + panic("cannot open db: " + err.Error()) + } + defer os.Remove(path) + defer db.Close() - // Log statistics. - // if *statsFlag { - // logStats(db) - // } + fn(db) - // Check database consistency after every test. - mustCheck(db) - }) + // Check database consistency after every test. + mustCheck(db) } // mustCheck runs a consistency check on the database and panics if any errors are found. func mustCheck(db *bolt.DB) { if err := db.Check(); err != nil { // Copy db off first. - db.CopyFile("/tmp/check.db", 0600) + var path = tempfile() + db.CopyFile(path, 0600) if errors, ok := err.(bolt.ErrorList); ok { for _, err := range errors { - warn(err) + fmt.Println(err) } } - warn(err) - panic("check failure: see /tmp/check.db") + fmt.Println(err) + panic("check failure: " + path) } } |