-
Notifications
You must be signed in to change notification settings - Fork 298
/
list.lua
1119 lines (1012 loc) · 30.7 KB
/
list.lua
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Use of this source code is governed by the Apache 2.0 license; see
-- COPYING.
module(..., package.seeall)
local murmur = require("lib.hash.murmur")
local ffi = require("ffi")
local C = ffi.C
local band, bor, bnot, lshift, rshift =
bit.band, bit.bor, bit.bnot, bit.lshift, bit.rshift
local min, max =
math.min, math.max
local Heap = {
line_size = 128,
block_lines = 64,
block_size = 64*128, -- 8KB
}
-- NB: `a' must be a power of two
local function pad (a, l) return band(-l, a-1) end
local function padded (a, l) return l + pad(a, l) end
local block_t = ffi.typeof(([[
struct {
uint8_t ref[%d];
uint8_t mem[%d];
}
]]):format(Heap.block_lines, Heap.block_size))
function Heap:new ()
local heap = {
_blocks = {
[0] = ffi.new(block_t)
},
_free = 0, _maxfree = Heap.block_size,
_recycle = nil, _maxrecycle = nil
}
return setmetatable(heap, {__index=Heap})
end
local _block_pow = 13
assert(Heap.block_size == lshift(1,_block_pow))
function Heap:_block (o)
local block = rshift(o, _block_pow)
local offset = band(o, lshift(1, _block_pow)-1)
return block, offset
end
function Heap:_bump_alloc (bytes)
local o, new_free = self._free, self._free + bytes
if new_free <= self._maxfree then
self._free = new_free
return o
end
end
local _line_pow = 7
assert(Heap.line_size == lshift(1, _line_pow))
function Heap:_ref (o, bytes, c)
local block, offset = self:_block(o)
local b = self._blocks[block]
while bytes > 0 do
local ref = rshift(offset, _line_pow)
b.ref[ref] = b.ref[ref] + c
local next_offset = lshift(ref+1, _line_pow)
bytes = bytes - (next_offset - offset)
offset = next_offset
end
end
function Heap:_has_ref (l)
local block, offset = self:_block(l)
local b = self._blocks[block]
local ref = rshift(offset, _line_pow)
return b.ref[ref] > 0
end
function Heap:_find_hole (recycle)
local block = self:_block(recycle)
while recycle < lshift(block+1, _block_pow) do
if not self:_has_ref(recycle) then
return recycle
end
recycle = recycle + Heap.line_size
end
end
function Heap:_find_recycle (recycle)
local hole
local block = self:_block(recycle)
local free_block = self:_block(self._free)
-- NB: scan only blocks before current free block
while not hole and block < free_block do
hole = self:_find_hole(recycle)
block = block + 1
recycle = lshift(block, _block_pow)
end
if hole then
return hole, hole + Heap.line_size
end
end
function Heap:_recycle_alloc (bytes)
assert(bytes <= Heap.line_size)
local o, new_recycle = self._recycle, self._recycle + bytes
if new_recycle <= self._maxrecycle then
self._recycle = new_recycle
return o
else
local next_line = padded(Heap.line_size, self._recycle)
self._recycle, self._maxrecycle = self:_find_recycle(next_line)
if self._recycle then
return self:_recycle_alloc(bytes)
end
end
end
function Heap:_new_block ()
local block = #self._blocks+1
self._blocks[block] = ffi.new(block_t)
local o = lshift(block, _block_pow)
return o, o + Heap.block_size
end
function Heap:_collect ()
self._free, self._maxfree = self:_new_block()
self._recycle, self._maxrecycle = self:_find_recycle(0)
end
function Heap:allocate (bytes)
assert(bytes <= Heap.block_size)
local o
if self._recycle and bytes <= Heap.line_size then
o = self:_recycle_alloc(bytes)
end
if not o then
o = self:_bump_alloc(bytes)
end
if o then
self:_ref(o, bytes, 1)
-- Allocated space is zeroed. We are civilized, after all.
ffi.fill(self:ptr(o), bytes, 0)
return o
else
self:_collect()
return self:allocate(bytes)
end
end
function Heap:free (o, bytes)
assert(bytes <= Heap.block_size)
self:_ref(o, bytes, -1)
end
function Heap:ptr (o)
local block, offset = self:_block(o)
return self._blocks[block].mem + offset
end
local function selftest_heap ()
local h = Heap:new()
local o1 = h:allocate(Heap.line_size/2)
assert(h:_has_ref(0*Heap.line_size))
local o2 = h:allocate(Heap.line_size*1)
assert(h:_has_ref(0*Heap.line_size))
assert(h:_has_ref(1*Heap.line_size))
h:free(o2, Heap.line_size*1)
assert(h:_has_ref(0*Heap.line_size))
assert(not h:_has_ref(1*Heap.line_size))
h:free(o1, Heap.line_size/2)
assert(not h:_has_ref(0*Heap.line_size))
local o1 = h:allocate(Heap.block_size)
local o1_b, o1_o = h:_block(o1)
assert(o1_b == 1 and o1_o == 0)
assert(#h._blocks == 1)
assert(h._recycle == 0)
assert(h._maxrecycle == Heap.line_size)
assert(h._free == Heap.block_size*2)
assert(h._maxfree == Heap.block_size*2)
local o2 = h:allocate(Heap.line_size/2)
assert(h._recycle == Heap.line_size/2)
local o3 = h:allocate(Heap.line_size)
assert(h._recycle == h._maxrecycle)
-- Stress
local h = Heap:new()
local obj = {}
math.randomseed(0)
local function alloc_obj ()
local size = math.random(10*Heap.line_size)
local s = ffi.new("char[?]", size)
for i=0, size-1 do
s[i] = math.random(127)
end
local o = h:allocate(size)
assert(not obj[o])
ffi.copy(h:ptr(o), s, size)
obj[o] = s
return o
end
local function free_obj ()
for o, s in pairs(obj) do
if math.random(10) == 1 then
h:free(o, ffi.sizeof(s))
obj[o] = nil
end
end
end
local function check_obj ()
for o, s in pairs(obj) do
if C.memcmp(h:ptr(o), s, ffi.sizeof(s)) ~= 0 then
return o
end
end
end
for i=1, 100000 do
local o = alloc_obj()
local err = check_obj()
if err then
error("error after allocation "..i.." ("..o..") in object "..err)
end
free_obj()
end
end
local List = {
trie_width = 4,
hash_width = 32,
node_children = 16
}
-- YANG built-in types:
-- +---------------------+-------------------------------------+
-- | Name | Description |
-- +---------------------+-------------------------------------+
-- | binary | Any binary data |
-- | bits | A set of bits or flags |
-- | boolean | "true" or "false" |
-- | decimal64 | 64-bit signed decimal number |
-- | empty | A leaf that does not have any value |
-- | enumeration | One of an enumerated set of strings |
-- | identityref | A reference to an abstract identity |
-- | instance-identifier | A reference to a data tree node |
-- | int8 | 8-bit signed integer |
-- | int16 | 16-bit signed integer |
-- | int32 | 32-bit signed integer |
-- | int64 | 64-bit signed integer |
-- | leafref | A reference to a leaf instance |
-- | string | A character string |
-- | uint8 | 8-bit unsigned integer |
-- | uint16 | 16-bit unsigned integer |
-- | uint32 | 32-bit unsigned integer |
-- | uint64 | 64-bit unsigned integer |
-- | union | Choice of member types |
-- +---------------------+-------------------------------------+
List.type_map = {
binary = {ctype='uint32_t', kind='string'}, -- same as string
bits = {ctype='uint64_t', kind='scalar'}, -- no more than 64 flags
boolean = {ctype='bool', kind='scalar'},
decimal64 = {ctype='double', kind='scalar'},
enumeration = {ctype='int32_t', kind='scalar'},
empty = {ctype='bool', kind='empty'}, -- no representation (always true)
int8 = {ctype='int8_t', kind='scalar'},
int16 = {ctype='int16_t', kind='scalar'},
int32 = {ctype='int32_t', kind='scalar'},
int64 = {ctype='int64_t', kind='scalar'},
string = {ctype='uint32_t', kind='string'}, -- pointer into heap
uint8 = {ctype='uint8_t', kind='scalar'},
uint16 = {ctype='uint16_t', kind='scalar'},
uint32 = {ctype='uint32_t', kind='scalar'},
uint64 = {ctype='uint64_t', kind='scalar'},
}
function List:type_info (type)
return assert(self.type_map[type], "Unsupported type: "..type)
end
List.type_cache = {}
function List:cached_type (t)
if not self.type_cache[t] then
self.type_cache[t] = ffi.typeof(t)
end
return self.type_cache[t]
end
List.node_t = List:cached_type [[
struct {
uint16_t occupied, leaf;
uint32_t parent;
uint32_t children[16];
}
]]
List.list_ts = [[
struct {
uint32_t prev, next;
}
]]
List.string_t = List:cached_type [[
struct {
uint16_t len;
uint8_t str[1];
} __attribute__((packed))
]]
List.optional_ts = [[
struct {
%s value;
bool present;
} __attribute__((packed))
]]
function List:new (keys, members)
local self = setmetatable({}, {__index=List})
for name, spec in pairs(keys) do
assert(not spec.optional, "Keys can not be optional: "..name)
end
local keys_ts = self:build_type(keys, true)
local members_ts = self:build_type(members)
self.keys = keys
self.members = members
self.keys_t = self:cached_type(keys_ts)
self.leaf_t = self:cached_type(self:build_leaf_type(keys_ts, members_ts))
self.heap = Heap:new()
self.first, self.last = nil, nil -- empty
self.root = self:alloc_node() -- heap obj=0 reserved for root node
self.hashin = ffi.new(self.keys_t)
self.length = 0
return self
end
function List:field_order (fields)
local order = {}
for name in pairs(fields) do
table.insert(order, name)
end
local function order_fields (x, y)
-- 1. mandatory fields (< name)
-- 2. optional fields (< name)
if not fields[x].optional and fields[y.optional] then
return true
elseif fields[x].optional and not fields[y.optional] then
return false
else
return x < y
end
end
table.sort(order, order_fields)
return order
end
function List:build_type (fields)
local t = "struct { "
for _, name in ipairs(self:field_order(fields)) do
local spec = fields[name]
assert(type(spec) == 'table' and type(spec.type) == 'string',
"Invalid field spec for "..name)
local ct = self:type_info(spec.type).ctype
if spec.optional then
ct = self.optional_ts:format(ct)
end
t = t..("%s %s; "):format(ct, name)
end
t = t.."} __attribute__((packed))"
return t
end
function List:build_leaf_type (keys_ts, members_ts)
return ("struct { %s list; %s keys; %s members; } __attribute__((packed))")
:format(self.list_ts, keys_ts, members_ts)
end
function List:heap_cast (t, o)
return ffi.cast(ffi.typeof('$*', t), self.heap:ptr(o))
end
function List:alloc_node ()
local o = self.heap:allocate(ffi.sizeof(self.node_t))
return o
end
function List:free_node (o)
self.heap:free(o, ffi.sizeof(self.node_t))
end
function List:node (o)
return self:heap_cast(self.node_t, o)
end
function List:alloc_leaf ()
local o = self.heap:allocate(ffi.sizeof(self.leaf_t))
return o
end
function List:free_leaf (o)
self.heap:free(o, ffi.sizeof(self.leaf_t))
end
function List:leaf (o)
return self:heap_cast(self.leaf_t, o)
end
function List:alloc_str (s)
local o = self.heap:allocate(ffi.sizeof(self.string_t)+#s-1)
local str = self:str(o)
ffi.copy(str.str, s, #s)
str.len = #s
return o
end
function List:free_str (o)
local str = self:str(o)
self.heap:free(o, ffi.sizeof(self.string_t)+str.len-1)
end
function List:str (o)
return self:heap_cast(self.string_t, o)
end
function List:tostring(o)
local str = self:str(o)
return ffi.string(str.str, str.len)
end
function List:str_equal_string (o, s)
local str = self:str(o)
if not str.len == #s then
return false
end
return C.memcmp(str.str, s, str.len) == 0
end
function List:pack_mandatory (dst, name, type_info, value)
assert(value ~= nil, "Missing value: "..name)
if type_info.kind == 'scalar' then
dst[name] = value
elseif type_info.kind == 'string' then
dst[name] = self:alloc_str(value)
elseif type_info.kind == 'empty' then
dst[name] = true
else
error("NYI: kind "..type_info.kind)
end
end
function List:unpack_mandatory (dst, name, type_info, value)
if type_info.kind == 'scalar' then
dst[name] = value
elseif type_info.kind == 'string' then
dst[name] = self:tostring(value)
elseif type_info.kind == 'empty' then
dst[name] = true
else
error("NYI: kind "..type_info.kind)
end
end
function List:free_mandatory (value, type_info)
if type_info.kind == 'scalar' then
-- nop
elseif type_info.kind == 'string' then
self:free_str(value)
elseif type_info.kind == 'empty' then
-- nop
else
error("NYI: kind "..type_info.kind)
end
end
function List:equal_mandatory (packed, unpacked, type_info)
if type_info.kind == 'scalar' then
return packed == unpacked
elseif type_info.kind == 'string' then
return self:str_equal_string(packed, unpacked)
elseif type_info.kind == 'empty' then
return true
else
error("NYI: kind "..type_info.kind)
end
end
function List:pack_optional (dst, name, type_info, value)
if value ~= nil then
self:pack_mandatory(dst[name], 'value', type_info, value)
dst[name].present = true
else
dst[name].value = 0
dst[name].present = false
end
end
function List:unpack_optional (dst, name, type_info, value)
if value.present then
self:unpack_mandatory(dst, name, type_info, value.value)
end
end
function List:free_optional (value, type_info)
if value.present then
self:free_mandatory(value.value, type_info)
end
end
function List:equal_optional (packed, unpacked, type_info)
if packed.present then
return self:equal_mandatory(packed.value, unpacked, type_info)
else
return unpacked == nil
end
end
function List:pack_field (dst, name, spec, value)
local type_info = self:type_info(spec.type)
if spec.optional then
self:pack_optional(dst, name, type_info, value)
else
self:pack_mandatory(dst, name, type_info, value)
end
end
function List:unpack_field (dst, name, spec, value)
local type_info = self:type_info(spec.type)
if spec.optional then
self:unpack_optional(dst, name, type_info, value)
else
self:unpack_mandatory(dst, name, type_info, value)
end
end
function List:free_field (value, spec)
local type_info = self:type_info(spec.type)
if spec.optional then
self:free_optional(value, type_info)
else
self:free_mandatory(value, type_info)
end
end
function List:equal_field (packed, unpacked, spec)
local type_info = self:type_info(spec.type)
if spec.optional then
return self:equal_optional(packed, unpacked, type_info)
else
return self:equal_mandatory(packed, unpacked, type_info)
end
end
function List:pack_fields (s, t, fields)
for name, spec in pairs(fields) do
self:pack_field(s, name, spec, t[name])
end
end
function List:unpack_fields (t, s, fields)
for name, spec in pairs(fields) do
self:unpack_field(t, name, spec, s[name])
end
end
function List:free_fields (s, fields)
for name, spec in pairs(fields) do
self:free_field(s[name], spec)
end
end
local murmur32 = murmur.MurmurHash3_x86_32:new()
local function hash32 (ptr, len, seed)
return murmur32:hash(ptr, len, seed).u32[0]
end
function List:entry_hash (e, seed)
for name, spec in pairs(self.keys) do
local type_info = self:type_info(spec.type)
if type_info.kind == 'scalar' then
self:pack_field(self.hashin, name, spec, e[name])
elseif type_info.kind == 'string' then
self.hashin[name] = hash32(e[name], #e[name], seed)
elseif type_info.kind == 'empty' then
self:pack_field(self.hashin, name, spec, e[name])
else
error("NYI: kind "..type_info.kind)
end
end
return hash32(self.hashin, ffi.sizeof(self.keys_t), seed)
end
-- Same as entry hash but for keys_t
function List:leaf_hash (keys, seed)
for name, spec in pairs(self.keys) do
local type_info = self:type_info(spec.type)
if type_info.kind == 'scalar' then
self:pack_field(self.hashin, name, spec, keys[name])
elseif type_info.kind == 'string' then
local str = self:str(keys[name])
self.hashin[name] = hash32(str.str, str.len, seed)
elseif type_info.kind == 'empty' then
self:pack_field(self.hashin, name, spec, keys[name])
else
error("NYI: kind "..type_info.kind)
end
end
return hash32(self.hashin, ffi.sizeof(self.keys_t), seed)
end
function List:new_leaf (e, members, prev, next)
local o = self:alloc_leaf()
local leaf = self:leaf(o)
leaf.list.prev = prev or 0 -- NB: obj=0 is root node, can not be a leaf!
leaf.list.next = next or 0
self:pack_fields(leaf.keys, e, self.keys)
self:pack_fields(leaf.members, members or e, self.members)
return o
end
function List:update_leaf (o, members)
local leaf = self:leaf(o)
self:free_fields(leaf.members, self.members)
self:pack_fields(leaf.members, members, self.members)
end
function List:destroy_leaf (o)
local leaf = self:leaf(o)
self:free_fields(leaf.keys, self.keys)
self:free_fields(leaf.members, self.members)
self:free_leaf(o)
end
local node_index_mask = List.node_children - 1
function List:node_index (node, d, h)
return band(node_index_mask, rshift(h, d))
end
function List:node_occupied (node, index, newval)
if newval == true then
node.occupied = bor(node.occupied, lshift(1, index))
elseif newval == false then
node.occupied = band(node.occupied, bnot(lshift(1, index)))
end
return band(node.occupied, lshift(1, index)) > 0
end
function List:node_leaf (node, index, newval)
if newval == true then
node.leaf = bor(node.leaf, lshift(1, index))
elseif newval == false then
node.leaf = band(node.leaf, bnot(lshift(1, index)))
end
return band(node.leaf, lshift(1, index)) > 0
end
function List:next_hash_parameters (d, s, h)
if d + self.trie_width < self.hash_width then
return d + self.trie_width, s, h
else
return 0, s + 1, nil
end
end
function List:prev_hash_parameters (d, s, h)
if d >= self.trie_width then
return d - self.trie_width, s, h
else
return self.hash_width - self.trie_width, s - 1, nil
end
end
function List:entry_keys_equal (e, o)
local keys = self:leaf(o).keys
for name, spec in pairs(self.keys) do
if not self:equal_field(keys[name], e[name], spec) then
return false
end
end
return true
end
-- NB: finds any node matching the keys hash!
function List:find_node (k, r, d, s, h)
r = r or self.root
d = d or 0
s = s or 0
h = h or self:entry_hash(k, s)
local node = self:node(r)
local index = self:node_index(node, d, h)
if self:node_occupied(node, index) and
not self:node_leaf(node, index)
then
-- Continue searching in child node.
d, s, h = self:next_hash_parameters(d, s, h)
return self:find_node(k, node.children[index], d, s, h)
else
-- Found!
return r, d, s, h
end
end
-- NB: finds leaf with matching keys in node.
function List:find_leaf (k, n, d, s, h)
local node = self:node(n)
local index = self:node_index(node, d, h)
if self:node_occupied(node, index) then
assert(self:node_leaf(node, index))
local o = node.children[index]
if self:entry_keys_equal(k, o) then
return o
end
end
end
-- NB: does not handle already existing identical keys!
function List:insert_leaf (o, r, d, s, h)
h = h or self:leaf_hash(self:leaf(o).keys, s)
local node = self:node(r)
local index = self:node_index(node, d, h)
if self:node_occupied(node, index) then
assert(self:node_leaf(node, index))
-- Occupied by leaf, replace with node and insert
-- both existing and new leaves into new node.
local l = node.children[index]
local n = self:alloc_node()
self:node(n).parent = r
node.children[index] = n
self:node_leaf(node, index, false)
d, s, h = self:next_hash_parameters(d, s, h)
self:insert_leaf(l, n, d, s, nil)
self:insert_leaf(o, n, d, s, h)
else
-- Not occupied, insert leaf.
self:node_occupied(node, index, true)
self:node_leaf(node, index, true)
node.children[index] = o
end
end
-- NB: does not handle non-existing keys!
function List:remove_child (k, r, d, s, h)
local node = self:node(r)
local index = self:node_index(node, d, h)
assert(self:node_occupied(node, index))
assert(self:node_leaf(node, index))
-- Remove
self:node_occupied(node, index, false)
self:node_leaf(node, index, false)
node.children[index] = 0
self:remove_obsolete_nodes(k, r, d, s, h)
end
assert(ffi.abi("le"))
local t = ffi.new("union { uint32_t u[2]; double d; }")
local function msb_set (v)
-- https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogIEEE64Float
-- "Finding integer log base 2 of an integer
-- (aka the position of the highest bit set)"
--
-- We use this function to find the only bit set. :-)
t.u[1] = 0x43300000
t.u[0] = v
t.d = t.d - 4503599627370496.0
return rshift(t.u[1], 20) - 0x3FF
end
function List:remove_obsolete_nodes (k, r, d, s, h)
if r == self.root then
-- Node is the root, and never obsolete.
return
end
local node = self:node(r)
local d, s, h = self:prev_hash_parameters(d, s, h)
h = h or self:entry_hash(k, s)
local parent = self:node(node.parent)
local parent_index = self:node_index(parent, d, h)
if node.occupied == 0 then
-- Node is now empty, remove from parent.
error("unreachable")
-- ^- This case never happens, because we only ever create
-- new nodes with at least two leaves (the new leaf, and
-- the displaced leaf).
parent.children[parent_index] = 0
self:node_occupied(parent, parent_index, false)
self:free_node(r)
return self:remove_obsolete_nodes(k, node.parent, d, s, h)
elseif band(node.occupied, node.occupied-1) == 0 then
-- Node has only one child, move it to parent.
local index = msb_set(node.occupied)
parent.children[parent_index] = node.children[index]
if self:node_leaf(node, index) then
self:node_leaf(parent, parent_index, true)
else
self:node(node.children[index]).parent = node.parent
end
self:free_node(r)
return self:remove_obsolete_nodes(k, node.parent, d, s, h)
end
end
function List:append_leaf (o, prev)
prev = prev or self.last
if not prev then
self.first, self.last = o, o
else
local leaf = self:leaf(o)
local pleaf = self:leaf(prev)
leaf.list.prev = prev
leaf.list.next = pleaf.list.next
pleaf.list.next = o
end
self.length = self.length + 1
end
function List:unlink_leaf (o)
local leaf = self:leaf(o)
local prev = self:leaf(leaf.list.prev)
local next = self:leaf(leaf.list.next)
prev.list.next = leaf.list.next
next.list.prev = leaf.list.prev
self.length = self.length - 1
end
function List:leaf_entry (o)
local leaf = self:leaf(o)
local ret = {}
self:unpack_fields(ret, leaf.keys, self.keys)
self:unpack_fields(ret, leaf.members, self.members)
return ret
end
function List:add_entry (e, update, members)
local n, d, s, h = self:find_node(e)
local o = self:find_leaf(e, n, d, s, h)
if o then
if update then
self:update_leaf(o, members or e)
else
error("Attempting to add duplicate entry to list")
end
else
local o = self:new_leaf(e, members)
self:insert_leaf(o, n, d, s, h)
self:append_leaf(o)
end
end
function List:add_or_update_entry (e, members)
self:add_entry(e, true, members)
end
function List:find_entry (k)
local o = self:find_leaf(k, self:find_node(k))
if o then
return self:leaf_entry(o)
end
end
function List:remove_entry (k)
local n, d, s, h = self:find_node(k)
local o = self:find_leaf(k, n, d, s, h)
if o then
self:remove_child(k, n, d, s, h)
self:unlink_leaf(o)
self:destroy_leaf(o)
return true
end
end
function List:ipairs ()
local n = 1
local o = self.first
return function ()
if o == 0 then
return
end
local i = n
local e = self:leaf_entry(o)
n = n + 1
o = self:leaf(o).list.next
return i, e
end
end
function selftest_list ()
local l = List:new(
{id={type='uint32'}, name={type='string'}},
{value={type='decimal64'}, description={type='string'}}
)
-- print("leaf_t", ffi.sizeof(l.leaf_t))
-- print("node_t", ffi.sizeof(l.node_t))
l:add_entry {
id=42, name="foobar",
value=3.14, description="PI"
}
local root = l:node(l.root)
assert(root.occupied == lshift(1, 14))
assert(root.occupied == root.leaf)
-- print(l.root, root.occupied, root.leaf, root.children[14])
local e1 = l:find_entry {id=42, name="foobar"}
assert(e1)
assert(e1.id == 42 and e1.name == "foobar")
assert(not l:find_entry {id=43, name="foobar"})
assert(not l:find_entry {id=42, name="foo"})
-- for k,v in pairs(e1) do print(k,v) end
l:add_entry {
id=127, name="hey",
value=1/0, description="inf"
}
for i, e in l:ipairs() do
if i == 1 then
assert(e.id == 42)
elseif i == 2 then
assert(e.id == 127)
else
error("unexpected index: "..i)
end
end
-- Test update
local ok = pcall(function ()
l:add_entry {
id=127, name="hey",
value=1, description="one"
}
end)
assert(not ok)
l:add_or_update_entry {
id=127, name="hey",
value=1, description="one"
}
local e_updated = l:find_entry {id=127, name="hey"}
assert(e_updated)
assert(e_updated.value == 1)
assert(e_updated.description == "one")
-- Test collisions
local lc = List:new({id={type='uint64'}}, {})
-- print("leaf_t", ffi.sizeof(lc.leaf_t))
-- print("node_t", ffi.sizeof(lc.node_t))
lc:add_entry {id=0ULL}
lc:add_entry {id=4895842651ULL}
local root = lc:node(lc.root)
assert(root.leaf == 0)
assert(root.occupied == lshift(1, 12))
-- print(lc.root, root.occupied, root.leaf, root.children[12])
local e1 = lc:find_entry {id=0ULL}
local e2 = lc:find_entry {id=4895842651ULL}
assert(e1)
assert(e2)
assert(e1.id == 0ULL)
assert(e2.id == 4895842651ULL)
assert(lc:remove_entry {id=0ULL})
assert(lc:remove_entry {id=4895842651ULL})
assert(lc.length == 0)
assert(root.occupied == 0)
-- Test optional
local l = List:new(
{id={type='string'}},
{value={type='decimal64', optional=true},
description={type='string', optional=true}}
)
l:add_entry{
id="foo",
value=3.14,
description="PI"
}
l:add_entry{
id="foo1",
value=42
}
l:add_entry{
id="foo2",
description="none"
}
l:add_entry{
id="foo3"
}
assert(l:find_entry{id="foo"}.value == 3.14)
assert(l:find_entry{id="foo"}.description == "PI")
assert(l:find_entry{id="foo1"}.value == 42)
assert(l:find_entry{id="foo1"}.description == nil)
assert(l:find_entry{id="foo2"}.value == nil)
assert(l:find_entry{id="foo2"}.description == "none")
assert(l:find_entry{id="foo3"}.value == nil)
assert(l:find_entry{id="foo3"}.description == nil)
-- Test empty type
local l = List:new(
{id={type='string'}, e={type='empty'}},
{value={type='empty', optional=true}}
)
l:add_entry {id="foo", e=true}
l:add_entry {id="foo1", e=true, value=true}
assert(l:find_entry{id="foo", e=true}.value == nil)
assert(l:find_entry{id="foo1", e=true}.value == true)
local ok, err = pcall(function () l:add_entry {id="foo2"} end)
assert(not ok)