Initial commit
[ric-plt/dbaas.git] / testapplication / go / sdl / sdl.go
1 //   Copyright (c) 2019 AT&T Intellectual Property.
2 //   Copyright (c) 2019 Nokia.
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "License");
5 //   you may not use this file except in compliance with the License.
6 //   You may obtain a copy of the License at
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //   Unless required by applicable law or agreed to in writing, software
11 //   distributed under the License is distributed on an "AS IS" BASIS,
12 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //   See the License for the specific language governing permissions and
14 //   limitations under the License.
15
16 package sdl
17
18 import (
19         "github.com/go-redis/redis"
20         "os"
21         "reflect"
22 )
23
24 type SdlInstance struct {
25         nameSpace string
26         nsPrefix  string
27         client    *redis.Client
28 }
29
30 func Create(nameSpace string) *SdlInstance {
31         hostname := os.Getenv("DBAAS_SERVICE_HOST")
32         if hostname == "" {
33                 hostname = "localhost"
34         }
35         port := os.Getenv("DBAAS_SERVICE_PORT")
36         if port == "" {
37                 port = "6379"
38         }
39         redisAddress := hostname + ":" + port
40         client := redis.NewClient(&redis.Options{
41                 Addr:     redisAddress,
42                 Password: "", // no password set
43                 DB:       0,  // use default DB
44         })
45
46         s := SdlInstance{
47                 nameSpace: nameSpace,
48                 nsPrefix:  "{" + nameSpace + "},",
49                 client:    client,
50         }
51
52         return &s
53 }
54
55 func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
56         var retVal []interface{}
57         for i, v := range pairs {
58                 if i%2 == 0 {
59                         reflectType := reflect.TypeOf(v)
60                         switch reflectType.Kind() {
61                         case reflect.Slice:
62                                 x := reflect.ValueOf(v)
63                                 for i2 := 0; i2 < x.Len(); i2++ {
64                                         if i2%2 == 0 {
65                                                 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
66                                         } else {
67                                                 retVal = append(retVal, x.Index(i2).Interface())
68                                         }
69                                 }
70                         case reflect.Array:
71                                 x := reflect.ValueOf(v)
72                                 for i2 := 0; i2 < x.Len(); i2++ {
73                                         if i2%2 == 0 {
74                                                 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
75                                         } else {
76                                                 retVal = append(retVal, x.Index(i2).Interface())
77                                         }
78                                 }
79                         default:
80                                 retVal = append(retVal, s.nsPrefix+v.(string))
81                         }
82                 } else {
83                         retVal = append(retVal, v)
84                 }
85         }
86         return retVal
87 }
88
89 func (s *SdlInstance) Set(pairs ...interface{}) error {
90         keyAndData := s.setNamespaceToKeys(pairs...)
91         err := s.client.MSet(keyAndData...).Err()
92         return err
93 }
94
95 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
96         var keysWithNs []string
97         for _, v := range keys {
98                 keysWithNs = append(keysWithNs, s.nsPrefix+v)
99         }
100         val, err := s.client.MGet(keysWithNs...).Result()
101         m := make(map[string]interface{})
102         if err != nil {
103                 return m, err
104         }
105         for i, v := range val {
106                 m[keys[i]] = v
107         }
108         return m, err
109 }
110
111 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) {
112         panic("SetIf not implemented\n")
113 }
114
115 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
116         panic("SetIfiNotExists not implemented\n")
117 }
118
119 func (s *SdlInstance) Remove(keys ...string) {
120         panic("Remove not implemented\n")
121 }
122
123 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
124         panic("RemoveIf not implemented\n")
125 }
126
127 func (s *SdlInstance) GetAll() []string {
128         panic("GetAll not implemented\n")
129 }
130
131 func (s *SdlInstance) RemoveAll() {
132         panic("RemoveAll not implemented\n")
133 }
134