First commit
This commit is contained in:
139
setup/dbaas/testapplication/go/sdl/sdl.go
Normal file
139
setup/dbaas/testapplication/go/sdl/sdl.go
Normal file
@@ -0,0 +1,139 @@
|
||||
// Copyright (c) 2019 AT&T Intellectual Property.
|
||||
// Copyright (c) 2019 Nokia.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//
|
||||
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
|
||||
// platform project (RICP).
|
||||
//
|
||||
|
||||
package sdl
|
||||
|
||||
import (
|
||||
"github.com/go-redis/redis"
|
||||
"os"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type SdlInstance struct {
|
||||
nameSpace string
|
||||
nsPrefix string
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func Create(nameSpace string) *SdlInstance {
|
||||
hostname := os.Getenv("DBAAS_SERVICE_HOST")
|
||||
if hostname == "" {
|
||||
hostname = "localhost"
|
||||
}
|
||||
port := os.Getenv("DBAAS_SERVICE_PORT")
|
||||
if port == "" {
|
||||
port = "6379"
|
||||
}
|
||||
redisAddress := hostname + ":" + port
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: redisAddress,
|
||||
Password: "", // no password set
|
||||
DB: 0, // use default DB
|
||||
})
|
||||
|
||||
s := SdlInstance{
|
||||
nameSpace: nameSpace,
|
||||
nsPrefix: "{" + nameSpace + "},",
|
||||
client: client,
|
||||
}
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s *SdlInstance) setNamespaceToKeys(pairs ...interface{}) []interface{} {
|
||||
var retVal []interface{}
|
||||
for i, v := range pairs {
|
||||
if i%2 == 0 {
|
||||
reflectType := reflect.TypeOf(v)
|
||||
switch reflectType.Kind() {
|
||||
case reflect.Slice:
|
||||
x := reflect.ValueOf(v)
|
||||
for i2 := 0; i2 < x.Len(); i2++ {
|
||||
if i2%2 == 0 {
|
||||
retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
|
||||
} else {
|
||||
retVal = append(retVal, x.Index(i2).Interface())
|
||||
}
|
||||
}
|
||||
case reflect.Array:
|
||||
x := reflect.ValueOf(v)
|
||||
for i2 := 0; i2 < x.Len(); i2++ {
|
||||
if i2%2 == 0 {
|
||||
retVal = append(retVal, s.nsPrefix+x.Index(i2).Interface().(string))
|
||||
} else {
|
||||
retVal = append(retVal, x.Index(i2).Interface())
|
||||
}
|
||||
}
|
||||
default:
|
||||
retVal = append(retVal, s.nsPrefix+v.(string))
|
||||
}
|
||||
} else {
|
||||
retVal = append(retVal, v)
|
||||
}
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
|
||||
func (s *SdlInstance) Set(pairs ...interface{}) error {
|
||||
keyAndData := s.setNamespaceToKeys(pairs...)
|
||||
err := s.client.MSet(keyAndData...).Err()
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SdlInstance) Get(keys []string) (map[string]interface{}, error) {
|
||||
var keysWithNs []string
|
||||
for _, v := range keys {
|
||||
keysWithNs = append(keysWithNs, s.nsPrefix+v)
|
||||
}
|
||||
val, err := s.client.MGet(keysWithNs...).Result()
|
||||
m := make(map[string]interface{})
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
for i, v := range val {
|
||||
m[keys[i]] = v
|
||||
}
|
||||
return m, err
|
||||
}
|
||||
|
||||
func (s *SdlInstance) SetIf(key string, oldData, newData interface{}) {
|
||||
panic("SetIf not implemented\n")
|
||||
}
|
||||
|
||||
func (s *SdlInstance) SetIfiNotExists(key string, data interface{}) {
|
||||
panic("SetIfiNotExists not implemented\n")
|
||||
}
|
||||
|
||||
func (s *SdlInstance) Remove(keys ...string) {
|
||||
panic("Remove not implemented\n")
|
||||
}
|
||||
|
||||
func (s *SdlInstance) RemoveIf(key string, data interface{}) {
|
||||
panic("RemoveIf not implemented\n")
|
||||
}
|
||||
|
||||
func (s *SdlInstance) GetAll() []string {
|
||||
panic("GetAll not implemented\n")
|
||||
}
|
||||
|
||||
func (s *SdlInstance) RemoveAll() {
|
||||
panic("RemoveAll not implemented\n")
|
||||
}
|
||||
|
104
setup/dbaas/testapplication/go/testapp.go
Normal file
104
setup/dbaas/testapplication/go/testapp.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// Copyright (c) 2019 AT&T Intellectual Property.
|
||||
// Copyright (c) 2019 Nokia.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//
|
||||
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
|
||||
// platform project (RICP).
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./sdl"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sdl1 := sdl.Create("test1")
|
||||
|
||||
var err error
|
||||
|
||||
err = sdl1.Set("key1", "data1", "key2", "data2")
|
||||
if err != nil {
|
||||
fmt.Printf("unable to write to DB\n")
|
||||
}
|
||||
|
||||
err = sdl1.Set("num1", 1, "num2", 2)
|
||||
if err != nil {
|
||||
fmt.Printf("unable to write to DB\n")
|
||||
}
|
||||
|
||||
d := make([]byte, 3)
|
||||
d[0] = 1
|
||||
d[1] = 2
|
||||
d[2] = 3
|
||||
err = sdl1.Set("arr1", d)
|
||||
if err != nil {
|
||||
fmt.Printf("unable to write to DB\n")
|
||||
}
|
||||
|
||||
p := []string{"pair1", "data1", "pair2", "data2"}
|
||||
err = sdl1.Set(p)
|
||||
if err != nil {
|
||||
fmt.Printf("unable to write to DB\n")
|
||||
}
|
||||
|
||||
a := [4]string{"array1", "adata1", "array2", "adata2"}
|
||||
err = sdl1.Set(a)
|
||||
if err != nil {
|
||||
fmt.Printf("unable to write to DB\n")
|
||||
}
|
||||
|
||||
mix1 := []interface{}{"mix1", "data1", "mix2", 2}
|
||||
err = sdl1.Set(mix1)
|
||||
if err != nil {
|
||||
fmt.Printf("unable to write to DB\n")
|
||||
}
|
||||
|
||||
mix2 := [4]interface{}{"mix3", "data3", "mix4", 4}
|
||||
err = sdl1.Set(mix2)
|
||||
if err != nil {
|
||||
fmt.Printf("unable to write to DB\n")
|
||||
}
|
||||
|
||||
retDataMap, err := sdl1.Get([]string{"key1", "key3", "key2"})
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to read from DB\n")
|
||||
} else {
|
||||
for i, v := range retDataMap {
|
||||
fmt.Printf("%s:%s\n", i, v)
|
||||
}
|
||||
}
|
||||
|
||||
retDataMap2, err := sdl1.Get([]string{"num1", "num2"})
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to read from DB\n")
|
||||
} else {
|
||||
for i, v := range retDataMap2 {
|
||||
fmt.Printf("%s:%s\n", i, v)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("-------------")
|
||||
allKeys := []string{"key1", "key2", "num1", "num2", "pair1", "pair2", "array1", "array2", "mix1", "mix2", "mix3", "mix4", "arr1"}
|
||||
retDataMap3, err := sdl1.Get(allKeys)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to read from DB\n")
|
||||
} else {
|
||||
for i3, v3 := range retDataMap3 {
|
||||
fmt.Printf("%s:%s\n", i3, v3)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user