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.
32 #include <sys/types.h>
47 #define SFSTREE_VERSION 0x0100
49 typedef uintptr_t Opaque; /* XXX sizeof(Opaque) == sizeof(void *) */
51 #define CHECK_MEMORY(tree) ( ( (tree)->plainmemory ) ? \
52 tlog(TL_CRIT|TL_EXIT, "Tree in plain memory - read only access") : (void)0 )
55 getNodeSize(SFSTree *info, SFSNode *node) {
60 size += info->datasize;
62 size += sizeof(SFSNode*);
67 SFSNodeData *data = node->data;
69 size += sizeof(SFSNodeData) * node->nchar;
70 size += sizeof(SFSNode*) * node->nchild;
72 for(i=0;i<node->nchar;i++)
73 nfound += data[i].isword;
75 size += nfound*info->datasize;
78 return PTRALIGN(size);
81 static __inline__ SFSNode*
82 getChildPointer(SFSTree *info, SFSNodeData *nodedata) {
83 char *p = ((char*)nodedata) + nodedata->child;
85 if ( info->plainmemory )
86 return (SFSNode*) ( ((char*)(info->node)) + *(Opaque*)p );
91 static __inline__ SFSNode*
92 getSkipChildPointer(SFSTree *info, SFSNode *node) {
93 if ( info->plainmemory )
94 return (SFSNode*) ( ((char*)(info->node)) + *(Opaque*)node->data );
96 return *(SFSNode**)( (char*)(node->data) );
99 static SFSNode* enlargeNode(SFSTree *info, SFSNode* node, u_int8_t val, int flag, SFSNodeData **nd);
100 static SFSNode* addRecord(SFSTree *info, SFSNode* node, SFSDataIO *in, int level);
102 #define STRNCMP(p1,p2,n) ( ((n)==1) ? ( *((char*)(p1))==*((char*)(p2)) ) : (strncmp((char*)(p1), (char*)(p2), (n))==0) )
105 SFSInit_dp(SFSTree *info, u_int32_t datasize, SFSDataIO *in) {
106 if ( datasize % sizeof(u_int32_t) )
107 tlog(TL_ALARM|TL_EXIT,"SFSInit_dp: datasize(%d) should be divided by sizeof(u_int32_t)", datasize);
110 info=(SFSTree*)tmalloc(sizeof(SFSTree));
111 memset(info,0,sizeof(SFSTree));
113 info->datasize = datasize;
115 while(in && in->key) {
124 SFSInit_c(SFSTree *info, char **in) {
129 info=(SFSTree*)tmalloc(sizeof(SFSTree));
130 memset(info,0,sizeof(SFSTree));
142 #define ISEND(p,w,l) ( (l>0) ? ( ((char*)(p))-(w) >= (l) ) : ( *(p) == '\0' ) )
145 SFSFindData(SFSTree *info, char *word, int len) {
146 SFSNode *node = info->node;
147 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
148 u_int8_t *ptr =(u_int8_t*)word;
150 while( node && !ISEND(ptr, word, len) ) {
151 if ( node->isskip ) {
153 if ( len>0 && len - (((char*)ptr) - word) > node->nchar )
155 else */ if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
157 if ( ISEND(ptr, word, len) && node->isword) {
158 return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
159 } else if ( node->haschild ) {
160 node = getSkipChildPointer(info, node);
167 StopLow = node->data;
168 StopHigh = StopLow + node->nchar;
169 while (StopLow < StopHigh) {
170 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
171 if ( StopMiddle->val == *ptr ) {
173 if ( ISEND(ptr, word, len) && StopMiddle->isword ) {
174 return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
175 } else if ( StopMiddle->haschild ) {
176 node = getChildPointer(info, StopMiddle);
181 } else if ( StopMiddle->val < *ptr ) {
182 StopLow = StopMiddle + 1;
184 StopHigh = StopMiddle;
187 if ( StopLow >= StopHigh )
195 freeFSFNode(SFSTree *info, SFSNode *node, void (*freefunc)(void*)) {
198 if ( node->isskip ) {
200 freeFSFNode(info, *(SFSNode**)(node->data), freefunc);
201 if (freefunc && node->isword && info->datasize)
202 (*freefunc)( (void*)( ((char*)(node->data))+ (node->haschild) ? sizeof(SFSNode*) : 0 ) );
204 SFSNodeData *nd = node->data;
205 char *data= ((char*)node) + node->dataptr;
207 for(i=0;i<node->nchar;i++) {
209 freeFSFNode(info, *(SFSNode**)( ((char*)nd) + nd->child ), freefunc);
210 if (freefunc && nd->isword && info->datasize) {
211 (*freefunc)( (void*)data );
212 data+=info->datasize;
222 SFSFree(SFSTree *info, void (*freefunc)(void*)) {
223 SFSNodeStack *s=info->stack;
225 if (info->node && !info->plainmemory) freeFSFNode(info, info->node, freefunc);
226 if (info->buf) tfree(info->buf);
239 makeSkipNode(SFSTree *info, SFSDataIO *io, int level) {
247 len = (io->keylen > 255) ? 255 : io->keylen;
248 size = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize) + len;
249 size = PTRALIGN(size);
251 res = (SFSNode*)tmalloc(size);
257 res->dataptr = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize);
258 memcpy(((char*)res) + res->dataptr, io->key, len);
260 if ( io->keylen > 255 ) {
263 io->key = io->key+len;
264 io->keylen = io->keylen - len;
265 *(SFSNode**)(res->data) = makeSkipNode(info, io, 0);
266 io->key = io->key-len;
267 io->keylen = io->keylen + len;
271 if (info->datasize) {
272 memcpy( res->data, io->data, info->datasize );
283 splitSkipNode(SFSTree *info, SFSNode* node, SFSDataIO *io, int level) {
289 tassert(node->isskip);
293 s1=((char*)node) + node->dataptr;
296 while( s1 - (((char*)node) + node->dataptr) < node->nchar && s2 - io->key < io->keylen && *s1==*s2) {
301 prefixlen = s2 - io->key;
303 if ( prefixlen==0 ) {
304 if ( node->nchar == 1 ) {
305 int flag = EN_VAL | ((node->isword) ? EN_DATA : 0) | ((node->haschild) ? EN_CHLD : 0);
306 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), flag, &ndata);
307 if ( node->isword ) {
308 if ( info->datasize )
310 ((char*)res) + res->dataptr + info->datasize * ndata->data,
311 ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0),
315 if ( node->haschild )
316 *(SFSNode**)( ((char*)ndata) + ndata->child ) = *(SFSNode**)( node->data );
317 info->totalen -= getNodeSize(info, node);
323 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), EN_VAL|EN_CHLD, &ndata);
325 size = getNodeSize(info,node);
329 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + 1, node->nchar);
331 size = getNodeSize(info,node);
334 *(SFSNode**)( ((char*)ndata) + ndata->child ) = (SFSNode*)trealloc(node, size);
336 res = addRecord(info, res, io, 0);
337 } else if (prefixlen==io->keylen) {
338 if (prefixlen==node->nchar) {
339 if ( node->isword || info->datasize==0) {
341 memcpy( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNodeData*) : 0),
347 int osize = PTRALIGN(SFSNHRDSZ + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
348 int nsize = PTRALIGN(SFSNHRDSZ + info->datasize + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
350 info->totalen += nsize - osize;
352 res=(SFSNode*)trealloc(node,nsize);
353 res->dataptr=SFSNHRDSZ + info->datasize + ((res->haschild) ? sizeof(SFSNodeData*) : 0);
355 memmove(((char*)res) + res->dataptr,
356 ((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), res->nchar);
357 memcpy(((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), io->data, info->datasize);
360 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*) + prefixlen;
361 size = PTRALIGN(size);
365 res = (SFSNode*)tmalloc(size);
369 res->nchar = prefixlen;
370 res->dataptr = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*);
371 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
373 memcpy(((char*)(res->data)) + sizeof(SFSNodeData*), io->data, info->datasize);
374 info->totalen-=getNodeSize(info,node);
375 node->nchar-=prefixlen;
376 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
377 size = getNodeSize(info,node);
379 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
381 } else if ( prefixlen==node->nchar ) {
382 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNode*) + node->nchar;
383 info->totalen+=sizeof(SFSNode*);
384 res=(SFSNode*)trealloc(node,size);
385 memmove( ((char*)(res->data)) + sizeof(SFSNode*), res->data, info->datasize + res->nchar);
387 res->dataptr+=sizeof(SFSNode*);
388 *(SFSNode**)(res->data) = makeSkipNode(info, io, prefixlen);
390 int size = SFSNHRDSZ + sizeof(SFSNodeData*) + prefixlen;
391 size = PTRALIGN(size);
394 res = (SFSNode*)tmalloc(size);
398 res->nchar = prefixlen;
399 res->dataptr = SFSNHRDSZ + sizeof(SFSNodeData*);
400 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
402 info->totalen-= getNodeSize(info,node);
403 node->nchar-=prefixlen;
405 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
407 size = getNodeSize(info,node);
408 info->totalen+= size;
410 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
411 *(SFSNode**)(res->data) = splitSkipNode(info, *(SFSNode**)(res->data), io, prefixlen);
421 enlargeNode(SFSTree *info, SFSNode* node, u_int8_t val, int flag, SFSNodeData **nd) {
422 u_int32_t nchild=0, nchar=0, nfound=0, i;
434 for(i=0;i<nchar;i++) {
435 nfound += data->isword;
441 /*info->totalen -= PTRALIGN(SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize);*/
442 info->totalen -= getNodeSize(info, node);
444 if ( flag & EN_VAL ) nchar++;
445 if ( flag & EN_CHLD ) nchild++;
446 if ( flag & EN_DATA ) nfound++;
449 sizenode = SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize;
450 sizenode = PTRALIGN(sizenode);
452 info->totalen+=sizenode;
453 if ( !node ) info->nnodes++;
454 rs=(SFSNode*)tmalloc(sizenode);
459 rs->dataptr=SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*);
460 dataptr=((char*)rs) + rs->dataptr;
461 chld = (SFSNode**)( ((char*)rs)+SFSNHRDSZ+nchar*sizeof(SFSNodeData) );
465 SFSNode **ochld=(SFSNode**)( ((char*)node)+SFSNHRDSZ+node->nchar*sizeof(SFSNodeData) );
466 SFSNodeData *odata = node->data;
467 char *odataptr=((char*)node) + node->dataptr;
470 for(i=0;i<node->nchar;i++) {
471 if ( val > odata->val ) {
472 *(u_int32_t*)data = *(u_int32_t*)odata;
473 if ( odata->haschild ) {
475 data->child = ((char*)chld) - ((char*)data);
478 if ( odata->isword && info->datasize ) {
479 memcpy(dataptr, odataptr, info->datasize);
480 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
481 dataptr += info->datasize; odataptr += info->datasize;
484 } else if ( val == odata->val ) {
485 tassert ( (flag&EN_VAL)==0 );
486 *(u_int32_t*)data = *(u_int32_t*)odata;
488 if ( odata->haschild || flag & EN_CHLD ) {
489 if (odata->haschild) *chld=*ochld;
490 data->child = ((char*)chld) - ((char*)data);
492 chld++; if (odata->haschild) ochld++;
495 if ( odata->isword || flag & EN_DATA ) {
497 if ( info->datasize && odata->isword ) {
498 memcpy(dataptr, odataptr, info->datasize);
499 odataptr += info->datasize;
501 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
502 dataptr += info->datasize;
509 tassert ( flag&EN_VAL );
511 if ( flag & EN_CHLD ) {
513 data->child = ((char*)chld) - ((char*)data);
517 if ( flag & EN_DATA ) {
519 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
520 dataptr += info->datasize;
528 *(u_int32_t*)data = *(u_int32_t*)odata;
529 if ( odata->haschild ) {
531 data->child = ((char*)chld) - ((char*)data);
534 if ( odata->isword && info->datasize ) {
535 memcpy(dataptr, odataptr, info->datasize);
536 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
537 dataptr += info->datasize; odataptr += info->datasize;
544 tassert ( flag&EN_VAL );
546 if ( flag & EN_CHLD ) {
548 data->child = ((char*)chld) - ((char*)data);
552 if ( flag & EN_DATA ) {
554 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
555 dataptr += info->datasize;
561 tassert ( flag & EN_VAL );
563 if ( flag & EN_CHLD ) {
565 data->child = ((char*)chld) - ((char*)data);
568 if ( flag & EN_DATA ) {
570 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
575 if (node) tfree(node);
580 addRecord(SFSTree *info, SFSNode* node, SFSDataIO *in, int level) {
581 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
582 u_int8_t *ptr = ((u_int8_t*)in->key) + level;
585 if ( node->isskip ) {
586 if ( node->haschild && in->keylen-level > node->nchar &&
587 strncmp(in->key+level, ((char*)node)+node->dataptr, node->nchar)==0 )
588 *(SFSNode**)( node->data ) = addRecord(info, *(SFSNode**)( node->data ), in, level+node->nchar);
590 node = splitSkipNode(info, node, in, level);
592 StopLow = node->data;
593 StopHigh = StopLow + node->nchar;
594 while (StopLow < StopHigh) {
595 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
596 if ( StopMiddle->val == *ptr ) {
597 if ( *(ptr+1)=='\0' ) {
598 if ( StopMiddle->isword ) {
600 if ( info->datasize )
601 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
602 in->data, info->datasize );
605 if (info->datasize) {
606 node = enlargeNode(info, node, *ptr, EN_DATA, &StopMiddle);
607 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
608 in->data, info->datasize );
610 StopMiddle->isword = 1;
612 } else if ( StopMiddle->haschild ) {
613 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
614 addRecord(info, *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ), in, level+1);
616 node = enlargeNode(info, node, *ptr, EN_CHLD, &StopMiddle);
617 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
618 makeSkipNode(info, in, level+1);
621 } else if ( StopMiddle->val < *ptr ) {
622 StopLow = StopMiddle + 1;
624 StopHigh = StopMiddle;
627 if ( *(ptr+1)=='\0' ) {
628 node = enlargeNode(info, node, *ptr, EN_VAL|EN_DATA, &StopMiddle);
629 if ( info->datasize )
630 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
631 in->data, info->datasize );
633 node = enlargeNode(info, node, *ptr, EN_VAL|EN_CHLD, &StopMiddle);
634 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
635 makeSkipNode(info, in, level+1);
639 node = makeSkipNode(info, in, level);
646 SFSAdd(SFSTree *info, SFSDataIO *in) {
649 in->keylen=strlen(in->key);
650 info->node = addRecord(info, info->node, in, 0);
654 pushStack(SFSNodeStack *top, SFSNode *node, int level ) {
655 SFSNodeStack *r=(SFSNodeStack*)tmalloc(sizeof(SFSNodeStack));
668 SFSIteratorStart(SFSTree *info) {
671 info->buf = (char*)tmalloc(info->tlen);
673 info->stack = pushStack(NULL, info->node, 0);
678 SFSPrefixIteratorStart(SFSTree *info, char *word) {
679 SFSNode *node = info->node;
680 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
681 u_int8_t *ptr =(u_int8_t*)word;
682 int len,wlen=strlen(word);
684 if ( wlen+1>=info->tlen ) {
686 info->buf = (info->buf) ?
687 (char*)trealloc(info->buf,info->tlen)
689 (char*)tmalloc(info->tlen);
693 while( node && *ptr) {
694 len = wlen - (((char*)ptr)-word);
695 if ( node->isskip ) {
696 if ( STRNCMP(ptr, ((char*)node)+node->dataptr, (len<node->nchar) ? len : node->nchar) ) {
697 if ( len<=node->nchar ) {
698 strcpy(info->buf,word);
699 info->stack = pushStack(NULL, node, ((char*)ptr) - word);
701 } else if ( node->haschild ) {
703 node = getSkipChildPointer(info, node);
710 StopLow = node->data;
711 StopHigh = StopLow + node->nchar;
712 while (StopLow < StopHigh) {
713 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
714 if ( StopMiddle->val == *ptr ) {
715 if ( *(ptr+1)=='\0' ) {
716 len =((char*)ptr)-word+1;
717 strcpy(info->buf,word);
718 if ( StopMiddle->isword ) {
720 info->wdata = (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
722 if ( StopMiddle->haschild )
723 info->stack = pushStack(NULL, getChildPointer( info, StopMiddle ), len);
725 } else if ( StopMiddle->haschild ) {
726 node = getChildPointer( info, StopMiddle );
732 } else if ( StopMiddle->val < *ptr ) {
733 StopLow = StopMiddle + 1;
735 StopHigh = StopMiddle;
738 if ( StopLow >= StopHigh )
746 SFSIterate(SFSTree *info, SFSDataIO *out) {
747 SFSNodeStack *s=info->stack;
749 if ( info->hasword ) {
750 out->key = info->buf;
751 out->keylen = strlen(out->key);
752 out->data = info->wdata;
757 if ( s == NULL || s->node == NULL)
760 while ( s->level + s->node->nchar + 1 >= info->tlen ) {
762 info->buf = (char*)trealloc(info->buf, info->tlen);
765 if ( s->node->isskip ) {
766 memcpy( info->buf + s->level, ((char*)s->node) + s->node->dataptr, s->node->nchar );
767 if ( s->node->isword && !s->checkedval) {
768 info->buf[ s->level+s->node->nchar ] = '\0';
769 out->key = info->buf;
770 out->keylen = s->level+s->node->nchar;
771 out->data =((char*)(s->node->data)) + ((s->node->haschild) ? sizeof(SFSNode*) : 0);
775 if ( s->node->haschild && !s->checkedchild) {
776 info->stack = pushStack(s, getSkipChildPointer(info, s->node), s->level+s->node->nchar);
778 if ( SFSIterate(info, out) )
782 while( s->data - s->node->data < s->node->nchar ) {
783 info->buf[ s->level ] = (char)s->data->val;
784 if ( s->checkedval==0 && s->data->isword ) {
785 info->buf[ s->level+1 ] = '\0';
786 out->key = info->buf;
787 out->keylen = s->level+1;
788 out->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
792 if ( s->checkedchild==0 && s->data->haschild ) {
793 info->stack = pushStack(s,
794 getChildPointer( info, s->data ), s->level+1);
796 if ( SFSIterate(info, out) )
799 s->checkedval = s->checkedchild = 0;
803 info->stack = s->next;
806 return SFSIterate(info, out);
810 SFSRange(SFSTree *info, char *word, SFSDataIO *f, SFSDataIO *l) {
811 SFSNodeStack *s=info->stack;
813 SFSPrefixIteratorStart(info, word);
816 if ( SFSIterate(info, f) ) {
817 SFSNodeStack *sptr = info->stack, *stmp;
818 while( sptr && sptr!=s ) {
825 memcpy(l,f,sizeof(SFSDataIO));
833 while( f->keylen + s->level + 2 >= info->tlen ) {
835 info->buf = (char*)trealloc(info->buf, info->tlen);
839 l->key = info->buf + f->keylen + 1;
840 memcpy(l->key, f->key, f->keylen + 1);
843 while( f->keylen + 1 + s->level + s->node->nchar + 1 >= info->tlen ) {
845 info->buf = (char*)trealloc(info->buf, info->tlen);
847 if ( s->node->isskip ) {
848 memcpy(info->buf + f->keylen + 1 + s->level,
849 ((char*)(s->node))+s->node->dataptr, s->node->nchar);
850 s->level+=s->node->nchar;
851 if (s->node->haschild) {
852 s->node=getSkipChildPointer(info, s->node);
853 } else { /* if (s->node->isword) */
854 info->buf[ f->keylen + 1 + s->level ] = '\0';
855 l->data = (void*)(s->node->data);
856 l->keylen = s->level+1;
860 s->data = s->node->data + s->node->nchar - 1;
861 while( s->data - s->node->data >= 0 ) {
862 info->buf[ f->keylen + 1 + s->level ] = (char)s->data->val;
863 if ( s->data->haschild ) {
864 s->node = getChildPointer( info, s->data );
868 if ( s->data->isword ) {
869 info->buf[ f->keylen + 1 + s->level ] = '\0';
870 l->keylen = s->level+1;
871 l->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
880 l->key = info->buf + f->keylen + 1;
886 typedef struct WorkPlain {
893 plainNode(WorkPlain *wp, SFSNode *node) {
894 int size = getNodeSize(wp->info, node);
895 off_t myoffset = wp->offset;
897 memcpy( wp->node + wp->offset, node, size );
900 tassert( wp->offset <= wp->info->totalen );
902 if ( node->isskip ) {
904 *(Opaque*)(wp->node + myoffset + SFSNHRDSZ) =
905 plainNode(wp, getSkipChildPointer(wp->info, node));
907 SFSNodeData *nd = node->data;
910 for(i=0;i<node->nchar;i++) {
912 *(Opaque*)(wp->node + myoffset + ( ((char*)nd) - ((char*)node) ) + nd->child) =
913 plainNode(wp, getChildPointer( wp->info, nd ) );
924 SFSMakePlain(SFSTree *info, void *pointer) {
927 if ( info->plainmemory )
933 wp.node = (char*)pointer;
935 wp.node = (char*)tmalloc(sizeof(char*) * info->totalen);
937 plainNode(&wp, info->node);
938 tassert( wp.offset == info->totalen );
940 info->node = (SFSNode*)wp.node;
941 info->plainmemory = 1;
945 revertNode(SFSTree *info, SFSNode* node) {
946 int size = getNodeSize(info, node);
949 newnode = (SFSNode*)tmalloc( size );
950 memcpy(newnode, node, size);
952 if ( node->isskip ) {
954 *(SFSNode**)( (char*)(newnode->data) ) =
955 revertNode(info, getSkipChildPointer(info, node));
957 SFSNodeData *nd = node->data;
958 SFSNodeData *nnd = newnode->data;
961 for(i=0;i<node->nchar;i++) {
963 *(SFSNode**) (((char*)nnd) + nnd->child ) =
964 revertNode(info, getChildPointer( info, nd ));
973 SFSRevertPlain(SFSTree *info) {
974 void *pointer = info->node;
976 if (! info->plainmemory )
979 info->node = revertNode(info, info->node);
980 info->plainmemory = 0;
986 writeNode(WorkPlain *wp, int fd, SFSNode *node, u_int32_t extrasize) {
987 int size = getNodeSize(wp->info, node);
988 SFSNode *wnode = (SFSNode*)tmalloc(size);
989 off_t myoffset = wp->offset;
991 memcpy( wnode, node, size );
994 tassert( wp->offset <= wp->info->totalen );
996 if ( node->isskip ) {
998 *(Opaque*)( ((char*)wnode) + SFSNHRDSZ) =
999 writeNode(wp, fd, getSkipChildPointer(wp->info, node), extrasize);
1001 SFSNodeData *nd = node->data;
1004 for(i=0;i<node->nchar;i++) {
1006 *(Opaque*)(((char*)wnode) + ( ((char*)nd) - ((char*)node) ) + nd->child) =
1007 writeNode(wp, fd, getChildPointer( wp->info, nd ), extrasize );
1012 if ( lseek(fd, myoffset + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1013 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1014 if ( write(fd, wnode, size) != size )
1015 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1023 SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize) {
1025 off_t size = info->totalen + SFSTDHSZ;
1026 SFSTreeDumpHeader dh;
1028 if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 )
1029 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", filename, strerror(errno));
1031 if ( flock(fd, LOCK_EX) < 0 )
1032 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1034 if ( extrasize == 0 )
1036 else if ( extradata == NULL )
1039 if ( lseek(fd, size + MAXALIGN(extrasize) , SEEK_SET) < 0 )
1040 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1042 dh.version = SFSTREE_VERSION;
1043 dh.opaquesize = sizeof(Opaque);
1044 dh.headersize = SFSTDHSZ;
1045 dh.datasize = info->datasize;
1046 dh.totalen = info->totalen;
1047 dh.nnodes = info->nnodes;
1048 dh.extrasize = extrasize;
1050 if ( lseek(fd, 0, SEEK_SET) < 0 )
1051 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1052 if ( write(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1053 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1056 if ( write(fd, extradata, extrasize) != extrasize )
1057 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1058 if ( extrasize != MAXALIGN(extrasize) ) {
1059 char dummy[8] = {0,0,0,0,0,0,0,0};
1060 if ( write(fd, dummy, MAXALIGN(extrasize) - extrasize ) != MAXALIGN(extrasize) - extrasize )
1061 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1063 extrasize = MAXALIGN(extrasize);
1067 if ( info->plainmemory ) {
1068 if ( write(fd, info->node, info->totalen) != info->totalen )
1069 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1076 writeNode(&wp, fd, info->node, extrasize);
1084 readNode(SFSTree *info, int fd, char *buf, int bufsize, int extrasize) {
1088 size = read(fd, buf, bufsize );
1089 if ( size < SFSNHRDSZ + sizeof(void*) )
1090 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1092 size = getNodeSize(info, (SFSNode*)buf);
1093 tassert( size <= bufsize );
1094 node = (SFSNode*)tmalloc( size );
1095 memcpy(node, buf, size);
1097 if ( node->isskip ) {
1098 if (node->haschild) {
1099 if ( lseek(fd, *(Opaque*)node->data + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1100 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1101 *(SFSNode**)( (char*)(node->data) ) =
1102 readNode(info, fd, buf, bufsize, extrasize);
1105 SFSNodeData *nd = node->data;
1108 for(i=0;i<node->nchar;i++) {
1110 if ( lseek(fd, *(Opaque*)(((char*)nd) + nd->child ) + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1111 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1112 *(SFSNode**) (((char*)nd) + nd->child ) =
1113 readNode(info, fd, buf, bufsize, extrasize);
1123 SFSReadDump(SFSTree *info, char *filename, void **extradata, u_int32_t *extrasize) {
1125 SFSTreeDumpHeader dh;
1134 memset(info,0,sizeof(SFSTree));
1136 if ( (fd = open(filename, O_RDONLY)) < 0 )
1137 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno));
1138 if ( flock(fd, LOCK_SH) < 0 )
1139 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1141 if ( read(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1142 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1144 if ( dh.version != SFSTREE_VERSION )
1145 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh.version);
1146 if ( dh.opaquesize != sizeof(Opaque) )
1147 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1148 if ( dh.headersize != SFSTDHSZ )
1149 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh.headersize);
1151 info->totalen = dh.totalen;
1152 info->nnodes = dh.nnodes;
1153 info->datasize = dh.datasize;
1155 if ( dh.extrasize ) {
1156 void *pointer = tmalloc( MAXALIGN(dh.extrasize) );
1158 if ( read(fd, pointer, MAXALIGN(dh.extrasize)) != MAXALIGN(dh.extrasize) )
1159 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1162 *extradata = pointer;
1167 *extrasize = dh.extrasize;
1170 /* allocate buffer with max allowed size */
1171 bufsize = SFSNHRDSZ + 256*(sizeof(SFSNodeData) + sizeof(void*) + info->datasize);
1172 buf = tmalloc( bufsize );
1173 info->node = readNode(info, fd, buf, bufsize, MAXALIGN(dh.extrasize));
1181 SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size, void **extradata, u_int32_t *extrasize) {
1182 SFSTreeDumpHeader *dh;
1189 memset(info,0,sizeof(SFSTree));
1191 if ( size && size < SFSTDHSZ )
1192 tlog(TL_CRIT|TL_EXIT, "Memsize too small");
1194 dh = (SFSTreeDumpHeader*)pointer;
1196 if ( dh->version != SFSTREE_VERSION )
1197 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh->version);
1198 if ( dh->opaquesize != sizeof(Opaque) )
1199 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1200 if ( dh->headersize != SFSTDHSZ )
1201 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh->headersize);
1202 if ( size && size != dh->totalen + SFSTDHSZ + dh->extrasize )
1203 tlog(TL_CRIT|TL_EXIT, "Memory size mismatch (should be %d but %d bytes)", dh->totalen + SFSTDHSZ + dh->extrasize , size);
1205 info->totalen = dh->totalen;
1206 info->nnodes = dh->nnodes;
1207 info->datasize = dh->datasize;
1208 info->plainmemory = 1;
1210 if ( dh->extrasize ) {
1212 *extradata = ((char*)pointer) + SFSTDHSZ;
1215 *extrasize = dh->extrasize;
1218 if ( info->totalen && info->nnodes )
1219 info->node = (SFSNode*)( ((char*)pointer) + SFSTDHSZ + MAXALIGN(dh->extrasize) );