blob: b9cb1ee9ed3abe056d8fe806d57ab26c66013a31 [file] [log] [blame]
William Kurkianea869482019-04-09 15:16:11 -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 */
16package kvstore
17
npujarec5762e2020-01-01 14:08:48 +053018import "context"
19
William Kurkianea869482019-04-09 15:16:11 -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{}
Manikkaraj kb1d51442019-07-23 10:41:02 -040039 Version int64
William Kurkianea869482019-04-09 15:16:11 -040040 Session string
41 Lease int64
42}
43
William Kurkianea869482019-04-09 15:16:11 -040044// NewKVPair creates a new KVPair object
Manikkaraj kb1d51442019-07-23 10:41:02 -040045func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair {
William Kurkianea869482019-04-09 15:16:11 -040046 kv := new(KVPair)
47 kv.Key = key
48 kv.Value = value
49 kv.Session = session
50 kv.Lease = lease
Manikkaraj kb1d51442019-07-23 10:41:02 -040051 kv.Version = version
William Kurkianea869482019-04-09 15:16:11 -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{}
Manikkaraj kb1d51442019-07-23 10:41:02 -040060 Version int64
William Kurkianea869482019-04-09 15:16:11 -040061}
62
63// NewEvent creates a new Event object
Manikkaraj kb1d51442019-07-23 10:41:02 -040064func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event {
William Kurkianea869482019-04-09 15:16:11 -040065 evnt := new(Event)
66 evnt.EventType = eventType
67 evnt.Key = key
68 evnt.Value = value
Manikkaraj kb1d51442019-07-23 10:41:02 -040069 evnt.Version = version
William Kurkianea869482019-04-09 15:16:11 -040070
71 return evnt
72}
73
Devmalya Paul495b94a2019-08-27 19:42:00 -040074// Client represents the set of APIs a KV Client must implement
William Kurkianea869482019-04-09 15:16:11 -040075type Client interface {
npujarec5762e2020-01-01 14:08:48 +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
Scott Bakere701b862020-02-20 16:19:16 -080084 Watch(ctx context.Context, key string, withPrefix bool) chan *Event
npujarec5762e2020-01-01 14:08:48 +053085 AcquireLock(ctx context.Context, lockName string, timeout int) error
William Kurkianea869482019-04-09 15:16:11 -040086 ReleaseLock(lockName string) error
npujarec5762e2020-01-01 14:08:48 +053087 IsConnectionUp(ctx context.Context) bool // timeout in second
William Kurkianea869482019-04-09 15:16:11 -040088 CloseWatch(key string, ch chan *Event)
89 Close()
90}