forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
313 lines (264 loc) · 7.51 KB
/
dao.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package replay
import (
"bytes"
"encoding/gob"
"errors"
"time"
"github.com/influxdata/kapacitor/services/storage"
)
var (
ErrRecordingExists = errors.New("recording already exists")
ErrNoRecordingExists = errors.New("no recording exists")
ErrReplayExists = errors.New("replay already exists")
ErrNoReplayExists = errors.New("no replay exists")
)
// Data access object for Recording data.
type RecordingDAO interface {
// Retrieve a recording
Get(id string) (Recording, error)
// Create a recording.
// ErrRecordingExists is returned if a recording already exists with the same ID.
Create(recording Recording) error
// Replace an existing recording.
// ErrNoRecordingExists is returned if the recording does not exist.
Replace(recording Recording) error
// Delete a recording.
// It is not an error to delete an non-existent recording.
Delete(id string) error
// List recordings matching a pattern.
// The pattern is shell/glob matching see https://golang.org/pkg/path/#Match
// Offset and limit are pagination bounds. Offset is inclusive starting at index 0.
// More results may exist while the number of returned items is equal to limit.
List(pattern string, offset, limit int) ([]Recording, error)
}
//--------------------------------------------------------------------
// The following structures are stored in a database via gob encoding.
// Changes to the structures could break existing data.
//
// Many of these structures are exact copies of structures found elsewhere,
// this is intentional so that all structures stored in the database are
// defined here and nowhere else. So as to not accidentally change
// the gob serialization format in incompatible ways.
type Status int
const (
Failed Status = iota
Running
Finished
)
type RecordingType int
const (
StreamRecording RecordingType = iota
BatchRecording
)
type Recording struct {
ID string
// URL for stored Recording data. Currently only file:// is supported.
DataURL string
Type RecordingType
Size int64
Date time.Time
Error string
Status Status
Progress float64
}
type rawRecording Recording
func (r Recording) ObjectID() string {
return r.ID
}
func (r Recording) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode((rawRecording)(r))
return buf.Bytes(), err
}
func (r *Recording) UnmarshalBinary(data []byte) error {
return gob.NewDecoder(bytes.NewReader(data)).Decode((*rawRecording)(r))
}
// Name of Date index
const recordingDateIndex = "date"
// Key/Value based implementation of the RecordingDAO.
type recordingKV struct {
store *storage.IndexedStore
}
func newRecordingKV(store storage.Interface) (*recordingKV, error) {
c := storage.DefaultIndexedStoreConfig("recordings", func() storage.BinaryObject {
return new(Recording)
})
c.Indexes = append(c.Indexes, storage.Index{
Name: recordingDateIndex,
ValueFunc: func(o storage.BinaryObject) (string, error) {
r, ok := o.(*Recording)
if !ok {
return "", storage.ImpossibleTypeErr(r, o)
}
return r.Date.UTC().Format(time.RFC3339), nil
},
})
istore, err := storage.NewIndexedStore(store, c)
if err != nil {
return nil, err
}
return &recordingKV{
store: istore,
}, nil
}
func (kv *recordingKV) error(err error) error {
if err == storage.ErrNoObjectExists {
return ErrNoRecordingExists
} else if err == storage.ErrObjectExists {
return ErrRecordingExists
}
return err
}
func (kv *recordingKV) Get(id string) (Recording, error) {
o, err := kv.store.Get(id)
if err != nil {
return Recording{}, kv.error(err)
}
r, ok := o.(*Recording)
if !ok {
return Recording{}, storage.ImpossibleTypeErr(r, o)
}
return *r, nil
}
func (kv *recordingKV) Create(r Recording) error {
return kv.error(kv.store.Create(&r))
}
func (kv *recordingKV) Replace(r Recording) error {
return kv.error(kv.store.Replace(&r))
}
func (kv *recordingKV) Delete(id string) error {
return kv.store.Delete(id)
}
func (kv *recordingKV) List(pattern string, offset, limit int) ([]Recording, error) {
objects, err := kv.store.ReverseList(recordingDateIndex, pattern, offset, limit)
if err != nil {
return nil, err
}
recordings := make([]Recording, len(objects))
for i, o := range objects {
r, ok := o.(*Recording)
if !ok {
return nil, storage.ImpossibleTypeErr(r, o)
}
recordings[i] = *r
}
return recordings, nil
}
// Data access object for Recording data.
type ReplayDAO interface {
// Retrieve a replay
Get(id string) (Replay, error)
// Create a replay.
// ErrReplayExists is returned if a replay already exists with the same ID.
Create(replay Replay) error
// Replace an existing replay.
// ErrNoReplayExists is returned if the replay does not exist.
Replace(replay Replay) error
// Delete a replay.
// It is not an error to delete an non-existent replay.
Delete(id string) error
// List replays matching a pattern.
// The pattern is shell/glob matching see https://golang.org/pkg/path/#Match
// Offset and limit are pagination bounds. Offset is inclusive starting at index 0.
// More results may exist while the number of returned items is equal to limit.
List(pattern string, offset, limit int) ([]Replay, error)
}
type Clock int
const (
Fast Clock = iota
Real
)
type Replay struct {
ID string
RecordingID string
TaskID string
RecordingTime bool
Clock Clock
Date time.Time
Error string
Status Status
Progress float64
}
type rawReplay Replay
func (r Replay) ObjectID() string {
return r.ID
}
func (r Replay) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(rawReplay(r))
return buf.Bytes(), err
}
func (r *Replay) UnmarshalBinary(data []byte) error {
return gob.NewDecoder(bytes.NewReader(data)).Decode((*rawReplay)(r))
}
// Name of the replay date index
const replayDateIndex = "date"
// Key/Value based implementation of the ReplayDAO.
type replayKV struct {
store *storage.IndexedStore
}
func newReplayKV(store storage.Interface) (*replayKV, error) {
c := storage.DefaultIndexedStoreConfig("replays", func() storage.BinaryObject {
return new(Replay)
})
c.Indexes = append(c.Indexes, storage.Index{
Name: replayDateIndex,
ValueFunc: func(o storage.BinaryObject) (string, error) {
r, ok := o.(*Replay)
if !ok {
return "", storage.ImpossibleTypeErr(r, o)
}
return r.Date.UTC().Format(time.RFC3339), nil
},
})
istore, err := storage.NewIndexedStore(store, c)
if err != nil {
return nil, err
}
return &replayKV{
store: istore,
}, nil
}
func (kv *replayKV) error(err error) error {
if err == storage.ErrNoObjectExists {
return ErrNoReplayExists
} else if err == storage.ErrObjectExists {
return ErrReplayExists
}
return err
}
func (kv *replayKV) Get(id string) (Replay, error) {
o, err := kv.store.Get(id)
if err != nil {
return Replay{}, kv.error(err)
}
r, ok := o.(*Replay)
if !ok {
return Replay{}, storage.ImpossibleTypeErr(r, o)
}
return *r, nil
}
func (kv *replayKV) Create(r Replay) error {
return kv.error(kv.store.Create(&r))
}
func (kv *replayKV) Replace(r Replay) error {
return kv.error(kv.store.Replace(&r))
}
func (kv *replayKV) Delete(id string) error {
return kv.store.Delete(id)
}
func (kv *replayKV) List(pattern string, offset, limit int) ([]Replay, error) {
objects, err := kv.store.ReverseList(replayDateIndex, pattern, offset, limit)
if err != nil {
return nil, err
}
replays := make([]Replay, len(objects))
for i, o := range objects {
r, ok := o.(*Replay)
if !ok {
return nil, storage.ImpossibleTypeErr(r, o)
}
replays[i] = *r
}
return replays, nil
}