github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/examples/batch/main.go (about) 1 package main 2 3 import ( 4 "github.com/rosedblabs/rosedb/v2" 5 ) 6 7 // this file shows how to use the batch operations of rosedb 8 9 func main() { 10 // specify the options 11 options := rosedb.DefaultOptions 12 options.DirPath = "/tmp/rosedb_batch" 13 14 // open a database 15 db, err := rosedb.Open(options) 16 if err != nil { 17 panic(err) 18 } 19 defer func() { 20 _ = db.Close() 21 }() 22 23 // create a batch 24 batch := db.NewBatch(rosedb.DefaultBatchOptions) 25 26 // set a key 27 _ = batch.Put([]byte("name"), []byte("rosedb")) 28 29 // get a key 30 val, _ := batch.Get([]byte("name")) 31 println(string(val)) 32 33 // delete a key 34 _ = batch.Delete([]byte("name")) 35 36 // commit the batch 37 _ = batch.Commit() 38 39 // if you want to cancel batch, you must call rollback(). 40 // _= batch.Rollback() 41 42 // once a batch is committed, it can't be used again 43 // _ = batch.Put([]byte("name1"), []byte("rosedb1")) // don't do this!!! 44 }