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(s.client.Context()).Result()
66 redisModuleCommands := []string{
67 "setie", "delie", "setiepub", "setnxpub",
68 "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 (s *SdlInstance) Set(pairs ...interface{}) error {
121 keyAndData := s.setNamespaceToKeys(pairs...)
122 err := s.client.MSet(s.client.Context(), keyAndData...).Err()
126 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
127 var keysWithNs []string
128 for _, v := range keys {
129 keysWithNs = append(keysWithNs, s.nsPrefix+v)
131 val, err := s.client.MGet(s.client.Context(), keysWithNs...).Result()
132 m := make(map[string]interface{})
136 for i, v := range val {
142 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) {
143 panic("SetIf not implemented\n")
146 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
147 panic("SetIfiNotExists not implemented\n")
150 func (s *SdlInstance) Remove(keys ...string) {
151 panic("Remove not implemented\n")
154 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
155 panic("RemoveIf not implemented\n")
158 func (s *SdlInstance) GetAll() []string {
159 panic("GetAll not implemented\n")
162 func (s *SdlInstance) RemoveAll() {
163 panic("RemoveAll not implemented\n")