New ListKeys API in syncstorage
[ric-plt/sdlgo.git] / cmd / sdltester / sdltester.go
1 /*
2    Copyright (c) 2019 AT&T Intellectual Property.
3    Copyright (c) 2018-2019 Nokia.
4
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
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
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.
16 */
17
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 package main
24
25 import (
26         "fmt"
27         "os"
28         "time"
29
30         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
31 )
32
33 /*
34  * This program demonsrates the basic usage of sdlgo module.
35  *
36  * The following scenarios are provided:
37  *
38  * - write: Write data. Performance is measured.
39  *
40  * - read: Read data. Performance is measured.
41  *
42  * - remove: Remove data. Performance is measured.
43  *
44  * - noexist: Read non-existing data. Performance is measured and empty container is returned as nothing was
45  *            found.
46  *
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.
49  *
50  * - removeall: Remove all keys within a namespace. Performance is measured.
51  *
52  * - emptymap: Write an empty container. Performance is measured.
53  *
54  * - multiple: Make two writes. Performance is measured.
55  *
56  * - emptydata: Write empty data for a key. Performance is measured.
57  *
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
60  *                           is measured.
61  *
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
64  *             is measured.
65  */
66
67 func main() {
68         sdl := sdlgo.NewSyncStorage()
69
70         if len(os.Args) > 1 {
71                 switch command := os.Args[1]; command {
72                 case "write":
73                         write(sdl)
74                 case "read":
75                         read(sdl)
76                 case "remove":
77                         remove(sdl)
78                 case "noexist":
79                         noexist(sdl)
80                 case "getall":
81                         getall(sdl)
82                 case "listkeys":
83                         listkeys(sdl)
84                 case "removeall":
85                         removeall(sdl)
86                 case "emptymap":
87                         emptymap(sdl)
88                 case "multiple":
89                         multiple(sdl)
90                 case "emptydata":
91                         emptydata(sdl)
92                 case "writeif":
93                         writeif(sdl)
94                 case "writeifnot":
95                         writeifnot(sdl)
96                 case "removeif":
97                         removeif(sdl)
98                 default:
99                         printHelp()
100                 }
101
102         } else {
103                 printHelp()
104         }
105 }
106
107 func printHelp() {
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")
129 }
130
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}
134         start := time.Now()
135         err := sdl.Set("tag1", "key1", data)
136         elapsed := time.Since(start)
137         if err == nil {
138                 fmt.Printf("Write: %s\n", elapsed)
139         } else {
140                 fmt.Println(err)
141         }
142 }
143
144 func read(sdl *sdlgo.SyncStorage) {
145         k := []string{"key1"}
146         start := time.Now()
147         data, err := sdl.Get("tag1", k)
148         elapsed := time.Since(start)
149         if err == nil {
150                 value, ok := data["key1"]
151                 if ok && value != nil {
152                         fmt.Printf("Read: %s\n", elapsed)
153                 } else {
154                         fmt.Printf("Read, not found: %s\n", elapsed)
155                 }
156
157         } else {
158                 fmt.Println(err)
159         }
160 }
161
162 func remove(sdl *sdlgo.SyncStorage) {
163         k := []string{"key1"}
164         start := time.Now()
165         err := sdl.Remove("tag1", k)
166         elapsed := time.Since(start)
167         if err == nil {
168                 fmt.Printf("Remove: %s\n", elapsed)
169         } else {
170                 fmt.Println(err)
171         }
172 }
173
174 func noexist(sdl *sdlgo.SyncStorage) {
175         start := time.Now()
176         _, err := sdl.Get("tag1", []string{"no1", "no2"})
177         elapsed := time.Since(start)
178         if err == nil {
179                 fmt.Printf("Noexist: %s\n", elapsed)
180         } else {
181                 fmt.Println(err)
182         }
183 }
184
185 func getall(sdl *sdlgo.SyncStorage) {
186         start := time.Now()
187         keys, err := sdl.GetAll("tag1")
188         elapsed := time.Since(start)
189         if err == nil {
190                 fmt.Printf("Getall: %s\n", elapsed)
191                 for _, i := range keys {
192                         fmt.Println(i)
193                 }
194         } else {
195                 fmt.Println(err)
196         }
197 }
198
199 func listkeys(sdl *sdlgo.SyncStorage) {
200         start := time.Now()
201         ns := "tag1"
202         pattern := "*"
203         keys, err := sdl.ListKeys(ns, pattern)
204         elapsed := time.Since(start)
205         if err == nil {
206                 fmt.Printf("ListKeys(%s, %s): %s\n", ns, pattern, elapsed)
207                 for _, i := range keys {
208                         fmt.Println(i)
209                 }
210         } else {
211                 fmt.Println(err)
212         }
213 }
214
215 func removeall(sdl *sdlgo.SyncStorage) {
216         start := time.Now()
217         err := sdl.RemoveAll("tag1")
218         elapsed := time.Since(start)
219         if err == nil {
220                 fmt.Printf("Removeall: %s\n", elapsed)
221         } else {
222                 fmt.Println(err)
223         }
224 }
225
226 func emptymap(sdl *sdlgo.SyncStorage) {
227         start := time.Now()
228         err := sdl.Set("tag1", "", "")
229         elapsed := time.Since(start)
230         if err == nil {
231                 fmt.Printf("Emptymap: %s\n", elapsed)
232         } else {
233                 fmt.Println(err)
234         }
235 }
236
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}
240         start := time.Now()
241         err := sdl.Set("tag1", "key1m", data)
242         elapsed := time.Since(start)
243         if err == nil {
244                 fmt.Printf("Multiple: %s ", elapsed)
245         } else {
246                 fmt.Println(err)
247         }
248         start = time.Now()
249         err = sdl.Set("tag1", "key2m", data)
250         elapsed = time.Since(start)
251         if err == nil {
252                 fmt.Printf(" %s \n", elapsed)
253         } else {
254                 fmt.Println(err)
255         }
256 }
257
258 func emptydata(sdl *sdlgo.SyncStorage) {
259         data := []byte{}
260         start := time.Now()
261         err := sdl.Set("tag1", "key1", data)
262         elapsed := time.Since(start)
263         if err == nil {
264                 fmt.Printf("Emptydata: %s\n", elapsed)
265         } else {
266                 fmt.Println(err)
267         }
268 }
269
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}
275         start := time.Now()
276         _, err := sdl.SetIf("tag1", "key1", oldVec, newVec)
277         elapsed := time.Since(start)
278         if err == nil {
279                 fmt.Printf("Writeif: %s\n", elapsed)
280         } else {
281                 fmt.Println(err)
282         }
283 }
284
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}
288         start := time.Now()
289         _, err := sdl.SetIfNotExists("tag1", "key1", vec)
290         elapsed := time.Since(start)
291         if err == nil {
292                 fmt.Printf("Writeifnot: %s\n", elapsed)
293         } else {
294                 fmt.Println(err)
295         }
296 }
297
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}
301         start := time.Now()
302         _, err := sdl.RemoveIf("tag1", "key1", vec)
303         elapsed := time.Since(start)
304         if err == nil {
305                 fmt.Printf("Removeif: %s\n", elapsed)
306         } else {
307                 fmt.Println(err)
308         }
309 }