forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
111 lines (91 loc) · 2.08 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
package load
import (
"encoding/json"
"path"
"github.com/influxdata/kapacitor/services/storage"
)
// version is the current version of the Item structure.
const version = 1
const (
tasksStr = "tasks"
templatesStr = "templates"
handlersStr = "handlers"
)
// Data access object for resources loaded from
// a directory
type ItemsDAO interface {
Set(i Item) error
Delete(id string) error
List(prefix string) ([]Item, error)
Rebuild() error
}
type Item struct {
ID string `json:"id"`
}
func newTaskItem(id string) Item {
return Item{
ID: path.Join(tasksStr, id),
}
}
func newTemplateItem(id string) Item {
return Item{
ID: path.Join(templatesStr, id),
}
}
func newTopicHandlerItem(topic, id string) Item {
return Item{
ID: path.Join(handlersStr, topic, id),
}
}
func (i Item) MarshalBinary() ([]byte, error) {
return storage.VersionJSONEncode(version, i)
}
func (i *Item) UnmarshalBinary(data []byte) error {
return storage.VersionJSONDecode(data, func(version int, dec *json.Decoder) error {
dec.UseNumber()
return dec.Decode(i)
})
}
func (i Item) ObjectID() string {
return i.ID
}
// Key/Value store based implementation of ItemsDAO
type itemKV struct {
store *storage.IndexedStore
}
func newItemKV(store storage.Interface) (*itemKV, error) {
c := storage.DefaultIndexedStoreConfig("load_items", func() storage.BinaryObject {
return new(Item)
})
istore, err := storage.NewIndexedStore(store, c)
if err != nil {
return nil, err
}
return &itemKV{
store: istore,
}, nil
}
func (kv *itemKV) Set(i Item) error {
return kv.store.Put(&i)
}
func (kv *itemKV) Delete(id string) error {
return kv.store.Delete(id)
}
func (kv *itemKV) List(prefix string) ([]Item, error) {
objects, err := kv.store.List(path.Join(storage.DefaultIDIndex, prefix), "", 0, -1)
if err != nil {
return nil, err
}
items := make([]Item, len(objects))
for i, object := range objects {
it, ok := object.(*Item)
if !ok {
return nil, storage.ImpossibleTypeErr(i, object)
}
items[i] = *it
}
return items, nil
}
func (kv *itemKV) Rebuild() error {
return kv.store.Rebuild()
}