2 * trie.c: implementation of trie.h.
9 #define alignof(typ) ( offsetof(struct { char c; typ t; }, t) )
12 * Compare functions for pathnames. Returns the relative order of
13 * the names, like strcmp; also passes back the offset of the
14 * first differing character if desired.
16 static int trieccmp(unsigned char a
, unsigned char b
)
18 int ia
= (a
== '\0' ?
'\0' : a
== pathsep ?
'\1' : a
+1);
19 int ib
= (b
== '\0' ?
'\0' : b
== pathsep ?
'\1' : b
+1);
23 static int triencmp(const char *a
, size_t alen
,
24 const char *b
, size_t blen
, int *offset
)
27 while (off
< alen
&& off
< blen
&& a
[off
] == b
[off
])
31 if (off
== alen
|| off
== blen
) return (off
== blen
) - (off
== alen
);
32 return trieccmp(a
[off
], b
[off
]);
35 static int triecmp(const char *a
, const char *b
, int *offset
)
37 return triencmp(a
, strlen(a
), b
, strlen(b
), offset
);
40 /* ----------------------------------------------------------------------
41 * Trie node structures.
43 * The trie format stored in the file consists of three distinct
44 * node types, each with a distinguishing type field at the start.
46 * TRIE_LEAF is a leaf node; it contains an actual trie_file
47 * structure, and indicates that when you're searching down the
48 * trie with a string, you should now expect to encounter
51 * TRIE_SWITCH indicates that the set of strings in the trie
52 * include strings with more than one distinct character after the
53 * prefix leading up to this point. Hence, it stores multiple
54 * subnode pointers and a different single character for each one.
56 * TRIE_STRING indicates that at this point everything in the trie
57 * has the same next few characters; it stores a single mandatory
58 * string fragment and exactly one subnode pointer.
61 TRIE_LEAF
= 0x7fffe000,
70 struct trie_switchentry
{
77 struct trie_file file
;
83 * sw[0] to sw[len-1] give the subnode pointers and element
84 * counts. At &sw[len] is stored len single bytes which are
85 * the characters corresponding to each subnode.
88 struct trie_switchentry sw
[];
100 off_t root
, indexroot
;
106 /* Union only used for computing alignment */
108 struct trie_leaf leaf
;
109 struct { /* fake trie_switch with indeterminate array length filled in */
110 struct trie_common c
;
112 struct trie_switchentry sw
[1];
114 struct { /* fake trie_string with indeterminate array length filled in */
115 struct trie_common c
;
121 #define TRIE_MAGIC 0x75646761UL
122 #define TRIE_ALIGN alignof(union trie_node)
124 /* ----------------------------------------------------------------------
125 * Trie-building functions.
139 int lastlen
, lastsize
;
141 struct tbswitch
*switches
;
146 static void tb_seek(triebuild
*tb
, off_t off
)
149 if (lseek(tb
->fd
, off
, SEEK_SET
) < 0) {
150 fprintf(stderr
, PNAME
": lseek: %s\n", strerror(errno
));
155 static void tb_write(triebuild
*tb
, const void *buf
, size_t len
)
159 int ret
= write(tb
->fd
, buf
, len
);
161 fprintf(stderr
, PNAME
": write: %s\n", strerror(errno
));
165 buf
= (const void *)((const char *)buf
+ ret
);
169 static char trie_align_zeroes
[TRIE_ALIGN
];
171 static void tb_align(triebuild
*tb
)
173 int off
= (TRIE_ALIGN
- ((tb
)->offset
% TRIE_ALIGN
)) % TRIE_ALIGN
;
174 tb_write(tb
, trie_align_zeroes
, off
);
177 triebuild
*triebuild_new(int fd
)
179 triebuild
*tb
= snew(triebuild
);
180 struct trie_header th
;
184 tb
->lastlen
= tb
->lastsize
= 0;
190 th
.magic
= TRIE_MAGIC
;
191 th
.root
= th
.count
= 0;
194 th
.pathsep
= (unsigned char)pathsep
;
197 tb_write(tb
, &th
, sizeof(th
));
202 static off_t
triebuild_unwind(triebuild
*tb
, int targetdepth
, int *outcount
)
207 if (tb
->lastoff
== 0) {
212 offset
= tb
->lastoff
;
214 depth
= tb
->lastlen
+ 1;
216 assert(depth
>= targetdepth
);
218 while (depth
> targetdepth
) {
220 while (depth
> targetdepth
&&
221 (depth
-1 > tb
->switchsize
|| !tb
->switches
||
222 tb
->switches
[depth
-1].len
== 0))
224 if (odepth
> depth
) {
226 * Write out a string node.
228 size_t nodesize
= sizeof(struct trie_string
) + odepth
- depth
;
229 struct trie_string
*st
= (struct trie_string
*)smalloc(nodesize
);
230 st
->c
.type
= TRIE_STRING
;
231 st
->stringlen
= odepth
- depth
;
232 st
->subnode
= offset
;
233 memcpy(st
->string
, tb
->lastpath
+ depth
, odepth
- depth
);
236 tb_write(tb
, st
, nodesize
);
240 assert(depth
>= targetdepth
);
241 if (depth
<= targetdepth
)
245 * Now we expect to be sitting just below a switch node.
246 * Add our final entry to it and write it out.
250 struct trie_switch
*sw
;
253 int swlen
= tb
->switches
[depth
].len
;
258 tb
->switches
[depth
].c
[swlen
] = tb
->lastpath
[depth
];
259 tb
->switches
[depth
].off
[swlen
] = offset
;
260 tb
->switches
[depth
].count
[swlen
] = count
;
263 nodesize
= sizeof(struct trie_switch
) +
264 swlen
* sizeof(struct trie_switchentry
) + swlen
;
265 sw
= (struct trie_switch
*)smalloc(nodesize
);
266 chars
= (char *)&sw
->sw
[swlen
];
268 sw
->c
.type
= TRIE_SWITCH
;
271 for (i
= 0; i
< swlen
; i
++) {
272 sw
->sw
[i
].subnode
= tb
->switches
[depth
].off
[i
];
273 sw
->sw
[i
].subcount
= tb
->switches
[depth
].count
[i
];
274 chars
[i
] = tb
->switches
[depth
].c
[i
];
276 count
+= tb
->switches
[depth
].count
[i
];
281 tb_write(tb
, sw
, nodesize
);
284 tb
->switches
[depth
].len
= 0; /* clear this node */
292 void triebuild_add(triebuild
*tb
, const char *pathname
,
293 const struct trie_file
*file
)
295 int pathlen
= strlen(pathname
);
298 if (tb
->maxpathlen
< pathlen
+1)
299 tb
->maxpathlen
= pathlen
+1;
306 * Find the first differing character between this pathname
307 * and the previous one.
309 int ret
= triecmp(tb
->lastpath
, pathname
, &depth
);
313 * Finalise all nodes above this depth.
315 offset
= triebuild_unwind(tb
, depth
+1, &count
);
318 * Add the final node we just acquired to the switch node
319 * at our chosen depth, creating it if it isn't already
322 if (tb
->switchsize
<= depth
) {
323 int oldsize
= tb
->switchsize
;
324 tb
->switchsize
= depth
* 3 / 2 + 64;
325 tb
->switches
= sresize(tb
->switches
, tb
->switchsize
,
327 while (oldsize
< tb
->switchsize
)
328 tb
->switches
[oldsize
++].len
= 0;
331 tb
->switches
[depth
].c
[tb
->switches
[depth
].len
] = tb
->lastpath
[depth
];
332 tb
->switches
[depth
].off
[tb
->switches
[depth
].len
] = offset
;
333 tb
->switches
[depth
].count
[tb
->switches
[depth
].len
] = count
;
334 tb
->switches
[depth
].len
++;
338 * Write out a leaf node for the new file, and remember its
342 struct trie_leaf leaf
;
344 leaf
.c
.type
= TRIE_LEAF
;
345 leaf
.file
= *file
; /* structure copy */
348 tb
->lastoff
= tb
->offset
;
349 tb_write(tb
, &leaf
, sizeof(leaf
));
353 * Store this pathname for comparison with the next one.
355 if (tb
->lastsize
< pathlen
+1) {
356 tb
->lastsize
= pathlen
* 3 / 2 + 64;
357 tb
->lastpath
= sresize(tb
->lastpath
, tb
->lastsize
, char);
359 strcpy(tb
->lastpath
, pathname
);
360 tb
->lastlen
= pathlen
;
363 int triebuild_finish(triebuild
*tb
)
365 struct trie_header th
;
367 th
.magic
= TRIE_MAGIC
;
368 th
.root
= triebuild_unwind(tb
, 0, &th
.count
);
370 th
.maxpathlen
= tb
->maxpathlen
;
371 th
.pathsep
= (unsigned char)pathsep
;
374 tb_write(tb
, &th
, sizeof(th
));
379 void triebuild_free(triebuild
*tb
)
386 /* ----------------------------------------------------------------------
387 * Memory-mapped trie modification.
390 #define MNODE(t, off, type) \
391 ((struct type *)((char *)(t) + (off)))
393 static unsigned long long fake_atime_recurse(void *t
, struct trie_common
*node
,
394 int last_seen_pathsep
)
396 while (node
->type
== TRIE_STRING
) {
397 struct trie_string
*st
= (struct trie_string
*)node
;
398 last_seen_pathsep
= (st
->string
[st
->stringlen
-1] == pathsep
);
399 node
= MNODE(t
, st
->subnode
, trie_common
);
402 if (node
->type
== TRIE_LEAF
) {
403 struct trie_leaf
*leaf
= (struct trie_leaf
*)node
;
404 return leaf
->file
.atime
;
405 } else if (assert(node
->type
== TRIE_SWITCH
), 1) {
406 struct trie_switch
*sw
= (struct trie_switch
*)node
;
407 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
408 unsigned long long max
= 0, subdir
, ret
;
410 int slashindex
= -1, bareindex
= -1;
413 * First, process all the children of this node whose
414 * switch characters are not \0 or pathsep. We do this in
415 * reverse order so as to maintain best cache locality
416 * (tracking generally backwards through the file), though
417 * it doesn't matter semantically.
419 * For each of these children, we're just recursing into
420 * it to do any fixups required below it, and amalgamating
421 * the max atimes we get back.
423 for (i
= sw
->len
; i
-- > 0 ;) {
424 if (chars
[i
] == '\0') {
426 } else if (chars
[i
] == pathsep
) {
429 ret
= fake_atime_recurse(t
, MNODE(t
, sw
->sw
[i
].subnode
,
437 * Now we have at most two child nodes left to deal with:
438 * one with a slash (or general pathsep) and one with \0.
440 * If there's a slash node and a bare node, then the slash
441 * node contains precisely everything inside the directory
442 * described by the bare node; so we must retrieve the max
443 * atime for the slash node and use it to fix up the bare
446 * If there's only a bare node but the pathname leading up
447 * to this point ends in a slash, then _all_ of the child
448 * nodes of this node contain stuff inside the directory
449 * described by the bare node; so we use the whole of the
450 * maximum value we've computed so far to update the bare
453 if (slashindex
>= 0) {
454 ret
= fake_atime_recurse(t
, MNODE(t
, sw
->sw
[slashindex
].subnode
,
460 } else if (last_seen_pathsep
) {
463 /* Don't update the bare subnode at all. */
467 if (bareindex
>= 0) {
468 struct trie_leaf
*leaf
;
470 leaf
= MNODE(t
, sw
->sw
[bareindex
].subnode
, trie_leaf
);
472 if (leaf
&& leaf
->c
.type
== TRIE_LEAF
) {
473 if (leaf
->file
.atime
< subdir
)
474 leaf
->file
.atime
= subdir
;
475 ret
= leaf
->file
.atime
;
477 /* Shouldn't really happen, but be cautious anyway */
478 ret
= fake_atime_recurse(t
, &leaf
->c
, 0);
487 return 0; /* placate lint */
490 void trie_fake_dir_atimes(void *t
)
492 struct trie_header
*hdr
= MNODE(t
, 0, trie_header
);
493 struct trie_common
*node
= MNODE(t
, hdr
->root
, trie_common
);
495 fake_atime_recurse(t
, node
, 1);
498 /* ----------------------------------------------------------------------
499 * Querying functions.
502 #define NODE(t, off, type) \
503 ((const struct type *)((const char *)(t) + (off)))
505 size_t trie_maxpathlen(const void *t
)
507 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
508 return hdr
->maxpathlen
;
511 unsigned long trie_before(const void *t
, const char *pathname
)
513 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
514 int ret
= 0, lastcount
= hdr
->count
;
515 int len
= 1 + strlen(pathname
), depth
= 0;
516 off_t off
= hdr
->root
;
519 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
520 if (node
->type
== TRIE_LEAF
) {
522 ret
+= lastcount
; /* _shouldn't_ happen, but in principle */
524 } else if (node
->type
== TRIE_STRING
) {
525 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
528 int cmp
= triencmp(st
->string
, st
->stringlen
,
529 pathname
+ depth
, len
-depth
, &offset
);
531 if (offset
< st
->stringlen
) {
537 depth
+= st
->stringlen
;
539 } else if (node
->type
== TRIE_SWITCH
) {
540 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
541 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
544 for (i
= 0; i
< sw
->len
; i
++) {
546 int cmp
= trieccmp(pathname
[depth
], c
);
548 ret
+= sw
->sw
[i
].subcount
;
552 off
= sw
->sw
[i
].subnode
;
553 lastcount
= sw
->sw
[i
].subcount
;
564 void trie_getpath(const void *t
, unsigned long n
, char *buf
)
566 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
568 off_t off
= hdr
->root
;
571 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
572 if (node
->type
== TRIE_LEAF
) {
573 assert(depth
> 0 && buf
[depth
-1] == '\0');
575 } else if (node
->type
== TRIE_STRING
) {
576 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
578 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
579 depth
+= st
->stringlen
;
581 } else if (node
->type
== TRIE_SWITCH
) {
582 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
583 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
586 for (i
= 0; i
< sw
->len
; i
++) {
587 if (n
< sw
->sw
[i
].subcount
) {
588 buf
[depth
++] = chars
[i
];
589 off
= sw
->sw
[i
].subnode
;
592 n
-= sw
->sw
[i
].subcount
;
599 const struct trie_file
*trie_getfile(const void *t
, unsigned long n
)
601 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
602 off_t off
= hdr
->root
;
605 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
606 if (node
->type
== TRIE_LEAF
) {
607 const struct trie_leaf
*leaf
= NODE(t
, off
, trie_leaf
);
609 } else if (node
->type
== TRIE_STRING
) {
610 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
612 } else if (node
->type
== TRIE_SWITCH
) {
613 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
616 for (i
= 0; i
< sw
->len
; i
++) {
617 if (n
< sw
->sw
[i
].subcount
) {
618 off
= sw
->sw
[i
].subnode
;
621 n
-= sw
->sw
[i
].subcount
;
628 unsigned long trie_count(const void *t
)
630 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
634 char trie_pathsep(const void *t
)
636 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
637 return (char)hdr
->pathsep
;
640 struct triewalk_switch
{
641 const struct trie_switch
*sw
;
642 int pos
, depth
, count
;
646 struct triewalk_switch
*switches
;
647 int nswitches
, switchsize
;
650 triewalk
*triewalk_new(const void *vt
)
652 triewalk
*tw
= snew(triewalk
);
654 tw
->t
= (const char *)vt
;
663 void triewalk_rebase(triewalk
*tw
, const void *t
)
665 ptrdiff_t diff
= ((const unsigned char *)t
- (const unsigned char *)(tw
->t
));
670 for (i
= 0; i
< tw
->nswitches
; i
++)
671 tw
->switches
[i
].sw
= (const struct trie_switch
*)
672 ((const unsigned char *)(tw
->switches
[i
].sw
) + diff
);
675 const struct trie_file
*triewalk_next(triewalk
*tw
, char *buf
)
680 if (tw
->nswitches
< 0) {
681 const struct trie_header
*hdr
= NODE(tw
->t
, 0, trie_header
);
688 const struct trie_switch
*sw
;
691 if (tw
->nswitches
== 0) {
692 assert(tw
->count
== NODE(tw
->t
, 0, trie_header
)->count
);
693 return NULL
; /* run out of trie */
696 swpos
= tw
->switches
[tw
->nswitches
-1].pos
;
697 sw
= tw
->switches
[tw
->nswitches
-1].sw
;
698 chars
= (const char *)&sw
->sw
[sw
->len
];
700 if (swpos
< sw
->len
) {
701 depth
= tw
->switches
[tw
->nswitches
-1].depth
;
702 off
= sw
->sw
[swpos
].subnode
;
704 buf
[depth
++] = chars
[swpos
];
705 assert(tw
->count
== tw
->switches
[tw
->nswitches
-1].count
);
706 tw
->switches
[tw
->nswitches
-1].count
+= sw
->sw
[swpos
].subcount
;
707 tw
->switches
[tw
->nswitches
-1].pos
++;
716 const struct trie_common
*node
= NODE(tw
->t
, off
, trie_common
);
717 if (node
->type
== TRIE_LEAF
) {
718 const struct trie_leaf
*lf
= NODE(tw
->t
, off
, trie_leaf
);
720 assert(depth
> 0 && buf
[depth
-1] == '\0');
723 } else if (node
->type
== TRIE_STRING
) {
724 const struct trie_string
*st
= NODE(tw
->t
, off
, trie_string
);
727 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
728 depth
+= st
->stringlen
;
730 } else if (node
->type
== TRIE_SWITCH
) {
731 const struct trie_switch
*sw
= NODE(tw
->t
, off
, trie_switch
);
732 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
734 if (tw
->nswitches
>= tw
->switchsize
) {
735 tw
->switchsize
= tw
->nswitches
* 3 / 2 + 32;
736 tw
->switches
= sresize(tw
->switches
, tw
->switchsize
,
737 struct triewalk_switch
);
740 tw
->switches
[tw
->nswitches
].sw
= sw
;
741 tw
->switches
[tw
->nswitches
].pos
= 1;
742 tw
->switches
[tw
->nswitches
].depth
= depth
;
743 tw
->switches
[tw
->nswitches
].count
= tw
->count
+ sw
->sw
[0].subcount
;
744 off
= sw
->sw
[0].subnode
;
746 buf
[depth
++] = chars
[0];
751 void triewalk_free(triewalk
*tw
)
757 void trie_set_index_offset(void *t
, off_t ptr
)
759 ((struct trie_header
*)t
)->indexroot
= ptr
;
761 off_t
trie_get_index_offset(const void *t
)
763 return ((const struct trie_header
*)t
)->indexroot
;
766 void make_successor(char *pathbuf
)
768 int len
= strlen(pathbuf
);
769 if (len
> 0 && pathbuf
[len
-1] == pathsep
)
771 pathbuf
[len
] = '\001';
772 pathbuf
[len
+1] = '\0';