github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/examples/iterate/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/rosedblabs/rosedb/v2" 7 ) 8 9 // this file shows how to use the iterate operations of rosedb 10 // you can use Ascend, Descend(and some other similar methods) to iterate all keys and values in order. 11 func main() { 12 // specify the options 13 options := rosedb.DefaultOptions 14 options.DirPath = "/tmp/rosedb_iterate" 15 16 // open a database 17 db, err := rosedb.Open(options) 18 if err != nil { 19 panic(err) 20 } 21 defer func() { 22 _ = db.Close() 23 }() 24 25 _ = db.Put([]byte("key13"), []byte("value13")) 26 _ = db.Put([]byte("key11"), []byte("value11")) 27 _ = db.Put([]byte("key35"), []byte("value35")) 28 _ = db.Put([]byte("key27"), []byte("value27")) 29 _ = db.Put([]byte("key41"), []byte("value41")) 30 31 // iterate all keys in order 32 db.AscendKeys(nil, true, func(k []byte) (bool, error) { 33 fmt.Println("key = ", string(k)) 34 return true, nil 35 }) 36 37 // iterate all keys and values in order 38 db.Ascend(func(k []byte, v []byte) (bool, error) { 39 fmt.Printf("key = %s, value = %s\n", string(k), string(v)) 40 return true, nil 41 }) 42 43 // iterate all keys in reverse order 44 db.DescendKeys(nil, true, func(k []byte) (bool, error) { 45 fmt.Println("key = ", string(k)) 46 return true, nil 47 }) 48 49 // iterate all keys and values in reverse order 50 db.Descend(func(k []byte, v []byte) (bool, error) { 51 fmt.Printf("key = %s, value = %s\n", string(k), string(v)) 52 return true, nil 53 }) 54 55 // you can also use some other similar methods to iterate the data. 56 // db.AscendRange() 57 // db.AscendGreaterOrEqual() 58 // db.DescendRange() 59 // db.DescendLessOrEqual() 60 }