blob: 158e62619aabf141d9eaf7e9aebb69b8060e3e56 [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
Neha Sharmacc656962020-04-14 14:26:11 +000018import (
19 "context"
20 "time"
21)
npujarec5762e2020-01-01 14:08:48 +053022
William Kurkianea869482019-04-09 15:16:11 -040023const (
24 // Default timeout in seconds when making a kvstore request
Neha Sharmacc656962020-04-14 14:26:11 +000025 defaultKVGetTimeout = 5 * time.Second
William Kurkianea869482019-04-09 15:16:11 -040026 // Maximum channel buffer between publisher/subscriber goroutines
27 maxClientChannelBufferSize = 10
28)
29
30// These constants represent the event types returned by the KV client
31const (
32 PUT = iota
33 DELETE
34 CONNECTIONDOWN
35 UNKNOWN
36)
37
38// KVPair is a common wrapper for key-value pairs returned from the KV store
39type KVPair struct {
40 Key string
41 Value interface{}
Manikkaraj kb1d51442019-07-23 10:41:02 -040042 Version int64
William Kurkianea869482019-04-09 15:16:11 -040043 Session string
44 Lease int64
45}
46
William Kurkianea869482019-04-09 15:16:11 -040047// NewKVPair creates a new KVPair object
Manikkaraj kb1d51442019-07-23 10:41:02 -040048func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair {
William Kurkianea869482019-04-09 15:16:11 -040049 kv := new(KVPair)
50 kv.Key = key
51 kv.Value = value
52 kv.Session = session
53 kv.Lease = lease
Manikkaraj kb1d51442019-07-23 10:41:02 -040054 kv.Version = version
William Kurkianea869482019-04-09 15:16:11 -040055 return kv
56}
57
58// Event is generated by the KV client when a key change is detected
59type Event struct {
60 EventType int
61 Key interface{}
62 Value interface{}
Manikkaraj kb1d51442019-07-23 10:41:02 -040063 Version int64
William Kurkianea869482019-04-09 15:16:11 -040064}
65
66// NewEvent creates a new Event object
Manikkaraj kb1d51442019-07-23 10:41:02 -040067func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event {
William Kurkianea869482019-04-09 15:16:11 -040068 evnt := new(Event)
69 evnt.EventType = eventType
70 evnt.Key = key
71 evnt.Value = value
Manikkaraj kb1d51442019-07-23 10:41:02 -040072 evnt.Version = version
William Kurkianea869482019-04-09 15:16:11 -040073
74 return evnt
75}
76
Devmalya Paul495b94a2019-08-27 19:42:00 -040077// Client represents the set of APIs a KV Client must implement
William Kurkianea869482019-04-09 15:16:11 -040078type Client interface {
npujarec5762e2020-01-01 14:08:48 +053079 List(ctx context.Context, key string) (map[string]*KVPair, error)
80 Get(ctx context.Context, key string) (*KVPair, error)
81 Put(ctx context.Context, key string, value interface{}) error
82 Delete(ctx context.Context, key string) error
Neha Sharmacc656962020-04-14 14:26:11 +000083 Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error)
npujarec5762e2020-01-01 14:08:48 +053084 ReleaseReservation(ctx context.Context, key string) error
85 ReleaseAllReservations(ctx context.Context) error
86 RenewReservation(ctx context.Context, key string) error
Scott Bakere701b862020-02-20 16:19:16 -080087 Watch(ctx context.Context, key string, withPrefix bool) chan *Event
Neha Sharmacc656962020-04-14 14:26:11 +000088 AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error
William Kurkianea869482019-04-09 15:16:11 -040089 ReleaseLock(lockName string) error
npujarec5762e2020-01-01 14:08:48 +053090 IsConnectionUp(ctx context.Context) bool // timeout in second
William Kurkianea869482019-04-09 15:16:11 -040091 CloseWatch(key string, ch chan *Event)
92 Close()
93}