-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
151 lines (124 loc) · 3.79 KB
/
store.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Package store provides the interface for a key-value store with different backends
package store
import (
"encoding/json"
"errors"
)
var (
// ErrKeyNotFound is returned when key was not found.
ErrKeyNotFound = errors.New("key not found")
// ErrResponseChannelClosed will be returned if the response channel of the keep-alive is closed
ErrResponseChannelClosed = errors.New("keepalive response channel has been closed")
)
// FilterFunc is a function that is called on (each) returned
// key-value pair during a Get request.
type FilterFunc func([]byte, []byte) bool
// HandlerFunc is a function that is called on (each) returned
// key-value pair during a Get request.
type HandlerFunc func([]byte, []byte) error
// ErrorFunc is a function called on (each) error not returned
type ErrorFunc func(error) error
// NotifyCallback is a function that is called after a watch create event
// is received.
type NotifyCallback func()
// Backend is the interface required for a key value store.
type Backend interface {
// Put is used to insert or update an entry.
//
// The entry is added if the key exists or not.
// If value is changed, true is returned, false is returned only if
// value stays unchanged.
//
// If WithInsert option is used and the key already
// exists, nothing is done and false is returned. If key does not exist
// the entry is added and true is returned.
Put(*Entry, ...PutOption) (bool, error)
// Get is used to fetch an entry. If key is not found and WithPrefix is absent ErrKeyNotFound is
// returned.
Get(string, ...GetOption) ([]Entry, error)
// Delete is used to permanently delte an entry. The number of deleted keys will be returned.
Del(string, ...DelOption) (int64, error)
// Watch a key
Watch(string, Watcher, ...WatchOption) error
// WatchChan creates a watcher for a key or prefix and unmarshals events into channel.
// The channel elements have to implement the store.KeyOpSetter interface.
WatchChan(string, interface{}, chan error, ...WatchOption) (WatchStarter, error)
// Close closes the connection.
Close() error
}
// Watcher interface
type Watcher interface {
BeforeWatch() error
BeforeLoop() error
OnDone() error
OnPut([]byte, []byte) error
OnDelete([]byte, []byte) error
}
// WatchStarter interface
type WatchStarter interface {
Start()
}
// BackendKeyer interface extends Backend with key handling
type BackendKeyer interface {
Backend
RelKey(k string) string
AbsKey(k string) string
JoinKey(args ...string) string
SplitKey(key string) []string
KeyLeaf(key string) string
}
// Entry is used to represent data stored by the physical backend
type Entry struct {
Key string
Value []byte
}
// Put is a wrapper around the Backend interface's Put method. This wrapper
// JSON marhals the interface v and uses the generated byte array as value.
func Put(b Backend, key string, v interface{}, opts ...PutOption) (bool, error) {
data, err := json.Marshal(v)
if err != nil {
return false, err
}
return b.Put(&Entry{
Key: key,
Value: data,
}, opts...)
}
// EventMeta contains store events meta data.
type EventMeta struct {
key string
op Operation
}
// SetKey sets the key.
func (e *EventMeta) SetKey(key string) {
e.key = key
}
// Key gets the key.
func (e *EventMeta) Key() string {
return e.key
}
// SetOp sets the operation.
func (e *EventMeta) SetOp(o Operation) {
e.op = o
}
// Op gets the operation.
func (e *EventMeta) Op() Operation {
return e.op
}
// KeyOpSetter interface.
type KeyOpSetter interface {
SetKey(string)
SetOp(Operation)
}
// KeyMarshaller sets struct fields from splitted key.
type KeyMarshaller interface {
MarshalKey([]string) error
}
// Operation represents a store operation.
type Operation string
// All supported operations.
const (
Create Operation = "create"
Update Operation = "update"
Delete Operation = "delete"
)