Skip to content
File

Blob: spec/chord/kv.go

go90 lines
1package chord
2 
3import (
4 "context"
5 "time"
6 
7 "go.miragespace.co/specter/spec/protocol"
8)
9 
10type HashFn func([]byte) uint64
11 
12type SimpleKV interface {
13 // Put will store the value to a node in the Chord network responsible for the given key.
14 // If the key did not exist, a new entry will be added.
15 // If the key already exist, the value will be overwrriten.
16 // If the key was concurrently modified by another request, ErrKVSimpleConflict error is returned.
17 Put(ctx context.Context, key, value []byte) error
18 // Get will fetch the value from a node in the Chord network.
19 Get(ctx context.Context, key []byte) (value []byte, err error)
20 // Delete will hard delete the key from the Chord network.
21 // If the key was concurrently modified by another request, ErrKVSimpleConflict error is returned.
22 // Note that Put/Get methods can share the same keyspace as Prefix methods, and
23 // Delete will not remove the Prefix children.
24 Delete(ctx context.Context, key []byte) error
25}
26 
27type PrefixKV interface {
28 // PrefixAppend appends the child under the prefix. This is useful for tracking
29 // hierarchical structure such as directories.
30 // If the child already exist, ErrKVPrefixConflict error is returned.
31 // Note that Prefix methods can share the same keyspace as Put/Get, and
32 // Delete will not remove the Prefix children.
33 PrefixAppend(ctx context.Context, prefix []byte, child []byte) error
34 // PrefixList returns the children under the prefix.
35 PrefixList(ctx context.Context, prefix []byte) (children [][]byte, err error)
36 // PrefixContains checks if the child is in the prefix children
37 PrefixContains(ctx context.Context, prefix []byte, child []byte) (bool, error)
38 // PrefixRemove removes the matching child under the prefix.
39 // If the child did not exist, this is an no-op
40 PrefixRemove(ctx context.Context, prefix []byte, child []byte) error
41}
42 
43type LeaseKV interface {
44 // Acquire obtains a lease with given time-to-live, and returns a token for
45 // later renewal and release.
46 // On conflicting lease acquisition, ErrKVLeaseConflict error is returned.
47 // If ttl is less than a second or its expiry cannot be represented,
48 // ErrKVLeaseInvalidTTL is returned.
49 // Not to be confused with memory ordering acquire/release semantics.
50 Acquire(ctx context.Context, lease []byte, ttl time.Duration) (token uint64, err error)
51 // Renew extends the lease without shortening its current expiry, given that
52 // prevToken is still valid. Success returns a larger token that replaces
53 // prevToken for subsequent renewal and release of this lease.
54 // If prevToken is no longer current, has expired, or cannot advance within
55 // the supported timestamp range, ErrKVLeaseExpired is returned.
56 // If ttl is less than a second or its expiry cannot be represented,
57 // ErrKVLeaseInvalidTTL is returned.
58 Renew(ctx context.Context, lease []byte, ttl time.Duration, prevToken uint64) (newToken uint64, err error)
59 // Release relinquish the lease held previously by the given token.
60 // If the lease holder has changed, ErrKVLeaseExpired error is returned.
61 // Not to be confused with memory ordering acquire/release semantics.
62 Release(ctx context.Context, lease []byte, token uint64) error
63}
64 
65type KV interface {
66 SimpleKV
67 PrefixKV
68 LeaseKV
69 // Import is used when a node is transferring its KV to a remote node.
70 // Used when a new node joins or a node leaves gracefully
71 Import(ctx context.Context, keys [][]byte, values []*protocol.KVTransfer) error
72 // ListKeys is used when a node is traversing the network to list keys
73 // based on a prefix
74 ListKeys(ctx context.Context, prefix []byte) ([]*protocol.KeyComposite, error)
75}
76 
77type KVProvider interface {
78 KV
79 // Export is used when a Local node is retrieving relevant keys to transfer.
80 // Only used locally, not used for RPC
81 Export(ctx context.Context, keys [][]byte) ([]*protocol.KVTransfer, error)
82 // RangeKeys retrieve actual byte values of the keys, given the [low, high]
83 // range of key hashes.
84 // Only used locally, not used for RPC
85 RangeKeys(ctx context.Context, low, high uint64) ([][]byte, error)
86 // RemoveKeys hard delete keys from local node.
87 // Only used locally, not used for RPC
88 RemoveKeys(ctx context.Context, keys [][]byte) error
89}