blob: b9cb1ee9ed3abe056d8fe806d57ab26c66013a31 [file] [log] [blame]
divyadesai81bb7ba2020-03-11 11:45:23 +00001/*
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
18import "context"
19
20const (
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{}
39 Version int64
40 Session string
41 Lease int64
42}
43
44// NewKVPair creates a new KVPair object
45func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair {
46 kv := new(KVPair)
47 kv.Key = key
48 kv.Value = value
49 kv.Session = session
50 kv.Lease = lease
51 kv.Version = version
52 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{}
60 Version int64
61}
62
63// NewEvent creates a new Event object
64func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event {
65 evnt := new(Event)
66 evnt.EventType = eventType
67 evnt.Key = key
68 evnt.Value = value
69 evnt.Version = version
70
71 return evnt
72}
73
74// Client represents the set of APIs a KV Client must implement
75type Client interface {
76 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, withPrefix bool) chan *Event
85 AcquireLock(ctx context.Context, lockName string, timeout int) error
86 ReleaseLock(lockName string) error
87 IsConnectionUp(ctx context.Context) bool // timeout in second
88 CloseWatch(key string, ch chan *Event)
89 Close()
90}