blob: 1d6097d796f577409ef345ff9c37fcb634ca431c [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001/*
2 * Copyright 2020-present Open Networking Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17// Package db holds utils for datastore implementation
18package db
19
20import (
21 "context"
22 "errors"
23
24 "github.com/opencord/voltha-lib-go/v4/pkg/log"
25)
26
27// Read function reads key value pair from db/kvstore
28func Read(ctx context.Context, key string) (string, error) {
29 if kvClient != nil {
30 logger.Debugw(ctx, "Reading-key-value-pair-from-kv-store", log.Fields{"key": key})
Prince Pereira32d40f22021-05-27 06:14:29 +000031 kvPair, err := kvClient.Get(ctx, key)
Prince Pereirac1c21d62021-04-22 08:38:15 +000032 if err != nil {
33 return "", err
34 }
35 if kvPair == nil {
36 return "", errors.New("key not found")
37 }
38 return string(kvPair.Value.([]byte)), nil
39
40 }
41 logger.Errorw(ctx, "Reading-key-value-pair-in kv-store-failed-because-kvstore-not-initialised", log.Fields{"key": key})
42 return "", errors.New("kvstore not initialised")
43}
44
45// ReadAll function reads all key value pair from db/kvstore
46func ReadAll(ctx context.Context, keyPrefix string) (map[string]string, error) {
47 keyValues := make(map[string]string)
48 if kvClient != nil {
49 logger.Debugw(ctx, "Reading-all-key-value-pairs-from-kv-store", log.Fields{"key-prefix": keyPrefix})
Prince Pereira32d40f22021-05-27 06:14:29 +000050 kvPairs, err := kvClient.List(ctx, keyPrefix)
Prince Pereirac1c21d62021-04-22 08:38:15 +000051 if err != nil {
52 return keyValues, err
53 }
54 if kvPairs == nil {
55 return keyValues, errors.New("key not found")
56 }
57
58 for key, kvPair := range kvPairs {
59 keyValues[key] = string(kvPair.Value.([]byte))
60 }
61 return keyValues, nil
62 }
63 logger.Errorw(ctx, "Reading-all-key-value-pair-in-kv-store-failed-because-kvstore-not-initialised", log.Fields{"key-prefix": keyPrefix})
64 return keyValues, errors.New("kvstore not initialised")
65}
66
67// Del function deletes key value pair from db/kvstore
68func Del(ctx context.Context, key string) error {
69 if kvClient != nil {
70 logger.Debugw(ctx, "Deleting-key-value-pair-from-kv-store", log.Fields{"key": key})
Prince Pereira32d40f22021-05-27 06:14:29 +000071 return kvClient.Delete(ctx, key)
Prince Pereirac1c21d62021-04-22 08:38:15 +000072 }
73 logger.Errorw(ctx, "Deleting-key-value-pair-in-kv-store-failed-because-kvstore-not-initialised", log.Fields{"key": key})
74 return errors.New("kvstore not initialised")
75}
76
77// DelAll function deletes all key value pair from db/kvstore with provided key prefix
78func DelAll(ctx context.Context, keyPrefix string) error {
79 if kvClient != nil {
80 logger.Debugw(ctx, "Deleting-all-key-value-pair-from-kv-store-with-prefix", log.Fields{"key-prefix": keyPrefix})
Prince Pereira32d40f22021-05-27 06:14:29 +000081 return kvClient.DeleteWithPrefix(ctx, keyPrefix)
Prince Pereirac1c21d62021-04-22 08:38:15 +000082 }
83 logger.Errorw(ctx, "Deleting-all-key-value-pair-in-kv-store-with-prefix-failed-because-kvstore-not-initialised", log.Fields{"key-prefix": keyPrefix})
84 return errors.New("kvstore not initialised")
85}
86
87// Put function stores key value pair in db/kvstore
88func Put(ctx context.Context, key string, val string) error {
89 if kvClient != nil {
90 logger.Debugw(ctx, "Storing-key-value-pair-in-kv-store", log.Fields{"key": key, "value": val})
Prince Pereira32d40f22021-05-27 06:14:29 +000091 return kvClient.Put(ctx, key, val)
Prince Pereirac1c21d62021-04-22 08:38:15 +000092 }
93 logger.Errorw(ctx, "Storing-key-value-pair-in-kv-store-failed-because-kvstore-not-initialised", log.Fields{"key": key, "value": val})
94 return errors.New("kvstore not initialised")
95}