-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_memory_store.go
63 lines (49 loc) · 1.4 KB
/
in_memory_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
package gokvstore
import (
"github.com/gustapinto/go-kv-store/gen"
)
// InMemoryStore A in memory based record store. The use of this store is not
// recommended, as it exists mainly to simplify internal library testing and does
// not support some features (ex: sub collections). Implements [RecordStore]
type InMemoryStore struct {
data map[string]*gen.Record
}
var _ RecordStore = (*InMemoryStore)(nil)
// NewInMemoryStore Create a new [InMemoryStore]
func NewInMemoryStore() *InMemoryStore {
return &InMemoryStore{
data: make(map[string]*gen.Record),
}
}
func (i *InMemoryStore) List() ([]string, error) {
var fileIds []string
for fileId := range i.data {
fileIds = append(fileIds, fileId)
}
return fileIds, nil
}
func (i *InMemoryStore) MakeRecordPath(fileId string) string {
return fileId
}
func (i *InMemoryStore) MakeStoreForCollection(_ string) (RecordStore, error) {
return NewInMemoryStore(), nil
}
func (i *InMemoryStore) Read(recordPath string) (*gen.Record, error) {
value, exists := i.data[recordPath]
if !exists {
return nil, ErrKeyNotFound
}
return value, nil
}
func (i *InMemoryStore) Remove(recordPath string) error {
delete(i.data, recordPath)
return nil
}
func (i *InMemoryStore) Write(recordPath string, record *gen.Record) error {
i.data[recordPath] = record
return nil
}
func (i *InMemoryStore) Truncate() error {
i.data = make(map[string]*gen.Record)
return nil
}