/
get.go
61 lines (48 loc) · 990 Bytes
/
get.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
package main
import (
"github.com/256dpi/fpack"
"github.com/256dpi/god"
"github.com/256dpi/turing"
)
type get struct {
Key uint64
Value uint64
}
var getDesc = &turing.Description{
Name: "get",
}
func (g *get) Describe() *turing.Description {
return getDesc
}
func (g *get) Effect() int {
return 0
}
var getCounter = god.NewCounter("get", nil)
func (g *get) Execute(mem turing.Memory, _ turing.Cache) error {
getCounter.Add(1)
// encode key
key := encodeNum(g.Key)
// get count
err := mem.Use(key, func(value []byte) error {
g.Value = decodeNum(value)
return nil
})
if err != nil {
return err
}
return nil
}
func (g *get) Encode() ([]byte, turing.Ref, error) {
return fpack.Encode(true, func(enc *fpack.Encoder) error {
enc.Uint64(g.Key)
enc.Uint64(g.Value)
return nil
})
}
func (g *get) Decode(bytes []byte) error {
return fpack.Decode(bytes, func(dec *fpack.Decoder) error {
g.Key = dec.Uint64()
g.Value = dec.Uint64()
return nil
})
}