Initial commit
[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 package main
19
20 import (
21         "fmt"
22         "os"
23         "time"
24
25         "gerrit.oran-osc.org/r/ric-plt/sdlgo"
26 )
27
28 /*
29  * This program demonsrates the basic usage of sdlgo module.
30  *
31  * The following scenarios are provided:
32  *
33  * - write: Write data. Performance is measured.
34  *
35  * - read: Read data. Performance is measured.
36  *
37  * - remove: Remove data. Performance is measured.
38  *
39  * - noexist: Read non-existing data. Performance is measured and empty container is returned as nothing was
40  *            found.
41  *
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.
44  *
45  * - removeall: Remove all keys within a namespace. Performance is measured.
46  *
47  * - emptymap: Write an empty container. Performance is measured.
48  *
49  * - multiple: Make two writes. Performance is measured.
50  *
51  * - emptydata: Write empty data for a key. Performance is measured.
52  *
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
55  *                           is measured.
56  *
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
59  *             is measured.
60  */
61
62 func main() {
63         sdl := sdlgo.NewSdlInstance("tag1", sdlgo.NewDatabase())
64
65         if len(os.Args) > 1 {
66                 switch command := os.Args[1]; command {
67                 case "write":
68                         write(sdl)
69                 case "read":
70                         read(sdl)
71                 case "remove":
72                         remove(sdl)
73                 case "noexist":
74                         noexist(sdl)
75                 case "getall":
76                         getall(sdl)
77                 case "removeall":
78                         removeall(sdl)
79                 case "emptymap":
80                         emptymap(sdl)
81                 case "multiple":
82                         multiple(sdl)
83                 case "emptydata":
84                         emptydata(sdl)
85                 case "writeif":
86                         writeif(sdl)
87                 case "writeifnot":
88                         writeifnot(sdl)
89                 case "removeif":
90                         removeif(sdl)
91                 default:
92                         printHelp()
93                 }
94
95         } else {
96                 printHelp()
97         }
98 }
99
100 func printHelp() {
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")
121 }
122
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}
126         start := time.Now()
127         err := sdl.Set("key1", data)
128         elapsed := time.Since(start)
129         if err == nil {
130                 fmt.Printf("Write: %s\n", elapsed)
131         } else {
132                 fmt.Println(err)
133         }
134 }
135
136 func read(sdl *sdlgo.SdlInstance) {
137         k := []string{"key1"}
138         start := time.Now()
139         data, err := sdl.Get(k)
140         elapsed := time.Since(start)
141         if err == nil {
142                 value, ok := data["key1"]
143                 if ok && value != nil {
144                         fmt.Printf("Read: %s\n", elapsed)
145                 } else {
146                         fmt.Printf("Read, not found: %s\n", elapsed)
147                 }
148
149         } else {
150                 fmt.Println(err)
151         }
152 }
153
154 func remove(sdl *sdlgo.SdlInstance) {
155         k := []string{"key1"}
156         start := time.Now()
157         err := sdl.Remove(k)
158         elapsed := time.Since(start)
159         if err == nil {
160                 fmt.Printf("Remove: %s\n", elapsed)
161         } else {
162                 fmt.Println(err)
163         }
164 }
165
166 func noexist(sdl *sdlgo.SdlInstance) {
167         start := time.Now()
168         _, err := sdl.Get([]string{"no1", "no2"})
169         elapsed := time.Since(start)
170         if err == nil {
171                 fmt.Printf("Noexist: %s\n", elapsed)
172         } else {
173                 fmt.Println(err)
174         }
175 }
176
177 func getall(sdl *sdlgo.SdlInstance) {
178         start := time.Now()
179         keys, err := sdl.GetAll()
180         elapsed := time.Since(start)
181         if err == nil {
182                 fmt.Printf("Getall: %s\n", elapsed)
183                 for _, i := range keys {
184                         fmt.Println(i)
185                 }
186         } else {
187                 fmt.Println(err)
188         }
189 }
190
191 func removeall(sdl *sdlgo.SdlInstance) {
192         start := time.Now()
193         err := sdl.RemoveAll()
194         elapsed := time.Since(start)
195         if err == nil {
196                 fmt.Printf("Removeall: %s\n", elapsed)
197         } else {
198                 fmt.Println(err)
199         }
200 }
201
202 func emptymap(sdl *sdlgo.SdlInstance) {
203         start := time.Now()
204         err := sdl.Set("", "")
205         elapsed := time.Since(start)
206         if err == nil {
207                 fmt.Printf("Emptymap: %s\n", elapsed)
208         } else {
209                 fmt.Println(err)
210         }
211 }
212
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}
216         start := time.Now()
217         err := sdl.Set("key1m", data)
218         elapsed := time.Since(start)
219         if err == nil {
220                 fmt.Printf("Multiple: %s ", elapsed)
221         } else {
222                 fmt.Println(err)
223         }
224         start = time.Now()
225         err = sdl.Set("key2m", data)
226         elapsed = time.Since(start)
227         if err == nil {
228                 fmt.Printf(" %s \n", elapsed)
229         } else {
230                 fmt.Println(err)
231         }
232 }
233
234 func emptydata(sdl *sdlgo.SdlInstance) {
235         data := []byte{}
236         start := time.Now()
237         err := sdl.Set("key1", data)
238         elapsed := time.Since(start)
239         if err == nil {
240                 fmt.Printf("Emptydata: %s\n", elapsed)
241         } else {
242                 fmt.Println(err)
243         }
244 }
245
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}
251         start := time.Now()
252         _, err := sdl.SetIf("key1", oldVec, newVec)
253         elapsed := time.Since(start)
254         if err == nil {
255                 fmt.Printf("Writeif: %s\n", elapsed)
256         } else {
257                 fmt.Println(err)
258         }
259 }
260
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}
264         start := time.Now()
265         _, err := sdl.SetIfNotExists("key1", vec)
266         elapsed := time.Since(start)
267         if err == nil {
268                 fmt.Printf("Writeifnot: %s\n", elapsed)
269         } else {
270                 fmt.Println(err)
271         }
272 }
273
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}
277         start := time.Now()
278         _, err := sdl.RemoveIf("key1", vec)
279         elapsed := time.Since(start)
280         if err == nil {
281                 fmt.Printf("Removeif: %s\n", elapsed)
282         } else {
283                 fmt.Println(err)
284         }
285 }