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).
25 "github.com/go-redis/redis"
30 type SdlInstance struct {
36 func Create(nameSpace string) *SdlInstance {
37 hostname := os.Getenv("DBAAS_SERVICE_HOST")
39 hostname = "localhost"
41 port := os.Getenv("DBAAS_SERVICE_PORT")
45 redisAddress := hostname + ":" + port
46 client := redis.NewClient(&redis.Options{
48 Password: "", // no password set
49 DB: 0, // use default DB
54 nsPrefix: "{" + nameSpace + "},",
57 s.CheckRedisModuleExtensionCommands()
62 func (s *SdlInstance) CheckRedisModuleExtensionCommands() {
64 commands, err := s.client.Command().Result()
66 redisModuleCommands := []string{
67 "setie", "delie", "setiepub", "deliepub",
68 "setnxpub", "msetmpub", "delmpub",
70 for _, v := range redisModuleCommands {
73 fmt.Println("ERROR: Missing command:", v)
78 fmt.Println("ERROR:", err)
81 fmt.Println("Please make sure that redis extension modules have been installed.")
82 fmt.Println("To install: redis-cli module load /usr/local/libexec/redismodule/libredismodule.so")
86 func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
87 var retVal []interface{}
88 for i, v := range pairs {
90 reflectType := reflect.TypeOf(v)
91 switch reflectType.Kind() {
93 x := reflect.ValueOf(v)
94 for i2 := 0; i2 < x.Len(); i2++ {
96 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
98 retVal = append(retVal, x.Index(i2).Interface())
102 x := reflect.ValueOf(v)
103 for i2 := 0; i2 < x.Len(); i2++ {
105 retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
107 retVal = append(retVal, x.Index(i2).Interface())
111 retVal = append(retVal, s.nsPrefix+v.(string))
114 retVal = append(retVal, v)
120 func checkResultAndError(result interface{}, err error) (bool, error) {
122 if err == redis.Nil {
133 func (s *SdlInstance) Set(pairs ...interface{}) error {
134 keyAndData := s.setNamespaceToKeys(pairs...)
135 err := s.client.MSet(keyAndData...).Err()
139 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
140 var keysWithNs []string
141 for _, v := range keys {
142 keysWithNs = append(keysWithNs, s.nsPrefix+v)
144 val, err := s.client.MGet(keysWithNs...).Result()
145 m := make(map[string]interface{})
149 for i, v := range val {
155 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) (bool, error) {
156 return checkResultAndError(s.client.Do("SETIE", key, newData, oldData).Result())
159 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
160 panic("SetIfiNotExists not implemented\n")
163 func (s *SdlInstance) Remove(keys ...string) {
164 panic("Remove not implemented\n")
167 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
168 panic("RemoveIf not implemented\n")
171 func (s *SdlInstance) GetAll() []string {
172 panic("GetAll not implemented\n")
175 func (s *SdlInstance) RemoveAll() {
176 panic("RemoveAll not implemented\n")