-
Notifications
You must be signed in to change notification settings - Fork 2
/
ARTfulkv5.c
1541 lines (1196 loc) · 34.6 KB
/
ARTfulkv5.c
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
// ARTful5: Adaptive Radix Trie key-value store
// Author: Karl Malbrain, [email protected]
// Date: 13 JAN 15
/*
This work, including the source code, documentation
and related data, is placed into the public domain.
The orginal author is Karl Malbrain.
THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
RESULTING FROM THE USE, MODIFICATION, OR
REDISTRIBUTION OF THIS SOFTWARE.
*/
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <xmmintrin.h>
typedef unsigned long int ulong;
typedef unsigned char uchar;
typedef unsigned int uint;
enum NodeType {
UnusedNode = 0, // node is not yet in use
LeafSlot, // node slot contains leaf offset
ValueSlot, // node slot contains ARTval offset
SpanNode, // node contains up to 8 key bytes and leaf element
Array4, // node contains 4 radix slots & leaf element
Array16, // node contains 16 radix slots & leaf element
Array48, // node contains 48 radix slots & leaf element
Array256 // node contains 256 radix slots & leaf element
};
typedef union {
struct {
ulong off:45; // offset to node sub-contents
uchar type:3; // type of radix node
uchar mutex[1]; // update/write synchronization
uchar nslot:7; // number of slots of node in use
uchar dead:1; // node is no longer in the tree
};
ulong bits;
} ARTslot;
// a node is broken down into two parts:
// the node proper and its pointer slot.
// the first few fields are generic to all nodes:
typedef struct {
ARTslot value[1]; // slot to a leaf value that ended before this node.
} ARTgeneric;
// radix node with four slots and their key bytes:
typedef struct {
ARTslot value[1]; // slot to a leaf value that ended before this node.
uchar keys[4];
ARTslot radix[4];
} ARTnode4;
// radix node with sixteen slots and their key bytes:
typedef struct {
ARTslot value[1]; // slot to a leaf value that ended before this node.
uchar keys[16];
ARTslot radix[16];
} ARTnode16;
// radix node with sixty-four slots and a 256 key byte array:
typedef struct {
ARTslot value[1]; // slot to a leaf value that ended before this node.
uchar keys[256];
ARTslot radix[48];
} ARTnode48;
// radix node all two hundred fifty six slots
typedef struct {
ARTslot value[1]; // slot to a leaf value that ended before this node.
ARTslot radix[256];
} ARTnode256;
// Span node containing up to 16 consecutive key bytes
typedef struct {
ARTslot value[1]; // slot to a leaf value that ended before this node.
uchar bytes[8];
ARTslot next[1]; // next node under span
} ARTspan;
// the ARTful trie containing the root node slot
// and the heap storage management.
typedef struct {
ARTslot root[1];
ulong arena_size; // size of Arena File
ulong arena_next; // next available offset
uchar arena_mutex[1];
} ARTtrie;
// the ARTful trie value string in the heap
typedef struct {
uchar len; // this can be changed to a ushort or uint
uchar value[0];
} ARTval;
typedef union {
ARTspan *span;
ARTnode4 *radix4;
ARTnode16 *radix16;
ARTnode48 *radix48;
ARTnode256 *radix256;
ARTgeneric *generic;
} ARTfan;
// the cursor stack element
typedef struct {
ARTslot *slot; // current slot
uint off; // offset within key
int idx; // current index within slot
} ARTstack;
// the cursor control
typedef struct {
uint maxdepth; // maximum depth of ARTful trie
uint depth; // current depth of cursor
ARTval *value; // current leaf node
ARTstack stack[0]; // cursor stack
} ARTcursor;
// Each thread gets one of these structures
typedef struct {
ulong base; // base of arena chunk assigned to thread
ulong offset; // next offset of this chunk to allocate
ARTtrie *trie; // ARTful trie
ARTcursor *cursor; // thread cursor
} ARTthread;
// one byte mutex spin lock
#define relax() asm volatile("pause\n": : : "memory")
void mutexlock(uchar *volatile latch)
{
while( __sync_lock_test_and_set (latch, 1) )
while( latch[0] )
relax();
}
void mutexrelease(uchar *latch)
{
// __sync_synchronize();
*latch = 0;
}
// Maximum Arena size, e.g. virtual memory size
ulong ArenaVM = 1024UL * 1024UL*1024UL *12;
// Initial/Incremental Arena file size
ulong ArenaInit = 1024UL*1024UL *100;
uchar *Arena; // pointer to base of heap
int ArenaFd; // arena file descriptor
// incremental amount to allocate to threads
// must be a power of two.
#define ARENA_chunk (1024 * 1024)
// release unused value heap area
uint Census[8], Free[8];
void art_free (ARTtrie *trie, uchar type, void *what)
{
mutexlock (trie->arena_mutex);
Free[type]++;
mutexrelease (trie->arena_mutex);
}
// allocate space in the Arena heap
ulong art_space (ARTthread *thread, uint size)
{
ulong offset;
uint xtra;
if( xtra = size & 0x7 )
size += 8 - xtra;
if( xtra = thread->offset & 0x7 )
thread->offset += 8 - xtra;
if( !thread->offset || thread->offset + size > ARENA_chunk ) {
mutexlock (thread->trie->arena_mutex);
if( thread->trie->arena_next + ARENA_chunk > thread->trie->arena_size ) {
thread->trie->arena_next = thread->trie->arena_size;
thread->trie->arena_size += ArenaInit;
#ifdef PERSIST
ftruncate (ArenaFd, thread->trie->arena_size);
#endif
}
thread->offset = 0;
thread->base = thread->trie->arena_next;
thread->trie->arena_next += ARENA_chunk;
mutexrelease (thread->trie->arena_mutex);
}
offset = thread->offset + thread->base;
// memset (Arena + offset, 0, size);
thread->offset += size;
return offset;
}
// allocate a new trie node in the Arena heap
ulong art_node (ARTthread *thread, uchar type)
{
uint size;
mutexlock (thread->trie->arena_mutex);
Census[type]++;
mutexrelease (thread->trie->arena_mutex);
switch( type ) {
case SpanNode:
size = sizeof(ARTspan);
break;
case Array4:
size = sizeof(ARTnode4);
break;
case Array16:
size = sizeof(ARTnode16);
break;
case Array48:
size = sizeof(ARTnode48);
break;
case Array256:
size = sizeof(ARTnode256);
break;
default:
abort();
}
return art_space (thread, size);
}
// allocate a new thread cursor object
ARTthread *ARTnewthread (ARTtrie *trie, uint depth)
{
ARTcursor *cursor = calloc (1, sizeof(ARTcursor) + depth * sizeof(ARTstack));
ARTthread *thread = calloc (1, sizeof(ARTthread));
cursor->maxdepth = depth;
thread->cursor = cursor;
thread->trie = trie;
return thread;
}
// create/open an ARTful trie
ARTtrie *ARTnew (int fd)
{
int flag = PROT_READ | PROT_WRITE;
ARTnode256 *radix256, *root256;
ARTtrie *trie;
ulong offset;
uint i, j;
#ifdef PERSIST
if( !(offset = lseek64 (fd, 0L, 2)) )
ftruncate64 (fd, offset = ArenaInit);
Arena = mmap (0, ArenaVM, flag, MAP_SHARED, fd, 0);
#else
offset = ArenaVM;
Arena = mmap(NULL, offset, flag, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
#endif
ArenaFd = fd;
trie = (ARTtrie *)Arena;
trie->arena_size = offset;
// is this a new file?
// if so, fill out the first two levels
// of the trie with radix256 nodes.
if( !trie->arena_next ) {
trie->arena_next = sizeof(ARTtrie);
root256 = (ARTnode256 *)(Arena + trie->arena_next);
trie->root->off = trie->arena_next >> 3;
trie->root->type = Array256;
trie->arena_next += sizeof(ARTnode256);
for( i = 0; i < 256; i++ ) {
radix256 = (ARTnode256 *)(Arena + trie->arena_next);
root256->radix[i].off = trie->arena_next >> 3;
root256->radix[i].type = Array256;
trie->arena_next += sizeof(ARTnode256);
// for( j = 0; j < 256; j++ ) { // fill in 3rd level
// radix256[i].radix[j].off = trie->arena_next >> 3;
// radix256[i].radix[j].type = Array256;
// trie->arena_next += sizeof(ARTnode256);
// }
}
// round up to complete the first chunks
trie->arena_next |= ARENA_chunk - 1;
trie->arena_next++;
}
return trie;
}
void ARTclosethread (ARTthread *thread)
{
free (thread->cursor);
free (thread);
}
void ARTclose (ARTtrie *trie)
{
}
// position cursor at largest key
void ARTlastkey (ARTthread *thread, uchar *key, uint keylen)
{
}
// position cursor before requested key
void ARTstartkey (ARTthread *thread, uchar *key, uint keylen)
{
}
// retrieve next key from cursor
uint ARTnextkey (ARTthread *thread, uchar *key, uint keymax)
{
}
// retrieve previous key from cursor
uint ARTprevkey (ARTthread *thread, uchar *key, uint keymax)
{
}
// find key in ARTful trie, returning its value slot address or zero
ARTslot *ARTfindkey (ARTthread *thread, uchar *key, uint keylen)
{
uint len, idx, off;
ARTfan node[1];
ARTslot *slot;
uchar *chr;
restart:
slot = thread->trie->root;
off = 0;
// loop through all the key bytes
while( off < keylen ) {
node->generic = (ARTgeneric *)(Arena + slot->off * 8);
len = slot->nslot;
if( slot->dead )
goto restart;
switch( slot->type ) {
case ValueSlot:
return NULL;
case LeafSlot:
return NULL;
case SpanNode:
if( keylen - off < len || memcmp (key + off, node->span->bytes, len) )
return NULL;
slot = node->span->next;
off += len;
continue;
case Array4:
for( idx = 0; idx < len; idx++ )
if( key[off] == node->radix4->keys[idx] )
break;
if( idx == len )
return NULL;
slot = node->radix4->radix + idx;
off++;
continue;
case Array16:
// is key byte in radix node?
if( chr = memchr (node->radix16->keys, key[off++], len) ) {
idx = chr - node->radix16->keys;
slot = node->radix16->radix + idx;
continue;
}
return NULL;
case Array48:
idx = node->radix48->keys[key[off++]];
// is the key byte assigned to a radix node?
if( idx == 0xff )
return NULL;
slot = node->radix48->radix + idx;
continue;
case Array256:
slot = node->radix256->radix + key[off++];
continue;
case UnusedNode:
return NULL;
}
}
if( slot->type > ValueSlot ) {
node->generic = (ARTgeneric *)(Arena + slot->off * 8);
if( node->generic->value->type )
return node->generic->value;
else
return NULL;
}
if( slot->type )
return slot;
return NULL;
}
// insert key/value into ARTful trie, returning pointer to value slot.
ARTslot *ARTinsert (ARTthread *thread, uchar *key, uint keylen)
{
ARTfan node[1], node2[1], node3[1], node4[1];
ARTslot *prev, newslot[1], *slot;
ARTslot *oldvalue, *retvalue;
uint len, idx, max, off;
uchar slot48, *update48;
uchar *chr, type;
uint prevoff;
restart:
slot = thread->trie->root;
oldvalue = NULL;
off = 0;
while( off < keylen ) {
newslot->bits = slot->bits;
type = newslot->type;
node->generic = (ARTgeneric*)(Arena + newslot->off * 8);
update48 = NULL;
prev = slot;
if( newslot->dead )
goto restart;
switch( type ) {
case SpanNode:
max = len = newslot->nslot;
if( len > keylen - off )
len = keylen - off;
for( idx = 0; idx < len; idx++ )
if( key[off + idx] != node->span->bytes[idx] )
break;
// did we use the entire span node?
if( idx == max ) {
slot = node->span->next;
off += idx;
continue;
}
// obtain write lock on the node
mutexlock (prev->mutex);
*newslot->mutex = 1;
// see if slot changed values
// and restart if so.
if( newslot->bits != prev->bits ) {
mutexrelease (prev->mutex);
continue;
}
prev->dead = 1;
off += idx;
// copy matching prefix bytes to a new span node
if( idx ) {
node2->span = (ARTspan *)(Arena + art_node(thread, SpanNode));
memcpy (node2->span->bytes, node->span->bytes, idx);
newslot->off = (uchar *)node2->span - Arena >> 3;
mutexlock (node->span->value->mutex);
node2->span->value->bits = node->span->value->bits;
node->span->value->dead = 1;
*node2->span->value->mutex = 0;
mutexrelease (node->span->value->mutex);
newslot->type = SpanNode;
slot = node2->span->next;
newslot->nslot = idx;
}
// else cut the span node from the tree by transforming
// the original node into a radix4 or span node
else
slot = newslot;
// place a radix node after span1 and before span2
// if needed for additional key byte(s)
if( off < keylen ) {
node3->radix4 = (ARTnode4 *)(Arena + art_node(thread, Array4));
// are we the first node?
if( !idx ) {
mutexlock (node->span->value->mutex);
node3->radix4->value->bits = node->span->value->bits;
node->span->value->dead = 1;
*node3->radix4->value->mutex = 0;
mutexrelease (node->span->value->mutex);
}
slot->off = (uchar *)node3->radix4 - Arena >> 3;
slot->type = Array4;
slot->nslot = 2;
// fill in first radix element
node3->radix4->keys[0] = node->span->bytes[idx++];
slot = node3->radix4->radix + 0;
}
// are there any original span bytes remaining?
// if so, place them in a second span node
if( max - idx ) {
node4->span = (ARTspan *)(Arena + art_node(thread, SpanNode));
memcpy (node4->span->bytes, node->span->bytes + idx, max - idx);
mutexlock (node->span->next->mutex);
node4->span->next->bits = node->span->next->bits;
node->span->next->dead = 1;
*node4->span->next->mutex = 0;
mutexrelease (node->span->next->mutex);
slot->off = (uchar *)node4->span - Arena >> 3;
slot->nslot = max - idx;
slot->type = SpanNode;
slot = node4->span->value;
} else {
mutexlock (node->span->next->mutex);
slot->bits = node->span->next->bits;
node->span->next->dead = 1;
*slot->mutex = 0;
mutexrelease (node->span->next->mutex);
slot = node3->radix4->value;
}
// does key stop at the beginning of radix/span node?
if( off == keylen )
break;
// if not, fill in the second radix element
// and the rest of the key in span nodes below
node3->radix4->keys[1] = key[off++];
slot = node3->radix4->radix + 1;
break;
case Array4:
max = newslot->nslot;
for( idx = 0; idx < max; idx++ )
if( key[off] == node->radix4->keys[idx] )
break;
if( idx < max ) {
slot = node->radix4->radix + idx;
off++;
continue;
}
// obtain write lock on the node
mutexlock (prev->mutex);
*newslot->mutex = 1;
// see if slot changed values
// and restart if so.
if( newslot->bits != prev->bits ) {
mutexrelease (prev->mutex);
continue;
}
// add to radix4 node if room
if( max < 4 ) {
node->radix4->keys[newslot->nslot] = key[off++];
slot = node->radix4->radix + newslot->nslot++;
break;
}
// the radix node is full, promote to
// the next larger size.
node2->radix16 = (ARTnode16 *)(Arena + art_node(thread, Array16));
prev->dead = 1;
for( idx = 0; idx < max; idx++ ) {
slot = node->radix4->radix + idx;
mutexlock (slot->mutex);
node2->radix16->radix[idx].bits = slot->bits;
node2->radix16->keys[idx] = node->radix4->keys[idx];
slot->dead = 1;
*node2->radix16->radix[idx].mutex = 0;
mutexrelease (slot->mutex);
}
node2->radix16->keys[max] = key[off++];
mutexlock (node->radix4->value->mutex);
node2->radix16->value->bits = node->radix4->value->bits;
node->radix4->value->dead = 1;
*node2->radix16->value->mutex = 0;
mutexrelease (node->radix4->value->mutex);
newslot->off = (uchar *)node2->radix16 - Arena >> 3;
newslot->type = Array16;
// fill in rest of the key in span nodes below
slot = node2->radix16->radix + newslot->nslot++;
break;
case Array16:
max = newslot->nslot;
// is key byte in this radix node?
if( chr = memchr (node->radix16->keys, key[off], max) ) {
idx = chr - node->radix16->keys;
slot = node->radix16->radix + idx;
off++;
continue;
}
// obtain write lock on the node
mutexlock (prev->mutex);
*newslot->mutex = 1;
// see if slot changed values
// and restart if so.
if( newslot->bits != prev->bits ) {
mutexrelease (prev->mutex);
continue;
}
// add to radix node if room
if( max < 16 ) {
node->radix16->keys[max] = key[off++];
slot = node->radix16->radix + newslot->nslot++;
break;
}
// the radix node is full, promote to
// the next larger size. mark all the
// keys as currently unused.
node2->radix48 = (ARTnode48 *)(Arena + art_node(thread, Array48));
prev->dead = 1;
memset (node2->radix48->keys, 0xff, sizeof(node2->radix48->keys));
for( idx = 0; idx < max; idx++ ) {
slot = node->radix16->radix + idx;
mutexlock (slot->mutex);
node2->radix48->radix[idx].bits = slot->bits;
node2->radix48->keys[node->radix16->keys[idx]] = idx;
slot->dead = 1;
*node2->radix48->radix[idx].mutex = 0;
mutexrelease (slot->mutex);
}
newslot->off = (uchar *)node2->radix48 - Arena >> 3;
newslot->type = Array48;
node2->radix48->keys[key[off++]] = max;
mutexlock (node->radix16->value->mutex);
node2->radix48->value->bits = node->radix16->value->bits;
node->radix16->value->dead = 1;
*node2->radix48->value->mutex = 0;
mutexrelease (node->radix16->value->mutex);
// fill in rest of the key bytes into
// span nodes below.
slot = node2->radix48->radix + newslot->nslot++;
break;
case Array48:
idx = node->radix48->keys[key[off]];
if( idx < 0xff ) {
slot = node->radix48->radix + idx;
off++;
continue;
}
// obtain write lock on the node
mutexlock (prev->mutex);
*newslot->mutex = 1;
// see if slot changed values
// and restart if so.
if( newslot->bits != prev->bits ) {
mutexrelease (prev->mutex);
continue;
}
// add to radix node
if( newslot->nslot < 48 ) {
update48 = node->radix48->keys + key[off++];
slot48 = newslot->nslot++;
slot = node->radix48->radix + slot48;
break;
}
// the radix node is full, promote to
// the next larger size.
node2->radix256 = (ARTnode256 *)(Arena + art_node(thread, Array256));
prev->dead = 1;
for( idx = 0; idx < 256; idx++ )
if( node->radix48->keys[idx] < 0xff ) {
slot = node->radix48->radix + node->radix48->keys[idx];
mutexlock (slot->mutex);
node2->radix256->radix[idx].bits = slot->bits;
slot->dead = 1;
*node2->radix256->radix[idx].mutex = 0;
mutexrelease (slot->mutex);
}
newslot->type = Array256;
newslot->off = (uchar *)node2->radix256 - Arena >> 3;
mutexlock (node->radix48->value->mutex);
node2->radix256->value->bits = node->radix48->value->bits;
node->radix48->value->dead = 1;
*node2->radix256->value->mutex = 0;
mutexrelease (node->radix48->value->mutex);
// fill in the rest of the key bytes
// into Span nodes below
slot = node2->radix256->radix + key[off++];
break;
case Array256:
slot = node->radix256->radix + key[off++];
continue;
case UnusedNode:
mutexlock (prev->mutex);
*newslot->mutex = 1;
// see if slot changed values
// and restart if so.
if( newslot->bits != prev->bits ) {
mutexrelease (prev->mutex);
continue;
}
slot = newslot;
break;
case ValueSlot:
case LeafSlot:
oldvalue = newslot;
slot = newslot;
break;
}
// did we drop down from Array/Span node w/empty slot?
// else we dropped down from last three types.
if( type > ValueSlot )
retvalue = slot;
else
retvalue = prev;
// fill in an empty slot with remaining key bytes
// i.e. copy remaining key bytes to span nodes
while( len = keylen - off ) {
node2->span = (ARTspan *)(Arena + art_node(thread, SpanNode));
if( oldvalue ) {
mutexlock (oldvalue->mutex);
node2->span->value->bits = oldvalue->bits;
mutexrelease (node2->span->value->mutex);
mutexrelease (oldvalue->mutex);
}
if( len > sizeof(node2->span->bytes) )
len = sizeof(node2->span->bytes);
memcpy (node2->span->bytes, key + off, len);
slot->off = (uchar *)node2->span - Arena >> 3;
slot->type = SpanNode;
slot->nslot = len;
oldvalue = NULL;
retvalue = slot = node2->span->next;
off += len;
}
// lock the slot for caller
// or leave lock in newslot
if( prev != retvalue ) {
mutexlock (retvalue->mutex);
*newslot->mutex = 0;
}
prev->bits = newslot->bits;
if( update48 )
*update48 = slot48;
return retvalue;
}
// return the leaf node slot
if( slot->type > ValueSlot ) {
node->generic = (ARTgeneric *)(Arena + slot->off * 8);
retvalue = node->generic->value;
} else
retvalue = slot;
mutexlock (retvalue->mutex);
return retvalue;
}
// scan the keys stored in the ARTtrie
ulong ARTscan (uchar *key, uint off, uint max, ARTslot *slot)
{
ulong children = 0;
ARTfan node[1];
ARTval *val;
uint i, j;
uint nxt;
uint idx;
int last;
switch( slot->type ) {
case SpanNode:
node->span = (ARTspan *)(Arena + slot->off * 8);
if( node->span->value->type > 0 ) {
fwrite (key, off, 1, stdout);
if( node->span->value->type == ValueSlot ) {
val = (ARTval *)(Arena + node->span->value->off * 8);
fwrite (val->value, val->len, 1, stdout);
} else for( idx = 1; idx < node->span->value->off; idx++ )
children++, fputc ('\n', stdout), fwrite (key, off, 1, stdout);
fputc ('\n', stdout);
children++;
}
memcpy (key + off, node->span->bytes, slot->nslot);
off += slot->nslot;
children += ARTscan (key, off, max, node->span->next);
return children;
case LeafSlot:
case ValueSlot:
fwrite (key, off, 1, stdout);
if( slot->type == ValueSlot ) {
val = (ARTval *)(Arena + slot->off * 8);
fwrite (val->value, val->len, 1, stdout);
} else for( idx = 1; idx < slot->off; idx++ )
children++, fputc ('\n', stdout), fwrite (key, off, 1, stdout);
fputc ('\n', stdout);
children++;
return children;
case Array4:
node->radix4 = (ARTnode4 *)(Arena + slot->off * 8);
if( node->radix4->value->type > 0 ) {
fwrite (key, off, 1, stdout);
if( node->radix4->value->type == ValueSlot ) {
val = (ARTval *)(Arena + node->radix4->value->off * 8);
fwrite (val->value, val->len, 1, stdout);
} else for( idx = 1; idx < node->span->value->off; idx++ )
children++, fputc ('\n', stdout), fwrite (key, off, 1, stdout);
fputc ('\n', stdout);
children++;
}
nxt = 0x100;
last = -1;
for( idx = 0; idx < slot->nslot; idx++ ) {
for( i = 0; i < slot->nslot; i++ )
if( node->radix4->keys[i] > last )
if( node->radix4->keys[i] < nxt )
nxt = node->radix4->keys[i], j = i;
key[off] = nxt;
children += ARTscan (key, off + 1, max, node->radix4->radix + j);
last = nxt;
nxt = 0x100;
}
return children;
case Array16:
node->radix16 = (ARTnode16 *)(Arena + slot->off * 8);
if( node->radix16->value->type > 0 ) {
fwrite (key, off, 1, stdout);
if( node->radix16->value->type == ValueSlot ) {
val = (ARTval *)(Arena + node->radix16->value->off * 8);
fwrite (val->value, val->len, 1, stdout);
} else for( idx = 1; idx < node->radix16->value->off; idx++ )
children++, fputc ('\n', stdout), fwrite (key, off, 1, stdout);
fputc ('\n', stdout);
children++;
}
nxt = 0x100;
last = -1;
for( idx = 0; idx < slot->nslot; idx++ ) {
for( i = 0; i < slot->nslot; i++ )
if( node->radix16->keys[i] > last )
if( node->radix16->keys[i] < nxt )
nxt = node->radix16->keys[i], j = i;
key[off] = nxt;
children += ARTscan (key, off + 1, max, node->radix16->radix + j);