Example #1
0
// Ensure that a C cursor can iterate over branches and leafs.
func TestCursor_Iterate_Deep(t *testing.T) {
	withDB(func(db *bolt.DB) {
		pgsz := db.Info().PageSize / 10
		assert.True(t, pgsz > 100)
		db.Update(func(tx *bolt.Tx) error {
			b, _ := tx.CreateBucket([]byte("widgets"))
			for i := 0; i < 1000; i++ {
				kv := []byte(fmt.Sprintf("%0*d", pgsz, i))
				b.Put(kv, kv)
			}
			return nil
		})
		db.View(func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte("widgets"))
			s := b.Stats()
			assert.True(t, s.Depth > 3)

			var index int
			c := NewCursor(b)
			for k, v := c.First(); len(k) > 0; k, v = c.Next() {
				kv := fmt.Sprintf("%0*d", pgsz, index)
				assert.Equal(t, kv, string(k))
				assert.Equal(t, kv, string(v))
				index++
			}
			assert.Equal(t, 1000, index)
			k, _ := c.Next()
			assert.Nil(t, k)
			return nil
		})
	})
}
Example #2
0
// 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")))

			k, v := c.First()
			assert.Equal(t, string(k), "bar")
			assert.Equal(t, []byte{1}, v)

			k, v = c.Next()
			assert.Equal(t, string(k), "baz")
			assert.Equal(t, []byte{}, v)

			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
		})
	})
}
Example #3
0
// 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.Nil(t, key)
			assert.Nil(t, value)
			key, value = c.Next()
			assert.Nil(t, key)
			assert.Nil(t, value)
			key, value, flags := c.Seek([]byte("bar"))
			assert.Nil(t, key)
			assert.Nil(t, value)
			assert.Equal(t, 0, flags)
			return nil
		})
	})
}
Example #4
0
// Ensure that a C cursor can iterate over 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("%05d", i)), []byte(fmt.Sprintf("%020d", i)))
			}
			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("%05d", index), string(k))
				assert.Equal(t, fmt.Sprintf("%020d", index), string(v))
				index++
			}
			assert.Equal(t, 1000, index)
			return nil
		})
	})
}