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.NewSyncStorage()
71 switch command := os.Args[1]; command {
108 fmt.Println("Usage: sdltester <command>")
109 fmt.Println("Commands:")
110 fmt.Println("write Write data. Performance is measured")
111 fmt.Println("read Read data. Performance is measured")
112 fmt.Println("remove Remove data. Performance is measured")
113 fmt.Println("noexist Read non-existing data. Performance is measured and empty container is returned as nothing was")
114 fmt.Println(" found")
115 fmt.Println("getall Read all keys within a namespace. One can manually add keys under the used namespace and all")
116 fmt.Println(" those keys should be returned here. Performance is measured")
117 fmt.Println("listkeys List keys in the given namespace matching key search pattern.")
118 fmt.Println("removeall Remove all keys within a namespace. Performance is measured")
119 fmt.Println("emptymap Write an empty container. Performance is measured")
120 fmt.Println("multiple Make two writes. Performance is measured")
121 fmt.Println("emptydata Write empty data for a key. Performance is measured")
122 fmt.Println("writeif Write if old data (written with the \"write\" option) has remained and remains")
123 fmt.Println(" unchanged during the function call. Do not write if data has changed. Performance")
124 fmt.Println(" is measured")
125 fmt.Println("writeifnot Write only if key is not set. Performance is measured")
126 fmt.Println("removeif Remove if old data (written with the \"write\" option) has remained and remains")
127 fmt.Println(" unchanged during the function call. Do not remove data if data has changed. Performance")
128 fmt.Println(" is measured")
131 func write(sdl *sdlgo.SyncStorage) {
132 data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
133 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
135 err := sdl.Set("tag1", "key1", data)
136 elapsed := time.Since(start)
138 fmt.Printf("Write: %s\n", elapsed)
144 func read(sdl *sdlgo.SyncStorage) {
145 k := []string{"key1"}
147 data, err := sdl.Get("tag1", k)
148 elapsed := time.Since(start)
150 value, ok := data["key1"]
151 if ok && value != nil {
152 fmt.Printf("Read: %s\n", elapsed)
154 fmt.Printf("Read, not found: %s\n", elapsed)
162 func remove(sdl *sdlgo.SyncStorage) {
163 k := []string{"key1"}
165 err := sdl.Remove("tag1", k)
166 elapsed := time.Since(start)
168 fmt.Printf("Remove: %s\n", elapsed)
174 func noexist(sdl *sdlgo.SyncStorage) {
176 _, err := sdl.Get("tag1", []string{"no1", "no2"})
177 elapsed := time.Since(start)
179 fmt.Printf("Noexist: %s\n", elapsed)
185 func getall(sdl *sdlgo.SyncStorage) {
187 keys, err := sdl.GetAll("tag1")
188 elapsed := time.Since(start)
190 fmt.Printf("Getall: %s\n", elapsed)
191 for _, i := range keys {
199 func listkeys(sdl *sdlgo.SyncStorage) {
203 keys, err := sdl.ListKeys(ns, pattern)
204 elapsed := time.Since(start)
206 fmt.Printf("ListKeys(%s, %s): %s\n", ns, pattern, elapsed)
207 for _, i := range keys {
215 func removeall(sdl *sdlgo.SyncStorage) {
217 err := sdl.RemoveAll("tag1")
218 elapsed := time.Since(start)
220 fmt.Printf("Removeall: %s\n", elapsed)
226 func emptymap(sdl *sdlgo.SyncStorage) {
228 err := sdl.Set("tag1", "", "")
229 elapsed := time.Since(start)
231 fmt.Printf("Emptymap: %s\n", elapsed)
237 func multiple(sdl *sdlgo.SyncStorage) {
238 data := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
239 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
241 err := sdl.Set("tag1", "key1m", data)
242 elapsed := time.Since(start)
244 fmt.Printf("Multiple: %s ", elapsed)
249 err = sdl.Set("tag1", "key2m", data)
250 elapsed = time.Since(start)
252 fmt.Printf(" %s \n", elapsed)
258 func emptydata(sdl *sdlgo.SyncStorage) {
261 err := sdl.Set("tag1", "key1", data)
262 elapsed := time.Since(start)
264 fmt.Printf("Emptydata: %s\n", elapsed)
270 func writeif(sdl *sdlgo.SyncStorage) {
271 oldVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
272 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x44}
273 newVec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
274 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x66}
276 _, err := sdl.SetIf("tag1", "key1", oldVec, newVec)
277 elapsed := time.Since(start)
279 fmt.Printf("Writeif: %s\n", elapsed)
285 func writeifnot(sdl *sdlgo.SyncStorage) {
286 vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
287 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
289 _, err := sdl.SetIfNotExists("tag1", "key1", vec)
290 elapsed := time.Since(start)
292 fmt.Printf("Writeifnot: %s\n", elapsed)
298 func removeif(sdl *sdlgo.SyncStorage) {
299 vec := []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00,
300 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x11, 0x22, 0x33, 0x88}
302 _, err := sdl.RemoveIf("tag1", "key1", vec)
303 elapsed := time.Since(start)
305 fmt.Printf("Removeif: %s\n", elapsed)