-
Notifications
You must be signed in to change notification settings - Fork 279
/
repository.go
88 lines (74 loc) · 2.56 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package state
import (
"context"
"errors"
)
//go:generate mockgen -source=./repository.go -destination=./repository_mock.go -package=state
var (
// ErrNotExist represents key not exist
ErrNotExist = errors.New("not exist")
)
// Repository stores state data, such as metadata/config/status/task etc.
type Repository interface {
// Get retrieves value for given key from repository
Get(ctx context.Context, key string) ([]byte, error)
// List retrieves list for given prefix from repository
List(ctx context.Context, prefix string) ([][]byte, error)
// Put puts a key-value pair into repository
Put(ctx context.Context, key string, val []byte) error
// Delete deletes value for given key from repository
Delete(ctx context.Context, key string) error
// Heartbeat does heartbeat on the key with a value and ttl
Heartbeat(ctx context.Context, key string, value []byte, ttl int64) (<-chan Closed, error)
// PutIfNotExist puts a key with a value,
// 1) returns success if the key does not exist and puts success
// 2) returns failure if key exist
// When this operation success, it will do keepalive background for keep session
PutIfNotExist(ctx context.Context, key string, value []byte, ttl int64) (bool, <-chan Closed, error)
// Watch watches on a key. The watched events will be returned through the returned channel.
Watch(ctx context.Context, key string) WatchEventChan
// WatchPrefix watches on a prefix.All of the changes who has the prefix
// will be notified through the WatchEventChan channel.
WatchPrefix(ctx context.Context, prefixKey string) WatchEventChan
// Batch puts k/v list, this operation is atomic
Batch(ctx context.Context, batch Batch) (bool, error)
// Close closes repository and release resources
Close() error
}
// EventType represents a watch event type.
type EventType int
// Event types.
const (
EventTypeModify EventType = iota
EventTypeDelete
EventTypeAll
)
// KeyValue represents key/value pair
type KeyValue struct {
Key string
Value []byte
}
// Batch represents put list for batch operation
type Batch struct {
KVs []KeyValue
}
// EventKeyValue represents task event
type EventKeyValue struct {
Key string
Value []byte
Rev int64
}
// Event defines repository watch event on key or perfix
type Event struct {
Type EventType
KeyValues []EventKeyValue
Err error
}
// Closed represents close status
type Closed struct{}
// WatchEventChan notify event channel
type WatchEventChan <-chan *Event
// NewRepo create state repository based on config
func NewRepo(config Config) (Repository, error) {
return newEtedRepository(config)
}