diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-08-16 15:20:31 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-08-16 15:20:31 -0600 |
commit | 1dc60ba7a153957ee40ea37878897c072ae2e521 (patch) | |
tree | fdff953c3b1531d4066512b3163854f87e9b35c4 /cursor_test.go | |
parent | Merge pull request #236 from siddontang/master (diff) | |
download | dedo-1dc60ba7a153957ee40ea37878897c072ae2e521.tar.gz dedo-1dc60ba7a153957ee40ea37878897c072ae2e521.tar.xz |
Add cursor examples.
Diffstat (limited to 'cursor_test.go')
-rw-r--r-- | cursor_test.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/cursor_test.go b/cursor_test.go index 6957a29..b12e1f9 100644 --- a/cursor_test.go +++ b/cursor_test.go @@ -3,6 +3,8 @@ package bolt_test import ( "bytes" "encoding/binary" + "fmt" + "os" "sort" "testing" "testing/quick" @@ -430,3 +432,80 @@ func TestCursor_QuickCheck_BucketsOnly_Reverse(t *testing.T) { return nil }) } + +func ExampleCursor() { + // Open the database. + db, _ := bolt.Open(tempfile(), 0666, nil) + defer os.Remove(db.Path()) + defer db.Close() + + // Start a read-write transaction. + db.Update(func(tx *bolt.Tx) error { + // Create a new bucket. + tx.CreateBucket([]byte("animals")) + + // Insert data into a bucket. + b := tx.Bucket([]byte("animals")) + b.Put([]byte("dog"), []byte("fun")) + b.Put([]byte("cat"), []byte("lame")) + b.Put([]byte("liger"), []byte("awesome")) + + // Create a cursor for iteration. + c := b.Cursor() + + // Iterate over items in sorted key order. This starts from the + // first key/value pair and updates the k/v variables to the + // next key/value on each iteration. + // + // The loop finishes at the end of the cursor when a nil key is returned. + for k, v := c.First(); k != nil; k, v = c.Next() { + fmt.Printf("A %s is %s.\n", k, v) + } + + return nil + }) + + // Output: + // A cat is lame. + // A dog is fun. + // A liger is awesome. +} + +func ExampleCursor_reverse() { + // Open the database. + db, _ := bolt.Open(tempfile(), 0666, nil) + defer os.Remove(db.Path()) + defer db.Close() + + // Start a read-write transaction. + db.Update(func(tx *bolt.Tx) error { + // Create a new bucket. + tx.CreateBucket([]byte("animals")) + + // Insert data into a bucket. + b := tx.Bucket([]byte("animals")) + b.Put([]byte("dog"), []byte("fun")) + b.Put([]byte("cat"), []byte("lame")) + b.Put([]byte("liger"), []byte("awesome")) + + // Create a cursor for iteration. + c := b.Cursor() + + // Iterate over items in reverse sorted key order. This starts + // from the last key/value pair and updates the k/v variables to + // the previous key/value on each iteration. + // + // The loop finishes at the beginning of the cursor when a nil key + // is returned. + for k, v := c.Last(); k != nil; k, v = c.Prev() { + fmt.Printf("A %s is %s.\n", k, v) + } + + return nil + }) + + // Output: + // A liger is awesome. + // A dog is fun. + // A cat is lame. +} |