blob: d30e049c5e77151e9ddd08285fbb2d66f2c5d57d [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
npujar467fe752020-01-16 20:17:45 +053018import "context"
19
khenaidoocfee5f42018-07-19 22:47:38 -040020const (
21 // Default timeout in seconds when making a kvstore request
22 defaultKVGetTimeout = 5
23 // Maximum channel buffer between publisher/subscriber goroutines
24 maxClientChannelBufferSize = 10
25)
26
27// These constants represent the event types returned by the KV client
28const (
29 PUT = iota
30 DELETE
31 CONNECTIONDOWN
32 UNKNOWN
33)
34
35// KVPair is a common wrapper for key-value pairs returned from the KV store
36type KVPair struct {
37 Key string
38 Value interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -040039 Version int64
khenaidoocfee5f42018-07-19 22:47:38 -040040 Session string
41 Lease int64
42}
43
khenaidoocfee5f42018-07-19 22:47:38 -040044// NewKVPair creates a new KVPair object
Stephane Barbarieef6650d2019-07-18 12:15:09 -040045func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair {
khenaidoocfee5f42018-07-19 22:47:38 -040046 kv := new(KVPair)
47 kv.Key = key
48 kv.Value = value
49 kv.Session = session
50 kv.Lease = lease
Stephane Barbarieef6650d2019-07-18 12:15:09 -040051 kv.Version = version
khenaidoocfee5f42018-07-19 22:47:38 -040052 return kv
53}
54
55// Event is generated by the KV client when a key change is detected
56type Event struct {
57 EventType int
58 Key interface{}
59 Value interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -040060 Version int64
khenaidoocfee5f42018-07-19 22:47:38 -040061}
62
63// NewEvent creates a new Event object
Stephane Barbarieef6650d2019-07-18 12:15:09 -040064func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event {
khenaidoocfee5f42018-07-19 22:47:38 -040065 evnt := new(Event)
66 evnt.EventType = eventType
67 evnt.Key = key
68 evnt.Value = value
Stephane Barbarieef6650d2019-07-18 12:15:09 -040069 evnt.Version = version
khenaidoocfee5f42018-07-19 22:47:38 -040070
71 return evnt
72}
73
khenaidoob3244212019-08-27 14:32:27 -040074// Client represents the set of APIs a KV Client must implement
khenaidoocfee5f42018-07-19 22:47:38 -040075type Client interface {
npujar467fe752020-01-16 20:17:45 +053076 List(ctx context.Context, key string) (map[string]*KVPair, error)
77 Get(ctx context.Context, key string) (*KVPair, error)
78 Put(ctx context.Context, key string, value interface{}) error
79 Delete(ctx context.Context, key string) error
80 Reserve(ctx context.Context, key string, value interface{}, ttl int64) (interface{}, error)
81 ReleaseReservation(ctx context.Context, key string) error
82 ReleaseAllReservations(ctx context.Context) error
83 RenewReservation(ctx context.Context, key string) error
84 Watch(ctx context.Context, key string) chan *Event
85 AcquireLock(ctx context.Context, lockName string, timeout int) error
khenaidoobdcb8e02019-03-06 16:28:56 -050086 ReleaseLock(lockName string) error
npujar467fe752020-01-16 20:17:45 +053087 IsConnectionUp(ctx context.Context) bool // timeout in second
khenaidoocfee5f42018-07-19 22:47:38 -040088 CloseWatch(key string, ch chan *Event)
89 Close()
90}