diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-02-21 17:14:56 -0700 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-02-21 22:57:50 -0700 |
commit | 1ad2b99f281d587b767b36f886401e81d17915a9 (patch) | |
tree | 7b46bfc96689af14938a9f93aab732e3e46ee709 /example_test.go | |
parent | Merge pull request #49 from benbjohnson/stat (diff) | |
download | dedo-1ad2b99f281d587b767b36f886401e81d17915a9.tar.gz dedo-1ad2b99f281d587b767b36f886401e81d17915a9.tar.xz |
Refactor Transaction/Bucket API.
Diffstat (limited to 'example_test.go')
-rw-r--r-- | example_test.go | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/example_test.go b/example_test.go index 8747f94..b3d5d20 100644 --- a/example_test.go +++ b/example_test.go @@ -67,11 +67,13 @@ func ExampleDB_Do() { defer db.Close() // Execute several commands within a write transaction. - err := db.Do(func(t *RWTransaction) error { + err := db.Do(func(t *Transaction) error { if err := t.CreateBucket("widgets"); err != nil { return err } - if err := t.Put("widgets", []byte("foo"), []byte("bar")); err != nil { + + b := t.Bucket("widgets") + if err := b.Put([]byte("foo"), []byte("bar")); err != nil { return err } return nil @@ -100,7 +102,7 @@ func ExampleDB_With() { // Access data from within a read-only transactional block. db.With(func(t *Transaction) error { - v, _ := t.Get("people", []byte("john")) + v := t.Bucket("people").Get([]byte("john")) fmt.Printf("John's last name is %s.\n", string(v)) return nil }) @@ -133,29 +135,30 @@ func ExampleDB_ForEach() { // A liger is awesome. } -func ExampleRWTransaction() { +func ExampleTransaction_Commit() { // Open the database. var db DB - db.Open("/tmp/bolt/rwtransaction.db", 0666) + db.Open("/tmp/bolt/db_rwtransaction.db", 0666) defer db.Close() // Create a bucket. db.CreateBucket("widgets") // Create several keys in a transaction. - rwtxn, _ := db.RWTransaction() - rwtxn.Put("widgets", []byte("john"), []byte("blue")) - rwtxn.Put("widgets", []byte("abby"), []byte("red")) - rwtxn.Put("widgets", []byte("zephyr"), []byte("purple")) - rwtxn.Commit() + txn, _ := db.RWTransaction() + b := txn.Bucket("widgets") + b.Put([]byte("john"), []byte("blue")) + b.Put([]byte("abby"), []byte("red")) + b.Put([]byte("zephyr"), []byte("purple")) + txn.Commit() // Iterate over the values in sorted key order. - txn, _ := db.Transaction() - c, _ := txn.Cursor("widgets") + txn, _ = db.Transaction() + c := txn.Bucket("widgets").Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { fmt.Printf("%s likes %s\n", string(k), string(v)) } - txn.Close() + txn.Rollback() // Output: // abby likes red @@ -163,10 +166,10 @@ func ExampleRWTransaction() { // zephyr likes purple } -func ExampleRWTransaction_rollback() { +func ExampleTransaction_Rollback() { // Open the database. var db DB - db.Open("/tmp/bolt/rwtransaction_rollback.db", 0666) + db.Open("/tmp/bolt/transaction_close.db", 0666) defer db.Close() // Create a bucket. @@ -176,9 +179,9 @@ func ExampleRWTransaction_rollback() { db.Put("widgets", []byte("foo"), []byte("bar")) // Update the key but rollback the transaction so it never saves. - rwtxn, _ := db.RWTransaction() - rwtxn.Put("widgets", []byte("foo"), []byte("baz")) - rwtxn.Rollback() + txn, _ := db.RWTransaction() + txn.Bucket("widgets").Put([]byte("foo"), []byte("baz")) + txn.Rollback() // Ensure that our original value is still set. value, _ := db.Get("widgets", []byte("foo")) |