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.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
30 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
34 * This program demonsrates the basic usage of sdlgo module.
36 * The following scenarios are provided:
38 * - write: Write data. Performance is measured.
40 * - read: Read data. Performance is measured.
42 * - remove: Remove data. Performance is measured.
44 * - noexist: Read non-existing data. Performance is measured and empty container is returned as nothing was
47 * - getall: Read all keys within a namespace. One can manually add keys under the used namespace and all
48 * those keys should be returned here. Performance is measured.
50 * - removeall: Remove all keys within a namespace. Performance is measured.
52 * - emptymap: Write an empty container. Performance is measured.
54 * - multiple: Make two writes. Performance is measured.
56 * - emptydata: Write empty data for a key. Performance is measured.
58 * - writeif and writeifnot: Write if old data (written with the "write" option) has remained and remains
59 * unchanged during the function call. Do not write if data has changed. Performance
62 * - removeif: Remove if old data (written with the "write" option) has remained and remains
63 * unchanged during the function call. Do not remove data if data has changed. Performance
68 sdl := sdlgo.NewSdlInstance("tag1", sdlgo.NewDatabase())
71 switch command := os.Args[1]; command {
106 fmt.Println("Usage: sdltester <command>")
107 fmt.Println("Commands:")
108 fmt.Println("write Write data. Performance is measured")
109 fmt.Println("read Read data. Performance is measured")
110 fmt.Println("remove Remove data. Performance is measured")
111 fmt.Println("noexist Read non-existing data. Performance is measured and empty container is returned as nothing was")
112 fmt.Println(" found")
113 fmt.Println("getall Read all keys within a namespace. One can manually add keys under the used namespace and all")
114 fmt.Println(" those keys should be returned here. Performance is measured")
115 fmt.Println("removeall Remove all keys within a namespace. Performance is measured")
116 fmt.Println("emptymap Write an empty container. Performance is measured")
117 fmt.Println("multiple Make two writes. Performance is measured")
118 fmt.Println("emptydata Write empty data for a key. Performance is measured")
119 fmt.Println("writeif Write if old data (written with the \"write\" option) has remained and remains")
120 fmt.Println(" unchanged during the function call. Do not write if data has changed. Performance")
121 fmt.Println(" is measured")
122 fmt.Println("writeifnot Write only if key is not set. Performance is measured")
123 fmt.Println("removeif Remove if old data (written with the \"write\" option) has remained and remains")
124 fmt.Println(" unchanged during the function call. Do not remove data if data has changed. Performance")
125 fmt.Println(" is measured")
128 func write(sdl *sdlgo.SdlInstance) {
129 data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
130 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
132 err := sdl.Set("key1", data)
133 elapsed := time.Since(start)
135 fmt.Printf("Write: %s\n", elapsed)
141 func read(sdl *sdlgo.SdlInstance) {
142 k := []string{"key1"}
144 data, err := sdl.Get(k)
145 elapsed := time.Since(start)
147 value, ok := data["key1"]
148 if ok && value != nil {
149 fmt.Printf("Read: %s\n", elapsed)
151 fmt.Printf("Read, not found: %s\n", elapsed)
159 func remove(sdl *sdlgo.SdlInstance) {
160 k := []string{"key1"}
163 elapsed := time.Since(start)
165 fmt.Printf("Remove: %s\n", elapsed)
171 func noexist(sdl *sdlgo.SdlInstance) {
173 _, err := sdl.Get([]string{"no1", "no2"})
174 elapsed := time.Since(start)
176 fmt.Printf("Noexist: %s\n", elapsed)
182 func getall(sdl *sdlgo.SdlInstance) {
184 keys, err := sdl.GetAll()
185 elapsed := time.Since(start)
187 fmt.Printf("Getall: %s\n", elapsed)
188 for _, i := range keys {
196 func removeall(sdl *sdlgo.SdlInstance) {
198 err := sdl.RemoveAll()
199 elapsed := time.Since(start)
201 fmt.Printf("Removeall: %s\n", elapsed)
207 func emptymap(sdl *sdlgo.SdlInstance) {
209 err := sdl.Set("", "")
210 elapsed := time.Since(start)
212 fmt.Printf("Emptymap: %s\n", elapsed)
218 func multiple(sdl *sdlgo.SdlInstance) {
219 data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
220 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
222 err := sdl.Set("key1m", data)
223 elapsed := time.Since(start)
225 fmt.Printf("Multiple: %s ", elapsed)
230 err = sdl.Set("key2m", data)
231 elapsed = time.Since(start)
233 fmt.Printf(" %s \n", elapsed)
239 func emptydata(sdl *sdlgo.SdlInstance) {
242 err := sdl.Set("key1", data)
243 elapsed := time.Since(start)
245 fmt.Printf("Emptydata: %s\n", elapsed)
251 func writeif(sdl *sdlgo.SdlInstance) {
252 oldVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
253 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
254 newVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
255 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x66}
257 _, err := sdl.SetIf("key1", oldVec, newVec)
258 elapsed := time.Since(start)
260 fmt.Printf("Writeif: %s\n", elapsed)
266 func writeifnot(sdl *sdlgo.SdlInstance) {
267 vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
268 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
270 _, err := sdl.SetIfNotExists("key1", vec)
271 elapsed := time.Since(start)
273 fmt.Printf("Writeifnot: %s\n", elapsed)
279 func removeif(sdl *sdlgo.SdlInstance) {
280 vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
281 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
283 _, err := sdl.RemoveIf("key1", vec)
284 elapsed := time.Since(start)
286 fmt.Printf("Removeif: %s\n", elapsed)