-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
59 lines (50 loc) · 1.02 KB
/
storage.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
package raft
import "sync"
/*
raft 持久化:
currentTerm - the latest term this server has observed
votedFor - the peer ID for whom this server voted in the latest term
log - Raft log entries
*/
type Storage interface {
Set(key string, value []byte)
Get(key string) ([]byte, bool)
HasData() bool
}
// MapStorage 用于测试
type MapStorage struct {
//m sync.Map
mu sync.Mutex
m map[string][]byte
}
func (m *MapStorage) Set(key string, value []byte) {
//m.m.Store(key, value)
m.mu.Lock()
defer m.mu.Unlock()
m.m[key]=value
}
func (m *MapStorage) Get(key string) ([]byte, bool) {
//value, ok := m.m.Load(key)
//return value.([]byte), ok
m.mu.Lock()
defer m.mu.Unlock()
value,ok:=m.m[key]
return value,ok
}
func (m *MapStorage) HasData() bool {
//ok:=false
//m.m.Range(func(key, value interface{}) bool {
// ok=true
// return false
//})
//return ok
m.mu.Lock()
defer m.mu.Unlock()
return len(m.m)>0
}
func NewMapStorage() *MapStorage {
return &MapStorage{
mu: sync.Mutex{},
m: make(map[string][]byte),
}
}