2 Copyright (c) 2019 AT&T Intellectual Property.
3 Copyright (c) 2018-2019 Nokia.
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
24 "gerrit.oran-osc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
27 type Idatabase interface {
28 MSet(pairs ...interface{}) error
29 MGet(keys []string) ([]interface{}, error)
31 Del(keys []string) error
32 Keys(key string) ([]string, error)
35 type SdlInstance struct {
41 func Create(NameSpace string) *SdlInstance {
42 db := sdlgoredis.Create()
45 NsPrefix: "{" + NameSpace + "},",
52 func (s *SdlInstance) Close() error {
56 func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
57 var retVal []interface{}
58 for i, v := range pairs {
60 reflectType := reflect.TypeOf(v)
61 switch reflectType.Kind() {
63 x := reflect.ValueOf(v)
64 for i2 := 0; i2 < x.Len(); i2++ {
66 retVal = append(retVal, s.NsPrefix+x.Index(i2).Interface().(string))
68 retVal = append(retVal, x.Index(i2).Interface())
72 x := reflect.ValueOf(v)
73 for i2 := 0; i2 < x.Len(); i2++ {
75 retVal = append(retVal, s.NsPrefix+x.Index(i2).Interface().(string))
77 retVal = append(retVal, x.Index(i2).Interface())
81 retVal = append(retVal, s.NsPrefix+v.(string))
84 retVal = append(retVal, v)
90 func (s *SdlInstance) Set(pairs ...interface{}) error {
95 keyAndData := s.setNamespaceToKeys(pairs...)
96 err := s.MSet(keyAndData...)
100 func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
101 m := make(map[string]interface{})
106 var keysWithNs []string
107 for _, v := range keys {
108 keysWithNs = append(keysWithNs, s.NsPrefix+v)
110 val, err := s.MGet(keysWithNs)
114 for i, v := range val {
120 func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) {
121 panic("SetIf not implemented\n")
124 func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
125 panic("SetIfiNotExists not implemented\n")
128 func (s *SdlInstance) Remove(keys []string) error {
133 var keysWithNs []string
134 for _, v := range keys {
135 keysWithNs = append(keysWithNs, s.NsPrefix+v)
137 err := s.Del(keysWithNs)
141 func (s *SdlInstance) RemoveIf(key string, data interface{}) {
142 panic("RemoveIf not implemented\n")
145 func (s *SdlInstance) GetAll() ([]string, error) {
146 keys, err := s.Keys(s.NsPrefix + "*")
147 var retVal []string = nil
151 for _, v := range keys {
152 retVal = append(retVal, strings.Split(v, s.NsPrefix)[1])
157 func (s *SdlInstance) RemoveAll() error {
158 keys, err := s.Keys(s.NsPrefix + "*")