2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
48 #define SFSTREE_VERSION 0x0100
50 typedef uintptr_t Opaque; /* XXX sizeof(Opaque) == sizeof(void *) */
52 #define CHECK_MEMORY(tree) ( ( (tree)->plainmemory ) ? \
53 tlog(TL_CRIT|TL_EXIT, "Tree in plain memory - read only access") : (void)0 )
56 getNodeSize(SFSTree *info, SFSNode *node) {
61 size += info->datasize;
63 size += sizeof(SFSNode*);
68 SFSNodeData *data = node->data;
70 size += sizeof(SFSNodeData) * node->nchar;
71 size += sizeof(SFSNode*) * node->nchild;
73 for(i=0;i<node->nchar;i++)
74 nfound += data[i].isword;
76 size += nfound*info->datasize;
79 return PTRALIGN(size);
82 static __inline__ SFSNode*
83 getChildPointer(SFSTree *info, SFSNodeData *nodedata) {
84 char *p = ((char*)nodedata) + nodedata->child;
86 if ( info->plainmemory )
87 return (SFSNode*) ( ((char*)(info->node)) + *(Opaque*)p );
92 static __inline__ SFSNode*
93 getSkipChildPointer(SFSTree *info, SFSNode *node) {
94 if ( info->plainmemory )
95 return (SFSNode*) ( ((char*)(info->node)) + *(Opaque*)node->data );
97 return *(SFSNode**)( (char*)(node->data) );
100 static SFSNode* enlargeNode(SFSTree *info, SFSNode* node, u_int8_t val, int flag, SFSNodeData **nd);
101 static SFSNode* addRecord(SFSTree *info, SFSNode* node, SFSDataIO *in, int level);
103 #define STRNCMP(p1,p2,n) ( ((n)==1) ? ( *((char*)(p1))==*((char*)(p2)) ) : (strncmp((char*)(p1), (char*)(p2), (n))==0) )
106 SFSInit_dp(SFSTree *info, u_int32_t datasize, SFSDataIO *in) {
107 if ( datasize % sizeof(u_int32_t) )
108 tlog(TL_ALARM|TL_EXIT,"SFSInit_dp: datasize(%d) should be divided by sizeof(u_int32_t)", datasize);
111 info=(SFSTree*)tmalloc(sizeof(SFSTree));
112 memset(info,0,sizeof(SFSTree));
114 info->datasize = datasize;
116 while(in && in->key) {
125 SFSInit_c(SFSTree *info, char **in) {
130 info=(SFSTree*)tmalloc(sizeof(SFSTree));
131 memset(info,0,sizeof(SFSTree));
143 #define ISEND(p,w,l) ( (l>0) ? ( ((char*)(p))-(w) >= (l) ) : ( *(p) == '\0' ) )
146 SFSFindData(SFSTree *info, char *word, int len) {
147 SFSNode *node = info->node;
148 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
149 u_int8_t *ptr =(u_int8_t*)word;
151 while( node && !ISEND(ptr, word, len) ) {
152 if ( node->isskip ) {
154 if ( len>0 && len - (((char*)ptr) - word) > node->nchar )
156 else */ if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
158 if ( ISEND(ptr, word, len) && node->isword) {
159 return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
160 } else if ( node->haschild ) {
161 node = getSkipChildPointer(info, node);
168 StopLow = node->data;
169 StopHigh = StopLow + node->nchar;
170 while (StopLow < StopHigh) {
171 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
172 if ( StopMiddle->val == *ptr ) {
174 if ( ISEND(ptr, word, len) && StopMiddle->isword ) {
175 return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
176 } else if ( StopMiddle->haschild ) {
177 node = getChildPointer(info, StopMiddle);
182 } else if ( StopMiddle->val < *ptr ) {
183 StopLow = StopMiddle + 1;
185 StopHigh = StopMiddle;
188 if ( StopLow >= StopHigh )
196 freeFSFNode(SFSTree *info, SFSNode *node, void (*freefunc)(void*)) {
199 if ( node->isskip ) {
201 freeFSFNode(info, *(SFSNode**)(node->data), freefunc);
202 if (freefunc && node->isword && info->datasize)
203 (*freefunc)( (void*)( ((char*)(node->data))+ (node->haschild) ? sizeof(SFSNode*) : 0 ) );
205 SFSNodeData *nd = node->data;
206 char *data= ((char*)node) + node->dataptr;
208 for(i=0;i<node->nchar;i++) {
210 freeFSFNode(info, *(SFSNode**)( ((char*)nd) + nd->child ), freefunc);
211 if (freefunc && nd->isword && info->datasize) {
212 (*freefunc)( (void*)data );
213 data+=info->datasize;
223 SFSFree(SFSTree *info, void (*freefunc)(void*)) {
224 SFSNodeStack *s=info->stack;
226 if (info->node && !info->plainmemory) freeFSFNode(info, info->node, freefunc);
227 if (info->buf) tfree(info->buf);
240 makeSkipNode(SFSTree *info, SFSDataIO *io, int level) {
248 len = (io->keylen > 255) ? 255 : io->keylen;
249 size = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize) + len;
250 size = PTRALIGN(size);
252 res = (SFSNode*)tmalloc(size);
258 res->dataptr = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize);
259 memcpy(((char*)res) + res->dataptr, io->key, len);
261 if ( io->keylen > 255 ) {
264 io->key = io->key+len;
265 io->keylen = io->keylen - len;
266 *(SFSNode**)(res->data) = makeSkipNode(info, io, 0);
267 io->key = io->key-len;
268 io->keylen = io->keylen + len;
272 if (info->datasize) {
273 memcpy( res->data, io->data, info->datasize );
284 splitSkipNode(SFSTree *info, SFSNode* node, SFSDataIO *io, int level) {
290 tassert(node->isskip);
294 s1=((char*)node) + node->dataptr;
297 while( s1 - (((char*)node) + node->dataptr) < node->nchar && s2 - io->key < io->keylen && *s1==*s2) {
302 prefixlen = s2 - io->key;
304 if ( prefixlen==0 ) {
305 if ( node->nchar == 1 ) {
306 int flag = EN_VAL | ((node->isword) ? EN_DATA : 0) | ((node->haschild) ? EN_CHLD : 0);
307 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), flag, &ndata);
308 if ( node->isword ) {
309 if ( info->datasize )
311 ((char*)res) + res->dataptr + info->datasize * ndata->data,
312 ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0),
316 if ( node->haschild )
317 *(SFSNode**)( ((char*)ndata) + ndata->child ) = *(SFSNode**)( node->data );
318 info->totalen -= getNodeSize(info, node);
324 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), EN_VAL|EN_CHLD, &ndata);
326 size = getNodeSize(info,node);
330 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + 1, node->nchar);
332 size = getNodeSize(info,node);
335 *(SFSNode**)( ((char*)ndata) + ndata->child ) = (SFSNode*)trealloc(node, size);
337 res = addRecord(info, res, io, 0);
338 } else if (prefixlen==io->keylen) {
339 if (prefixlen==node->nchar) {
340 if ( node->isword || info->datasize==0) {
342 memcpy( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNodeData*) : 0),
348 int osize = PTRALIGN(SFSNHRDSZ + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
349 int nsize = PTRALIGN(SFSNHRDSZ + info->datasize + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
351 info->totalen += nsize - osize;
353 res=(SFSNode*)trealloc(node,nsize);
354 res->dataptr=SFSNHRDSZ + info->datasize + ((res->haschild) ? sizeof(SFSNodeData*) : 0);
356 memmove(((char*)res) + res->dataptr,
357 ((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), res->nchar);
358 memcpy(((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), io->data, info->datasize);
361 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*) + prefixlen;
362 size = PTRALIGN(size);
366 res = (SFSNode*)tmalloc(size);
370 res->nchar = prefixlen;
371 res->dataptr = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*);
372 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
374 memcpy(((char*)(res->data)) + sizeof(SFSNodeData*), io->data, info->datasize);
375 info->totalen-=getNodeSize(info,node);
376 node->nchar-=prefixlen;
377 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
378 size = getNodeSize(info,node);
380 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
382 } else if ( prefixlen==node->nchar ) {
383 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNode*) + node->nchar;
384 info->totalen+=sizeof(SFSNode*);
385 res=(SFSNode*)trealloc(node,size);
386 memmove( ((char*)(res->data)) + sizeof(SFSNode*), res->data, info->datasize + res->nchar);
388 res->dataptr+=sizeof(SFSNode*);
389 *(SFSNode**)(res->data) = makeSkipNode(info, io, prefixlen);
391 int size = SFSNHRDSZ + sizeof(SFSNodeData*) + prefixlen;
392 size = PTRALIGN(size);
395 res = (SFSNode*)tmalloc(size);
399 res->nchar = prefixlen;
400 res->dataptr = SFSNHRDSZ + sizeof(SFSNodeData*);
401 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
403 info->totalen-= getNodeSize(info,node);
404 node->nchar-=prefixlen;
406 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
408 size = getNodeSize(info,node);
409 info->totalen+= size;
411 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
412 *(SFSNode**)(res->data) = splitSkipNode(info, *(SFSNode**)(res->data), io, prefixlen);
422 enlargeNode(SFSTree *info, SFSNode* node, u_int8_t val, int flag, SFSNodeData **nd) {
423 u_int32_t nchild=0, nchar=0, nfound=0, i;
435 for(i=0;i<nchar;i++) {
436 nfound += data->isword;
442 /*info->totalen -= PTRALIGN(SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize);*/
443 info->totalen -= getNodeSize(info, node);
445 if ( flag & EN_VAL ) nchar++;
446 if ( flag & EN_CHLD ) nchild++;
447 if ( flag & EN_DATA ) nfound++;
450 sizenode = SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize;
451 sizenode = PTRALIGN(sizenode);
453 info->totalen+=sizenode;
454 if ( !node ) info->nnodes++;
455 rs=(SFSNode*)tmalloc(sizenode);
460 rs->dataptr=SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*);
461 dataptr=((char*)rs) + rs->dataptr;
462 chld = (SFSNode**)( ((char*)rs)+SFSNHRDSZ+nchar*sizeof(SFSNodeData) );
466 SFSNode **ochld=(SFSNode**)( ((char*)node)+SFSNHRDSZ+node->nchar*sizeof(SFSNodeData) );
467 SFSNodeData *odata = node->data;
468 char *odataptr=((char*)node) + node->dataptr;
471 for(i=0;i<node->nchar;i++) {
472 if ( val > odata->val ) {
473 *(u_int32_t*)data = *(u_int32_t*)odata;
474 if ( odata->haschild ) {
476 data->child = ((char*)chld) - ((char*)data);
479 if ( odata->isword && info->datasize ) {
480 memcpy(dataptr, odataptr, info->datasize);
481 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
482 dataptr += info->datasize; odataptr += info->datasize;
485 } else if ( val == odata->val ) {
486 tassert ( (flag&EN_VAL)==0 );
487 *(u_int32_t*)data = *(u_int32_t*)odata;
489 if ( odata->haschild || flag & EN_CHLD ) {
490 if (odata->haschild) *chld=*ochld;
491 data->child = ((char*)chld) - ((char*)data);
493 chld++; if (odata->haschild) ochld++;
496 if ( odata->isword || flag & EN_DATA ) {
498 if ( info->datasize && odata->isword ) {
499 memcpy(dataptr, odataptr, info->datasize);
500 odataptr += info->datasize;
502 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
503 dataptr += info->datasize;
510 tassert ( flag&EN_VAL );
512 if ( flag & EN_CHLD ) {
514 data->child = ((char*)chld) - ((char*)data);
518 if ( flag & EN_DATA ) {
520 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
521 dataptr += info->datasize;
529 *(u_int32_t*)data = *(u_int32_t*)odata;
530 if ( odata->haschild ) {
532 data->child = ((char*)chld) - ((char*)data);
535 if ( odata->isword && info->datasize ) {
536 memcpy(dataptr, odataptr, info->datasize);
537 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
538 dataptr += info->datasize; odataptr += info->datasize;
545 tassert ( flag&EN_VAL );
547 if ( flag & EN_CHLD ) {
549 data->child = ((char*)chld) - ((char*)data);
553 if ( flag & EN_DATA ) {
555 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
556 dataptr += info->datasize;
562 tassert ( flag & EN_VAL );
564 if ( flag & EN_CHLD ) {
566 data->child = ((char*)chld) - ((char*)data);
569 if ( flag & EN_DATA ) {
571 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
576 if (node) tfree(node);
581 addRecord(SFSTree *info, SFSNode* node, SFSDataIO *in, int level) {
582 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
583 u_int8_t *ptr = ((u_int8_t*)in->key) + level;
586 if ( node->isskip ) {
587 if ( node->haschild && in->keylen-level > node->nchar &&
588 strncmp(in->key+level, ((char*)node)+node->dataptr, node->nchar)==0 )
589 *(SFSNode**)( node->data ) = addRecord(info, *(SFSNode**)( node->data ), in, level+node->nchar);
591 node = splitSkipNode(info, node, in, level);
593 StopLow = node->data;
594 StopHigh = StopLow + node->nchar;
595 while (StopLow < StopHigh) {
596 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
597 if ( StopMiddle->val == *ptr ) {
598 if ( *(ptr+1)=='\0' ) {
599 if ( StopMiddle->isword ) {
601 if ( info->datasize )
602 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
603 in->data, info->datasize );
606 if (info->datasize) {
607 node = enlargeNode(info, node, *ptr, EN_DATA, &StopMiddle);
608 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
609 in->data, info->datasize );
611 StopMiddle->isword = 1;
613 } else if ( StopMiddle->haschild ) {
614 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
615 addRecord(info, *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ), in, level+1);
617 node = enlargeNode(info, node, *ptr, EN_CHLD, &StopMiddle);
618 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
619 makeSkipNode(info, in, level+1);
622 } else if ( StopMiddle->val < *ptr ) {
623 StopLow = StopMiddle + 1;
625 StopHigh = StopMiddle;
628 if ( *(ptr+1)=='\0' ) {
629 node = enlargeNode(info, node, *ptr, EN_VAL|EN_DATA, &StopMiddle);
630 if ( info->datasize )
631 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
632 in->data, info->datasize );
634 node = enlargeNode(info, node, *ptr, EN_VAL|EN_CHLD, &StopMiddle);
635 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
636 makeSkipNode(info, in, level+1);
640 node = makeSkipNode(info, in, level);
647 SFSAdd(SFSTree *info, SFSDataIO *in) {
650 in->keylen=strlen(in->key);
651 info->node = addRecord(info, info->node, in, 0);
655 pushStack(SFSNodeStack *top, SFSNode *node, int level ) {
656 SFSNodeStack *r=(SFSNodeStack*)tmalloc(sizeof(SFSNodeStack));
669 SFSIteratorStart(SFSTree *info) {
672 info->buf = (char*)tmalloc(info->tlen);
674 info->stack = pushStack(NULL, info->node, 0);
679 SFSPrefixIteratorStart(SFSTree *info, char *word) {
680 SFSNode *node = info->node;
681 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
682 u_int8_t *ptr =(u_int8_t*)word;
683 int len,wlen=strlen(word);
685 if ( wlen+1>=info->tlen ) {
687 info->buf = (info->buf) ?
688 (char*)trealloc(info->buf,info->tlen)
690 (char*)tmalloc(info->tlen);
694 while( node && *ptr) {
695 len = wlen - (((char*)ptr)-word);
696 if ( node->isskip ) {
697 if ( STRNCMP(ptr, ((char*)node)+node->dataptr, (len<node->nchar) ? len : node->nchar) ) {
698 if ( len<=node->nchar ) {
699 strcpy(info->buf,word);
700 info->stack = pushStack(NULL, node, ((char*)ptr) - word);
702 } else if ( node->haschild ) {
704 node = getSkipChildPointer(info, node);
711 StopLow = node->data;
712 StopHigh = StopLow + node->nchar;
713 while (StopLow < StopHigh) {
714 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
715 if ( StopMiddle->val == *ptr ) {
716 if ( *(ptr+1)=='\0' ) {
717 len =((char*)ptr)-word+1;
718 strcpy(info->buf,word);
719 if ( StopMiddle->isword ) {
721 info->wdata = (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
723 if ( StopMiddle->haschild )
724 info->stack = pushStack(NULL, getChildPointer( info, StopMiddle ), len);
726 } else if ( StopMiddle->haschild ) {
727 node = getChildPointer( info, StopMiddle );
733 } else if ( StopMiddle->val < *ptr ) {
734 StopLow = StopMiddle + 1;
736 StopHigh = StopMiddle;
739 if ( StopLow >= StopHigh )
747 SFSIterate(SFSTree *info, SFSDataIO *out) {
748 SFSNodeStack *s=info->stack;
750 if ( info->hasword ) {
751 out->key = info->buf;
752 out->keylen = strlen(out->key);
753 out->data = info->wdata;
758 if ( s == NULL || s->node == NULL)
761 while ( s->level + s->node->nchar + 1 >= info->tlen ) {
763 info->buf = (char*)trealloc(info->buf, info->tlen);
766 if ( s->node->isskip ) {
767 memcpy( info->buf + s->level, ((char*)s->node) + s->node->dataptr, s->node->nchar );
768 if ( s->node->isword && !s->checkedval) {
769 info->buf[ s->level+s->node->nchar ] = '\0';
770 out->key = info->buf;
771 out->keylen = s->level+s->node->nchar;
772 out->data =((char*)(s->node->data)) + ((s->node->haschild) ? sizeof(SFSNode*) : 0);
776 if ( s->node->haschild && !s->checkedchild) {
777 info->stack = pushStack(s, getSkipChildPointer(info, s->node), s->level+s->node->nchar);
779 if ( SFSIterate(info, out) )
783 while( s->data - s->node->data < s->node->nchar ) {
784 info->buf[ s->level ] = (char)s->data->val;
785 if ( s->checkedval==0 && s->data->isword ) {
786 info->buf[ s->level+1 ] = '\0';
787 out->key = info->buf;
788 out->keylen = s->level+1;
789 out->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
793 if ( s->checkedchild==0 && s->data->haschild ) {
794 info->stack = pushStack(s,
795 getChildPointer( info, s->data ), s->level+1);
797 if ( SFSIterate(info, out) )
800 s->checkedval = s->checkedchild = 0;
804 info->stack = s->next;
807 return SFSIterate(info, out);
811 SFSRange(SFSTree *info, char *word, SFSDataIO *f, SFSDataIO *l) {
812 SFSNodeStack *s=info->stack;
814 SFSPrefixIteratorStart(info, word);
817 if ( SFSIterate(info, f) ) {
818 SFSNodeStack *sptr = info->stack, *stmp;
819 while( sptr && sptr!=s ) {
826 memcpy(l,f,sizeof(SFSDataIO));
834 while( f->keylen + s->level + 2 >= info->tlen ) {
836 info->buf = (char*)trealloc(info->buf, info->tlen);
840 l->key = info->buf + f->keylen + 1;
841 memcpy(l->key, f->key, f->keylen + 1);
844 while( f->keylen + 1 + s->level + s->node->nchar + 1 >= info->tlen ) {
846 info->buf = (char*)trealloc(info->buf, info->tlen);
848 if ( s->node->isskip ) {
849 memcpy(info->buf + f->keylen + 1 + s->level,
850 ((char*)(s->node))+s->node->dataptr, s->node->nchar);
851 s->level+=s->node->nchar;
852 if (s->node->haschild) {
853 s->node=getSkipChildPointer(info, s->node);
854 } else { /* if (s->node->isword) */
855 info->buf[ f->keylen + 1 + s->level ] = '\0';
856 l->data = (void*)(s->node->data);
857 l->keylen = s->level+1;
861 s->data = s->node->data + s->node->nchar - 1;
862 while( s->data - s->node->data >= 0 ) {
863 info->buf[ f->keylen + 1 + s->level ] = (char)s->data->val;
864 if ( s->data->haschild ) {
865 s->node = getChildPointer( info, s->data );
869 if ( s->data->isword ) {
870 info->buf[ f->keylen + 1 + s->level ] = '\0';
871 l->keylen = s->level+1;
872 l->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
881 l->key = info->buf + f->keylen + 1;
887 typedef struct WorkPlain {
894 plainNode(WorkPlain *wp, SFSNode *node) {
895 int size = getNodeSize(wp->info, node);
896 off_t myoffset = wp->offset;
898 memcpy( wp->node + wp->offset, node, size );
901 tassert( wp->offset <= wp->info->totalen );
903 if ( node->isskip ) {
905 *(Opaque*)(wp->node + myoffset + SFSNHRDSZ) =
906 plainNode(wp, getSkipChildPointer(wp->info, node));
908 SFSNodeData *nd = node->data;
911 for(i=0;i<node->nchar;i++) {
913 *(Opaque*)(wp->node + myoffset + ( ((char*)nd) - ((char*)node) ) + nd->child) =
914 plainNode(wp, getChildPointer( wp->info, nd ) );
925 SFSMakePlain(SFSTree *info, void *pointer) {
928 if ( info->plainmemory )
934 wp.node = (char*)pointer;
936 wp.node = (char*)tmalloc(sizeof(char*) * info->totalen);
938 plainNode(&wp, info->node);
939 tassert( wp.offset == info->totalen );
941 info->node = (SFSNode*)wp.node;
942 info->plainmemory = 1;
946 revertNode(SFSTree *info, SFSNode* node) {
947 int size = getNodeSize(info, node);
950 newnode = (SFSNode*)tmalloc( size );
951 memcpy(newnode, node, size);
953 if ( node->isskip ) {
955 *(SFSNode**)( (char*)(newnode->data) ) =
956 revertNode(info, getSkipChildPointer(info, node));
958 SFSNodeData *nd = node->data;
959 SFSNodeData *nnd = newnode->data;
962 for(i=0;i<node->nchar;i++) {
964 *(SFSNode**) (((char*)nnd) + nnd->child ) =
965 revertNode(info, getChildPointer( info, nd ));
974 SFSRevertPlain(SFSTree *info) {
975 void *pointer = info->node;
977 if (! info->plainmemory )
980 info->node = revertNode(info, info->node);
981 info->plainmemory = 0;
987 writeNode(WorkPlain *wp, int fd, SFSNode *node, u_int32_t extrasize) {
988 int size = getNodeSize(wp->info, node);
989 SFSNode *wnode = (SFSNode*)tmalloc(size);
990 off_t myoffset = wp->offset;
992 memcpy( wnode, node, size );
995 tassert( wp->offset <= wp->info->totalen );
997 if ( node->isskip ) {
999 *(Opaque*)( ((char*)wnode) + SFSNHRDSZ) =
1000 writeNode(wp, fd, getSkipChildPointer(wp->info, node), extrasize);
1002 SFSNodeData *nd = node->data;
1005 for(i=0;i<node->nchar;i++) {
1007 *(Opaque*)(((char*)wnode) + ( ((char*)nd) - ((char*)node) ) + nd->child) =
1008 writeNode(wp, fd, getChildPointer( wp->info, nd ), extrasize );
1013 if ( lseek(fd, myoffset + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1014 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1015 if ( write(fd, wnode, size) != size )
1016 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1024 SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize) {
1026 off_t size = info->totalen + SFSTDHSZ;
1027 SFSTreeDumpHeader dh;
1029 if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 )
1030 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", filename, strerror(errno));
1032 if ( flock(fd, LOCK_EX) < 0 )
1033 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1035 if ( extrasize == 0 )
1037 else if ( extradata == NULL )
1040 if ( lseek(fd, size + MAXALIGN(extrasize) , SEEK_SET) < 0 )
1041 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1043 dh.version = SFSTREE_VERSION;
1044 dh.opaquesize = sizeof(Opaque);
1045 dh.headersize = SFSTDHSZ;
1046 dh.datasize = info->datasize;
1047 dh.totalen = info->totalen;
1048 dh.nnodes = info->nnodes;
1049 dh.extrasize = extrasize;
1051 if ( lseek(fd, 0, SEEK_SET) < 0 )
1052 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1053 if ( write(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1054 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1057 if ( write(fd, extradata, extrasize) != extrasize )
1058 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1059 if ( extrasize != MAXALIGN(extrasize) ) {
1060 char dummy[8] = {0,0,0,0,0,0,0,0};
1061 if ( write(fd, dummy, MAXALIGN(extrasize) - extrasize ) != MAXALIGN(extrasize) - extrasize )
1062 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1064 extrasize = MAXALIGN(extrasize);
1068 if ( info->plainmemory ) {
1069 if ( write(fd, info->node, info->totalen) != info->totalen )
1070 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1077 writeNode(&wp, fd, info->node, extrasize);
1085 readNode(SFSTree *info, int fd, char *buf, int bufsize, int extrasize) {
1089 size = read(fd, buf, bufsize );
1090 if ( size < SFSNHRDSZ + sizeof(void*) )
1091 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1093 size = getNodeSize(info, (SFSNode*)buf);
1094 tassert( size <= bufsize );
1095 node = (SFSNode*)tmalloc( size );
1096 memcpy(node, buf, size);
1098 if ( node->isskip ) {
1099 if (node->haschild) {
1100 if ( lseek(fd, *(Opaque*)node->data + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1101 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1102 *(SFSNode**)( (char*)(node->data) ) =
1103 readNode(info, fd, buf, bufsize, extrasize);
1106 SFSNodeData *nd = node->data;
1109 for(i=0;i<node->nchar;i++) {
1111 if ( lseek(fd, *(Opaque*)(((char*)nd) + nd->child ) + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1112 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1113 *(SFSNode**) (((char*)nd) + nd->child ) =
1114 readNode(info, fd, buf, bufsize, extrasize);
1124 SFSReadDump(SFSTree *info, char *filename, void **extradata, u_int32_t *extrasize) {
1126 SFSTreeDumpHeader dh;
1135 memset(info,0,sizeof(SFSTree));
1137 if ( (fd = open(filename, O_RDONLY)) < 0 )
1138 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno));
1139 if ( flock(fd, LOCK_SH) < 0 )
1140 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1142 if ( read(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1143 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1145 if ( dh.version != SFSTREE_VERSION )
1146 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh.version);
1147 if ( dh.opaquesize != sizeof(Opaque) )
1148 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1149 if ( dh.headersize != SFSTDHSZ )
1150 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh.headersize);
1152 info->totalen = dh.totalen;
1153 info->nnodes = dh.nnodes;
1154 info->datasize = dh.datasize;
1156 if ( dh.extrasize ) {
1157 void *pointer = tmalloc( MAXALIGN(dh.extrasize) );
1159 if ( read(fd, pointer, MAXALIGN(dh.extrasize)) != MAXALIGN(dh.extrasize) )
1160 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1163 *extradata = pointer;
1168 *extrasize = dh.extrasize;
1171 /* allocate buffer with max allowed size */
1172 bufsize = SFSNHRDSZ + 256*(sizeof(SFSNodeData) + sizeof(void*) + info->datasize);
1173 buf = tmalloc( bufsize );
1174 info->node = readNode(info, fd, buf, bufsize, MAXALIGN(dh.extrasize));
1182 SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size, void **extradata, u_int32_t *extrasize) {
1183 SFSTreeDumpHeader *dh;
1190 memset(info,0,sizeof(SFSTree));
1192 if ( size && size < SFSTDHSZ )
1193 tlog(TL_CRIT|TL_EXIT, "Memsize too small");
1195 dh = (SFSTreeDumpHeader*)pointer;
1197 if ( dh->version != SFSTREE_VERSION )
1198 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh->version);
1199 if ( dh->opaquesize != sizeof(Opaque) )
1200 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1201 if ( dh->headersize != SFSTDHSZ )
1202 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh->headersize);
1203 if ( size && size != dh->totalen + SFSTDHSZ + dh->extrasize )
1204 tlog(TL_CRIT|TL_EXIT, "Memory size mismatch (should be %d but %d bytes)", dh->totalen + SFSTDHSZ + dh->extrasize , size);
1206 info->totalen = dh->totalen;
1207 info->nnodes = dh->nnodes;
1208 info->datasize = dh->datasize;
1209 info->plainmemory = 1;
1211 if ( dh->extrasize ) {
1213 *extradata = ((char*)pointer) + SFSTDHSZ;
1216 *extrasize = dh->extrasize;
1219 if ( info->totalen && info->nnodes )
1220 info->node = (SFSNode*)( ((char*)pointer) + SFSTDHSZ + MAXALIGN(dh->extrasize) );