github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/examples/strs/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/flower-corp/rosedb" 6 "path/filepath" 7 "time" 8 ) 9 10 func main() { 11 path := filepath.Join("/tmp", "rosedb") 12 opts := rosedb.DefaultOptions(path) 13 db, err := rosedb.Open(opts) 14 if err != nil { 15 fmt.Printf("open rosedb err: %v", err) 16 return 17 } 18 19 err = db.Set([]byte("name"), []byte("RoseDB")) 20 if err != nil { 21 fmt.Printf("write data err: %v", err) 22 return 23 } 24 25 v, err := db.Get([]byte("name")) 26 if err != nil { 27 fmt.Printf("read data err: %v", err) 28 return 29 } 30 fmt.Println("val = ", string(v)) 31 32 err = db.SetEX([]byte("type"), []byte("RoseDB-Strs"), time.Second*5) 33 if err != nil { 34 fmt.Printf("write data err: %v", err) 35 return 36 } 37 38 err = db.Delete([]byte("name")) 39 if err != nil { 40 fmt.Printf("delete data err: %v", err) 41 return 42 } 43 44 err = db.SetNX([]byte("cmd"), []byte("SetNX")) 45 if err != nil { 46 fmt.Printf("write data err: %v", err) 47 return 48 } 49 50 v, err = db.Get([]byte("cmd")) 51 if err != nil { 52 fmt.Printf("read data err: %v", err) 53 return 54 } 55 fmt.Printf("cmd-type = %s\n", string(v)) 56 57 err = db.MSet([]byte("key-1"), []byte("value-1"), []byte("key-2"), []byte("value-2")) 58 if err != nil { 59 fmt.Printf("mset error: %v\n", err) 60 return 61 } 62 fmt.Println("Multiple key-value pair added.") 63 64 // Missing value. 65 err = db.MSet([]byte("key-1"), []byte("value-1"), []byte("key-2")) 66 if err != nil { 67 fmt.Printf("mset error: %v\n", err) 68 } 69 70 err = db.MSetNX([]byte("key-11"), []byte("value-11"), []byte("key-22"), []byte("value-22")) 71 if err != nil { 72 fmt.Printf("msetnx error: %v\n", err) 73 } 74 val, _ := db.Get([]byte("key-11")) 75 fmt.Printf("key-11: %v\n", string(val)) 76 fmt.Printf("A example of missing value: %v\n", err) 77 78 // mget 79 keys := [][]byte{ 80 []byte("key-1"), 81 []byte("not exist"), 82 []byte("key-11"), 83 } 84 vals, err := db.MGet(keys) 85 if err != nil { 86 fmt.Printf("mget err : %v\n", err) 87 } else { 88 fmt.Printf("mget values : %v\n", vals) 89 } 90 91 // example of append 92 err = db.Delete([]byte("append")) 93 if err != nil { 94 fmt.Printf("delete data err: %v", err) 95 return 96 } 97 98 _, err = db.GetDel([]byte("not-exist")) 99 if err != nil { 100 fmt.Printf("getdel data err: %v", err) 101 } 102 gdVal, err := db.GetDel([]byte("key-22")) 103 if err != nil { 104 fmt.Printf("getdel data err: %v", err) 105 } else { 106 fmt.Println("getdel val : ", string(gdVal)) 107 } 108 109 err = db.Append([]byte("append"), []byte("Rose")) 110 if err != nil { 111 fmt.Printf("write data err: %v", err) 112 return 113 } 114 115 err = db.Append([]byte("append"), []byte("DB")) 116 if err != nil { 117 fmt.Printf("write data err: %v", err) 118 return 119 } 120 121 v, err = db.Get([]byte("append")) 122 if err != nil { 123 fmt.Printf("read data err: %v", err) 124 return 125 } 126 fmt.Printf("append = %s\n", string(v)) 127 128 strLen := db.StrLen([]byte("key-1")) 129 fmt.Printf("StrLen %v\n", strLen) 130 131 _ = db.Set([]byte("int"), []byte("12")) 132 valInt, err := db.Decr([]byte("int")) 133 if err != nil { 134 fmt.Printf(err.Error()) 135 } 136 fmt.Printf("new value after Decr(): %v\n", valInt) 137 138 valInt, err = db.DecrBy([]byte("int"), 5) 139 if err != nil { 140 fmt.Printf(err.Error()) 141 } 142 fmt.Printf("new value after DecrBy(5): %v\n", valInt) 143 }