Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 | */ |
| 16 | package kvstore |
| 17 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 18 | import "context" |
| 19 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 20 | const ( |
| 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 |
| 28 | const ( |
| 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 |
| 36 | type KVPair struct { |
| 37 | Key string |
| 38 | Value interface{} |
| 39 | Version int64 |
| 40 | Session string |
| 41 | Lease int64 |
| 42 | } |
| 43 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 44 | // NewKVPair creates a new KVPair object |
| 45 | func 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 |
| 56 | type Event struct { |
| 57 | EventType int |
| 58 | Key interface{} |
| 59 | Value interface{} |
| 60 | Version int64 |
| 61 | } |
| 62 | |
| 63 | // NewEvent creates a new Event object |
| 64 | func 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 |
| 75 | type Client interface { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 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 |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 84 | Watch(ctx context.Context, key string, withPrefix bool) chan *Event |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 85 | AcquireLock(ctx context.Context, lockName string, timeout int) error |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 86 | ReleaseLock(lockName string) error |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 87 | IsConnectionUp(ctx context.Context) bool // timeout in second |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 88 | CloseWatch(key string, ch chan *Event) |
| 89 | Close() |
| 90 | } |