X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=rset%2Frsmultior.c;h=cf2e29872e6baf93d3707852405060b73db64b01;hb=927733195c5646988dcc65365bb45b978b02a15a;hp=6a4944cc62001a1f8b05eff6ff79ce3bb337eaaa;hpb=3335cd5f7e7af06ac6ed943859c473e092d559a1;p=idzebra-moved-to-github.git diff --git a/rset/rsmultior.c b/rset/rsmultior.c index 6a4944c..cf2e298 100644 --- a/rset/rsmultior.c +++ b/rset/rsmultior.c @@ -1,4 +1,4 @@ -/* $Id: rsmultior.c,v 1.2 2004-08-19 12:49:15 heikki Exp $ +/* $Id: rsmultior.c,v 1.6 2004-08-24 14:25:16 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -32,27 +32,26 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include -static void *r_create(RSET ct, const struct rset_control *sel, void *parms); static RSFD r_open (RSET ct, int flag); static void r_close (RSFD rfd); static void r_delete (RSET ct); static void r_rewind (RSFD rfd); -static int r_read (RSFD rfd, void *buf, int *term_index); +static int r_read (RSFD rfd, void *buf); static int r_write (RSFD rfd, const void *buf); -static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index, +static int r_forward(RSET ct, RSFD rfd, void *buf, int (*cmpfunc)(const void *p1, const void *p2), const void *untilbuf); +static void r_pos (RSFD rfd, double *current, double *total); static const struct rset_control control = { "multi-or", - r_create, r_open, r_close, r_delete, r_rewind, r_forward, - rset_default_pos, /* FIXME */ + r_pos, r_read, r_write, }; @@ -89,7 +88,6 @@ struct rset_multior_info { int (*cmp)(const void *p1, const void *p2); int no_rsets; RSET *rsets; - int term_index; struct rset_multior_rfd *rfd_list; }; @@ -100,8 +98,9 @@ struct rset_multior_rfd { HEAP h; struct rset_multior_rfd *next; struct rset_multior_info *info; - zint *countp; + zint hits; /* returned so far */ char *prevvalue; /* to see if we are in another record */ + /* FIXME - is this really needed? */ }; #if 0 @@ -218,13 +217,39 @@ static void heap_destroy (HEAP h) } +RSET rsmultior_create( NMEM nmem, int key_size, + int (*cmp)(const void *p1, const void *p2), + int no_rsets, RSET* rsets) +{ + RSET rnew=rset_create_base(&control, nmem); + struct rset_multior_info *info; + info = (struct rset_multior_info *) nmem_malloc(rnew->nmem,sizeof(*info)); + info->key_size = key_size; + info->cmp = cmp; + info->no_rsets=no_rsets; + info->rsets=(RSET*)nmem_malloc(rnew->nmem, no_rsets*sizeof(*rsets)); + memcpy(info->rsets,rsets,no_rsets*sizeof(*rsets)); + info->rfd_list = NULL; + rnew->priv=info; + return rnew; +} + +static void r_delete (RSET ct) +{ + struct rset_multior_info *info = (struct rset_multior_info *) ct->priv; + int i; + + assert (info->rfd_list == NULL); + for(i=0;ino_rsets;i++) + rset_delete(info->rsets[i]); +/* xfree(info->rsets); */ /* nmem'd */ +/* xfree(info); */ /* nmem'd */ +} +#if 0 static void *r_create (RSET ct, const struct rset_control *sel, void *parms) { rset_multior_parms *r_parms = (rset_multior_parms *) parms; struct rset_multior_info *info; - - ct->flags |= RSET_FLAG_VOLATILE; - /* FIXME - Remove the whole flags thing, from all rsets */ info = (struct rset_multior_info *) xmalloc (sizeof(*info)); info->key_size = r_parms->key_size; assert (info->key_size > 1); @@ -232,19 +257,15 @@ static void *r_create (RSET ct, const struct rset_control *sel, void *parms) info->no_rsets= r_parms->no_rsets; info->rsets=r_parms->rsets; /* now we own it! */ info->rfd_list=0; - info->term_index=0 ; /* r_parms->rset_term; */ /*??*/ /*FIXME */ - ct->no_rset_terms = 1; - ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms)); - ct->rset_terms[0] = r_parms->rset_term; return info; } +#endif static RSFD r_open (RSET ct, int flag) { struct rset_multior_rfd *rfd; - struct rset_multior_info *info = (struct rset_multior_info *) ct->buf; + struct rset_multior_info *info = (struct rset_multior_info *) ct->priv; int i; - int dummy_termindex; if (flag & RSETF_WRITE) { @@ -256,12 +277,9 @@ static RSFD r_open (RSET ct, int flag) rfd->next = info->rfd_list; rfd->info = info; info->rfd_list = rfd; - if (ct->no_rset_terms==1) - rfd->countp=&ct->rset_terms[0]->count; - else - rfd->countp=0; rfd->h = heap_create( info->no_rsets, info->key_size, info->cmp); rfd->prevvalue=0; + rfd->hits=0; rfd->items=(struct heap_item *) xmalloc(info->no_rsets*sizeof(*rfd->items)); for (i=0; ino_rsets; i++){ rfd->items[i].rset=info->rsets[i]; @@ -269,7 +287,7 @@ static RSFD r_open (RSET ct, int flag) rfd->items[i].fd=rset_open(info->rsets[i],RSETF_READ); /* if (item_readbuf(&(rfd->items[i]))) */ if ( rset_read(rfd->items[i].rset, rfd->items[i].fd, - rfd->items[i].buf, &dummy_termindex) ) + rfd->items[i].buf) ) heap_insert(rfd->h, &(rfd->items[i])); } return rfd; @@ -303,21 +321,6 @@ static void r_close (RSFD rfd) assert (0); } -static void r_delete (RSET ct) -{ - struct rset_multior_info *info = (struct rset_multior_info *) ct->buf; - int i; - - assert (info->rfd_list == NULL); - for(i=0;ino_rsets;i++) - rset_delete(info->rsets[i]); - xfree(info->rsets); - xfree(info); - - for (i = 0; ino_rset_terms; i++) /* usually only 1 */ - rset_term_destroy (ct->rset_terms[i]); - xfree (ct->rset_terms); -} static void r_rewind (RSFD rfd) { @@ -325,7 +328,7 @@ static void r_rewind (RSFD rfd) } -static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index, +static int r_forward(RSET ct, RSFD rfd, void *buf, int (*cmpfunc)(const void *p1, const void *p2), const void *untilbuf) { @@ -333,29 +336,17 @@ static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index, struct rset_multior_info *info = mrfd->info; struct heap_item it; int rdres; - int dummycount=0; if (heap_empty(mrfd->h)) return 0; if (cmpfunc) assert(cmpfunc==mrfd->info->cmp); - *term_index=0; it = *(mrfd->h->heap[1]); memcpy(buf,it.buf, info->key_size); - if (mrfd->countp) { - if (mrfd->prevvalue) { /* in another record */ - if ( (*mrfd->info->cmp)(mrfd->prevvalue,it.buf) < -1) - (*mrfd->countp)++; - } else { - mrfd->prevvalue=xmalloc(info->key_size); - (*mrfd->countp)++; - } - memcpy(mrfd->prevvalue,it.buf, info->key_size); - } + (mrfd->hits)++; if (untilbuf) - rdres=rset_forward(it.rset, it.fd, it.buf, &dummycount, - cmpfunc,untilbuf); + rdres=rset_forward(it.rset, it.fd, it.buf, cmpfunc,untilbuf); else - rdres=rset_read(it.rset, it.fd, it.buf, &dummycount); + rdres=rset_read(it.rset, it.fd, it.buf); if ( rdres ) heap_balance(mrfd->h); else @@ -364,68 +355,33 @@ static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index, } -static int r_read (RSFD rfd, void *buf, int *term_index) +static int r_read (RSFD rfd, void *buf) { - return r_forward(0,rfd, buf, term_index,0,0); + return r_forward(0,rfd, buf,0,0); } -#if 0 -static int old_read +static void r_pos (RSFD rfd, double *current, double *total) { struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd; - struct trunc_info *ti = mrfd->ti; - int n = ti->indx[ti->ptr[1]]; - - if (!ti->heapnum) - return 0; - *term_index = 0; - memcpy (buf, ti->heap[ti->ptr[1]], ti->keysize); - if (((struct rset_multior_rfd *) rfd)->position) - { - if (isc_pp_read (((struct rset_multior_rfd *) rfd)->ispt[n], ti->tmpbuf)) - { - heap_delete (ti); - if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1) - ((struct rset_multior_rfd *) rfd)->position--; - heap_insert (ti, ti->tmpbuf, n); - } - else - heap_delete (ti); - if (mrfd->countp && ( - *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1)) - { - memcpy (mrfd->pbuf, buf, ti->keysize); - (*mrfd->countp)++; - } - return 1; - } - while (1) - { - if (!isc_pp_read (((struct rset_multior_rfd *) rfd)->ispt[n], ti->tmpbuf)) - { - heap_delete (ti); - break; - } - if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1) - { - heap_delete (ti); - heap_insert (ti, ti->tmpbuf, n); - break; - } + struct rset_multior_info *info = mrfd->info; + double cur, tot; + double scur=0.0, stot=0.0; + int i; + for (i=0; ino_rsets; i++){ + rset_pos(mrfd->items[i].rset, mrfd->items[i].fd, &cur, &tot); + logf(LOG_LOG, "r_pos: %d %0.1f %0.1f", i, cur,tot); + scur += cur; + stot += tot; } - if (mrfd->countp && ( - *mrfd->countp == 0 || (*ti->cmp)(buf, mrfd->pbuf) > 1)) - { - memcpy (mrfd->pbuf, buf, ti->keysize); - (*mrfd->countp)++; + if (stot <1.0) { /* nothing there */ + *current=0; + *total=0; + return; } - return 1; + *current=mrfd->hits; + *total=*current*stot/scur; } -#endif -/*!*/ /* FIXME Forward */ -/*!*/ /* FIXME pos */ - static int r_write (RSFD rfd, const void *buf) { logf (LOG_FATAL, "multior set type is read-only");