-
Notifications
You must be signed in to change notification settings - Fork 0
/
hot.go
85 lines (65 loc) · 2.12 KB
/
hot.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
package storage
import (
"errors"
"fmt"
)
type HotStorageService struct {
collections map[string]Collection
}
func NewHotStorageService() (*HotStorageService, error) {
svc := &HotStorageService{
collections: make(map[string]Collection),
}
return svc, nil
}
func (svc *HotStorageService) ListCollections() []string {
var ids []string
for id := range svc.collections {
ids = append(ids, id)
}
return ids
}
func (svc *HotStorageService) CollectionExists(id string) bool {
_, exists := svc.collections[id]
return exists
}
func (svc *HotStorageService) ReadKey(id, key string) (string, error) {
if collection, exists := svc.collections[id]; exists {
// return value for given key from collection given it exists
if value, exists := collection[key]; exists {
return value, nil
}
// return "", err if value does not exist for given key in collection
return "", errors.New(fmt.Sprintf("No value found for key [%s] in hot collection [%s].", key, id))
}
// return "", err if collection does not exist
return "", errors.New(fmt.Sprintf("No collection found for collection id [%s] in hot storage.", id))
}
func (svc *HotStorageService) WriteKey(id, key, value string) error {
if collection, exists := svc.collections[id]; exists {
// write to collection if it exists
collection[key] = value
return nil
}
// return error if collection does not exist
return errors.New(fmt.Sprintf("No collection found for collection id [%s] in hot storage.", id))
}
func (svc *HotStorageService) CreateCollection(id string) error {
// return error if collection already exists
if svc.CollectionExists(id) {
return errors.New(fmt.Sprintf("Collection already exists with id [%s] in hot storage.", id))
}
// create collection
svc.collections[id] = Collection{}
return nil
}
func (svc *HotStorageService) DropCollection(id string) error {
// check if collection exists
if _, exists := svc.collections[id]; exists {
// drop collection
delete(svc.collections, id)
return nil
}
// return error if collection does not exist
return errors.New(fmt.Sprintf("No collection found for collection id [%s] in hot storage.", id))
}