2 Copyright (c) 2019 AT&T Intellectual Property.
3 Copyright (c) 2018-2019 Nokia.
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
25 "gerrit.oran-osc.org/r/ric-plt/sdlgo"
29 * This program demonsrates the basic usage of sdlgo module.
31 * The following scenarios are provided:
33 * - write: Write data. Performance is measured.
35 * - read: Read data. Performance is measured.
37 * - remove: Remove data. Performance is measured.
39 * - noexist: Read non-existing data. Performance is measured and empty container is returned as nothing was
42 * - getall: Read all keys within a namespace. One can manually add keys under the used namespace and all
43 * those keys should be returned here. Performance is measured.
45 * - removeall: Remove all keys within a namespace. Performance is measured.
47 * - emptymap: Write an empty container. Performance is measured.
49 * - multiple: Make two writes. Performance is measured.
51 * - emptydata: Write empty data for a key. Performance is measured.
53 * - writeif and writeifnot: Write if old data (written with the "write" option) has remained and remains
54 * unchanged during the function call. Do not write if data has changed. Performance
57 * - removeif: Remove if old data (written with the "write" option) has remained and remains
58 * unchanged during the function call. Do not remove data if data has changed. Performance
63 sdl := sdlgo.NewSdlInstance("tag1", sdlgo.NewDatabase())
66 switch command := os.Args[1]; command {
101 fmt.Println("Usage: sdltester <command>")
102 fmt.Println("Commands:")
103 fmt.Println("write Write data. Performance is measured")
104 fmt.Println("read Read data. Performance is measured")
105 fmt.Println("remove Remove data. Performance is measured")
106 fmt.Println("noexist Read non-existing data. Performance is measured and empty container is returned as nothing was")
107 fmt.Println(" found")
108 fmt.Println("getall Read all keys within a namespace. One can manually add keys under the used namespace and all")
109 fmt.Println(" those keys should be returned here. Performance is measured")
110 fmt.Println("removeall Remove all keys within a namespace. Performance is measured")
111 fmt.Println("emptymap Write an empty container. Performance is measured")
112 fmt.Println("multiple Make two writes. Performance is measured")
113 fmt.Println("emptydata Write empty data for a key. Performance is measured")
114 fmt.Println("writeif Write if old data (written with the \"write\" option) has remained and remains")
115 fmt.Println(" unchanged during the function call. Do not write if data has changed. Performance")
116 fmt.Println(" is measured")
117 fmt.Println("writeifnot Write only if key is not set. Performance is measured")
118 fmt.Println("removeif Remove if old data (written with the \"write\" option) has remained and remains")
119 fmt.Println(" unchanged during the function call. Do not remove data if data has changed. Performance")
120 fmt.Println(" is measured")
123 func write(sdl *sdlgo.SdlInstance) {
124 data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
125 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
127 err := sdl.Set("key1", data)
128 elapsed := time.Since(start)
130 fmt.Printf("Write: %s\n", elapsed)
136 func read(sdl *sdlgo.SdlInstance) {
137 k := []string{"key1"}
139 data, err := sdl.Get(k)
140 elapsed := time.Since(start)
142 value, ok := data["key1"]
143 if ok && value != nil {
144 fmt.Printf("Read: %s\n", elapsed)
146 fmt.Printf("Read, not found: %s\n", elapsed)
154 func remove(sdl *sdlgo.SdlInstance) {
155 k := []string{"key1"}
158 elapsed := time.Since(start)
160 fmt.Printf("Remove: %s\n", elapsed)
166 func noexist(sdl *sdlgo.SdlInstance) {
168 _, err := sdl.Get([]string{"no1", "no2"})
169 elapsed := time.Since(start)
171 fmt.Printf("Noexist: %s\n", elapsed)
177 func getall(sdl *sdlgo.SdlInstance) {
179 keys, err := sdl.GetAll()
180 elapsed := time.Since(start)
182 fmt.Printf("Getall: %s\n", elapsed)
183 for _, i := range keys {
191 func removeall(sdl *sdlgo.SdlInstance) {
193 err := sdl.RemoveAll()
194 elapsed := time.Since(start)
196 fmt.Printf("Removeall: %s\n", elapsed)
202 func emptymap(sdl *sdlgo.SdlInstance) {
204 err := sdl.Set("", "")
205 elapsed := time.Since(start)
207 fmt.Printf("Emptymap: %s\n", elapsed)
213 func multiple(sdl *sdlgo.SdlInstance) {
214 data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
215 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
217 err := sdl.Set("key1m", data)
218 elapsed := time.Since(start)
220 fmt.Printf("Multiple: %s ", elapsed)
225 err = sdl.Set("key2m", data)
226 elapsed = time.Since(start)
228 fmt.Printf(" %s \n", elapsed)
234 func emptydata(sdl *sdlgo.SdlInstance) {
237 err := sdl.Set("key1", data)
238 elapsed := time.Since(start)
240 fmt.Printf("Emptydata: %s\n", elapsed)
246 func writeif(sdl *sdlgo.SdlInstance) {
247 oldVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
248 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
249 newVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
250 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x66}
252 _, err := sdl.SetIf("key1", oldVec, newVec)
253 elapsed := time.Since(start)
255 fmt.Printf("Writeif: %s\n", elapsed)
261 func writeifnot(sdl *sdlgo.SdlInstance) {
262 vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
263 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
265 _, err := sdl.SetIfNotExists("key1", vec)
266 elapsed := time.Since(start)
268 fmt.Printf("Writeifnot: %s\n", elapsed)
274 func removeif(sdl *sdlgo.SdlInstance) {
275 vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
276 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
278 _, err := sdl.RemoveIf("key1", vec)
279 elapsed := time.Since(start)
281 fmt.Printf("Removeif: %s\n", elapsed)