MCPcopy
hub / github.com/etcd-io/bbolt / ExampleCursor_reverse

Function ExampleCursor_reverse

cursor_test.go:932–986  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

930}
931
932func ExampleCursor_reverse() {
933 // Open the database.
934 db, err := bolt.Open(tempfile(), 0600, nil)
935 if err != nil {
936 log.Fatal(err)
937 }
938 defer os.Remove(db.Path())
939
940 // Start a read-write transaction.
941 if err := db.Update(func(tx *bolt.Tx) error {
942 // Create a new bucket.
943 b, err := tx.CreateBucket([]byte("animals"))
944 if err != nil {
945 return err
946 }
947
948 // Insert data into a bucket.
949 if err := b.Put([]byte("dog"), []byte("fun")); err != nil {
950 log.Fatal(err)
951 }
952 if err := b.Put([]byte("cat"), []byte("lame")); err != nil {
953 log.Fatal(err)
954 }
955 if err := b.Put([]byte("liger"), []byte("awesome")); err != nil {
956 log.Fatal(err)
957 }
958
959 // Create a cursor for iteration.
960 c := b.Cursor()
961
962 // Iterate over items in reverse sorted key order. This starts
963 // from the last key/value pair and updates the k/v variables to
964 // the previous key/value on each iteration.
965 //
966 // The loop finishes at the beginning of the cursor when a nil key
967 // is returned.
968 for k, v := c.Last(); k != nil; k, v = c.Prev() {
969 fmt.Printf("A %s is %s.\n", k, v)
970 }
971
972 return nil
973 }); err != nil {
974 log.Fatal(err)
975 }
976
977 // Close the database to release the file lock.
978 if err := db.Close(); err != nil {
979 log.Fatal(err)
980 }
981
982 // Output:
983 // A liger is awesome.
984 // A dog is fun.
985 // A cat is lame.
986}

Callers

nothing calls this directly

Calls 10

tempfileFunction · 0.85
UpdateMethod · 0.80
LastMethod · 0.80
PrevMethod · 0.80
FatalMethod · 0.65
PathMethod · 0.45
CreateBucketMethod · 0.45
PutMethod · 0.45
CursorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected