-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
71 lines (59 loc) · 1.53 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
package raft
import (
"encoding/binary"
"sync"
)
// Store is used to provide stable storage
// of key configurations to ensure safety.
type Store interface {
Set(key []byte, val []byte) error
// Get returns the value for key, or an empty byte slice if key was not found.
Get(key []byte) ([]byte, error)
SetUint64(key []byte, val uint64) error
// GetUint64 returns the uint64 value for key, or 0 if key was not found.
GetUint64(key []byte) (uint64, error)
}
var _ Store = (*memoryStore)(nil)
// memoryStore just for testing
type memoryStore struct {
mux sync.Mutex
m map[string]string
}
func (s *memoryStore) Set(key []byte, val []byte) error {
s.mux.Lock()
defer s.mux.Unlock()
if s.m == nil {
s.m = make(map[string]string)
}
s.m[string(key)] = string(val)
return nil
}
// Get returns the value for key, or an empty byte slice if key was not found.
func (s *memoryStore) Get(key []byte) ([]byte, error) {
s.mux.Lock()
defer s.mux.Unlock()
if s.m == nil {
s.m = make(map[string]string)
}
value, ok := s.m[string(key)]
if !ok {
return []byte{}, nil
}
return []byte(value), nil
}
func (s *memoryStore) SetUint64(key []byte, val uint64) error {
value := make([]byte, 8)
binary.BigEndian.PutUint64(value, val)
return s.Set(key, value)
}
// GetUint64 returns the uint64 value for key, or 0 if key was not found.
func (s *memoryStore) GetUint64(key []byte) (uint64, error) {
value, err := s.Get(key)
if err != nil {
return 0, err
}
if len(value) == 0 {
return 0, err
}
return binary.BigEndian.Uint64(value), nil
}