-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathbtree.tla
More file actions
336 lines (285 loc) · 11.9 KB
/
Copy pathbtree.tla
File metadata and controls
336 lines (285 loc) · 11.9 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
\* Note: deletes have not been implemented
---- MODULE btree ----
EXTENDS Naturals,
FiniteSets,
Sequences,
Relation
CONSTANTS Vals,
Keys,
Nodes,
MaxOccupancy,
\* states
READY,
GET_VALUE,
FIND_LEAF_TO_ADD,
WHICH_TO_SPLIT,
ADD_TO_LEAF,
SPLIT_LEAF,
SPLIT_INNER,
SPLIT_ROOT_LEAF,
SPLIT_ROOT_INNER,
UPDATE_LEAF
States == {READY, GET_VALUE, FIND_LEAF_TO_ADD, WHICH_TO_SPLIT, ADD_TO_LEAF,
SPLIT_LEAF, SPLIT_INNER, SPLIT_ROOT_LEAF, SPLIT_ROOT_INNER, UPDATE_LEAF}
\*
\* Assumptions the algorithm imposes
\*
ASSUME StatesAreDistinct == IsFiniteSet(States) /\ Cardinality(States) = 10
\* All the tree does with a key is compare it: ChildNodeFor descends by
\* comparing a key against the ones a node holds, and PivotOf splits a node's
\* keys into a smaller and a larger half. A strict total order is therefore
\* everything the algorithm needs of the key domain, which it does not bound.
ASSUME KeysAreOrdered == IsStrictlyTotallyOrderedUnder(<, Keys)
\* MaxOccupancy is the branching factor. Below 2, PivotOf leaves one side of
\* every split empty, and IsFree reports the emptied in-tree leaf as free for
\* ChooseFreeNode to hand out a second time.
ASSUME MaxOccupancyPermitsSplitting == MaxOccupancy \in Nat /\ MaxOccupancy >= 2
\* Even the empty tree is a root node, which Init takes from the free nodes.
ASSUME NodePoolIsNonEmpty == Nodes # {}
NIL == CHOOSE x : x \notin Nodes
MISSING == CHOOSE v : v \notin Vals
VARIABLES root,
isLeaf, keysOf, childOf, lastOf, valOf,
focus,
toSplit,
op, args, ret,
state
TypeOk == /\ root \in Nodes
/\ isLeaf \in [Nodes -> BOOLEAN]
/\ keysOf \in [Nodes -> SUBSET Keys]
/\ childOf \in [Nodes \X Keys -> Nodes \union {NIL}]
/\ lastOf \in [Nodes -> Nodes \union {NIL}]
/\ valOf \in [Nodes \X Keys -> Vals \union {NIL}]
/\ focus \in Nodes \union {NIL}
/\ toSplit \in Seq(Nodes)
/\ op \in {"get", "insert", "update", NIL}
/\ ret \in Vals \union {"ok", "error", MISSING, NIL}
/\ state \in States
\* Max element in a set
Max(xs) == CHOOSE x \in xs : (\A y \in xs \ {x} : x > y)
\* Find the appropriate child node associated with the key
ChildNodeFor(node, key) ==
LET keys == keysOf[node]
maxKey == Max(keys)
closestKey == CHOOSE k \in keys : /\ k>key
/\ ~(\E j \in keys \ {k} : j>key /\ j<k)
IN IF keys = {} \/ key >= maxKey
THEN lastOf[node]
\* smallest k that's bigger than key
ELSE
childOf[node, closestKey]
\* Identify the leaf node based on key
\* Find the leaf node associated with a key
RECURSIVE FindLeafNode(_, _)
FindLeafNode(node, key) ==
IF isLeaf[node] THEN node ELSE FindLeafNode(ChildNodeFor(node, key), key)
AtMaxOccupancy(node) == Cardinality(keysOf[node]) = MaxOccupancy
\* We model a "free" (not yet part of the tree) node as one as a leaf with no keys
IsFree(node) == isLeaf[node] /\ keysOf[node] = {}
ChooseFreeNode == CHOOSE n \in Nodes : IsFree(n)
Init == /\ isLeaf = [n \in Nodes |-> TRUE]
/\ keysOf = [n \in Nodes |-> {}]
/\ childOf = [n \in Nodes, k \in Keys |-> NIL]
/\ lastOf = [n \in Nodes |-> NIL]
/\ valOf = [n \in Nodes, k \in Keys |-> NIL]
/\ root = ChooseFreeNode
/\ focus = NIL
/\ toSplit = <<>>
/\ op = NIL
/\ args = NIL
/\ ret = NIL
/\ state = READY
GetReq(key) ==
/\ state = READY
/\ op' = "get"
/\ args' = <<key>>
/\ ret' = NIL
/\ state' = GET_VALUE
/\ UNCHANGED <<root, isLeaf, keysOf, childOf, lastOf, valOf, focus, toSplit>>
GetValue ==
LET key == args[1]
node == FindLeafNode(root, key) IN
/\ state = GET_VALUE
/\ state' = READY
/\ ret' = IF key \in keysOf[node] THEN valOf[node, key] ELSE MISSING
/\ UNCHANGED <<root, isLeaf, keysOf, childOf, lastOf, valOf, focus, toSplit, args, op>>
InsertReq(key, val) ==
/\ state = READY
/\ op' = "insert"
/\ args' = <<key, val>>
/\ ret' = NIL
/\ state' = FIND_LEAF_TO_ADD
/\ UNCHANGED <<root, isLeaf, keysOf, childOf, lastOf, valOf, focus, toSplit>>
UpdateReq(key, val) ==
LET leaf == FindLeafNode(root, key)
IN /\ state = READY
/\ op' = "update"
/\ args' = <<key, val>>
/\ ret' = NIL
/\ focus' = leaf
/\ state' = UPDATE_LEAF
/\ UNCHANGED <<root, isLeaf, keysOf, childOf, lastOf, valOf, toSplit>>
UpdateLeaf ==
LET key == args[1]
val == args[2]
IN /\ state = UPDATE_LEAF
/\ valOf' = IF key \in keysOf[focus] THEN [valOf EXCEPT ![focus, key]=val] ELSE valOf
/\ ret' = IF key \in keysOf[focus] THEN "ok" ELSE "error"
/\ state' = READY
/\ focus' = NIL
/\ UNCHANGED <<root, isLeaf, keysOf, childOf, lastOf, toSplit, args, op>>
FindLeafToAdd ==
LET key == args[1]
leaf == FindLeafNode(root, key)
IN /\ state = FIND_LEAF_TO_ADD
/\ focus' = leaf
/\ toSplit' = IF AtMaxOccupancy(leaf) THEN <<leaf>> ELSE <<>>
/\ state' = IF AtMaxOccupancy(leaf) THEN WHICH_TO_SPLIT ELSE ADD_TO_LEAF
/\ UNCHANGED <<root, isLeaf, keysOf, childOf, lastOf, valOf, args, op, ret>>
ParentOf(n) == CHOOSE p \in Nodes: \/ \E k \in Keys: n = childOf[p, k]
\/ lastOf[p]=n
WhichToSplit ==
LET node == Head(toSplit)
parent == ParentOf(node)
splitParent == AtMaxOccupancy(parent)
noMoreSplits == ~splitParent \* if the parent doesn't need splitting, we don't need to consider more nodes for splitting
IN /\ state = WHICH_TO_SPLIT
/\ toSplit' =
CASE node = root -> toSplit
[] splitParent -> <<parent>> \o toSplit
[] OTHER -> toSplit
/\ state' =
CASE node # root /\ noMoreSplits /\ isLeaf[node] -> SPLIT_LEAF
[] node # root /\ noMoreSplits /\ ~isLeaf[node] -> SPLIT_INNER
[] node = root /\ isLeaf[node] -> SPLIT_ROOT_LEAF
[] node = root /\ ~isLeaf[node] -> SPLIT_ROOT_INNER
[] OTHER -> WHICH_TO_SPLIT
/\ UNCHANGED <<root, isLeaf, keysOf, childOf, lastOf, valOf, op, args, ret, focus>>
\* Adding the <<key, val>> pair in args to the node indicated by focus
\* If the key is already present, this is an error
AddToLeaf ==
LET key == args[1]
val == args[2] IN
/\ state = ADD_TO_LEAF
/\ ret' = IF key \notin keysOf[focus] THEN "ok" ELSE "error"
/\ keysOf' = IF key \notin keysOf[focus] THEN [keysOf EXCEPT ![focus]=@ \union {key}] ELSE keysOf
/\ valOf' = IF key \notin keysOf[focus] THEN [valOf EXCEPT ![focus,key]=val] ELSE valOf
/\ state' = READY
/\ UNCHANGED <<root, isLeaf, childOf, lastOf, op, args, focus, toSplit>>
\* Return the pivot (midpoint) of a set of keys. If there are an even number of keys, bias towards the smaller one
PivotOf(keys) == CHOOSE k \in keys :
LET smaller == {x \in keys : x < k}
larger == {x \in keys: x > k} IN
\/ Cardinality(smaller) = Cardinality(larger)
\/ Cardinality(smaller) = Cardinality(larger)+1
SplitRootLeaf ==
LET n1 == Head(toSplit)
n2 == ChooseFreeNode
newRoot == CHOOSE n \in Nodes : IsFree(n) /\ (n # n2)
keys == keysOf[n1]
pivot == PivotOf(keys)
n1Keys == {x \in keys: x<pivot}
n2Keys == {x \in keys: x>=pivot}
keyToInsert == args[1] IN
/\ state = SPLIT_ROOT_LEAF
/\ root' = newRoot
/\ isLeaf' = [isLeaf EXCEPT ![newRoot]=FALSE, ![n2]=TRUE]
/\ keysOf' = [keysOf EXCEPT ![newRoot]={pivot}, ![n1]=n1Keys, ![n2]=n2Keys]
/\ childOf' = [childOf EXCEPT ![newRoot, pivot]=n1]
/\ lastOf' = [lastOf EXCEPT ![newRoot]=n2]
/\ valOf' = [n \in Nodes, k \in Keys |->
CASE n=n1 /\ k \in n2Keys -> NIL
[] n=n2 /\ k \in n2Keys -> valOf[n1, k]
[] OTHER -> valOf[n, k]]
\* No more splits necessary, add the focus to the leaf
\* Note that the focus may have changed due to the split
/\ state' = ADD_TO_LEAF
/\ focus' = IF keyToInsert < pivot THEN n1 ELSE n2
/\ UNCHANGED <<op, args, ret, toSplit>>
ParentKeyOf(node) ==
LET p == ParentOf(node) IN
CHOOSE k \in keysOf[p]: childOf[p, k] = node
IsLastOfParent(node) == lastOf[ParentOf(node)] = node
SplitRootInner ==
LET n1 == Head(toSplit)
n2 == ChooseFreeNode
newRoot == CHOOSE n \in Nodes : IsFree(n) /\ (n # n2)
keys == keysOf[n1]
pivot == PivotOf(keys)
(* when splitting an inner node, pivot does not appear in either node, only in parent *)
n1Keys == {x \in keys: x<pivot}
n2Keys == {x \in keys: x>pivot} IN
/\ state = SPLIT_ROOT_INNER
/\ root' = newRoot
/\ isLeaf' = [isLeaf EXCEPT ![newRoot]=FALSE, ![n2]=FALSE]
/\ keysOf' = [keysOf EXCEPT ![newRoot]={pivot}, ![n1]=n1Keys, ![n2]=n2Keys]
/\ childOf' = [n \in Nodes, k \in Keys |->
CASE n=newRoot /\ k=pivot -> n1
[] n=n1 /\ k \in n2Keys -> NIL
[] n=n1 /\ k \in n1Keys -> childOf[n1, k]
[] n=n2 /\ k \in n2Keys -> childOf[n1, k]
[] OTHER -> childOf[n, k]]
/\ lastOf' = [lastOf EXCEPT ![newRoot]=n2, ![n1]=childOf[n1, pivot], ![n2]=lastOf[n1]]
/\ toSplit' = <<>>
/\ state' = ADD_TO_LEAF
/\ UNCHANGED <<op, args, ret, focus, valOf>>
SplitLeaf ==
LET n1 == Head(toSplit)
n2 == ChooseFreeNode
keys == keysOf[n1]
pivot == PivotOf(keys)
parent == ParentOf(n1)
n1Keys == {x \in keys: x<pivot}
n2Keys == {x \in keys: x>=pivot}
keyToInsert == args[1]
IN
/\ state = SPLIT_LEAF
/\ isLeaf' = [isLeaf EXCEPT ![n2]=TRUE]
/\ keysOf' = [keysOf EXCEPT ![parent]=@ \union {pivot}, ![n1]=n1Keys, ![n2]=n2Keys]
\* In the parent, point the pivot key to n1, and point the parent key to n2.
\* TODO: handle the edge case where n1 was the last element
/\ childOf' = IF IsLastOfParent(n1)
THEN [childOf EXCEPT ![parent, pivot]=n1]
ELSE [childOf EXCEPT ![parent, pivot]=n1, ![parent, ParentKeyOf(n1)]=n2]
/\ lastOf' = IF IsLastOfParent(n1) THEN [lastOf EXCEPT ![parent]=n2] ELSE lastOf
/\ valOf' = [n \in Nodes, k \in Keys |->
CASE n=n1 /\ k \in n2Keys -> NIL
[] n=n2 /\ k \in n2Keys -> valOf[n1, k]
[] OTHER -> valOf[n, k]]
/\ state' = ADD_TO_LEAF
/\ focus' = IF keyToInsert < pivot THEN n1 ELSE n2
/\ UNCHANGED <<root, toSplit, op, args, ret>>
Next == \/ \E key \in Keys, val \in Vals :
\/ InsertReq(key, val)
\/ UpdateReq(key, val)
\/ \E key \in Keys: GetReq(key)
\/ GetValue
\/ FindLeafToAdd
\/ WhichToSplit
\/ AddToLeaf
\/ SplitLeaf
\/ SplitRootLeaf
\/ SplitRootInner
\/ UpdateLeaf
vars == <<root, isLeaf, keysOf, childOf, lastOf, valOf, focus, toSplit, op, args, ret, state>>
Spec == Init /\ [][Next]_vars /\ WF_op(\E key \in Keys: GetReq(key))
\*
\* Refinement mapping
\*
Leaves == {n \in Nodes : isLeaf[n]}
Mapping == INSTANCE kvstore
WITH dict <- [key \in Keys |-> IF \E leaf \in Leaves : key \in keysOf[leaf]
THEN LET leaf == CHOOSE leaf \in Leaves : key \in keysOf[leaf]
IN valOf[leaf, key] ELSE MISSING],
state <- IF state = READY THEN "ready" ELSE "working"
Refinement == Mapping!Spec
\*
\* Invariants
\*
Inners == {n \in Nodes: ~isLeaf[n]}
InnersMustHaveLast == \A n \in Inners : lastOf[n] # NIL
KeyOrderPreserved == \A n \in Inners : (\A k \in keysOf[n] : (\A kc \in keysOf[childOf[n, k]]: kc < k))
LeavesCantHaveLast == \A n \in Leaves : lastOf[n] = NIL
KeysInLeavesAreUnique ==
\A n1, n2 \in Leaves : ((keysOf[n1] \intersect keysOf[n2]) # {}) => n1=n2
====