blob: e4b1ffffbd2cd8989e6c2a6b5f0338cf681ac242 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
2 * Copyright 2018-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 */
khenaidoocfee5f42018-07-19 22:47:38 -040016package kvstore
17
Neha Sharma7d6f3a92020-04-14 15:26:22 +000018import (
19 "context"
20 "time"
21)
npujar467fe752020-01-16 20:17:45 +053022
khenaidoocfee5f42018-07-19 22:47:38 -040023const (
khenaidoocfee5f42018-07-19 22:47:38 -040024 // Maximum channel buffer between publisher/subscriber goroutines
25 maxClientChannelBufferSize = 10
26)
27
28// These constants represent the event types returned by the KV client
29const (
30 PUT = iota
31 DELETE
32 CONNECTIONDOWN
33 UNKNOWN
34)
35
36// KVPair is a common wrapper for key-value pairs returned from the KV store
37type KVPair struct {
38 Key string
39 Value interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -040040 Version int64
khenaidoocfee5f42018-07-19 22:47:38 -040041 Session string
42 Lease int64
43}
44
khenaidoocfee5f42018-07-19 22:47:38 -040045// NewKVPair creates a new KVPair object
Stephane Barbarieef6650d2019-07-18 12:15:09 -040046func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair {
khenaidoocfee5f42018-07-19 22:47:38 -040047 kv := new(KVPair)
48 kv.Key = key
49 kv.Value = value
50 kv.Session = session
51 kv.Lease = lease
Stephane Barbarieef6650d2019-07-18 12:15:09 -040052 kv.Version = version
khenaidoocfee5f42018-07-19 22:47:38 -040053 return kv
54}
55
56// Event is generated by the KV client when a key change is detected
57type Event struct {
58 EventType int
59 Key interface{}
60 Value interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -040061 Version int64
khenaidoocfee5f42018-07-19 22:47:38 -040062}
63
64// NewEvent creates a new Event object
Stephane Barbarieef6650d2019-07-18 12:15:09 -040065func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event {
khenaidoocfee5f42018-07-19 22:47:38 -040066 evnt := new(Event)
67 evnt.EventType = eventType
68 evnt.Key = key
69 evnt.Value = value
Stephane Barbarieef6650d2019-07-18 12:15:09 -040070 evnt.Version = version
khenaidoocfee5f42018-07-19 22:47:38 -040071
72 return evnt
73}
74
khenaidoob3244212019-08-27 14:32:27 -040075// Client represents the set of APIs a KV Client must implement
khenaidoocfee5f42018-07-19 22:47:38 -040076type Client interface {
npujar467fe752020-01-16 20:17:45 +053077 List(ctx context.Context, key string) (map[string]*KVPair, error)
78 Get(ctx context.Context, key string) (*KVPair, error)
79 Put(ctx context.Context, key string, value interface{}) error
80 Delete(ctx context.Context, key string) error
serkant.uluderyac431f2c2021-02-24 17:32:43 +030081 DeleteWithPrefix(ctx context.Context, prefixKey string) error
khenaidooff4a7bb2021-06-23 13:04:16 -040082 Watch(ctx context.Context, key string, withPrefix bool) chan *Event
83 IsConnectionUp(ctx context.Context) bool // timeout in second
84 CloseWatch(ctx context.Context, key string, ch chan *Event)
85 Close(ctx context.Context)
86
87 // These APIs are not used. They will be cleaned up in release Voltha 2.9.
88 // It's not cleaned now to limit changes in all components
Neha Sharma7d6f3a92020-04-14 15:26:22 +000089 Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error)
npujar467fe752020-01-16 20:17:45 +053090 ReleaseReservation(ctx context.Context, key string) error
91 ReleaseAllReservations(ctx context.Context) error
92 RenewReservation(ctx context.Context, key string) error
Neha Sharma7d6f3a92020-04-14 15:26:22 +000093 AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error
khenaidoobdcb8e02019-03-06 16:28:56 -050094 ReleaseLock(lockName string) error
khenaidoocfee5f42018-07-19 22:47:38 -040095}