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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
24 "github.com/go-redis/redis"
29 type SdlInstance struct {
35 func Create(nameSpace string) *SdlInstance {
36 hostname := os.Getenv("DBAAS_SERVICE_HOST")
38 hostname = "localhost"
40 port := os.Getenv("DBAAS_SERVICE_PORT")
44 redisAddress := hostname + ":" + port
45 client := redis.NewClient(&redis.Options{
47 Password: "", // no password set
48 DB: 0, // use default DB
53 nsPrefix: "{" + nameSpace + "},",
60 func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
61 var retVal []interface{}
62 for i, v := range pairs {
64 reflectType := reflect.TypeOf(v)
65 switch reflectType.Kind() {
67 x := reflect.ValueOf(v)
68 for i2 := 0; i2 < x.Len(); i2++ {
70 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
72 retVal = append(retVal, x.Index(i2).Interface())
76 x := reflect.ValueOf(v)
77 for i2 := 0; i2 < x.Len(); i2++ {
79 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
81 retVal = append(retVal, x.Index(i2).Interface())
85 retVal = append(retVal, s.nsPrefix+v.(string))
88 retVal = append(retVal, v)
94 func (s *SdlInstance) Set(pairs ...interface{}) error {
95 keyAndData := s.setNamespaceToKeys(pairs...)
96 err := s.client.MSet(keyAndData...).Err()
100 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
101 var keysWithNs []string
102 for _, v := range keys {
103 keysWithNs = append(keysWithNs, s.nsPrefix+v)
105 val, err := s.client.MGet(keysWithNs...).Result()
106 m := make(map[string]interface{})
110 for i, v := range val {
116 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) {
117 panic("SetIf not implemented\n")
120 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
121 panic("SetIfiNotExists not implemented\n")
124 func (s *SdlInstance) Remove(keys ...string) {
125 panic("Remove not implemented\n")
128 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
129 panic("RemoveIf not implemented\n")
132 func (s *SdlInstance) GetAll() []string {
133 panic("GetAll not implemented\n")
136 func (s *SdlInstance) RemoveAll() {
137 panic("RemoveAll not implemented\n")