Last active
February 6, 2021 00:18
-
-
Save gaspiman/b23596f32cf2d089fc1d818bea3d4e73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| badger "github.com/dgraph-io/badger/v3" | |
| ) | |
| func main() { | |
| log.Println("Creating updates") | |
| opts := badger.DefaultOptions("").WithInMemory(true) | |
| // Open the Badger database located in the /tmp/badger directory. | |
| // It will be created if it doesn't exist. | |
| db, err := badger.Open(opts) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer db.Close() | |
| log.Println("Creating updates") | |
| updates := map[string]string{ | |
| "aaaa": "", | |
| "aaab": "", | |
| "aabb": "", | |
| "abbb": "", | |
| "bbbb": "", | |
| } | |
| txn := db.NewTransaction(true) | |
| for k, v := range updates { | |
| if err := txn.Set([]byte(k), []byte(v)); err == badger.ErrTxnTooBig { | |
| _ = txn.Commit() | |
| txn = db.NewTransaction(true) | |
| _ = txn.Set([]byte(k), []byte(v)) | |
| } | |
| } | |
| _ = txn.Commit() | |
| log.Println("Iterating over the entire dataset") | |
| err = db.View(func(txn *badger.Txn) error { | |
| opts := badger.DefaultIteratorOptions | |
| opts.PrefetchSize = 10 | |
| it := txn.NewIterator(opts) | |
| defer it.Close() | |
| for it.Rewind(); it.Valid(); it.Next() { | |
| item := it.Item() | |
| k := item.Key() | |
| err := item.Value(func(v []byte) error { | |
| fmt.Printf("key=%s, value=%s\n", k, v) | |
| return nil | |
| }) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| return nil | |
| }) | |
| log.Println("Let's jump to \"aabb\" and continue forward...") | |
| err = db.View(func(txn *badger.Txn) error { | |
| opts := badger.DefaultIteratorOptions | |
| opts.PrefetchSize = 10 | |
| it := txn.NewIterator(opts) | |
| defer it.Close() | |
| for it.Seek([]byte("aabb")); it.ValidForPrefix([]byte("")); it.Next() { | |
| item := it.Item() | |
| k := item.Key() | |
| err := item.Value(func(v []byte) error { | |
| fmt.Printf("key=%s, value=%s\n", k, v) | |
| return nil | |
| }) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| return nil | |
| }) | |
| log.Println("Let's jump to \"aa\" and scan with prefix \"aa\"") | |
| err = db.View(func(txn *badger.Txn) error { | |
| opts := badger.DefaultIteratorOptions | |
| opts.PrefetchSize = 10 | |
| it := txn.NewIterator(opts) | |
| defer it.Close() | |
| for it.Seek([]byte("aa")); it.ValidForPrefix([]byte("aa")); it.Next() { | |
| item := it.Item() | |
| k := item.Key() | |
| err := item.Value(func(v []byte) error { | |
| fmt.Printf("key=%s, value=%s\n", k, v) | |
| return nil | |
| }) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| return nil | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment