-
Notifications
You must be signed in to change notification settings - Fork 32
/
store.go
114 lines (98 loc) · 3.08 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package kademlia
import (
"crypto/sha1"
"sync"
"time"
)
// Store is the interface for implementing the storage mechanism for the
// DHT.
type Store interface {
// Store should store a key/value pair for the local node with the
// given replication and expiration times.
Store(key []byte, data []byte, replication time.Time, expiration time.Time, publisher bool) error
// Retrieve should return the local key/value if it exists.
Retrieve(key []byte) (data []byte, found bool)
// Delete should delete a key/value pair from the Store
Delete(key []byte)
// Init initializes the Store
Init()
// GetAllKeysForReplication should return the keys of all data to be
// replicated across the network. Typically all data should be
// replicated every tReplicate seconds.
GetAllKeysForReplication() [][]byte
// ExpireKeys should expire all key/values due for expiration.
ExpireKeys()
// GetKey returns the key for data
GetKey(data []byte) []byte
}
// MemoryStore is a simple in-memory key/value store used for unit testing, and
// the CLI example
type MemoryStore struct {
mutex *sync.Mutex
data map[string][]byte
replicateMap map[string]time.Time
expireMap map[string]time.Time
}
// GetAllKeysForReplication should return the keys of all data to be
// replicated across the network. Typically all data should be
// replicated every tReplicate seconds.
func (ms *MemoryStore) GetAllKeysForReplication() [][]byte {
ms.mutex.Lock()
defer ms.mutex.Unlock()
var keys [][]byte
for k := range ms.data {
if time.Now().After(ms.replicateMap[k]) {
keys = append(keys, []byte(k))
}
}
return keys
}
// ExpireKeys should expire all key/values due for expiration.
func (ms *MemoryStore) ExpireKeys() {
ms.mutex.Lock()
defer ms.mutex.Unlock()
for k, v := range ms.expireMap {
if time.Now().After(v) {
delete(ms.replicateMap, k)
delete(ms.expireMap, k)
delete(ms.data, k)
}
}
}
// Init initializes the Store
func (ms *MemoryStore) Init() {
ms.data = make(map[string][]byte)
ms.mutex = &sync.Mutex{}
ms.replicateMap = make(map[string]time.Time)
ms.expireMap = make(map[string]time.Time)
}
// GetKey returns the key for data
func (ms *MemoryStore) GetKey(data []byte) []byte {
sha := sha1.Sum(data)
return sha[:]
}
// Store will store a key/value pair for the local node with the given
// replication and expiration times.
func (ms *MemoryStore) Store(key []byte, data []byte, replication time.Time, expiration time.Time, publisher bool) error {
ms.mutex.Lock()
defer ms.mutex.Unlock()
ms.replicateMap[string(key)] = replication
ms.expireMap[string(key)] = expiration
ms.data[string(key)] = data
return nil
}
// Retrieve will return the local key/value if it exists
func (ms *MemoryStore) Retrieve(key []byte) (data []byte, found bool) {
ms.mutex.Lock()
defer ms.mutex.Unlock()
data, found = ms.data[string(key)]
return data, found
}
// Delete deletes a key/value pair from the MemoryStore
func (ms *MemoryStore) Delete(key []byte) {
ms.mutex.Lock()
defer ms.mutex.Unlock()
delete(ms.replicateMap, string(key))
delete(ms.expireMap, string(key))
delete(ms.data, string(key))
}