-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv.go
More file actions
121 lines (101 loc) · 2.32 KB
/
Copy pathkv.go
File metadata and controls
121 lines (101 loc) · 2.32 KB
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
package main
import (
"bufio"
"flag"
. "moss/common"
. "moss/tango"
"os"
"strings"
"time"
)
type KV struct {
objs map[int64]*KVPair
}
type KVPair struct {
oid int64
tid int64
state string
}
var (
kv KV
)
func init() {
kv.objs = make(map[int64]*KVPair)
}
// can't load cmd/values because they come through apply later on
func (kv *KV) access(oid, tid int64) *KVPair {
if _, ok := kv.objs[oid]; !ok {
Pdebug("access NEW oid %v, tid %v\n", oid, tid)
kv.objs[oid] = &KVPair{oid: oid, tid: tid}
}
kv.objs[oid].tid = tid
return kv.objs[oid]
}
// =====================================================================
func (obj *KVPair) StartTransaction(oid int) {
TangoUpdateHelper(obj, "START")
Pdebug("start xtion %v\n", obj.oid)
}
func (obj *KVPair) Oid() int64 {
return obj.oid
}
func (obj *KVPair) Tid() int64 {
return obj.tid
}
func (obj *KVPair) Apply(s string) {
Pdebug("APPLY oid %v, tid %v, old %q, new %q\n", obj.oid, obj.tid, obj.state, s)
obj.state = s
}
func (obj *KVPair) Read() string {
TangoQueryHelper(obj)
Palways("read %2v,%2v: %q\n", obj.oid, obj.tid, obj.state)
return obj.state
}
func (obj *KVPair) Write(s string) {
result := TangoUpdateHelper(obj, s)
if (s == "START") || (s == "FINISH") {
Palways("%s trans %v\n", result, obj.tid)
} else {
Palways("write %2d,%2d: %q\n", obj.oid, obj.tid, result)
}
}
func main() {
flag.BoolVar(&Debug, "d", false, "toggle debug")
flag.Parse()
args := flag.Args()
Passert(len(args) == 2, "USAGE: kv [-d] <server:port> <script file>\n")
server := args[0]
fname := args[1]
TangoInit(server)
f, err := os.Open(fname)
Passert(err == nil, "ERROR Opening %q\n", fname)
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
l := scanner.Text()
flds := strings.Split(l, ",")
if len(flds) != 3 {
continue
}
oid, tid, cmd := Atoi64(flds[0]), Atoi64(flds[1]), flds[2]
obj := kv.access(oid, tid)
Pdebug("script: %v, %v, %q\n", oid, tid, cmd)
switch cmd {
case "START", "FINISH":
obj.Write(cmd)
case "SLEEP":
seconds := time.Duration(Atoi(flds[0]))
time.Sleep(seconds * time.Second)
case "READ":
val := obj.Read()
Palways("read %q\n", val)
default:
obj.Write(cmd)
}
}
// print the contents of the map
for _, obj := range kv.objs {
Palways("kv %v: %q\n", obj.oid, obj.state)
}
TangoFinish()
}