khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 16 | package kvstore |
| 17 | |
| 18 | const ( |
| 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 |
| 26 | const ( |
| 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 |
| 34 | type KVPair struct { |
| 35 | Key string |
| 36 | Value interface{} |
| 37 | Session string |
| 38 | Lease int64 |
| 39 | } |
| 40 | |
| 41 | // NewKVPair creates a new KVPair object |
| 42 | func NewKVPair(key string, value interface{}, session string, lease int64) *KVPair { |
| 43 | kv := new(KVPair) |
| 44 | kv.Key = key |
| 45 | kv.Value = value |
| 46 | kv.Session = session |
| 47 | kv.Lease = lease |
| 48 | return kv |
| 49 | } |
| 50 | |
| 51 | // Event is generated by the KV client when a key change is detected |
| 52 | type Event struct { |
| 53 | EventType int |
| 54 | Key interface{} |
| 55 | Value interface{} |
| 56 | } |
| 57 | |
| 58 | // NewEvent creates a new Event object |
| 59 | func NewEvent(eventType int, key interface{}, value interface{}) *Event { |
| 60 | evnt := new(Event) |
| 61 | evnt.EventType = eventType |
| 62 | evnt.Key = key |
| 63 | evnt.Value = value |
| 64 | |
| 65 | return evnt |
| 66 | } |
| 67 | |
| 68 | // Client represents the set of APIs a KV Client must implement |
| 69 | type Client interface { |
| 70 | List(key string, timeout int) (map[string]*KVPair, error) |
| 71 | Get(key string, timeout int) (*KVPair, error) |
| 72 | Put(key string, value interface{}, timeout int) error |
| 73 | Delete(key string, timeout int) error |
| 74 | Reserve(key string, value interface{}, ttl int64) (interface{}, error) |
| 75 | ReleaseReservation(key string) error |
| 76 | ReleaseAllReservations() error |
| 77 | RenewReservation(key string) error |
| 78 | Watch(key string) chan *Event |
| 79 | CloseWatch(key string, ch chan *Event) |
| 80 | Close() |
| 81 | } |