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;
804 if ( s->node == NULL ) {
805 info->stack = s->next;
807 return SFSIterate(info, out);
810 while ( s->level + s->node->nchar + 1 >= info->tlen ) {
812 info->buf = (char*)trealloc(info->buf, info->tlen);
815 if ( s->node->isskip ) {
816 memcpy( info->buf + s->level, ((char*)s->node) + s->node->dataptr, s->node->nchar );
817 if ( s->node->isword && !s->checkedval) {
818 info->buf[ s->level+s->node->nchar ] = '\0';
819 out->key = info->buf;
820 out->keylen = s->level+s->node->nchar;
821 out->data =((char*)(s->node->data)) + ((s->node->haschild) ? sizeof(SFSNode*) : 0);
825 if ( s->node->haschild && !s->checkedchild) {
826 info->stack = pushStack(s, getSkipChildPointer(info, s->node), s->level+s->node->nchar);
828 if ( SFSIterate(info, out) )
832 while( s->data - s->node->data < s->node->nchar ) {
833 info->buf[ s->level ] = (char)s->data->val;
834 if ( s->checkedval==0 && s->data->isword ) {
835 info->buf[ s->level+1 ] = '\0';
836 out->key = info->buf;
837 out->keylen = s->level+1;
838 out->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
842 if ( s->checkedchild==0 && s->data->haschild ) {
843 info->stack = pushStack(s,
844 getChildPointer( info, s->data ), s->level+1);
846 if ( SFSIterate(info, out) )
849 s->checkedval = s->checkedchild = 0;
853 info->stack = s->next;
856 return SFSIterate(info, out);
860 SFSRange(SFSTree *info, char *word, SFSDataIO *f, SFSDataIO *l) {
861 SFSNodeStack *s=info->stack;
863 SFSPrefixIteratorStart(info, word);
866 if ( SFSIterate(info, f) ) {
867 SFSNodeStack *sptr = info->stack, *stmp;
868 while( sptr && sptr!=s ) {
875 memcpy(l,f,sizeof(SFSDataIO));
883 while( f->keylen + s->level + 2 >= info->tlen ) {
885 info->buf = (char*)trealloc(info->buf, info->tlen);
889 l->key = info->buf + f->keylen + 1;
890 memcpy(l->key, f->key, f->keylen + 1);
893 while( f->keylen + 1 + s->level + s->node->nchar + 1 >= info->tlen ) {
895 info->buf = (char*)trealloc(info->buf, info->tlen);
897 if ( s->node->isskip ) {
898 memcpy(info->buf + f->keylen + 1 + s->level,
899 ((char*)(s->node))+s->node->dataptr, s->node->nchar);
900 s->level+=s->node->nchar;
901 if (s->node->haschild) {
902 s->node=getSkipChildPointer(info, s->node);
903 } else { /* if (s->node->isword) */
904 info->buf[ f->keylen + 1 + s->level ] = '\0';
905 l->data = (void*)(s->node->data);
906 l->keylen = s->level+1;
910 s->data = s->node->data + s->node->nchar - 1;
911 while( s->data - s->node->data >= 0 ) {
912 info->buf[ f->keylen + 1 + s->level ] = (char)s->data->val;
913 if ( s->data->haschild ) {
914 s->node = getChildPointer( info, s->data );
918 if ( s->data->isword ) {
919 info->buf[ f->keylen + 1 + s->level ] = '\0';
920 l->keylen = s->level+1;
921 l->data =((char*)s->node) + s->node->dataptr + info->datasize * s->data->data;
930 l->key = info->buf + f->keylen + 1;
936 typedef struct WorkPlain {
943 plainNode(WorkPlain *wp, SFSNode *node) {
944 int size = getNodeSize(wp->info, node);
945 off_t myoffset = wp->offset;
947 memcpy( wp->node + wp->offset, node, size );
950 tassert( wp->offset <= wp->info->totalen );
952 if ( node->isskip ) {
954 *(Opaque*)(wp->node + myoffset + SFSNHRDSZ) =
955 plainNode(wp, getSkipChildPointer(wp->info, node));
957 SFSNodeData *nd = node->data;
960 for(i=0;i<node->nchar;i++) {
962 *(Opaque*)(wp->node + myoffset + ( ((char*)nd) - ((char*)node) ) + nd->child) =
963 plainNode(wp, getChildPointer( wp->info, nd ) );
974 SFSMakePlain(SFSTree *info, void *pointer) {
977 if ( info->plainmemory )
983 wp.node = (char*)pointer;
985 wp.node = (char*)tmalloc(sizeof(char*) * info->totalen);
987 plainNode(&wp, info->node);
988 tassert( wp.offset == info->totalen );
990 info->node = (SFSNode*)wp.node;
991 info->plainmemory = 1;
995 revertNode(SFSTree *info, SFSNode* node) {
996 int size = getNodeSize(info, node);
999 newnode = (SFSNode*)tmalloc( size );
1000 memcpy(newnode, node, size);
1002 if ( node->isskip ) {
1004 *(SFSNode**)( (char*)(newnode->data) ) =
1005 revertNode(info, getSkipChildPointer(info, node));
1007 SFSNodeData *nd = node->data;
1008 SFSNodeData *nnd = newnode->data;
1011 for(i=0;i<node->nchar;i++) {
1013 *(SFSNode**) (((char*)nnd) + nnd->child ) =
1014 revertNode(info, getChildPointer( info, nd ));
1023 SFSRevertPlain(SFSTree *info) {
1024 void *pointer = info->node;
1026 if (! info->plainmemory )
1029 info->node = revertNode(info, info->node);
1030 info->plainmemory = 0;
1036 writeNode(WorkPlain *wp, int fd, SFSNode *node, u_int32_t extrasize) {
1037 int size = getNodeSize(wp->info, node);
1038 SFSNode *wnode = (SFSNode*)tmalloc(size);
1039 off_t myoffset = wp->offset;
1041 memcpy( wnode, node, size );
1044 tassert( wp->offset <= wp->info->totalen );
1046 if ( node->isskip ) {
1048 *(Opaque*)( ((char*)wnode) + SFSNHRDSZ) =
1049 writeNode(wp, fd, getSkipChildPointer(wp->info, node), extrasize);
1051 SFSNodeData *nd = node->data;
1054 for(i=0;i<node->nchar;i++) {
1056 *(Opaque*)(((char*)wnode) + ( ((char*)nd) - ((char*)node) ) + nd->child) =
1057 writeNode(wp, fd, getChildPointer( wp->info, nd ), extrasize );
1062 if ( lseek(fd, myoffset + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1063 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1064 if ( write(fd, wnode, size) != size )
1065 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1073 SFSWriteDump(SFSTree *info, char *filename, void *extradata, u_int32_t extrasize) {
1075 off_t size = info->totalen + SFSTDHSZ;
1076 SFSTreeDumpHeader dh;
1078 if ( (fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0 )
1079 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", filename, strerror(errno));
1081 if ( flock(fd, LOCK_EX) < 0 )
1082 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1084 if ( extrasize == 0 )
1086 else if ( extradata == NULL )
1089 if ( lseek(fd, size + MAXALIGN(extrasize) , SEEK_SET) < 0 )
1090 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1092 dh.version = SFSTREE_VERSION;
1093 dh.opaquesize = sizeof(Opaque);
1094 dh.headersize = SFSTDHSZ;
1095 dh.datasize = info->datasize;
1096 dh.totalen = info->totalen;
1097 dh.nnodes = info->nnodes;
1098 dh.extrasize = extrasize;
1100 if ( lseek(fd, 0, SEEK_SET) < 0 )
1101 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1102 if ( write(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1103 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1106 if ( write(fd, extradata, extrasize) != extrasize )
1107 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1108 if ( extrasize != MAXALIGN(extrasize) ) {
1109 char dummy[8] = {0,0,0,0,0,0,0,0};
1110 if ( write(fd, dummy, MAXALIGN(extrasize) - extrasize ) != MAXALIGN(extrasize) - extrasize )
1111 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1113 extrasize = MAXALIGN(extrasize);
1117 if ( info->plainmemory ) {
1118 if ( write(fd, info->node, info->totalen) != info->totalen )
1119 tlog(TL_CRIT|TL_EXIT, "write failed: %s", strerror(errno));
1126 writeNode(&wp, fd, info->node, extrasize);
1134 readNode(SFSTree *info, int fd, char *buf, int bufsize, int extrasize) {
1138 size = read(fd, buf, bufsize );
1139 if ( size < SFSNHRDSZ + sizeof(void*) )
1140 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1142 size = getNodeSize(info, (SFSNode*)buf);
1143 tassert( size <= bufsize );
1144 node = (SFSNode*)tmalloc( size );
1145 memcpy(node, buf, size);
1147 if ( node->isskip ) {
1148 if (node->haschild) {
1149 if ( lseek(fd, *(Opaque*)node->data + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1150 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1151 *(SFSNode**)( (char*)(node->data) ) =
1152 readNode(info, fd, buf, bufsize, extrasize);
1155 SFSNodeData *nd = node->data;
1158 for(i=0;i<node->nchar;i++) {
1160 if ( lseek(fd, *(Opaque*)(((char*)nd) + nd->child ) + SFSTDHSZ + extrasize, SEEK_SET) < 0 )
1161 tlog(TL_CRIT|TL_EXIT, "lseek failed: %s", strerror(errno));
1162 *(SFSNode**) (((char*)nd) + nd->child ) =
1163 readNode(info, fd, buf, bufsize, extrasize);
1173 SFSReadDump(SFSTree *info, char *filename, void **extradata, u_int32_t *extrasize) {
1175 SFSTreeDumpHeader dh;
1184 memset(info,0,sizeof(SFSTree));
1186 if ( (fd = open(filename, O_RDONLY)) < 0 )
1187 tlog(TL_CRIT|TL_EXIT, "Can not open file '%s': %s", strerror(errno));
1188 if ( flock(fd, LOCK_SH) < 0 )
1189 tlog(TL_CRIT|TL_EXIT, "flock failed: %s", strerror(errno));
1191 if ( read(fd, &dh, SFSTDHSZ) != SFSTDHSZ )
1192 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1194 if ( dh.version != SFSTREE_VERSION )
1195 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh.version);
1196 if ( dh.opaquesize != sizeof(Opaque) )
1197 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1198 if ( dh.headersize != SFSTDHSZ )
1199 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh.headersize);
1201 info->totalen = dh.totalen;
1202 info->nnodes = dh.nnodes;
1203 info->datasize = dh.datasize;
1205 if ( dh.extrasize ) {
1206 void *pointer = tmalloc( MAXALIGN(dh.extrasize) );
1208 if ( read(fd, pointer, MAXALIGN(dh.extrasize)) != MAXALIGN(dh.extrasize) )
1209 tlog(TL_CRIT|TL_EXIT, "read failed: %s", strerror(errno));
1212 *extradata = pointer;
1217 *extrasize = dh.extrasize;
1220 /* allocate buffer with max allowed size */
1221 bufsize = SFSNHRDSZ + 256*(sizeof(SFSNodeData) + sizeof(void*) + info->datasize);
1222 buf = tmalloc( bufsize );
1223 info->node = readNode(info, fd, buf, bufsize, MAXALIGN(dh.extrasize));
1231 SFSInitFromDump(SFSTree *info, void *pointer, u_int64_t size, void **extradata, u_int32_t *extrasize) {
1232 SFSTreeDumpHeader *dh;
1239 memset(info,0,sizeof(SFSTree));
1241 if ( size && size < SFSTDHSZ )
1242 tlog(TL_CRIT|TL_EXIT, "Memsize too small");
1244 dh = (SFSTreeDumpHeader*)pointer;
1246 if ( dh->version != SFSTREE_VERSION )
1247 tlog(TL_CRIT|TL_EXIT, "Tree version mismatch (should be 0x%04x but 0x%04x)", SFSTREE_VERSION, dh->version);
1248 if ( dh->opaquesize != sizeof(Opaque) )
1249 tlog(TL_CRIT|TL_EXIT, "sizeof(Opaque) mismatch");
1250 if ( dh->headersize != SFSTDHSZ )
1251 tlog(TL_CRIT|TL_EXIT, "Tree's header size mismatch (should be %d but %d bytes)", SFSTDHSZ, dh->headersize);
1252 if ( size && size != dh->totalen + SFSTDHSZ + MAXALIGN(dh->extrasize) )
1253 tlog(TL_CRIT|TL_EXIT, "Memory size mismatch (should be %d but %d bytes)", dh->totalen + SFSTDHSZ + dh->extrasize , size);
1255 info->totalen = dh->totalen;
1256 info->nnodes = dh->nnodes;
1257 info->datasize = dh->datasize;
1258 info->plainmemory = 1;
1260 if ( dh->extrasize ) {
1262 *extradata = ((char*)pointer) + SFSTDHSZ;
1265 *extrasize = dh->extrasize;
1268 if ( info->totalen && info->nnodes )
1269 info->node = (SFSNode*)( ((char*)pointer) + SFSTDHSZ + MAXALIGN(dh->extrasize) );