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) {
152 return SFSFindDataOrSave(info, &in, NULL);
156 SFSFindDataOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
157 SFSNode *node = info->node;
158 SFSNode **pnode = &(info->node);
159 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
160 u_int8_t *ptr =(u_int8_t*)in->key;
163 position->nodeptr = NULL;
164 position->node = NULL;
168 while( node && !ISEND(ptr, in->key, in->keylen) ) {
170 position->nodeptr = pnode;
171 position->node = node;
172 position->level = ((char*)ptr) - in->key;
175 if ( node->isskip ) {
176 if ( in->keylen>0 && in->keylen - (((char*)ptr) - in->key) > node->nchar )
178 else if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
180 if ( ISEND(ptr, in->key, in->keylen) && node->isword) {
181 return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
182 } else if ( node->haschild ) {
183 node = getSkipChildPointer(info, node);
184 pnode = (SFSNode**)( (char*)(node->data) );
191 StopLow = node->data;
192 StopHigh = StopLow + node->nchar;
193 while (StopLow < StopHigh) {
194 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
195 if ( StopMiddle->val == *ptr ) {
197 if ( ISEND(ptr, in->key, in->keylen) && StopMiddle->isword ) {
198 return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
199 } else if ( StopMiddle->haschild ) {
200 node = getChildPointer(info, StopMiddle);
201 pnode = (SFSNode**)(((char*)StopMiddle) + StopMiddle->child);
206 } else if ( StopMiddle->val < *ptr ) {
207 StopLow = StopMiddle + 1;
209 StopHigh = StopMiddle;
212 if ( StopLow >= StopHigh )
220 SFSAddSaved(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
223 if ( !(position->nodeptr && position->node) ) {
228 *(position->nodeptr) = addRecord(info, position->node, in, position->level);
232 freeFSFNode(SFSTree *info, SFSNode *node, void (*freefunc)(void*)) {
235 if ( node->isskip ) {
237 freeFSFNode(info, *(SFSNode**)(node->data), freefunc);
238 if (freefunc && node->isword && info->datasize)
239 (*freefunc)( (void*)( ((char*)(node->data))+ (node->haschild) ? sizeof(SFSNode*) : 0 ) );
241 SFSNodeData *nd = node->data;
242 char *data= ((char*)node) + node->dataptr;
244 for(i=0;i<node->nchar;i++) {
246 freeFSFNode(info, *(SFSNode**)( ((char*)nd) + nd->child ), freefunc);
247 if (freefunc && nd->isword && info->datasize) {
248 (*freefunc)( (void*)data );
249 data+=info->datasize;
259 SFSFree(SFSTree *info, void (*freefunc)(void*)) {
260 SFSNodeStack *s=info->stack;
262 if (info->node && !info->plainmemory) freeFSFNode(info, info->node, freefunc);
263 if (info->buf) tfree(info->buf);
276 makeSkipNode(SFSTree *info, SFSDataIO *io, int level) {
284 len = (io->keylen > 255) ? 255 : io->keylen;
285 size = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize) + len;
286 size = PTRALIGN(size);
288 res = (SFSNode*)tmalloc(size);
294 res->dataptr = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize);
295 memcpy(((char*)res) + res->dataptr, io->key, len);
297 if ( io->keylen > 255 ) {
300 io->key = io->key+len;
301 io->keylen = io->keylen - len;
302 *(SFSNode**)(res->data) = makeSkipNode(info, io, 0);
303 io->key = io->key-len;
304 io->keylen = io->keylen + len;
308 if (info->datasize) {
309 memcpy( res->data, io->data, info->datasize );
320 splitSkipNode(SFSTree *info, SFSNode* node, SFSDataIO *io, int level) {
326 tassert(node->isskip);
330 s1=((char*)node) + node->dataptr;
333 while( s1 - (((char*)node) + node->dataptr) < node->nchar && s2 - io->key < io->keylen && *s1==*s2) {
338 prefixlen = s2 - io->key;
340 if ( prefixlen==0 ) {
341 if ( node->nchar == 1 ) {
342 int flag = EN_VAL | ((node->isword) ? EN_DATA : 0) | ((node->haschild) ? EN_CHLD : 0);
343 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), flag, &ndata);
344 if ( node->isword ) {
345 if ( info->datasize )
347 ((char*)res) + res->dataptr + info->datasize * ndata->data,
348 ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0),
352 if ( node->haschild )
353 *(SFSNode**)( ((char*)ndata) + ndata->child ) = *(SFSNode**)( node->data );
354 info->totalen -= getNodeSize(info, node);
360 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), EN_VAL|EN_CHLD, &ndata);
362 size = getNodeSize(info,node);
366 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + 1, node->nchar);
368 size = getNodeSize(info,node);
371 *(SFSNode**)( ((char*)ndata) + ndata->child ) = (SFSNode*)trealloc(node, size);
373 res = addRecord(info, res, io, 0);
374 } else if (prefixlen==io->keylen) {
375 if (prefixlen==node->nchar) {
376 if ( node->isword || info->datasize==0) {
378 memcpy( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNodeData*) : 0),
384 int osize = PTRALIGN(SFSNHRDSZ + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
385 int nsize = PTRALIGN(SFSNHRDSZ + info->datasize + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
387 info->totalen += nsize - osize;
389 res=(SFSNode*)trealloc(node,nsize);
390 res->dataptr=SFSNHRDSZ + info->datasize + ((res->haschild) ? sizeof(SFSNodeData*) : 0);
392 memmove(((char*)res) + res->dataptr,
393 ((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), res->nchar);
394 memcpy(((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), io->data, info->datasize);
397 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*) + prefixlen;
398 size = PTRALIGN(size);
402 res = (SFSNode*)tmalloc(size);
406 res->nchar = prefixlen;
407 res->dataptr = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*);
408 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
410 memcpy(((char*)(res->data)) + sizeof(SFSNodeData*), io->data, info->datasize);
411 info->totalen-=getNodeSize(info,node);
412 node->nchar-=prefixlen;
413 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
414 size = getNodeSize(info,node);
416 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
418 } else if ( prefixlen==node->nchar ) {
419 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNode*) + node->nchar;
420 info->totalen+=sizeof(SFSNode*);
421 res=(SFSNode*)trealloc(node,size);
422 memmove( ((char*)(res->data)) + sizeof(SFSNode*), res->data, info->datasize + res->nchar);
424 res->dataptr+=sizeof(SFSNode*);
425 *(SFSNode**)(res->data) = makeSkipNode(info, io, prefixlen);
427 int size = SFSNHRDSZ + sizeof(SFSNodeData*) + prefixlen;
428 size = PTRALIGN(size);
431 res = (SFSNode*)tmalloc(size);
435 res->nchar = prefixlen;
436 res->dataptr = SFSNHRDSZ + sizeof(SFSNodeData*);
437 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
439 info->totalen-= getNodeSize(info,node);
440 node->nchar-=prefixlen;
442 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
444 size = getNodeSize(info,node);
445 info->totalen+= size;
447 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
448 *(SFSNode**)(res->data) = splitSkipNode(info, *(SFSNode**)(res->data), io, prefixlen);
458 enlargeNode(SFSTree *info, SFSNode* node, u_int8_t val, int flag, SFSNodeData **nd) {
459 u_int32_t nchild=0, nchar=0, nfound=0, i;
471 for(i=0;i<nchar;i++) {
472 nfound += data->isword;
478 /*info->totalen -= PTRALIGN(SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize);*/
479 info->totalen -= getNodeSize(info, node);
481 if ( flag & EN_VAL ) nchar++;
482 if ( flag & EN_CHLD ) nchild++;
483 if ( flag & EN_DATA ) nfound++;
486 sizenode = SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize;
487 sizenode = PTRALIGN(sizenode);
489 info->totalen+=sizenode;
490 if ( !node ) info->nnodes++;
491 rs=(SFSNode*)tmalloc(sizenode);
496 rs->dataptr=SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*);
497 dataptr=((char*)rs) + rs->dataptr;
498 chld = (SFSNode**)( ((char*)rs)+SFSNHRDSZ+nchar*sizeof(SFSNodeData) );
502 SFSNode **ochld=(SFSNode**)( ((char*)node)+SFSNHRDSZ+node->nchar*sizeof(SFSNodeData) );
503 SFSNodeData *odata = node->data;
504 char *odataptr=((char*)node) + node->dataptr;
507 for(i=0;i<node->nchar;i++) {
508 if ( val > odata->val ) {
509 *(u_int32_t*)data = *(u_int32_t*)odata;
510 if ( odata->haschild ) {
512 data->child = ((char*)chld) - ((char*)data);
515 if ( odata->isword && info->datasize ) {
516 memcpy(dataptr, odataptr, info->datasize);
517 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
518 dataptr += info->datasize; odataptr += info->datasize;
521 } else if ( val == odata->val ) {
522 tassert ( (flag&EN_VAL)==0 );
523 *(u_int32_t*)data = *(u_int32_t*)odata;
525 if ( odata->haschild || flag & EN_CHLD ) {
526 if (odata->haschild) *chld=*ochld;
527 data->child = ((char*)chld) - ((char*)data);
529 chld++; if (odata->haschild) ochld++;
532 if ( odata->isword || flag & EN_DATA ) {
534 if ( info->datasize && odata->isword ) {
535 memcpy(dataptr, odataptr, info->datasize);
536 odataptr += info->datasize;
538 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
539 dataptr += info->datasize;
546 tassert ( flag&EN_VAL );
548 if ( flag & EN_CHLD ) {
550 data->child = ((char*)chld) - ((char*)data);
554 if ( flag & EN_DATA ) {
556 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
557 dataptr += info->datasize;
565 *(u_int32_t*)data = *(u_int32_t*)odata;
566 if ( odata->haschild ) {
568 data->child = ((char*)chld) - ((char*)data);
571 if ( odata->isword && info->datasize ) {
572 memcpy(dataptr, odataptr, info->datasize);
573 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
574 dataptr += info->datasize; odataptr += info->datasize;
581 tassert ( flag&EN_VAL );
583 if ( flag & EN_CHLD ) {
585 data->child = ((char*)chld) - ((char*)data);
589 if ( flag & EN_DATA ) {
591 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
592 dataptr += info->datasize;
598 tassert ( flag & EN_VAL );
600 if ( flag & EN_CHLD ) {
602 data->child = ((char*)chld) - ((char*)data);
605 if ( flag & EN_DATA ) {
607 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
612 if (node) tfree(node);
617 addRecord(SFSTree *info, SFSNode* node, SFSDataIO *in, int level) {
618 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
619 u_int8_t *ptr = ((u_int8_t*)in->key) + level;
622 if ( node->isskip ) {
623 if ( node->haschild && in->keylen-level > node->nchar &&
624 strncmp(in->key+level, ((char*)node)+node->dataptr, node->nchar)==0 )
625 *(SFSNode**)( node->data ) = addRecord(info, *(SFSNode**)( node->data ), in, level+node->nchar);
627 node = splitSkipNode(info, node, in, level);
629 StopLow = node->data;
630 StopHigh = StopLow + node->nchar;
631 while (StopLow < StopHigh) {
632 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
633 if ( StopMiddle->val == *ptr ) {
634 if ( *(ptr+1)=='\0' ) {
635 if ( StopMiddle->isword ) {
637 if ( info->datasize )
638 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
639 in->data, info->datasize );
642 if (info->datasize) {
643 node = enlargeNode(info, node, *ptr, EN_DATA, &StopMiddle);
644 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
645 in->data, info->datasize );
647 StopMiddle->isword = 1;
649 } else if ( StopMiddle->haschild ) {
650 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
651 addRecord(info, *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ), in, level+1);
653 node = enlargeNode(info, node, *ptr, EN_CHLD, &StopMiddle);
654 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
655 makeSkipNode(info, in, level+1);
658 } else if ( StopMiddle->val < *ptr ) {
659 StopLow = StopMiddle + 1;
661 StopHigh = StopMiddle;
664 if ( *(ptr+1)=='\0' ) {
665 node = enlargeNode(info, node, *ptr, EN_VAL|EN_DATA, &StopMiddle);
666 if ( info->datasize )
667 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
668 in->data, info->datasize );
670 node = enlargeNode(info, node, *ptr, EN_VAL|EN_CHLD, &StopMiddle);
671 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
672 makeSkipNode(info, in, level+1);
676 node = makeSkipNode(info, in, level);
683 SFSAdd(SFSTree *info, SFSDataIO *in) {
686 in->keylen=strlen(in->key);
687 info->node = addRecord(info, info->node, in, 0);
691 pushStack(SFSNodeStack *top, SFSNode *node, int level ) {
692 SFSNodeStack *r=(SFSNodeStack*)tmalloc(sizeof(SFSNodeStack));
705 SFSIteratorStart(SFSTree *info) {
708 info->buf = (char*)tmalloc(info->tlen);
710 info->stack = pushStack(NULL, info->node, 0);
715 SFSPrefixIteratorStart(SFSTree *info, char *word) {
716 SFSNode *node = info->node;
717 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
718 u_int8_t *ptr =(u_int8_t*)word;
719 int len,wlen=strlen(word);
721 if ( wlen+1>=info->tlen ) {
723 info->buf = (info->buf) ?
724 (char*)trealloc(info->buf,info->tlen)
726 (char*)tmalloc(info->tlen);
730 while( node && *ptr) {
731 len = wlen - (((char*)ptr)-word);
732 if ( node->isskip ) {
733 if ( STRNCMP(ptr, ((char*)node)+node->dataptr, (len<node->nchar) ? len : node->nchar) ) {
734 if ( len<=node->nchar ) {
735 strcpy(info->buf,word);
736 info->stack = pushStack(NULL, node, ((char*)ptr) - word);
738 } else if ( node->haschild ) {
740 node = getSkipChildPointer(info, node);
747 StopLow = node->data;
748 StopHigh = StopLow + node->nchar;
749 while (StopLow < StopHigh) {
750 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
751 if ( StopMiddle->val == *ptr ) {
752 if ( *(ptr+1)=='\0' ) {
753 len =((char*)ptr)-word+1;
754 strcpy(info->buf,word);
755 if ( StopMiddle->isword ) {
757 info->wdata = (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
759 if ( StopMiddle->haschild )
760 info->stack = pushStack(NULL, getChildPointer( info, StopMiddle ), len);
762 } else if ( StopMiddle->haschild ) {
763 node = getChildPointer( info, StopMiddle );
769 } else if ( StopMiddle->val < *ptr ) {
770 StopLow = StopMiddle + 1;
772 StopHigh = StopMiddle;
775 if ( StopLow >= StopHigh )
783 SFSIterate(SFSTree *info, SFSDataIO *out) {
784 SFSNodeStack *s=info->stack;
786 if ( info->hasword ) {
787 out->key = info->buf;
788 out->keylen = strlen(out->key);
789 out->data = info->wdata;
794 if ( s == NULL || s->node == NULL)
797 while ( s->level + s->node->nchar + 1 >= info->tlen ) {
799 info->buf = (char*)trealloc(info->buf, info->tlen);
802 if ( s->node->isskip ) {
803 memcpy( info->buf + s->level, ((char*)s->node) + s->node->dataptr, s->node->nchar );
804 if ( s->node->isword && !s->checkedval) {
805 info->buf[ s->level+s->node->nchar ] = '\0';
806 out->key = info->buf;
807 out->keylen = s->level+s->node->nchar;
808 out->data =((char*)(s->node->data)) + ((s->node->haschild) ? sizeof(SFSNode*) : 0);
812 if ( s->node->haschild && !s->checkedchild) {
813 info->stack = pushStack(s, getSkipChildPointer(info, s->node), s->level+s->node->nchar);
815 if ( SFSIterate(info, out) )
819 while( s->data - s->node->data < s->node->nchar ) {
820 info->buf[ s->level ] = (char)s->data->val;
821 if ( s->checkedval==0 && s->data->isword ) {
822 info->buf[ s->level+1 ] = '\0';
823 out->key = info->buf;
824 out->keylen = s->level+1;
825 out->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
829 if ( s->checkedchild==0 && s->data->haschild ) {
830 info->stack = pushStack(s,
831 getChildPointer( info, s->data ), s->level+1);
833 if ( SFSIterate(info, out) )
836 s->checkedval = s->checkedchild = 0;
840 info->stack = s->next;
843 return SFSIterate(info, out);
847 SFSRange(SFSTree *info, char *word, SFSDataIO *f, SFSDataIO *l) {
848 SFSNodeStack *s=info->stack;
850 SFSPrefixIteratorStart(info, word);
853 if ( SFSIterate(info, f) ) {
854 SFSNodeStack *sptr = info->stack, *stmp;
855 while( sptr && sptr!=s ) {
862 memcpy(l,f,sizeof(SFSDataIO));
870 while( f->keylen + s->level + 2 >= info->tlen ) {
872 info->buf = (char*)trealloc(info->buf, info->tlen);
876 l->key = info->buf + f->keylen + 1;
877 memcpy(l->key, f->key, f->keylen + 1);
880 while( f->keylen + 1 + s->level + s->node->nchar + 1 >= info->tlen ) {
882 info->buf = (char*)trealloc(info->buf, info->tlen);
884 if ( s->node->isskip ) {
885 memcpy(info->buf + f->keylen + 1 + s->level,
886 ((char*)(s->node))+s->node->dataptr, s->node->nchar);
887 s->level+=s->node->nchar;
888 if (s->node->haschild) {
889 s->node=getSkipChildPointer(info, s->node);
890 } else { /* if (s->node->isword) */
891 info->buf[ f->keylen + 1 + s->level ] = '\0';
892 l->data = (void*)(s->node->data);
893 l->keylen = s->level+1;
897 s->data = s->node->data + s->node->nchar - 1;
898 while( s->data - s->node->data >= 0 ) {
899 info->buf[ f->keylen + 1 + s->level ] = (char)s->data->val;
900 if ( s->data->haschild ) {
901 s->node = getChildPointer( info, s->data );
905 if ( s->data->isword ) {
906 info->buf[ f->keylen + 1 + s->level ] = '\0';
907 l->keylen = s->level+1;
908 l->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
917 l->key = info->buf + f->keylen + 1;
923 typedef struct WorkPlain {
930 plainNode(WorkPlain *wp, SFSNode *node) {
931 int size = getNodeSize(wp->info, node);
932 off_t myoffset = wp->offset;
934 memcpy( wp->node + wp->offset, node, size );
937 tassert( wp->offset <= wp->info->totalen );
939 if ( node->isskip ) {
941 *(Opaque*)(wp->node + myoffset + SFSNHRDSZ) =
942 plainNode(wp, getSkipChildPointer(wp->info, node));
944 SFSNodeData *nd = node->data;
947 for(i=0;i<node->nchar;i++) {
949 *(Opaque*)(wp->node + myoffset + ( ((char*)nd) - ((char*)node) ) + nd->child) =
950 plainNode(wp, getChildPointer( wp->info, nd ) );
961 SFSMakePlain(SFSTree *info, void *pointer) {
964 if ( info->plainmemory )
970 wp.node = (char*)pointer;
972 wp.node = (char*)tmalloc(sizeof(char*) * info->totalen);
974 plainNode(&wp, info->node);
975 tassert( wp.offset == info->totalen );
977 info->node = (SFSNode*)wp.node;
978 info->plainmemory = 1;
982 revertNode(SFSTree *info, SFSNode* node) {
983 int size = getNodeSize(info, node);
986 newnode = (SFSNode*)tmalloc( size );
987 memcpy(newnode, node, size);
989 if ( node->isskip ) {
991 *(SFSNode**)( (char*)(newnode->data) ) =
992 revertNode(info, getSkipChildPointer(info, node));
994 SFSNodeData *nd = node->data;
995 SFSNodeData *nnd = newnode->data;
998 for(i=0;i<node->nchar;i++) {
1000 *(SFSNode**) (((char*)nnd) + nnd->child ) =
1001 revertNode(info, getChildPointer( info, nd ));
1010 SFSRevertPlain(SFSTree *info) {
1011 void *pointer = info->node;
1013 if (! info->plainmemory )
1016 info->node = revertNode(info, info->node);
1017 info->plainmemory = 0;
1023 writeNode(WorkPlain *wp, int fd, SFSNode *node, u_int32_t extrasize) {
1024 int size = getNodeSize(wp->info, node);
1025 SFSNode *wnode = (SFSNode*)tmalloc(size);
1026 off_t myoffset = wp->offset;
1028 memcpy( wnode, node, size );
1031 tassert( wp->offset <= wp->info->totalen );
1033 if ( node->isskip ) {
1035 *(Opaque*)( ((char*)wnode) + SFSNHRDSZ) =
1036 writeNode(wp, fd, getSkipChildPointer(wp->info, node), extrasize);
1038 SFSNodeData *nd = node->data;
1041 for(i=0;i<node->nchar;i++) {
1043 *(Opaque*)(((char*)wnode) + ( ((char*)nd) - ((char*)node) ) + nd->child) =
1044 writeNode(wp, fd, getChildPointer( wp->info, nd ), extrasize );
1049 if ( lseek(fd, myoffset + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1050 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1051 if ( write(fd, wnode, size) != size )
1052 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1060 SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize) {
1062 off_t size = info->totalen + SFSTDHSZ;
1063 SFSTreeDumpHeader dh;
1065 if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 )
1066 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", filename, strerror(errno));
1068 if ( flock(fd, LOCK_EX) < 0 )
1069 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1071 if ( extrasize == 0 )
1073 else if ( extradata == NULL )
1076 if ( lseek(fd, size + MAXALIGN(extrasize) , SEEK_SET) < 0 )
1077 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1079 dh.version = SFSTREE_VERSION;
1080 dh.opaquesize = sizeof(Opaque);
1081 dh.headersize = SFSTDHSZ;
1082 dh.datasize = info->datasize;
1083 dh.totalen = info->totalen;
1084 dh.nnodes = info->nnodes;
1085 dh.extrasize = extrasize;
1087 if ( lseek(fd, 0, SEEK_SET) < 0 )
1088 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1089 if ( write(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1090 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1093 if ( write(fd, extradata, extrasize) != extrasize )
1094 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1095 if ( extrasize != MAXALIGN(extrasize) ) {
1096 char dummy[8] = {0,0,0,0,0,0,0,0};
1097 if ( write(fd, dummy, MAXALIGN(extrasize) - extrasize ) != MAXALIGN(extrasize) - extrasize )
1098 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1100 extrasize = MAXALIGN(extrasize);
1104 if ( info->plainmemory ) {
1105 if ( write(fd, info->node, info->totalen) != info->totalen )
1106 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1113 writeNode(&wp, fd, info->node, extrasize);
1121 readNode(SFSTree *info, int fd, char *buf, int bufsize, int extrasize) {
1125 size = read(fd, buf, bufsize );
1126 if ( size < SFSNHRDSZ + sizeof(void*) )
1127 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1129 size = getNodeSize(info, (SFSNode*)buf);
1130 tassert( size <= bufsize );
1131 node = (SFSNode*)tmalloc( size );
1132 memcpy(node, buf, size);
1134 if ( node->isskip ) {
1135 if (node->haschild) {
1136 if ( lseek(fd, *(Opaque*)node->data + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1137 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1138 *(SFSNode**)( (char*)(node->data) ) =
1139 readNode(info, fd, buf, bufsize, extrasize);
1142 SFSNodeData *nd = node->data;
1145 for(i=0;i<node->nchar;i++) {
1147 if ( lseek(fd, *(Opaque*)(((char*)nd) + nd->child ) + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1148 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1149 *(SFSNode**) (((char*)nd) + nd->child ) =
1150 readNode(info, fd, buf, bufsize, extrasize);
1160 SFSReadDump(SFSTree *info, char *filename, void **extradata, u_int32_t *extrasize) {
1162 SFSTreeDumpHeader dh;
1171 memset(info,0,sizeof(SFSTree));
1173 if ( (fd = open(filename, O_RDONLY)) < 0 )
1174 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno));
1175 if ( flock(fd, LOCK_SH) < 0 )
1176 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1178 if ( read(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1179 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1181 if ( dh.version != SFSTREE_VERSION )
1182 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh.version);
1183 if ( dh.opaquesize != sizeof(Opaque) )
1184 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1185 if ( dh.headersize != SFSTDHSZ )
1186 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh.headersize);
1188 info->totalen = dh.totalen;
1189 info->nnodes = dh.nnodes;
1190 info->datasize = dh.datasize;
1192 if ( dh.extrasize ) {
1193 void *pointer = tmalloc( MAXALIGN(dh.extrasize) );
1195 if ( read(fd, pointer, MAXALIGN(dh.extrasize)) != MAXALIGN(dh.extrasize) )
1196 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1199 *extradata = pointer;
1204 *extrasize = dh.extrasize;
1207 /* allocate buffer with max allowed size */
1208 bufsize = SFSNHRDSZ + 256*(sizeof(SFSNodeData) + sizeof(void*) + info->datasize);
1209 buf = tmalloc( bufsize );
1210 info->node = readNode(info, fd, buf, bufsize, MAXALIGN(dh.extrasize));
1218 SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size, void **extradata, u_int32_t *extrasize) {
1219 SFSTreeDumpHeader *dh;
1226 memset(info,0,sizeof(SFSTree));
1228 if ( size && size < SFSTDHSZ )
1229 tlog(TL_CRIT|TL_EXIT, "Memsize too small");
1231 dh = (SFSTreeDumpHeader*)pointer;
1233 if ( dh->version != SFSTREE_VERSION )
1234 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh->version);
1235 if ( dh->opaquesize != sizeof(Opaque) )
1236 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1237 if ( dh->headersize != SFSTDHSZ )
1238 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh->headersize);
1239 if ( size && size != dh->totalen + SFSTDHSZ + MAXALIGN(dh->extrasize) )
1240 tlog(TL_CRIT|TL_EXIT, "Memory size mismatch (should be %d but %d bytes)", dh->totalen + SFSTDHSZ + dh->extrasize , size);
1242 info->totalen = dh->totalen;
1243 info->nnodes = dh->nnodes;
1244 info->datasize = dh->datasize;
1245 info->plainmemory = 1;
1247 if ( dh->extrasize ) {
1249 *extradata = ((char*)pointer) + SFSTDHSZ;
1252 *extrasize = dh->extrasize;
1255 if ( info->totalen && info->nnodes )
1256 info->node = (SFSNode*)( ((char*)pointer) + SFSTDHSZ + MAXALIGN(dh->extrasize) );