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 SFSFindDataFromSavedOrSave(info, &in, NULL);
156 SFSFindDataOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
158 memset(position, 0, sizeof(position));
160 return SFSFindDataFromSavedOrSave(info, in, position);
164 SFSFindDataFromSavedOrSave(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
165 SFSNode *node = info->node;
166 SFSNode **pnode = &(info->node);
167 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
168 u_int8_t *ptr =(u_int8_t*)in->key;
170 if ( position && position->nodeptr && position->node && in->keylen > position->level ) {
171 node = position->node;
172 pnode = position->nodeptr;
173 ptr += position->level;
176 while( node && !ISEND(ptr, in->key, in->keylen) ) {
178 position->nodeptr = pnode;
179 position->node = node;
180 position->level = ((char*)ptr) - in->key;
183 if ( node->isskip ) {
184 if ( in->keylen>0 && in->keylen - (((char*)ptr) - in->key) > node->nchar )
186 else if ( STRNCMP(ptr, ((char*)node)+node->dataptr, node->nchar) ) {
188 if ( ISEND(ptr, in->key, in->keylen) && node->isword) {
189 return (void*) ( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0) );
190 } else if ( node->haschild ) {
191 node = getSkipChildPointer(info, node);
192 pnode = (SFSNode**)( (char*)(node->data) );
199 StopLow = node->data;
200 StopHigh = StopLow + node->nchar;
201 while (StopLow < StopHigh) {
202 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
203 if ( StopMiddle->val == *ptr ) {
205 if ( ISEND(ptr, in->key, in->keylen) && StopMiddle->isword ) {
206 return (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
207 } else if ( StopMiddle->haschild ) {
208 node = getChildPointer(info, StopMiddle);
209 pnode = (SFSNode**)(((char*)StopMiddle) + StopMiddle->child);
214 } else if ( StopMiddle->val < *ptr ) {
215 StopLow = StopMiddle + 1;
217 StopHigh = StopMiddle;
220 if ( StopLow >= StopHigh )
228 SFSAddSaved(SFSTree *info, SFSDataIO *in, SFSTreePosition *position) {
231 if ( !(position->nodeptr && position->node) ) {
236 position->node = *(position->nodeptr) = addRecord(info, position->node, in, position->level);
240 freeFSFNode(SFSTree *info, SFSNode *node, void (*freefunc)(void*)) {
243 if ( node->isskip ) {
245 freeFSFNode(info, *(SFSNode**)(node->data), freefunc);
246 if (freefunc && node->isword && info->datasize)
247 (*freefunc)( (void*)( ((char*)(node->data))+ (node->haschild) ? sizeof(SFSNode*) : 0 ) );
249 SFSNodeData *nd = node->data;
250 char *data= ((char*)node) + node->dataptr;
252 for(i=0;i<node->nchar;i++) {
254 freeFSFNode(info, *(SFSNode**)( ((char*)nd) + nd->child ), freefunc);
255 if (freefunc && nd->isword && info->datasize) {
256 (*freefunc)( (void*)data );
257 data+=info->datasize;
267 SFSFree(SFSTree *info, void (*freefunc)(void*)) {
268 SFSNodeStack *s=info->stack;
270 if (info->node && !info->plainmemory) freeFSFNode(info, info->node, freefunc);
271 if (info->buf) tfree(info->buf);
284 makeSkipNode(SFSTree *info, SFSDataIO *io, int level) {
292 len = (io->keylen > 255) ? 255 : io->keylen;
293 size = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize) + len;
294 size = PTRALIGN(size);
296 res = (SFSNode*)tmalloc(size);
302 res->dataptr = SFSNHRDSZ + ((io->keylen > 255) ? sizeof(SFSNode*) : info->datasize);
303 memcpy(((char*)res) + res->dataptr, io->key, len);
305 if ( io->keylen > 255 ) {
308 io->key = io->key+len;
309 io->keylen = io->keylen - len;
310 *(SFSNode**)(res->data) = makeSkipNode(info, io, 0);
311 io->key = io->key-len;
312 io->keylen = io->keylen + len;
316 if (info->datasize) {
317 memcpy( res->data, io->data, info->datasize );
328 splitSkipNode(SFSTree *info, SFSNode* node, SFSDataIO *io, int level) {
334 tassert(node->isskip);
338 s1=((char*)node) + node->dataptr;
341 while( s1 - (((char*)node) + node->dataptr) < node->nchar && s2 - io->key < io->keylen && *s1==*s2) {
346 prefixlen = s2 - io->key;
348 if ( prefixlen==0 ) {
349 if ( node->nchar == 1 ) {
350 int flag = EN_VAL | ((node->isword) ? EN_DATA : 0) | ((node->haschild) ? EN_CHLD : 0);
351 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), flag, &ndata);
352 if ( node->isword ) {
353 if ( info->datasize )
355 ((char*)res) + res->dataptr + info->datasize * ndata->data,
356 ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNode*) : 0),
360 if ( node->haschild )
361 *(SFSNode**)( ((char*)ndata) + ndata->child ) = *(SFSNode**)( node->data );
362 info->totalen -= getNodeSize(info, node);
368 res = enlargeNode(info, NULL, *(u_int8_t*)(((char*)node) + node->dataptr), EN_VAL|EN_CHLD, &ndata);
370 size = getNodeSize(info,node);
374 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + 1, node->nchar);
376 size = getNodeSize(info,node);
379 *(SFSNode**)( ((char*)ndata) + ndata->child ) = (SFSNode*)trealloc(node, size);
381 res = addRecord(info, res, io, 0);
382 } else if (prefixlen==io->keylen) {
383 if (prefixlen==node->nchar) {
384 if ( node->isword || info->datasize==0) {
386 memcpy( ((char*)(node->data)) + ((node->haschild) ? sizeof(SFSNodeData*) : 0),
392 int osize = PTRALIGN(SFSNHRDSZ + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
393 int nsize = PTRALIGN(SFSNHRDSZ + info->datasize + ((node->haschild) ? sizeof(SFSNodeData*) : 0) + node->nchar);
395 info->totalen += nsize - osize;
397 res=(SFSNode*)trealloc(node,nsize);
398 res->dataptr=SFSNHRDSZ + info->datasize + ((res->haschild) ? sizeof(SFSNodeData*) : 0);
400 memmove(((char*)res) + res->dataptr,
401 ((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), res->nchar);
402 memcpy(((char*)(res->data)) + ((res->haschild) ? sizeof(SFSNodeData*) : 0), io->data, info->datasize);
405 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*) + prefixlen;
406 size = PTRALIGN(size);
410 res = (SFSNode*)tmalloc(size);
414 res->nchar = prefixlen;
415 res->dataptr = SFSNHRDSZ + info->datasize + sizeof(SFSNodeData*);
416 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
418 memcpy(((char*)(res->data)) + sizeof(SFSNodeData*), io->data, info->datasize);
419 info->totalen-=getNodeSize(info,node);
420 node->nchar-=prefixlen;
421 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
422 size = getNodeSize(info,node);
424 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
426 } else if ( prefixlen==node->nchar ) {
427 int size = SFSNHRDSZ + info->datasize + sizeof(SFSNode*) + node->nchar;
428 info->totalen+=sizeof(SFSNode*);
429 res=(SFSNode*)trealloc(node,size);
430 memmove( ((char*)(res->data)) + sizeof(SFSNode*), res->data, info->datasize + res->nchar);
432 res->dataptr+=sizeof(SFSNode*);
433 *(SFSNode**)(res->data) = makeSkipNode(info, io, prefixlen);
435 int size = SFSNHRDSZ + sizeof(SFSNodeData*) + prefixlen;
436 size = PTRALIGN(size);
439 res = (SFSNode*)tmalloc(size);
443 res->nchar = prefixlen;
444 res->dataptr = SFSNHRDSZ + sizeof(SFSNodeData*);
445 memcpy(((char*)res)+res->dataptr, io->key, prefixlen);
447 info->totalen-= getNodeSize(info,node);
448 node->nchar-=prefixlen;
450 memmove( ((char*)node) + node->dataptr, ((char*)node) + node->dataptr + prefixlen, node->nchar);
452 size = getNodeSize(info,node);
453 info->totalen+= size;
455 *(SFSNode**)(res->data) = (SFSNode*)trealloc(node, size);
456 *(SFSNode**)(res->data) = splitSkipNode(info, *(SFSNode**)(res->data), io, prefixlen);
466 enlargeNode(SFSTree *info, SFSNode* node, u_int8_t val, int flag, SFSNodeData **nd) {
467 u_int32_t nchild=0, nchar=0, nfound=0, i;
479 for(i=0;i<nchar;i++) {
480 nfound += data->isword;
486 /*info->totalen -= PTRALIGN(SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize);*/
487 info->totalen -= getNodeSize(info, node);
489 if ( flag & EN_VAL ) nchar++;
490 if ( flag & EN_CHLD ) nchild++;
491 if ( flag & EN_DATA ) nfound++;
494 sizenode = SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*)+nfound*info->datasize;
495 sizenode = PTRALIGN(sizenode);
497 info->totalen+=sizenode;
498 if ( !node ) info->nnodes++;
499 rs=(SFSNode*)tmalloc(sizenode);
504 rs->dataptr=SFSNHRDSZ+nchar*sizeof(SFSNodeData)+nchild*sizeof(SFSNode*);
505 dataptr=((char*)rs) + rs->dataptr;
506 chld = (SFSNode**)( ((char*)rs)+SFSNHRDSZ+nchar*sizeof(SFSNodeData) );
510 SFSNode **ochld=(SFSNode**)( ((char*)node)+SFSNHRDSZ+node->nchar*sizeof(SFSNodeData) );
511 SFSNodeData *odata = node->data;
512 char *odataptr=((char*)node) + node->dataptr;
515 for(i=0;i<node->nchar;i++) {
516 if ( val > odata->val ) {
517 *(u_int32_t*)data = *(u_int32_t*)odata;
518 if ( odata->haschild ) {
520 data->child = ((char*)chld) - ((char*)data);
523 if ( odata->isword && info->datasize ) {
524 memcpy(dataptr, odataptr, info->datasize);
525 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
526 dataptr += info->datasize; odataptr += info->datasize;
529 } else if ( val == odata->val ) {
530 tassert ( (flag&EN_VAL)==0 );
531 *(u_int32_t*)data = *(u_int32_t*)odata;
533 if ( odata->haschild || flag & EN_CHLD ) {
534 if (odata->haschild) *chld=*ochld;
535 data->child = ((char*)chld) - ((char*)data);
537 chld++; if (odata->haschild) ochld++;
540 if ( odata->isword || flag & EN_DATA ) {
542 if ( info->datasize && odata->isword ) {
543 memcpy(dataptr, odataptr, info->datasize);
544 odataptr += info->datasize;
546 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
547 dataptr += info->datasize;
554 tassert ( flag&EN_VAL );
556 if ( flag & EN_CHLD ) {
558 data->child = ((char*)chld) - ((char*)data);
562 if ( flag & EN_DATA ) {
564 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
565 dataptr += info->datasize;
573 *(u_int32_t*)data = *(u_int32_t*)odata;
574 if ( odata->haschild ) {
576 data->child = ((char*)chld) - ((char*)data);
579 if ( odata->isword && info->datasize ) {
580 memcpy(dataptr, odataptr, info->datasize);
581 data->data = ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize;
582 dataptr += info->datasize; odataptr += info->datasize;
589 tassert ( flag&EN_VAL );
591 if ( flag & EN_CHLD ) {
593 data->child = ((char*)chld) - ((char*)data);
597 if ( flag & EN_DATA ) {
599 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
600 dataptr += info->datasize;
606 tassert ( flag & EN_VAL );
608 if ( flag & EN_CHLD ) {
610 data->child = ((char*)chld) - ((char*)data);
613 if ( flag & EN_DATA ) {
615 data->data = ( info->datasize ) ? ((dataptr - ((char*)rs)) - rs->dataptr)/info->datasize : 0;
620 if (node) tfree(node);
625 addRecord(SFSTree *info, SFSNode* node, SFSDataIO *in, int level) {
626 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
627 u_int8_t *ptr = ((u_int8_t*)in->key) + level;
630 if ( node->isskip ) {
631 if ( node->haschild && in->keylen-level > node->nchar &&
632 strncmp(in->key+level, ((char*)node)+node->dataptr, node->nchar)==0 )
633 *(SFSNode**)( node->data ) = addRecord(info, *(SFSNode**)( node->data ), in, level+node->nchar);
635 node = splitSkipNode(info, node, in, level);
637 StopLow = node->data;
638 StopHigh = StopLow + node->nchar;
639 while (StopLow < StopHigh) {
640 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
641 if ( StopMiddle->val == *ptr ) {
642 if ( *(ptr+1)=='\0' ) {
643 if ( StopMiddle->isword ) {
645 if ( info->datasize )
646 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
647 in->data, info->datasize );
650 if (info->datasize) {
651 node = enlargeNode(info, node, *ptr, EN_DATA, &StopMiddle);
652 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
653 in->data, info->datasize );
655 StopMiddle->isword = 1;
657 } else if ( StopMiddle->haschild ) {
658 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
659 addRecord(info, *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ), in, level+1);
661 node = enlargeNode(info, node, *ptr, EN_CHLD, &StopMiddle);
662 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
663 makeSkipNode(info, in, level+1);
666 } else if ( StopMiddle->val < *ptr ) {
667 StopLow = StopMiddle + 1;
669 StopHigh = StopMiddle;
672 if ( *(ptr+1)=='\0' ) {
673 node = enlargeNode(info, node, *ptr, EN_VAL|EN_DATA, &StopMiddle);
674 if ( info->datasize )
675 memcpy(((char*)node) + node->dataptr + info->datasize * StopMiddle->data,
676 in->data, info->datasize );
678 node = enlargeNode(info, node, *ptr, EN_VAL|EN_CHLD, &StopMiddle);
679 *(SFSNode**)( ((char*)StopMiddle) + StopMiddle->child ) =
680 makeSkipNode(info, in, level+1);
684 node = makeSkipNode(info, in, level);
691 SFSAdd(SFSTree *info, SFSDataIO *in) {
694 in->keylen=strlen(in->key);
695 info->node = addRecord(info, info->node, in, 0);
699 pushStack(SFSNodeStack *top, SFSNode *node, int level ) {
700 SFSNodeStack *r=(SFSNodeStack*)tmalloc(sizeof(SFSNodeStack));
713 SFSIteratorStart(SFSTree *info) {
716 info->buf = (char*)tmalloc(info->tlen);
718 info->stack = pushStack(NULL, info->node, 0);
723 SFSPrefixIteratorStart(SFSTree *info, char *word) {
724 SFSNode *node = info->node;
725 SFSNodeData *StopLow, *StopHigh, *StopMiddle;
726 u_int8_t *ptr =(u_int8_t*)word;
727 int len,wlen=strlen(word);
729 if ( wlen+1>=info->tlen ) {
731 info->buf = (info->buf) ?
732 (char*)trealloc(info->buf,info->tlen)
734 (char*)tmalloc(info->tlen);
738 while( node && *ptr) {
739 len = wlen - (((char*)ptr)-word);
740 if ( node->isskip ) {
741 if ( STRNCMP(ptr, ((char*)node)+node->dataptr, (len<node->nchar) ? len : node->nchar) ) {
742 if ( len<=node->nchar ) {
743 strcpy(info->buf,word);
744 info->stack = pushStack(NULL, node, ((char*)ptr) - word);
746 } else if ( node->haschild ) {
748 node = getSkipChildPointer(info, node);
755 StopLow = node->data;
756 StopHigh = StopLow + node->nchar;
757 while (StopLow < StopHigh) {
758 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
759 if ( StopMiddle->val == *ptr ) {
760 if ( *(ptr+1)=='\0' ) {
761 len =((char*)ptr)-word+1;
762 strcpy(info->buf,word);
763 if ( StopMiddle->isword ) {
765 info->wdata = (void*)( ((char*)node) + node->dataptr + info->datasize * StopMiddle->data );
767 if ( StopMiddle->haschild )
768 info->stack = pushStack(NULL, getChildPointer( info, StopMiddle ), len);
770 } else if ( StopMiddle->haschild ) {
771 node = getChildPointer( info, StopMiddle );
777 } else if ( StopMiddle->val < *ptr ) {
778 StopLow = StopMiddle + 1;
780 StopHigh = StopMiddle;
783 if ( StopLow >= StopHigh )
791 SFSIterate(SFSTree *info, SFSDataIO *out) {
792 SFSNodeStack *s=info->stack;
794 if ( info->hasword ) {
795 out->key = info->buf;
796 out->keylen = strlen(out->key);
797 out->data = info->wdata;
802 if ( s == NULL || s->node == NULL)
805 while ( s->level + s->node->nchar + 1 >= info->tlen ) {
807 info->buf = (char*)trealloc(info->buf, info->tlen);
810 if ( s->node->isskip ) {
811 memcpy( info->buf + s->level, ((char*)s->node) + s->node->dataptr, s->node->nchar );
812 if ( s->node->isword && !s->checkedval) {
813 info->buf[ s->level+s->node->nchar ] = '\0';
814 out->key = info->buf;
815 out->keylen = s->level+s->node->nchar;
816 out->data =((char*)(s->node->data)) + ((s->node->haschild) ? sizeof(SFSNode*) : 0);
820 if ( s->node->haschild && !s->checkedchild) {
821 info->stack = pushStack(s, getSkipChildPointer(info, s->node), s->level+s->node->nchar);
823 if ( SFSIterate(info, out) )
827 while( s->data - s->node->data < s->node->nchar ) {
828 info->buf[ s->level ] = (char)s->data->val;
829 if ( s->checkedval==0 && s->data->isword ) {
830 info->buf[ s->level+1 ] = '\0';
831 out->key = info->buf;
832 out->keylen = s->level+1;
833 out->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
837 if ( s->checkedchild==0 && s->data->haschild ) {
838 info->stack = pushStack(s,
839 getChildPointer( info, s->data ), s->level+1);
841 if ( SFSIterate(info, out) )
844 s->checkedval = s->checkedchild = 0;
848 info->stack = s->next;
851 return SFSIterate(info, out);
855 SFSRange(SFSTree *info, char *word, SFSDataIO *f, SFSDataIO *l) {
856 SFSNodeStack *s=info->stack;
858 SFSPrefixIteratorStart(info, word);
861 if ( SFSIterate(info, f) ) {
862 SFSNodeStack *sptr = info->stack, *stmp;
863 while( sptr && sptr!=s ) {
870 memcpy(l,f,sizeof(SFSDataIO));
878 while( f->keylen + s->level + 2 >= info->tlen ) {
880 info->buf = (char*)trealloc(info->buf, info->tlen);
884 l->key = info->buf + f->keylen + 1;
885 memcpy(l->key, f->key, f->keylen + 1);
888 while( f->keylen + 1 + s->level + s->node->nchar + 1 >= info->tlen ) {
890 info->buf = (char*)trealloc(info->buf, info->tlen);
892 if ( s->node->isskip ) {
893 memcpy(info->buf + f->keylen + 1 + s->level,
894 ((char*)(s->node))+s->node->dataptr, s->node->nchar);
895 s->level+=s->node->nchar;
896 if (s->node->haschild) {
897 s->node=getSkipChildPointer(info, s->node);
898 } else { /* if (s->node->isword) */
899 info->buf[ f->keylen + 1 + s->level ] = '\0';
900 l->data = (void*)(s->node->data);
901 l->keylen = s->level+1;
905 s->data = s->node->data + s->node->nchar - 1;
906 while( s->data - s->node->data >= 0 ) {
907 info->buf[ f->keylen + 1 + s->level ] = (char)s->data->val;
908 if ( s->data->haschild ) {
909 s->node = getChildPointer( info, s->data );
913 if ( s->data->isword ) {
914 info->buf[ f->keylen + 1 + s->level ] = '\0';
915 l->keylen = s->level+1;
916 l->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
925 l->key = info->buf + f->keylen + 1;
931 typedef struct WorkPlain {
938 plainNode(WorkPlain *wp, SFSNode *node) {
939 int size = getNodeSize(wp->info, node);
940 off_t myoffset = wp->offset;
942 memcpy( wp->node + wp->offset, node, size );
945 tassert( wp->offset <= wp->info->totalen );
947 if ( node->isskip ) {
949 *(Opaque*)(wp->node + myoffset + SFSNHRDSZ) =
950 plainNode(wp, getSkipChildPointer(wp->info, node));
952 SFSNodeData *nd = node->data;
955 for(i=0;i<node->nchar;i++) {
957 *(Opaque*)(wp->node + myoffset + ( ((char*)nd) - ((char*)node) ) + nd->child) =
958 plainNode(wp, getChildPointer( wp->info, nd ) );
969 SFSMakePlain(SFSTree *info, void *pointer) {
972 if ( info->plainmemory )
978 wp.node = (char*)pointer;
980 wp.node = (char*)tmalloc(sizeof(char*) * info->totalen);
982 plainNode(&wp, info->node);
983 tassert( wp.offset == info->totalen );
985 info->node = (SFSNode*)wp.node;
986 info->plainmemory = 1;
990 revertNode(SFSTree *info, SFSNode* node) {
991 int size = getNodeSize(info, node);
994 newnode = (SFSNode*)tmalloc( size );
995 memcpy(newnode, node, size);
997 if ( node->isskip ) {
999 *(SFSNode**)( (char*)(newnode->data) ) =
1000 revertNode(info, getSkipChildPointer(info, node));
1002 SFSNodeData *nd = node->data;
1003 SFSNodeData *nnd = newnode->data;
1006 for(i=0;i<node->nchar;i++) {
1008 *(SFSNode**) (((char*)nnd) + nnd->child ) =
1009 revertNode(info, getChildPointer( info, nd ));
1018 SFSRevertPlain(SFSTree *info) {
1019 void *pointer = info->node;
1021 if (! info->plainmemory )
1024 info->node = revertNode(info, info->node);
1025 info->plainmemory = 0;
1031 writeNode(WorkPlain *wp, int fd, SFSNode *node, u_int32_t extrasize) {
1032 int size = getNodeSize(wp->info, node);
1033 SFSNode *wnode = (SFSNode*)tmalloc(size);
1034 off_t myoffset = wp->offset;
1036 memcpy( wnode, node, size );
1039 tassert( wp->offset <= wp->info->totalen );
1041 if ( node->isskip ) {
1043 *(Opaque*)( ((char*)wnode) + SFSNHRDSZ) =
1044 writeNode(wp, fd, getSkipChildPointer(wp->info, node), extrasize);
1046 SFSNodeData *nd = node->data;
1049 for(i=0;i<node->nchar;i++) {
1051 *(Opaque*)(((char*)wnode) + ( ((char*)nd) - ((char*)node) ) + nd->child) =
1052 writeNode(wp, fd, getChildPointer( wp->info, nd ), extrasize );
1057 if ( lseek(fd, myoffset + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1058 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1059 if ( write(fd, wnode, size) != size )
1060 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1068 SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize) {
1070 off_t size = info->totalen + SFSTDHSZ;
1071 SFSTreeDumpHeader dh;
1073 if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 )
1074 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", filename, strerror(errno));
1076 if ( flock(fd, LOCK_EX) < 0 )
1077 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1079 if ( extrasize == 0 )
1081 else if ( extradata == NULL )
1084 if ( lseek(fd, size + MAXALIGN(extrasize) , SEEK_SET) < 0 )
1085 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1087 dh.version = SFSTREE_VERSION;
1088 dh.opaquesize = sizeof(Opaque);
1089 dh.headersize = SFSTDHSZ;
1090 dh.datasize = info->datasize;
1091 dh.totalen = info->totalen;
1092 dh.nnodes = info->nnodes;
1093 dh.extrasize = extrasize;
1095 if ( lseek(fd, 0, SEEK_SET) < 0 )
1096 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1097 if ( write(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1098 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1101 if ( write(fd, extradata, extrasize) != extrasize )
1102 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1103 if ( extrasize != MAXALIGN(extrasize) ) {
1104 char dummy[8] = {0,0,0,0,0,0,0,0};
1105 if ( write(fd, dummy, MAXALIGN(extrasize) - extrasize ) != MAXALIGN(extrasize) - extrasize )
1106 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1108 extrasize = MAXALIGN(extrasize);
1112 if ( info->plainmemory ) {
1113 if ( write(fd, info->node, info->totalen) != info->totalen )
1114 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1121 writeNode(&wp, fd, info->node, extrasize);
1129 readNode(SFSTree *info, int fd, char *buf, int bufsize, int extrasize) {
1133 size = read(fd, buf, bufsize );
1134 if ( size < SFSNHRDSZ + sizeof(void*) )
1135 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1137 size = getNodeSize(info, (SFSNode*)buf);
1138 tassert( size <= bufsize );
1139 node = (SFSNode*)tmalloc( size );
1140 memcpy(node, buf, size);
1142 if ( node->isskip ) {
1143 if (node->haschild) {
1144 if ( lseek(fd, *(Opaque*)node->data + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1145 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1146 *(SFSNode**)( (char*)(node->data) ) =
1147 readNode(info, fd, buf, bufsize, extrasize);
1150 SFSNodeData *nd = node->data;
1153 for(i=0;i<node->nchar;i++) {
1155 if ( lseek(fd, *(Opaque*)(((char*)nd) + nd->child ) + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1156 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1157 *(SFSNode**) (((char*)nd) + nd->child ) =
1158 readNode(info, fd, buf, bufsize, extrasize);
1168 SFSReadDump(SFSTree *info, char *filename, void **extradata, u_int32_t *extrasize) {
1170 SFSTreeDumpHeader dh;
1179 memset(info,0,sizeof(SFSTree));
1181 if ( (fd = open(filename, O_RDONLY)) < 0 )
1182 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno));
1183 if ( flock(fd, LOCK_SH) < 0 )
1184 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1186 if ( read(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1187 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1189 if ( dh.version != SFSTREE_VERSION )
1190 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh.version);
1191 if ( dh.opaquesize != sizeof(Opaque) )
1192 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1193 if ( dh.headersize != SFSTDHSZ )
1194 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh.headersize);
1196 info->totalen = dh.totalen;
1197 info->nnodes = dh.nnodes;
1198 info->datasize = dh.datasize;
1200 if ( dh.extrasize ) {
1201 void *pointer = tmalloc( MAXALIGN(dh.extrasize) );
1203 if ( read(fd, pointer, MAXALIGN(dh.extrasize)) != MAXALIGN(dh.extrasize) )
1204 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1207 *extradata = pointer;
1212 *extrasize = dh.extrasize;
1215 /* allocate buffer with max allowed size */
1216 bufsize = SFSNHRDSZ + 256*(sizeof(SFSNodeData) + sizeof(void*) + info->datasize);
1217 buf = tmalloc( bufsize );
1218 info->node = readNode(info, fd, buf, bufsize, MAXALIGN(dh.extrasize));
1226 SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size, void **extradata, u_int32_t *extrasize) {
1227 SFSTreeDumpHeader *dh;
1234 memset(info,0,sizeof(SFSTree));
1236 if ( size && size < SFSTDHSZ )
1237 tlog(TL_CRIT|TL_EXIT, "Memsize too small");
1239 dh = (SFSTreeDumpHeader*)pointer;
1241 if ( dh->version != SFSTREE_VERSION )
1242 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh->version);
1243 if ( dh->opaquesize != sizeof(Opaque) )
1244 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1245 if ( dh->headersize != SFSTDHSZ )
1246 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh->headersize);
1247 if ( size && size != dh->totalen + SFSTDHSZ + MAXALIGN(dh->extrasize) )
1248 tlog(TL_CRIT|TL_EXIT, "Memory size mismatch (should be %d but %d bytes)", dh->totalen + SFSTDHSZ + dh->extrasize , size);
1250 info->totalen = dh->totalen;
1251 info->nnodes = dh->nnodes;
1252 info->datasize = dh->datasize;
1253 info->plainmemory = 1;
1255 if ( dh->extrasize ) {
1257 *extradata = ((char*)pointer) + SFSTDHSZ;
1260 *extrasize = dh->extrasize;
1263 if ( info->totalen && info->nnodes )
1264 info->node = (SFSNode*)( ((char*)pointer) + SFSTDHSZ + MAXALIGN(dh->extrasize) );