-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.go
More file actions
161 lines (149 loc) · 4.53 KB
/
Copy pathcache.go
File metadata and controls
161 lines (149 loc) · 4.53 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package client
import (
"fmt"
"goshawkdb.io/common"
msgs "goshawkdb.io/common/capnp"
"log"
"sync"
)
type valueRef struct {
version *common.TxnId
capability *common.Capability
value []byte
references []refCap
}
type refCap struct {
vUUId *common.VarUUId
capability *common.Capability
}
func (rc refCap) String() string {
return fmt.Sprintf("%v(%v)", rc.vUUId, rc.capability)
}
type cache struct {
sync.RWMutex
m map[common.VarUUId]*valueRef
}
func newCache() *cache {
return &cache{
m: make(map[common.VarUUId]*valueRef),
}
}
func (c *cache) Get(vUUId *common.VarUUId) *valueRef {
c.RLock()
defer c.RUnlock()
return c.m[*vUUId]
}
func (c *cache) SetRoots(roots map[string]*refCap) {
for _, rc := range roots {
c.m[*rc.vUUId] = &valueRef{capability: rc.capability}
}
}
func (c *cache) updateFromTxnCommit(txn *msgs.ClientTxn, txnId *common.TxnId) {
// fmt.Println("Updating from commit")
actions := txn.Actions()
c.Lock()
defer c.Unlock()
for idx, l := 0, actions.Len(); idx < l; idx++ {
action := actions.At(idx)
vUUId := common.MakeVarUUId(action.VarId())
switch action.Which() {
case msgs.CLIENTACTION_WRITE:
write := action.Write()
refs := write.References()
c.updateFromWrite(txnId, vUUId, write.Value(), &refs, false)
case msgs.CLIENTACTION_READWRITE:
rw := action.Readwrite()
refs := rw.References()
c.updateFromWrite(txnId, vUUId, rw.Value(), &refs, false)
case msgs.CLIENTACTION_CREATE:
create := action.Create()
refs := create.References()
c.updateFromWrite(txnId, vUUId, create.Value(), &refs, true)
case msgs.CLIENTACTION_READ:
// do nothing
}
}
}
func (c *cache) updateFromTxnAbort(updates *msgs.ClientUpdate_List) []*common.VarUUId {
// fmt.Println("Updating from abort")
modifiedVars := make([]*common.VarUUId, 0, updates.Len())
c.Lock()
defer c.Unlock()
for idx, l := 0, updates.Len(); idx < l; idx++ {
update := updates.At(idx)
txnId := common.MakeTxnId(update.Version())
actions := update.Actions()
for idy, m := 0, actions.Len(); idy < m; idy++ {
action := actions.At(idy)
vUUId := common.MakeVarUUId(action.VarId())
// fmt.Printf("abort %v@%v ", vUUId, txnId)
switch action.Which() {
case msgs.CLIENTACTION_DELETE:
c.updateFromDelete(vUUId, txnId)
modifiedVars = append(modifiedVars, vUUId)
case msgs.CLIENTACTION_WRITE:
// We're missing TxnId and TxnId made a write of vUUId (to
// version TxnId).
write := action.Write()
refs := write.References()
if c.updateFromWrite(txnId, vUUId, write.Value(), &refs, false) {
modifiedVars = append(modifiedVars, vUUId)
}
default:
log.Fatal("Received update that was neither a read or write action:", action.Which())
}
}
}
// fmt.Println(".")
return modifiedVars
}
func (c *cache) updateFromDelete(vUUId *common.VarUUId, txnId *common.TxnId) {
if vr, found := c.m[*vUUId]; found && vr.version != nil && vr.version.Compare(txnId) != common.EQ {
// fmt.Printf("%v removed from cache (req ver: %v; found ver: %v)\n", vUUId, txnId, vr.version)
vr.version = nil
vr.value = nil
vr.references = nil
} else if found {
log.Fatal("Divergence discovered on deletion of ", vUUId, ": server thinks we don't have ", txnId, " but we do!")
} else {
log.Fatal("Divergence discovered on deletion of ", vUUId, ": server thinks we had it cached, but we don't!")
}
}
func (c *cache) updateFromWrite(txnId *common.TxnId, vUUId *common.VarUUId, value []byte, refs *msgs.ClientVarIdPos_List, created bool) bool {
vr, found := c.m[*vUUId]
updated := found && vr.version != nil
references := make([]refCap, refs.Len())
switch {
case found && vr.version.Compare(txnId) == common.EQ:
log.Fatal("Divergence discovered on update of ", vUUId, ": server thinks we don't have ", txnId, " but we do!")
return false
case found:
default:
vr = &valueRef{}
c.m[*vUUId] = vr
}
if created {
vr.capability = common.MaxCapability
}
// fmt.Printf("%v updated (%v -> %v)\n", vUUId, vr.version, txnId)
vr.references = references
vr.version = txnId
vr.value = value
for idz, n := 0, refs.Len(); idz < n; idz++ {
ref := refs.At(idz)
if varId := ref.VarId(); len(varId) == common.KeyLen {
rc := &references[idz]
rc.vUUId = common.MakeVarUUId(varId)
rc.capability = common.NewCapability(ref.Capability())
vr, found := c.m[*rc.vUUId]
if found {
vr.capability = vr.capability.Union(rc.capability)
} else {
vr = &valueRef{capability: rc.capability}
c.m[*rc.vUUId] = vr
}
}
}
// fmt.Printf("%v@%v (%v)\n (-> %v)\n", vUUId, txnId, value, references)
return updated
}