-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
67 lines (61 loc) · 1.45 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
package tbkv
type KVStore struct {
data map[string]string
save chan ItemRequest
fetch chan Request
delete chan Request
}
func (s *KVStore) Get(key string) (string, error) {
request := Request{
Key: key,
Result: make(chan Result, 1),
}
s.fetch <- request
result := <-request.Result
if !result.Ok {
return "", ErrNotFound
}
return result.Value, nil
}
func (s *KVStore) Set(key, value string) {
setItemRequest := ItemRequest{
Item: Item{
Key: key,
Value: value,
},
Ok: make(chan bool, 1),
}
s.save <- setItemRequest
<-setItemRequest.Ok
}
func (s *KVStore) Delete(key string) {
request := Request{
Key: key,
Result: make(chan Result, 1),
}
s.delete <- request
<-request.Result
}
// NewStore creates a new KVStore.
// requestBufferSize is the size of the request buffer.
// If requestBufferSize is 0, it will use a default of 20.
func NewStore(requestBufferSize uint8) KVStore {
var buffer uint8
if requestBufferSize == 0 {
buffer = 20
} else {
buffer = requestBufferSize
}
kvs := KVStore{}
kvs.data = make(map[string]string)
kvs.save = make(chan ItemRequest, buffer)
kvs.fetch = make(chan Request, buffer)
kvs.delete = make(chan Request, buffer)
go kvs.loop() // starts listening for requests
return kvs
}
// NewDefaultStore is a convenience function for creating a KVStore with sensible defaults.
// It will create a KVStore with a request buffer size of 20.
func NewDefaultStore() KVStore {
return NewStore(0)
}