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
|
defmodule SData.MerkleSearchTree do
@moduledoc"""
A Merkle search tree.
A node of the tree is
{
level,
hash_of_node | nil,
[
{ item_low_bound, hash_of_node | nil },
{ item_low_bound, hash_of_node | nil },
...
}
}
"""
alias SData.PageStore, as: Store
@doc"""
Create a new Merkle search tree.
This structure can be used as a set with only true keys,
or as a map if a merge function is given.
`cmp` is a compare function for keys as defined in module `SData`.
`merge` is a function for merging two items that have the same key.
"""
defstruct root: nil,
store: SData.LocalStore.new,
cmp: &SData.cmp_term/2,
merge: &SData.merge_true/2
defmodule Page do
defstruct [:level, :low, :list]
end
defimpl SData.Page, for: Page do
def refs(page) do
refs = for {_, _, h} <- page.list, h != nil, do: h
if page.low != nil do
[ page.low | refs ]
else
refs
end
end
end
def insert(state, key, value \\ true) do
level = calc_level(key)
{hash, store} = insert_at(state, state.store, state.root, key, level, value)
%{ state | root: hash, store: store }
end
def remove(_state, _key) do
# TODO
end
defp insert_at(s, store, root, key, level, value) do
{new_page, store} = if root == nil do
{ %Page{ level: level, low: nil, list: [ { key, value, nil } ] }, store }
else
%Page{ level: plevel, low: low, list: lst } = Store.get(store, root)
[ { k0, _, _} | _ ] = lst
cond do
plevel < level ->
{low, high, store} = split(s, store, root, key)
{ %Page{ level: level, low: low, list: [ { key, value, high } ] }, store }
plevel == level ->
store = Store.free(store, root)
case s.cmp.(key, k0) do
:before ->
{low2a, low2b, store} = split(s, store, low, key)
{ %Page{ level: level, low: low2a, list: [ { key, value, low2b } | lst] }, store }
_ ->
{new_lst, store} = aux_insert_after_first(s, store, lst, key, value)
{ %Page{ level: plevel, low: low, list: new_lst }, store }
end
plevel > level ->
store = Store.free(store, root)
case s.cmp.(key, k0) do
:before ->
{new_low, store} = insert_at(s, store, low, key, level, value)
{ %Page{ level: plevel, low: new_low, list: lst }, store }
:after ->
{new_lst, store} = aux_insert_sub_after_first(s, store, lst, key, level, value)
{ %Page{ level: plevel, low: low, list: new_lst }, store }
end
end
end
Store.put(store, new_page) # returns {hash, store}
end
defp split(s, store, hash, key) do
if hash == nil do
{nil, nil, store}
else
%Page{ level: level, low: low, list: lst } = Store.get(store, hash)
store = Store.free(store, hash)
[ { k0, _, _} | _ ] = lst
case s.cmp.(key, k0) do
:before ->
{lowlow, lowhi, store} = split(s, store, low, key)
newp2 = %Page{ level: level, low: lowhi, list: lst }
{newp2h, store} = Store.put(store, newp2)
{lowlow, newp2h, store}
:after ->
{lst1, p2, store} = split_aux(s, store, lst, key, level)
newp1 = %Page{ level: level, low: low, list: lst1}
{newp1h, store} = Store.put(store, newp1)
{newp1h, p2, store}
end
end
end
defp split_aux(s, store, lst, key, level) do
case lst do
[ {k1, v1, r1} ] ->
if s.cmp.(k1, key) == :duplicate do
raise "Bad logic"
end
{r1l, r1h, store} = split(s, store, r1, key)
{ [{k1, v1, r1l}], r1h, store }
[ {k1, v1, r1} = fst, {k2, v2, r2} = rst1 | rst ] ->
case s.cmp.(key, k2) do
:before ->
{r1l, r1h, store} = split(s, store, r1, key)
p2 = %Page{ level: level, low: r1h, list: [ {k2, v2, r2} | rst ] }
{p2h, store} = Store.put(store, p2)
{ [{k1, v1, r1l}], p2h, store }
:after ->
{rst2, hi, store} = split_aux(s, store, [rst1 | rst], key, level)
{ [ fst | rst2 ], hi, store }
:duplicate ->
raise "Bad logic"
end
end
end
defp aux_insert_after_first(s, store, lst, key, value) do
case lst do
[ {k1, v1, r1} ] ->
case s.cmp.(k1, key) do
:duplicate ->
{ [ {k1, s.merge.(v1, value), r1} ], store }
:before ->
{r1a, r1b, new_store} = split(s, store, r1, key)
{ [ {k1, v1, r1a}, {key, value, r1b} ], new_store }
end
[ {k1, v1, r1} = fst, {k2, v2, r2} = rst1 | rst ] ->
case s.cmp.(k1, key) do
:duplicate ->
{ [ {k1, s.merge.(v1, value), r1}, rst1 | rst ], store }
:before ->
case s.cmp.(k2, key) do
:after ->
{r1a, r1b, new_store} = split(s, store, r1, key)
{ [ {k1, v1, r1a}, {key, value, r1b}, {k2, v2, r2} | rst ], new_store }
_ ->
{rst2, new_store} = aux_insert_after_first(s, store, [rst1 | rst], key, value)
{ [ fst | rst2 ], new_store }
end
end
end
end
defp aux_insert_sub_after_first(s, store, lst, key, level, value) do
case lst do
[ {k1, v1, r1} ] ->
if s.cmp.(k1, key) == :duplicate do
raise "Bad logic"
end
{r1new, store_new} = insert_at(s, store, r1, key, level, value)
{ [{k1, v1, r1new}], store_new }
[ {k1, v1, r1} = fst, {k2, _, _} = rst1 | rst ] ->
if s.cmp.(k1, key) == :duplicate do
raise "Bad logic"
end
case s.cmp.(key, k2) do
:before ->
{r1new, store_new} = insert_at(s, store, r1, key, level, value)
{ [{k1, v1, r1new}, rst1 | rst], store_new }
_ ->
{rst2, new_store} = aux_insert_sub_after_first(s, store, [rst1 |rst], key, level, value)
{ [ fst | rst2 ], new_store }
end
end
end
def get(state, key) do
get(state, state.store, state.root, key)
end
defp get(s, store, root, key) do
case root do
nil -> nil
_ ->
%Page{ level: _, low: low, list: lst } = Store.get(store, root)
get_aux(s, store, low, lst, key)
end
end
defp get_aux(s, store, low, lst, key) do
case lst do
[] ->
get(s, store, low, key)
[ {k, v, low2} | rst ] ->
case s.cmp.(key, k) do
:duplicate -> v
:before ->
get(s, store, low, key)
:after ->
get_aux(s, store, low2, rst, key)
end
end
end
def dump(state) do
dump(state.store, state.root, "")
end
defp dump(store, root, lvl) do
case root do
nil ->
IO.puts(lvl <> "nil")
_ ->
%Page{ level: level, low: low, list: lst} = Store.get(store, root)
IO.puts(lvl <> "#{root|>Base.encode16} (#{level})")
dump(store, low, lvl <> " ")
for {k, v, r} <- lst do
IO.puts(lvl<>"- #{inspect k} => #{inspect v}")
dump(store, r, lvl <> " ")
end
end
end
defp calc_level(key) do
key
|> SData.term_hash
|> Base.encode16
|> String.to_charlist
|> count_leading_zeroes
end
defp count_leading_zeroes('0' ++ rest) do
1 + count_leading_zeroes(rest)
end
defp count_leading_zeroes(_) do
0
end
end
|