Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 3 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 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 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 9 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 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. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 15 | */ |
| 16 | package kvstore |
| 17 | |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 18 | import ( |
| 19 | "context" |
| 20 | "time" |
| 21 | ) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 22 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | const ( |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 24 | // Maximum channel buffer between publisher/subscriber goroutines |
| 25 | maxClientChannelBufferSize = 10 |
| 26 | ) |
| 27 | |
| 28 | // These constants represent the event types returned by the KV client |
| 29 | const ( |
| 30 | PUT = iota |
| 31 | DELETE |
| 32 | CONNECTIONDOWN |
| 33 | UNKNOWN |
| 34 | ) |
| 35 | |
| 36 | // KVPair is a common wrapper for key-value pairs returned from the KV store |
| 37 | type KVPair struct { |
| 38 | Key string |
| 39 | Value interface{} |
| 40 | Version int64 |
| 41 | Session string |
| 42 | Lease int64 |
| 43 | } |
| 44 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 45 | // NewKVPair creates a new KVPair object |
| 46 | func NewKVPair(key string, value interface{}, session string, lease int64, version int64) *KVPair { |
| 47 | kv := new(KVPair) |
| 48 | kv.Key = key |
| 49 | kv.Value = value |
| 50 | kv.Session = session |
| 51 | kv.Lease = lease |
| 52 | kv.Version = version |
| 53 | return kv |
| 54 | } |
| 55 | |
| 56 | // Event is generated by the KV client when a key change is detected |
| 57 | type Event struct { |
| 58 | EventType int |
| 59 | Key interface{} |
| 60 | Value interface{} |
| 61 | Version int64 |
| 62 | } |
| 63 | |
| 64 | // NewEvent creates a new Event object |
| 65 | func NewEvent(eventType int, key interface{}, value interface{}, version int64) *Event { |
| 66 | evnt := new(Event) |
| 67 | evnt.EventType = eventType |
| 68 | evnt.Key = key |
| 69 | evnt.Value = value |
| 70 | evnt.Version = version |
| 71 | |
| 72 | return evnt |
| 73 | } |
| 74 | |
| 75 | // Client represents the set of APIs a KV Client must implement |
| 76 | type Client interface { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 77 | List(ctx context.Context, key string) (map[string]*KVPair, error) |
| 78 | Get(ctx context.Context, key string) (*KVPair, error) |
| 79 | Put(ctx context.Context, key string, value interface{}) error |
| 80 | Delete(ctx context.Context, key string) error |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 81 | DeleteWithPrefix(ctx context.Context, prefixKey string) error |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 82 | Watch(ctx context.Context, key string, withPrefix bool) chan *Event |
| 83 | IsConnectionUp(ctx context.Context) bool // timeout in second |
| 84 | CloseWatch(ctx context.Context, key string, ch chan *Event) |
| 85 | Close(ctx context.Context) |
| 86 | |
| 87 | // These APIs are not used. They will be cleaned up in release Voltha 2.9. |
| 88 | // It's not cleaned now to limit changes in all components |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 89 | Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 90 | ReleaseReservation(ctx context.Context, key string) error |
| 91 | ReleaseAllReservations(ctx context.Context) error |
| 92 | RenewReservation(ctx context.Context, key string) error |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 93 | AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 94 | ReleaseLock(lockName string) error |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 95 | } |