|
| 1 | +// Copyright 2018-2022 Burak Sezer |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package olric |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/buraksezer/olric/internal/dmap" |
| 22 | + "github.com/buraksezer/olric/stats" |
| 23 | +) |
| 24 | + |
| 25 | +const DefaultScanCount = 10 |
| 26 | + |
| 27 | +type Iterator interface { |
| 28 | + Next() bool |
| 29 | + Key() string |
| 30 | + Close() |
| 31 | +} |
| 32 | + |
| 33 | +type LockContext interface { |
| 34 | + Unlock(ctx context.Context) error |
| 35 | + Lease(ctx context.Context, duration time.Duration) error |
| 36 | +} |
| 37 | + |
| 38 | +type PutOption func(*dmap.PutConfig) |
| 39 | + |
| 40 | +func EX(ex time.Duration) PutOption { |
| 41 | + return func(cfg *dmap.PutConfig) { |
| 42 | + cfg.HasEX = true |
| 43 | + cfg.EX = ex |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func PX(px time.Duration) PutOption { |
| 48 | + return func(cfg *dmap.PutConfig) { |
| 49 | + cfg.HasPX = true |
| 50 | + cfg.PX = px |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func EXAT(exat time.Duration) PutOption { |
| 55 | + return func(cfg *dmap.PutConfig) { |
| 56 | + cfg.HasEXAT = true |
| 57 | + cfg.EXAT = exat |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func PXAT(pxat time.Duration) PutOption { |
| 62 | + return func(cfg *dmap.PutConfig) { |
| 63 | + cfg.HasPXAT = true |
| 64 | + cfg.PXAT = pxat |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func NX() PutOption { |
| 69 | + return func(cfg *dmap.PutConfig) { |
| 70 | + cfg.HasNX = true |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func XX() PutOption { |
| 75 | + return func(cfg *dmap.PutConfig) { |
| 76 | + cfg.HasXX = true |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +type dmapConfig struct{} |
| 81 | + |
| 82 | +type DMapOption func(*dmapConfig) |
| 83 | + |
| 84 | +type ScanOption func(*dmap.ScanConfig) |
| 85 | + |
| 86 | +func Count(c int) ScanOption { |
| 87 | + return func(cfg *dmap.ScanConfig) { |
| 88 | + cfg.HasCount = true |
| 89 | + cfg.Count = c |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func Match(s string) ScanOption { |
| 94 | + return func(cfg *dmap.ScanConfig) { |
| 95 | + cfg.HasMatch = true |
| 96 | + cfg.Match = s |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// DMap describes a distributed map rc. |
| 101 | +type DMap interface { |
| 102 | + // Name exposes name of the DMap. |
| 103 | + Name() string |
| 104 | + |
| 105 | + // Put sets the value for the given key. It overwrites any previous value for |
| 106 | + // that key, and it's thread-safe. The key has to be string. value type is arbitrary. |
| 107 | + // It is safe to modify the contents of the arguments after Put returns but not before. |
| 108 | + Put(ctx context.Context, key string, value interface{}, options ...PutOption) error |
| 109 | + |
| 110 | + // Get gets the value for the given key. It returns ErrKeyNotFound if the DB |
| 111 | + // does not contain the key. It's thread-safe. It is safe to modify the contents |
| 112 | + // of the returned value. |
| 113 | + Get(ctx context.Context, key string) (*GetResponse, error) |
| 114 | + |
| 115 | + // Delete deletes the value for the given key. Delete will not return error |
| 116 | + // if key doesn't exist. It's thread-safe. It is safe to modify the contents |
| 117 | + // of the argument after Delete returns. |
| 118 | + Delete(ctx context.Context, key string) error |
| 119 | + |
| 120 | + // Incr atomically increments key by delta. The return value is the new value |
| 121 | + // after being incremented or an error. |
| 122 | + Incr(ctx context.Context, key string, delta int) (int, error) |
| 123 | + |
| 124 | + // Decr atomically decrements key by delta. The return value is the new value |
| 125 | + // after being decremented or an error. |
| 126 | + Decr(ctx context.Context, key string, delta int) (int, error) |
| 127 | + |
| 128 | + // GetPut atomically sets key to value and returns the old value stored at key. |
| 129 | + GetPut(ctx context.Context, key string, value interface{}) (*GetResponse, error) |
| 130 | + |
| 131 | + // Expire updates the expiry for the given key. It returns ErrKeyNotFound if |
| 132 | + // the DB does not contain the key. It's thread-safe. |
| 133 | + Expire(ctx context.Context, key string, timeout time.Duration) error |
| 134 | + |
| 135 | + // Lock sets a lock for the given key. Acquired lock is only for the key in |
| 136 | + // this dmap. |
| 137 | + // |
| 138 | + // It returns immediately if it acquires the lock for the given key. Otherwise, |
| 139 | + // it waits until deadline. |
| 140 | + // |
| 141 | + // You should know that the locks are approximate, and only to be used for |
| 142 | + // non-critical purposes. |
| 143 | + Lock(ctx context.Context, key string, deadline time.Duration) (LockContext, error) |
| 144 | + |
| 145 | + // LockWithTimeout sets a lock for the given key. If the lock is still unreleased |
| 146 | + // the end of given period of time, |
| 147 | + // it automatically releases the lock. Acquired lock is only for the key in |
| 148 | + // this dmap. |
| 149 | + // |
| 150 | + // It returns immediately if it acquires the lock for the given key. Otherwise, |
| 151 | + // it waits until deadline. |
| 152 | + // |
| 153 | + // You should know that the locks are approximate, and only to be used for |
| 154 | + // non-critical purposes. |
| 155 | + LockWithTimeout(ctx context.Context, key string, timeout, deadline time.Duration) (LockContext, error) |
| 156 | + |
| 157 | + Scan(ctx context.Context, options ...ScanOption) (Iterator, error) |
| 158 | + |
| 159 | + // Destroy flushes the given dmap on the cluster. You should know that there |
| 160 | + // is no global lock on DMaps. So if you call Put/PutEx and Destroy methods |
| 161 | + // concurrently on the cluster, Put call may set new values to the dmap. |
| 162 | + Destroy(ctx context.Context) error |
| 163 | +} |
| 164 | + |
| 165 | +type statsConfig struct { |
| 166 | + CollectRuntime bool |
| 167 | +} |
| 168 | + |
| 169 | +type StatsOption func(*statsConfig) |
| 170 | + |
| 171 | +type Client interface { |
| 172 | + NewDMap(name string, options ...DMapOption) (DMap, error) |
| 173 | + Stats(ctx context.Context, options ...StatsOption) (stats.Stats, error) |
| 174 | + Ping(ctx context.Context, addr string) error |
| 175 | + PingWithMessage(ctx context.Context, addr, message string) (string, error) |
| 176 | + RoutingTable(ctx context.Context) (RoutingTable, error) |
| 177 | + Close(ctx context.Context) error |
| 178 | +} |
0 commit comments