forked from vksysd/mrgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kv.go
69 lines (65 loc) · 1.52 KB
/
kv.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
package models
import "../porcupine"
import "fmt"
import "sort"
type KvInput struct {
Op uint8 // 0 => get, 1 => put, 2 => append
Key string
Value string
}
type KvOutput struct {
Value string
}
var KvModel = porcupine.Model{
Partition: func(history []porcupine.Operation) [][]porcupine.Operation {
m := make(map[string][]porcupine.Operation)
for _, v := range history {
key := v.Input.(KvInput).Key
m[key] = append(m[key], v)
}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
ret := make([][]porcupine.Operation, 0, len(keys))
for _, k := range keys {
ret = append(ret, m[k])
}
return ret
},
Init: func() interface{} {
// note: we are modeling a single key's value here;
// we're partitioning by key, so this is okay
return ""
},
Step: func(state, input, output interface{}) (bool, interface{}) {
inp := input.(KvInput)
out := output.(KvOutput)
st := state.(string)
if inp.Op == 0 {
// get
return out.Value == st, state
} else if inp.Op == 1 {
// put
return true, inp.Value
} else {
// append
return true, (st + inp.Value)
}
},
DescribeOperation: func(input, output interface{}) string {
inp := input.(KvInput)
out := output.(KvOutput)
switch inp.Op {
case 0:
return fmt.Sprintf("get('%s') -> '%s'", inp.Key, out.Value)
case 1:
return fmt.Sprintf("put('%s', '%s')", inp.Key, inp.Value)
case 2:
return fmt.Sprintf("append('%s', '%s')", inp.Key, inp.Value)
default:
return "<invalid>"
}
},
}