1 // Copyright (c) 2019 AT&T Intellectual Property.
2 // Copyright (c) 2019 Nokia.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 "github.com/go-redis/redis"
24 type SdlInstance struct {
30 func Create(nameSpace string) *SdlInstance {
31 hostname := os.Getenv("DBAAS_SERVICE_HOST")
33 hostname = "localhost"
35 port := os.Getenv("DBAAS_SERVICE_PORT")
39 redisAddress := hostname + ":" + port
40 client := redis.NewClient(&redis.Options{
42 Password: "", // no password set
43 DB: 0, // use default DB
48 nsPrefix: "{" + nameSpace + "},",
55 func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
56 var retVal []interface{}
57 for i, v := range pairs {
59 reflectType := reflect.TypeOf(v)
60 switch reflectType.Kind() {
62 x := reflect.ValueOf(v)
63 for i2 := 0; i2 < x.Len(); i2++ {
65 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
67 retVal = append(retVal, x.Index(i2).Interface())
71 x := reflect.ValueOf(v)
72 for i2 := 0; i2 < x.Len(); i2++ {
74 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
76 retVal = append(retVal, x.Index(i2).Interface())
80 retVal = append(retVal, s.nsPrefix+v.(string))
83 retVal = append(retVal, v)
89 func (s *SdlInstance) Set(pairs ...interface{}) error {
90 keyAndData := s.setNamespaceToKeys(pairs...)
91 err := s.client.MSet(keyAndData...).Err()
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)
100 val, err := s.client.MGet(keysWithNs...).Result()
101 m := make(map[string]interface{})
105 for i, v := range val {
111 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) {
112 panic("SetIf not implemented\n")
115 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
116 panic("SetIfiNotExists not implemented\n")
119 func (s *SdlInstance) Remove(keys ...string) {
120 panic("Remove not implemented\n")
123 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
124 panic("RemoveIf not implemented\n")
127 func (s *SdlInstance) GetAll() []string {
128 panic("GetAll not implemented\n")
131 func (s *SdlInstance) RemoveAll() {
132 panic("RemoveAll not implemented\n")