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 a
= (a
== '\0' ?
'\0' : a
== pathsep ?
'\1' : a
+1);
19 b
= (b
== '\0' ?
'\0' : b
== pathsep ?
'\1' : b
+1);
20 return (int)a
- (int)b
;
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
[depth
-1].len
== 0))
223 if (odepth
> depth
) {
225 * Write out a string node.
227 size_t nodesize
= sizeof(struct trie_string
) + odepth
- depth
;
228 struct trie_string
*st
= (struct trie_string
*)smalloc(nodesize
);
229 st
->c
.type
= TRIE_STRING
;
230 st
->stringlen
= odepth
- depth
;
231 st
->subnode
= offset
;
232 memcpy(st
->string
, tb
->lastpath
+ depth
, odepth
- depth
);
235 tb_write(tb
, st
, nodesize
);
239 assert(depth
>= targetdepth
);
240 if (depth
<= targetdepth
)
244 * Now we expect to be sitting just below a switch node.
245 * Add our final entry to it and write it out.
249 struct trie_switch
*sw
;
252 int swlen
= tb
->switches
[depth
].len
;
257 tb
->switches
[depth
].c
[swlen
] = tb
->lastpath
[depth
];
258 tb
->switches
[depth
].off
[swlen
] = offset
;
259 tb
->switches
[depth
].count
[swlen
] = count
;
262 nodesize
= sizeof(struct trie_switch
) +
263 swlen
* sizeof(struct trie_switchentry
) + swlen
;
264 sw
= (struct trie_switch
*)smalloc(nodesize
);
265 chars
= (char *)&sw
->sw
[swlen
];
267 sw
->c
.type
= TRIE_SWITCH
;
270 for (i
= 0; i
< swlen
; i
++) {
271 sw
->sw
[i
].subnode
= tb
->switches
[depth
].off
[i
];
272 sw
->sw
[i
].subcount
= tb
->switches
[depth
].count
[i
];
273 chars
[i
] = tb
->switches
[depth
].c
[i
];
275 count
+= tb
->switches
[depth
].count
[i
];
280 tb_write(tb
, sw
, nodesize
);
283 tb
->switches
[depth
].len
= 0; /* clear this node */
291 void triebuild_add(triebuild
*tb
, const char *pathname
,
292 const struct trie_file
*file
)
294 int pathlen
= strlen(pathname
);
297 if (tb
->maxpathlen
< pathlen
+1)
298 tb
->maxpathlen
= pathlen
+1;
305 * Find the first differing character between this pathname
306 * and the previous one.
308 int ret
= triecmp(tb
->lastpath
, pathname
, &depth
);
312 * Finalise all nodes above this depth.
314 offset
= triebuild_unwind(tb
, depth
+1, &count
);
317 * Add the final node we just acquired to the switch node
318 * at our chosen depth, creating it if it isn't already
321 if (tb
->switchsize
<= depth
) {
322 int oldsize
= tb
->switchsize
;
323 tb
->switchsize
= depth
* 3 / 2 + 64;
324 tb
->switches
= sresize(tb
->switches
, tb
->switchsize
,
326 while (oldsize
< tb
->switchsize
)
327 tb
->switches
[oldsize
++].len
= 0;
330 tb
->switches
[depth
].c
[tb
->switches
[depth
].len
] = tb
->lastpath
[depth
];
331 tb
->switches
[depth
].off
[tb
->switches
[depth
].len
] = offset
;
332 tb
->switches
[depth
].count
[tb
->switches
[depth
].len
] = count
;
333 tb
->switches
[depth
].len
++;
337 * Write out a leaf node for the new file, and remember its
341 struct trie_leaf leaf
;
343 leaf
.c
.type
= TRIE_LEAF
;
344 leaf
.file
= *file
; /* structure copy */
347 tb
->lastoff
= tb
->offset
;
348 tb_write(tb
, &leaf
, sizeof(leaf
));
352 * Store this pathname for comparison with the next one.
354 if (tb
->lastsize
< pathlen
+1) {
355 tb
->lastsize
= pathlen
* 3 / 2 + 64;
356 tb
->lastpath
= sresize(tb
->lastpath
, tb
->lastsize
, char);
358 strcpy(tb
->lastpath
, pathname
);
359 tb
->lastlen
= pathlen
;
362 int triebuild_finish(triebuild
*tb
)
364 struct trie_header th
;
366 th
.magic
= TRIE_MAGIC
;
367 th
.root
= triebuild_unwind(tb
, 0, &th
.count
);
369 th
.maxpathlen
= tb
->maxpathlen
;
370 th
.pathsep
= (unsigned char)pathsep
;
373 tb_write(tb
, &th
, sizeof(th
));
378 void triebuild_free(triebuild
*tb
)
385 /* ----------------------------------------------------------------------
386 * Memory-mapped trie modification.
389 #define MNODE(t, off, type) \
390 ((struct type *)((char *)(t) + (off)))
392 static unsigned long long fake_atime_recurse(void *t
, struct trie_common
*node
,
393 int last_seen_pathsep
)
395 while (node
->type
== TRIE_STRING
) {
396 struct trie_string
*st
= (struct trie_string
*)node
;
397 last_seen_pathsep
= (st
->string
[st
->stringlen
-1] == pathsep
);
398 node
= MNODE(t
, st
->subnode
, trie_common
);
401 if (node
->type
== TRIE_LEAF
) {
402 struct trie_leaf
*leaf
= (struct trie_leaf
*)node
;
403 return leaf
->file
.atime
;
404 } else if (assert(node
->type
== TRIE_SWITCH
), 1) {
405 struct trie_switch
*sw
= (struct trie_switch
*)node
;
406 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
407 unsigned long long max
= 0, subdir
, ret
;
409 int slashindex
= -1, bareindex
= -1;
412 * First, process all the children of this node whose
413 * switch characters are not \0 or pathsep. We do this in
414 * reverse order so as to maintain best cache locality
415 * (tracking generally backwards through the file), though
416 * it doesn't matter semantically.
418 * For each of these children, we're just recursing into
419 * it to do any fixups required below it, and amalgamating
420 * the max atimes we get back.
422 for (i
= sw
->len
; i
-- > 0 ;) {
423 if (chars
[i
] == '\0') {
425 } else if (chars
[i
] == pathsep
) {
428 ret
= fake_atime_recurse(t
, MNODE(t
, sw
->sw
[i
].subnode
,
436 * Now we have at most two child nodes left to deal with:
437 * one with a slash (or general pathsep) and one with \0.
439 * If there's a slash node and a bare node, then the slash
440 * node contains precisely everything inside the directory
441 * described by the bare node; so we must retrieve the max
442 * atime for the slash node and use it to fix up the bare
445 * If there's only a bare node but the pathname leading up
446 * to this point ends in a slash, then _all_ of the child
447 * nodes of this node contain stuff inside the directory
448 * described by the bare node; so we use the whole of the
449 * maximum value we've computed so far to update the bare
452 if (slashindex
>= 0) {
453 ret
= fake_atime_recurse(t
, MNODE(t
, sw
->sw
[slashindex
].subnode
,
459 } else if (last_seen_pathsep
) {
462 /* Don't update the bare subnode at all. */
466 if (bareindex
>= 0) {
467 struct trie_leaf
*leaf
;
469 leaf
= MNODE(t
, sw
->sw
[bareindex
].subnode
, trie_leaf
);
471 if (leaf
&& leaf
->c
.type
== TRIE_LEAF
) {
472 if (leaf
->file
.atime
< subdir
)
473 leaf
->file
.atime
= subdir
;
474 ret
= leaf
->file
.atime
;
476 /* Shouldn't really happen, but be cautious anyway */
477 ret
= fake_atime_recurse(t
, &leaf
->c
, 0);
488 void trie_fake_dir_atimes(void *t
)
490 struct trie_header
*hdr
= MNODE(t
, 0, trie_header
);
491 struct trie_common
*node
= MNODE(t
, hdr
->root
, trie_common
);
493 fake_atime_recurse(t
, node
, 1);
496 /* ----------------------------------------------------------------------
497 * Querying functions.
500 #define NODE(t, off, type) \
501 ((const struct type *)((const char *)(t) + (off)))
503 size_t trie_maxpathlen(const void *t
)
505 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
506 return hdr
->maxpathlen
;
509 unsigned long trie_before(const void *t
, const char *pathname
)
511 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
512 int ret
= 0, lastcount
= hdr
->count
;
513 int len
= 1 + strlen(pathname
), depth
= 0;
514 off_t off
= hdr
->root
;
517 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
518 if (node
->type
== TRIE_LEAF
) {
520 ret
+= lastcount
; /* _shouldn't_ happen, but in principle */
522 } else if (node
->type
== TRIE_STRING
) {
523 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
526 int cmp
= triencmp(st
->string
, st
->stringlen
,
527 pathname
+ depth
, len
-depth
, &offset
);
529 if (offset
< st
->stringlen
) {
535 depth
+= st
->stringlen
;
537 } else if (node
->type
== TRIE_SWITCH
) {
538 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
539 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
542 for (i
= 0; i
< sw
->len
; i
++) {
544 int cmp
= trieccmp(pathname
[depth
], c
);
546 ret
+= sw
->sw
[i
].subcount
;
550 off
= sw
->sw
[i
].subnode
;
551 lastcount
= sw
->sw
[i
].subcount
;
562 void trie_getpath(const void *t
, unsigned long n
, char *buf
)
564 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
566 off_t off
= hdr
->root
;
569 const struct trie_common
*node
= NODE(t
, off
, trie_common
);
570 if (node
->type
== TRIE_LEAF
) {
571 assert(depth
> 0 && buf
[depth
-1] == '\0');
573 } else if (node
->type
== TRIE_STRING
) {
574 const struct trie_string
*st
= NODE(t
, off
, trie_string
);
576 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
577 depth
+= st
->stringlen
;
579 } else if (node
->type
== TRIE_SWITCH
) {
580 const struct trie_switch
*sw
= NODE(t
, off
, trie_switch
);
581 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
584 for (i
= 0; i
< sw
->len
; i
++) {
585 if (n
< sw
->sw
[i
].subcount
) {
586 buf
[depth
++] = chars
[i
];
587 off
= sw
->sw
[i
].subnode
;
590 n
-= sw
->sw
[i
].subcount
;
597 unsigned long trie_count(const void *t
)
599 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
603 char trie_pathsep(const void *t
)
605 const struct trie_header
*hdr
= NODE(t
, 0, trie_header
);
606 return (char)hdr
->pathsep
;
609 struct triewalk_switch
{
610 const struct trie_switch
*sw
;
611 int pos
, depth
, count
;
615 struct triewalk_switch
*switches
;
616 int nswitches
, switchsize
;
619 triewalk
*triewalk_new(const void *vt
)
621 triewalk
*tw
= snew(triewalk
);
623 tw
->t
= (const char *)vt
;
632 void triewalk_rebase(triewalk
*tw
, const void *t
)
634 ptrdiff_t diff
= ((const unsigned char *)t
- (const unsigned char *)(tw
->t
));
639 for (i
= 0; i
< tw
->nswitches
; i
++)
640 tw
->switches
[i
].sw
= (const struct trie_switch
*)
641 ((const unsigned char *)(tw
->switches
[i
].sw
) + diff
);
644 const struct trie_file
*triewalk_next(triewalk
*tw
, char *buf
)
649 if (tw
->nswitches
< 0) {
650 const struct trie_header
*hdr
= NODE(tw
->t
, 0, trie_header
);
657 const struct trie_switch
*sw
;
660 if (tw
->nswitches
== 0) {
661 assert(tw
->count
== NODE(tw
->t
, 0, trie_header
)->count
);
662 return NULL
; /* run out of trie */
665 swpos
= tw
->switches
[tw
->nswitches
-1].pos
;
666 sw
= tw
->switches
[tw
->nswitches
-1].sw
;
667 chars
= (const char *)&sw
->sw
[sw
->len
];
669 if (swpos
< sw
->len
) {
670 depth
= tw
->switches
[tw
->nswitches
-1].depth
;
671 off
= sw
->sw
[swpos
].subnode
;
673 buf
[depth
++] = chars
[swpos
];
674 assert(tw
->count
== tw
->switches
[tw
->nswitches
-1].count
);
675 tw
->switches
[tw
->nswitches
-1].count
+= sw
->sw
[swpos
].subcount
;
676 tw
->switches
[tw
->nswitches
-1].pos
++;
685 const struct trie_common
*node
= NODE(tw
->t
, off
, trie_common
);
686 if (node
->type
== TRIE_LEAF
) {
687 const struct trie_leaf
*lf
= NODE(tw
->t
, off
, trie_leaf
);
689 assert(depth
> 0 && buf
[depth
-1] == '\0');
692 } else if (node
->type
== TRIE_STRING
) {
693 const struct trie_string
*st
= NODE(tw
->t
, off
, trie_string
);
696 memcpy(buf
+ depth
, st
->string
, st
->stringlen
);
697 depth
+= st
->stringlen
;
699 } else if (node
->type
== TRIE_SWITCH
) {
700 const struct trie_switch
*sw
= NODE(tw
->t
, off
, trie_switch
);
701 const char *chars
= (const char *)&sw
->sw
[sw
->len
];
703 if (tw
->nswitches
>= tw
->switchsize
) {
704 tw
->switchsize
= tw
->nswitches
* 3 / 2 + 32;
705 tw
->switches
= sresize(tw
->switches
, tw
->switchsize
,
706 struct triewalk_switch
);
709 tw
->switches
[tw
->nswitches
].sw
= sw
;
710 tw
->switches
[tw
->nswitches
].pos
= 1;
711 tw
->switches
[tw
->nswitches
].depth
= depth
;
712 tw
->switches
[tw
->nswitches
].count
= tw
->count
+ sw
->sw
[0].subcount
;
713 off
= sw
->sw
[0].subnode
;
715 buf
[depth
++] = chars
[0];
720 void triewalk_free(triewalk
*tw
)
726 void trie_set_index_offset(void *t
, off_t ptr
)
728 ((struct trie_header
*)t
)->indexroot
= ptr
;
730 off_t
trie_get_index_offset(const void *t
)
732 return ((const struct trie_header
*)t
)->indexroot
;
735 void make_successor(char *pathbuf
)
737 int len
= strlen(pathbuf
);
738 if (len
> 0 && pathbuf
[len
-1] == pathsep
)
740 pathbuf
[len
] = '\001';
741 pathbuf
[len
+1] = '\0';