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 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 18 | import ( |
| 19 | "github.com/opencord/voltha-go/common/log" |
| 20 | ) |
| 21 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 22 | const ( |
| 23 | // Default timeout in seconds when making a kvstore request |
| 24 | defaultKVGetTimeout = 5 |
| 25 | // Maximum channel buffer between publisher/subscriber goroutines |
| 26 | maxClientChannelBufferSize = 10 |
| 27 | ) |
| 28 | |
| 29 | // These constants represent the event types returned by the KV client |
| 30 | const ( |
| 31 | PUT = iota |
| 32 | DELETE |
| 33 | CONNECTIONDOWN |
| 34 | UNKNOWN |
| 35 | ) |
| 36 | |
| 37 | // KVPair is a common wrapper for key-value pairs returned from the KV store |
| 38 | type KVPair struct { |
| 39 | Key string |
| 40 | Value interface{} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 41 | Version int64 |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 42 | Session string |
| 43 | Lease int64 |
| 44 | } |
| 45 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 46 | func init() { |
| 47 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 48 | } |
| 49 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 50 | // NewKVPair creates a new KVPair object |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 51 | func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 52 | kv := new(KVPair) |
| 53 | kv.Key = key |
| 54 | kv.Value = value |
| 55 | kv.Session = session |
| 56 | kv.Lease = lease |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 57 | kv.Version = version |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 58 | return kv |
| 59 | } |
| 60 | |
| 61 | // Event is generated by the KV client when a key change is detected |
| 62 | type Event struct { |
| 63 | EventType int |
| 64 | Key interface{} |
| 65 | Value interface{} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 66 | Version int64 |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // NewEvent creates a new Event object |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 70 | func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 71 | evnt := new(Event) |
| 72 | evnt.EventType = eventType |
| 73 | evnt.Key = key |
| 74 | evnt.Value = value |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 75 | evnt.Version = version |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 76 | |
| 77 | return evnt |
| 78 | } |
| 79 | |
| 80 | // Client represents the set of APIs a KV Client must implement |
| 81 | type Client interface { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 82 | List(key string, timeout int, lock ...bool) (map[string]*KVPair, error) |
| 83 | Get(key string, timeout int, lock ...bool) (*KVPair, error) |
| 84 | Put(key string, value interface{}, timeout int, lock ...bool) error |
| 85 | Delete(key string, timeout int, lock ...bool) error |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 86 | Reserve(key string, value interface{}, ttl int64) (interface{}, error) |
| 87 | ReleaseReservation(key string) error |
| 88 | ReleaseAllReservations() error |
| 89 | RenewReservation(key string) error |
| 90 | Watch(key string) chan *Event |
khenaidoo | bdcb8e0 | 2019-03-06 16:28:56 -0500 | [diff] [blame] | 91 | AcquireLock(lockName string, timeout int) error |
| 92 | ReleaseLock(lockName string) error |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 93 | CloseWatch(key string, ch chan *Event) |
| 94 | Close() |
| 95 | } |