-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.go
143 lines (116 loc) · 3.55 KB
/
fsm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package fidias
import (
"bytes"
"fmt"
kelips "github.com/hexablock/go-kelips"
"github.com/hexablock/hexalog"
"github.com/hexablock/log"
"github.com/hexablock/phi"
)
const (
// OpSet is the op to set a ke-value pair
opKVSet byte = iota + 1
// OpDel is the op to delete a key-value pair
opKVDel
)
// KVStore is the kv store used by the FSM to perform write operations
type KVStore interface {
// Get a key
Get(key []byte) (*KVPair, error)
// Set a key. Called by the fsm. It returns any directory keys that may
// have been implicitly created
Set(kvp *KVPair) ([]*KVPair, error)
// Delete a key. Called by the fsm
Remove(key []byte) error
// Iterate over kv's starting at the prefix. If recurse is true then all
// keys in subdirs are also returned
Iter(prefix []byte, recurse bool, f func(kv *KVPair) bool)
}
// FSM is a hexalog FSM for an in-memory key-value store. It implements the
// FSM interface and provides a get function to retrieve keys as all write
// are handled by the FSM
type FSM struct {
// Hexalog entry prefix for kv's
kvprefix []byte
// Local host DHT address
localTuple kelips.TupleHost
// Actual kvstore
kvs KVStore
// DHT
dht phi.DHT
}
// NewFSM inits a new FSM. localTuple is the local host port tuple for the dht
func NewFSM(kvprefix string, localTuple kelips.TupleHost, kvs KVStore) *FSM {
return &FSM{
kvprefix: []byte(kvprefix),
localTuple: localTuple,
kvs: kvs,
}
}
// RegisterDHT registers the dht to the state machine. THis is to allow inserts
// to the dht when keys log entries are applied
func (fsm *FSM) RegisterDHT(dht phi.DHT) {
fsm.dht = dht
}
// Apply applies the given entry to the FSM. entryID is the hash id of the
// entry. The first byte in entry.Data contains the operation to be performed
// followed by the actual value.
func (fsm *FSM) Apply(entryID []byte, entry *hexalog.Entry) interface{} {
if entry.Data == nil || len(entry.Data) == 0 {
log.Printf("[WARNING] Hexalog entry has no data key=%s", entry.Key)
return nil
}
var (
op = entry.Data[0]
resp interface{}
)
switch op {
case opKVSet:
resp = fsm.applyKVSet(entryID, entry, entry.Data[1:])
case opKVDel:
resp = fsm.applyKVDelete(entry)
default:
resp = fmt.Errorf("invalid operation: %x", op)
}
return resp
}
func (fsm *FSM) applyKVSet(entryID []byte, entry *hexalog.Entry, value []byte) error {
kv := &KVPair{
Key: bytes.TrimPrefix(entry.Key, fsm.kvprefix),
Value: value,
Modification: entryID,
ModTime: entry.Timestamp,
LTime: entry.LTime,
Height: entry.Height,
}
createdDirs, err := fsm.kvs.Set(kv)
if err != nil {
return err
}
// Insert key to dht
if err = fsm.dht.Insert(entry.Key, fsm.localTuple); err != nil {
log.Println("[ERROR] FSM dht insert failed:", err)
}
// Insert any directories created to dht
for _, c := range createdDirs {
nskey := append(fsm.kvprefix, c.Key...)
log.Printf("%s", nskey)
if er := fsm.dht.Insert(nskey, fsm.localTuple); er != nil {
log.Println("[ERROR] FSM dht insert failed:", er)
err = er
}
}
log.Printf("[DEBUG] FSM nskey=%s op=set dirs-created=%d height=%d error='%v'",
entry.Key, len(createdDirs), kv.Height, err)
return err
}
// ApplyDelete applies a hexalog delete operation entry to the fsm
func (fsm *FSM) applyKVDelete(entry *hexalog.Entry) error {
key := bytes.TrimPrefix(entry.Key, fsm.kvprefix)
err := fsm.kvs.Remove(key)
if err == nil {
err = fsm.dht.Delete(entry.Key, fsm.localTuple)
}
log.Printf("[DEBUG] FSM nskey=%s op=delete height=%d error='%v'", entry.Key, entry.Height, err)
return err
}