-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (43 loc) · 951 Bytes
/
main.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
package main
import (
"encoding/json"
"log"
maelstrom "github.com/jepsen-io/maelstrom/demo/go"
)
type TxnRequest struct {
maelstrom.MessageBody
Txn Txn `json:"txn"`
}
type TxnResponse struct {
maelstrom.MessageBody
Txn Txn `json:"txn"`
}
var node = maelstrom.NewNode()
func main() {
store := NewStore()
node.Handle("txn", func(msg maelstrom.Message) error {
var body TxnRequest
if err := json.Unmarshal(msg.Body, &body); err != nil {
return err
}
committedTxn := Txn{}
for _, op := range body.Txn {
if op.fn == READ {
committedTxn = append(committedTxn, Op{READ, op.key, store.Read(op.key)})
continue
}
store.Write(op.key, op.value)
committedTxn = append(committedTxn, op)
}
resBody := TxnResponse{
MessageBody: maelstrom.MessageBody{
Type: "txn_ok",
},
Txn: committedTxn,
}
return node.Reply(msg, resBody)
})
if err := node.Run(); err != nil {
log.Fatal(err)
}
}