-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
375 lines (309 loc) · 7.23 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// concurrency-safe file-backed element store
//
// - Asynchronous writes (errors reported on next action)
// - Optional cache with LRU eviction on either
// store insertion or store retrieval
//
// To avoid a lot of casting from store.Element on Store#Get, you should probably
// write a wrapper around this element store for each type you intend
// to use it for.
//
// Pull requests welcome
package store
import (
"container/list"
"errors"
"io"
"os"
"path/filepath"
"strconv"
"sync"
)
var (
ErrAlreadyExists = errors.New("element already exists in store")
ErrDoesNotExist = errors.New("element not present in store")
)
type ElementID uint64
func (id ElementID) String() string {
return strconv.FormatUint(uint64(id), 36)
}
func (id *ElementID) FromString(str string) error {
n, err := strconv.ParseUint(str, 36, 64)
if err != nil {
return err
}
*id = ElementID(n)
return nil
}
type Element interface {
// load an element from a reader
Load(io.Reader) error
// store an element to a writer
Store(io.Writer) error
// must return a file-system safe ID
// the two first letters of ID will be
// used to create directories and the
// complete ID string will be used as
// a file name
ID() ElementID
}
type Cache interface {
// update (insert, promote) an element in the cache
Cache(Element)
// retrieve an Element from the cache, or nil if non-existant
Get(id ElementID) Element
}
// cache with LRU eviction policy
type LRUCache struct {
l *list.List
m map[ElementID]*list.Element
mutex sync.Mutex
size int
}
// create a new cache with room for 'size' elements
func NewLRUCache(size int) *LRUCache {
if size <= 0 {
return nil
}
return &LRUCache{
l: list.New(),
m: make(map[ElementID]*list.Element),
size: size,
}
}
// update (insert, promote) an element in the cache
func (l *LRUCache) Cache(el Element) {
// how cache with LRU eviction works:
//
// The cache itself is a linked list and a hash table.
// new elements are put in the front of the list and
// old ones are removed from the end back of the list.
// The hash table is used for lookups in O(1) time
//
// If we assume a constant element size, this
// implementation gives us constant space over time
//
// cache algorithm:
// if in cache:
// promote list element to front of list
// if not in cache:
// if cache not full:
// insert to front of list
// insert front element to hash table
// if cache is full:
// remove last element in list from hash table
// replace last element in list with new element
// move last element in list to front
// insert the now front element in list to hash table
if l == nil || el == nil {
return
}
id := el.ID()
l.mutex.Lock()
defer l.mutex.Unlock()
listElem, exists := l.m[id]
if exists {
l.l.MoveToFront(listElem)
} else {
if len(l.m) < l.size {
listElem = l.l.PushFront(el)
l.m[id] = listElem
} else {
listElem = l.l.Back()
listVal := listElem.Value.(Element)
delete(l.m, listVal.ID())
listElem.Value = el
l.l.MoveToFront(listElem)
l.m[id] = listElem
}
}
}
// retrieve an element from the cache, or nil if
// the element is not in cache
func (l *LRUCache) Get(id ElementID) Element {
if l == nil {
return nil
}
l.mutex.Lock()
defer l.mutex.Unlock()
el, exists := l.m[id]
if !exists {
return nil
}
return el.Value.(Element)
}
// Tells the store when to pass an element
// to the cache
type CacheMode int
const (
CacheOnGet CacheMode = (1 << 0)
CacheOnPut = (1 << 1)
)
type Store struct {
path string
cache Cache
cacheMode CacheMode
inventory map[ElementID]struct{}
ilock sync.RWMutex
inMem map[ElementID]Element
inMemLock sync.RWMutex
writeErr error
activeWrites sync.WaitGroup
}
func New(path string) (*Store, error) {
s := &Store{
path: filepath.Clean(path),
inventory: make(map[ElementID]struct{}),
inMem: make(map[ElementID]Element),
}
os.MkdirAll(s.path, 0700)
walker := func(path string, info os.FileInfo, err error) error {
if err == nil && info.Mode().IsRegular() {
var id ElementID
if err := id.FromString(info.Name()); err == nil {
var x struct{}
s.inventory[id] = x
}
}
return err
}
if err := filepath.Walk(s.path, walker); err != nil {
return nil, err
}
return s, nil
}
func (s *Store) SetCache(c Cache, mode CacheMode) {
s.cache = c
s.cacheMode = mode
}
func (s *Store) Has(id ElementID) bool {
// we check inMem first, because the inventory
// is updated before inMem is updated
s.inMemLock.RLock()
_, exists := s.inMem[id]
s.inMemLock.RUnlock()
if exists {
return true
}
s.ilock.RLock()
_, exists = s.inventory[id]
s.ilock.RUnlock()
return exists
}
func (s *Store) eldir(el Element) string {
id := el.ID().String()
if len(id) == 0 {
panic("zero length ID size")
}
dirLen := 2
if len(id) < dirLen {
dirLen = len(id)
}
return filepath.Join(s.path, id[:dirLen])
}
func (s *Store) get(el Element) error {
id := el.ID()
path := filepath.Join(s.eldir(el), id.String())
fh, err := os.Open(path)
if err != nil {
return err
}
defer fh.Close()
return el.Load(fh)
}
// retrieve an Element either from cache or from disk.
// If retrieved from cache, the cache Element will be returned.
// If retrieved from disk, the Element passed to Get will be
// loaded with the data from disk. Therefor, this function
// should always be called like:
// var el = SomeElement{ID: someID}
// ret, err := s.Get(&el)
func (s *Store) Get(element Element) (Element, error) {
id := element.ID()
if s.cache != nil {
if el := s.cache.Get(id); el != nil {
return el, nil
}
}
s.inMemLock.RLock()
el, exists := s.inMem[id]
s.inMemLock.RUnlock()
if exists {
return el, nil
}
// We check the inventory last because
// an element is only added to the
// inventory on a successful write.
// it may be in write transfer cache (inMem)
// and not in the inventory yet
s.ilock.RLock()
_, exists = s.inventory[id]
s.ilock.RUnlock()
if !exists {
return nil, ErrDoesNotExist
}
err := s.get(element)
if err != nil {
return nil, err
}
if s.cache != nil && (s.cacheMode&CacheOnGet != 0) {
s.cache.Cache(element)
}
return element, nil
}
func (s Store) put(el Element) error {
dir := s.eldir(el)
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
path := filepath.Join(dir, el.ID().String())
fh, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
if err := el.Store(fh); err != nil {
return err
}
return fh.Close()
}
func (s *Store) Put(el Element) error {
if s.writeErr != nil {
return s.writeErr
}
id := el.ID()
if s.Has(id) {
return ErrAlreadyExists
}
s.inMemLock.Lock()
s.inMem[id] = el
s.inMemLock.Unlock()
s.activeWrites.Add(1)
go func() {
defer s.activeWrites.Done()
defer func() {
s.inMemLock.Lock()
delete(s.inMem, id)
s.inMemLock.Unlock()
}()
if err := s.put(el); err != nil {
s.writeErr = err
} else {
var x struct{}
s.ilock.Lock()
s.inventory[id] = x
s.ilock.Unlock()
if s.cache != nil && (s.cacheMode&CacheOnPut) != 0 {
s.cache.Cache(el)
}
}
}()
return nil
}
func (s *Store) Sync() {
s.activeWrites.Wait()
}
func (s *Store) Remove() error {
s.Sync()
return os.RemoveAll(s.path)
}