-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
222 lines (183 loc) · 5.09 KB
/
engine.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
package storage
import (
"errors"
"fmt"
"go.uber.org/zap"
)
type Engine struct {
cold *ColdStorageService
hot *HotStorageService
autoCreateCollection bool
}
func NewEngine(dataDir string, autoCreate bool) (*Engine, error) {
// create backing ColdStorageService
cold, err := NewColdStorageService(dataDir)
if err != nil {
return nil, err
}
// create backing HotStorageService
hot, err := NewHotStorageService()
if err != nil {
return nil, err
}
// create and return StorageEngine
svc := &Engine{
cold: cold,
hot: hot,
autoCreateCollection: autoCreate,
}
return svc, nil
}
// list all distinct collections across both hot and cold storage
func (e *Engine) ListCollections() ([]string, error) {
hotCollections := e.hot.ListCollections()
coldCollections, err := e.cold.ListCollections()
if err != nil {
return nil, err
}
// merge collection lists into set of distinct collection ids
ids := make(map[string]bool)
for _, id := range hotCollections {
if _, exists := ids[id]; !exists {
ids[id] = true
}
}
for _, id := range coldCollections {
if _, exists := ids[id]; !exists {
ids[id] = true
}
}
// build list of unique collection ids from map
var uniqueIds []string
for id := range ids {
uniqueIds = append(uniqueIds, id)
}
return uniqueIds, nil
}
// check if a collection exists across hot and cold storage for a given collection id
func (e *Engine) CollectionExists(id string) (bool, error) {
// first check if collection exists hot
if e.hot.CollectionExists(id) {
return true, nil
}
// if not, check if collection exists cold, as this is slower due to requiring filesystem io
exists, err := e.cold.CollectionExists(id)
if err != nil {
return false, err
}
return exists, nil
}
// check if a collection is hot for a given collection id
func (e *Engine) IsHot(id string) (bool, error) {
// check collection exists (hot or cold)
exists, err := e.CollectionExists(id)
if err != nil {
return false, err
}
if exists {
// check if collection is hot specifically
return e.hot.CollectionExists(id), nil
}
return false, errors.New(fmt.Sprintf("No collection found for collection id [%s] in hot or cold storage", id))
}
func (e *Engine) LoadCollection(id string) error {
collection, err := e.cold.ReadFromDiskForId(id)
if err != nil {
return err
}
e.hot.collections[id] = collection
return nil
}
func (e *Engine) FlushCollection(id string) error {
// check that collection is hot
if e.hot.CollectionExists(id) {
// flush collection to disk
err := e.cold.FlushToDisk(id, e.hot.collections[id])
if err != nil {
return err
}
// drop collection from hot storage
return e.hot.DropCollection(id)
}
// return error if collection does not exist
return errors.New(fmt.Sprintf("No collection found for collection id [%s] in hot or cold storage", id))
}
func (e *Engine) FlushAllCollections() []error {
var errs []error
for id := range e.hot.collections {
if err := e.FlushCollection(id); err != nil {
errs = append(errs, err)
}
}
return errs
}
func (e *Engine) ReadKey(id, key string) (string, error) {
// read from hot storage if collection exists there
if e.hot.CollectionExists(id) {
value, err := e.hot.ReadKey(id, key)
if err != nil {
return "", err
}
return value, nil
}
// if collection does not exist in hot storage, check cold storage
exists, err := e.cold.CollectionExists(id)
if err != nil {
return "", err
}
if exists {
// load collection into hot storage
err = e.LoadCollection(id)
if err != nil {
return "", err
}
// now read from hot storage
value, err := e.hot.ReadKey(id, key)
if err != nil {
return "", err
}
return value, nil
}
// return "", err if collection does not exist in either hot or cold storage
return "", errors.New(fmt.Sprintf("No collection found for collection id [%s] in hot or cold storage", id))
}
func (e *Engine) WriteKey(id, key, value string) error {
// ensure collection exists hot, and return error if not
err := e.LoadCollectionIfNotPresent(id)
if err != nil {
zap.S().Error(err)
return err
}
// write key and pass any error encountered upward
return e.hot.WriteKey(id, key, value)
}
func (e *Engine) LoadCollectionIfNotPresent(id string) error {
// return nil if collection is already hot
if e.hot.CollectionExists(id) {
return nil
}
// check if collection exists cold, and load it if it does
exists, err := e.cold.CollectionExists(id)
if err != nil {
return err
}
// if collection exists cold, load it into hot storage
if exists {
err = e.LoadCollection(id)
if err != nil {
return err
}
return nil
}
// if collection auto-creation is enabled, create collection hot
if e.autoCreateCollection {
zap.S().Infof("Creating collection [%s] in hot storage as it does not currently exist.", id)
err = e.hot.CreateCollection(id)
if err != nil {
return err
}
return nil
}
// return error if collection is not present or newly created for given id in hot storage
return errors.New(fmt.Sprintf("Unable to find a collection to load into hot storage with id [%s].", id))
}