blob: 088593af6b66c344dba25635f8337b7b642567ef [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
18const (
19 // Default timeout in seconds when making a kvstore request
20 defaultKVGetTimeout = 5
21 // Maximum channel buffer between publisher/subscriber goroutines
22 maxClientChannelBufferSize = 10
23)
24
25// These constants represent the event types returned by the KV client
26const (
27 PUT = iota
28 DELETE
29 CONNECTIONDOWN
30 UNKNOWN
31)
32
33// KVPair is a common wrapper for key-value pairs returned from the KV store
34type KVPair struct {
35 Key string
36 Value interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -040037 Version int64
khenaidoocfee5f42018-07-19 22:47:38 -040038 Session string
39 Lease int64
40}
41
khenaidoocfee5f42018-07-19 22:47:38 -040042// NewKVPair creates a new KVPair object
Stephane Barbarieef6650d2019-07-18 12:15:09 -040043func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair {
khenaidoocfee5f42018-07-19 22:47:38 -040044 kv := new(KVPair)
45 kv.Key = key
46 kv.Value = value
47 kv.Session = session
48 kv.Lease = lease
Stephane Barbarieef6650d2019-07-18 12:15:09 -040049 kv.Version = version
khenaidoocfee5f42018-07-19 22:47:38 -040050 return kv
51}
52
53// Event is generated by the KV client when a key change is detected
54type Event struct {
55 EventType int
56 Key interface{}
57 Value interface{}
Stephane Barbarieef6650d2019-07-18 12:15:09 -040058 Version int64
khenaidoocfee5f42018-07-19 22:47:38 -040059}
60
61// NewEvent creates a new Event object
Stephane Barbarieef6650d2019-07-18 12:15:09 -040062func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event {
khenaidoocfee5f42018-07-19 22:47:38 -040063 evnt := new(Event)
64 evnt.EventType = eventType
65 evnt.Key = key
66 evnt.Value = value
Stephane Barbarieef6650d2019-07-18 12:15:09 -040067 evnt.Version = version
khenaidoocfee5f42018-07-19 22:47:38 -040068
69 return evnt
70}
71
khenaidoob3244212019-08-27 14:32:27 -040072// Client represents the set of APIs a KV Client must implement
khenaidoocfee5f42018-07-19 22:47:38 -040073type Client interface {
sbarbari17d7e222019-11-05 10:02:29 -050074 List(key string, timeout int) (map[string]*KVPair, error)
75 Get(key string, timeout int) (*KVPair, error)
76 Put(key string, value interface{}, timeout int) error
77 Delete(key string, timeout int) error
khenaidoocfee5f42018-07-19 22:47:38 -040078 Reserve(key string, value interface{}, ttl int64) (interface{}, error)
79 ReleaseReservation(key string) error
80 ReleaseAllReservations() error
81 RenewReservation(key string) error
82 Watch(key string) chan *Event
khenaidoobdcb8e02019-03-06 16:28:56 -050083 AcquireLock(lockName string, timeout int) error
84 ReleaseLock(lockName string) error
khenaidoob3244212019-08-27 14:32:27 -040085 IsConnectionUp(timeout int) bool // timeout in second
khenaidoocfee5f42018-07-19 22:47:38 -040086 CloseWatch(key string, ch chan *Event)
87 Close()
88}