X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=rset%2Frsmultior.c;h=cb755335ce89f2743db654b68b91868713e4f14f;hb=69d2833ca96efad6777b13a1e52f23a09c749509;hp=77d2aca909f6d9b037d9da37297f3f04f6b66831;hpb=b4470de12aa07f21f5394c19af0d21b196087225;p=idzebra-moved-to-github.git diff --git a/rset/rsmultior.c b/rset/rsmultior.c index 77d2aca..cb75533 100644 --- a/rset/rsmultior.c +++ b/rset/rsmultior.c @@ -1,4 +1,4 @@ -/* $Id: rsmultior.c,v 1.5 2004-08-23 12:38:53 heikki Exp $ +/* $Id: rsmultior.c,v 1.10 2004-09-09 10:08:06 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -30,27 +30,24 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include -#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); static int r_write (RSFD rfd, const void *buf); -static int r_forward(RSET ct, RSFD rfd, void *buf, - int (*cmpfunc)(const void *p1, const void *p2), +static int r_forward(RSFD rfd, void *buf, const void *untilbuf); static void r_pos (RSFD rfd, double *current, double *total); static const struct rset_control control = { "multi-or", - r_create, + r_delete, r_open, r_close, - r_delete, r_rewind, r_forward, r_pos, @@ -77,20 +74,15 @@ struct heap_item { struct heap { int heapnum; int heapmax; - int keysize; - int (*cmp)(const void *p1, const void *p2); + const struct key_control *kctrl; struct heap_item **heap; /* ptrs to the rfd */ }; typedef struct heap *HEAP; struct rset_multior_info { - int key_size; - int no_rec; - int (*cmp)(const void *p1, const void *p2); int no_rsets; RSET *rsets; - struct rset_multior_rfd *rfd_list; }; @@ -98,11 +90,7 @@ struct rset_multior_rfd { int flag; struct heap_item *items; /* we alloc and free them here */ HEAP h; - struct rset_multior_rfd *next; - struct rset_multior_info *info; zint hits; /* returned so far */ - char *prevvalue; /* to see if we are in another record */ - /* FIXME - is this really needed? */ }; #if 0 @@ -132,7 +120,7 @@ static void heap_swap (HEAP h, int x, int y) static int heap_cmp(HEAP h, int x, int y) { - return (*h->cmp)(h->heap[x]->buf,h->heap[y]->buf); + return (*h->kctrl->cmp)(h->heap[x]->buf,h->heap[y]->buf); } static int heap_empty(HEAP h) @@ -197,46 +185,58 @@ static void heap_insert (HEAP h, struct heap_item *hi) static -HEAP heap_create (int size, int key_size, - int (*cmp)(const void *p1, const void *p2)) +HEAP heap_create (NMEM nmem, int size, const struct key_control *kctrl) { - HEAP h = (HEAP) xmalloc (sizeof(*h)); + HEAP h = (HEAP) nmem_malloc (nmem, sizeof(*h)); ++size; /* heap array starts at 1 */ h->heapnum = 0; h->heapmax = size; - h->keysize = key_size; - h->cmp = cmp; - h->heap = (struct heap_item**) xmalloc((size)*sizeof(*h->heap)); + h->kctrl=kctrl; + h->heap = (struct heap_item**) nmem_malloc(nmem,size*sizeof(*h->heap)); h->heap[0]=0; /* not used */ return h; } +static void heap_clear( HEAP h) +{ + assert(h); + h->heapnum=0; +} + static void heap_destroy (HEAP h) { - xfree (h->heap); /* safe, they all point to the rfd */ - xfree (h); + /* nothing to delete, all is nmem'd, and will go away in due time */ } -static void *r_create (RSET ct, const struct rset_control *sel, void *parms) +RSET rsmultior_create( NMEM nmem, const struct key_control *kcontrol, int scope, + int no_rsets, RSET* rsets) { - rset_multior_parms *r_parms = (rset_multior_parms *) parms; + RSET rnew=rset_create_base(&control, nmem,kcontrol, scope); struct rset_multior_info *info; - info = (struct rset_multior_info *) xmalloc (sizeof(*info)); - info->key_size = r_parms->key_size; - assert (info->key_size > 1); - info->cmp = r_parms->cmp; - info->no_rsets= r_parms->no_rsets; - info->rsets=r_parms->rsets; /* now we own it! */ - info->rfd_list=0; - return info; + info = (struct rset_multior_info *) nmem_malloc(rnew->nmem,sizeof(*info)); + info->no_rsets=no_rsets; + info->rsets=(RSET*)nmem_malloc(rnew->nmem, no_rsets*sizeof(*rsets)); + memcpy(info->rsets,rsets,no_rsets*sizeof(*rsets)); + rnew->priv=info; + return rnew; +} + +static void r_delete (RSET ct) +{ + struct rset_multior_info *info = (struct rset_multior_info *) ct->priv; + int i; + for(i=0;ino_rsets;i++) + rset_delete(info->rsets[i]); } static RSFD r_open (RSET ct, int flag) { - struct rset_multior_rfd *rfd; - struct rset_multior_info *info = (struct rset_multior_info *) ct->buf; + RSFD rfd; + struct rset_multior_rfd *p; + struct rset_multior_info *info = (struct rset_multior_info *) ct->priv; + const struct key_control *kctrl = ct->keycontrol; int i; if (flag & RSETF_WRITE) @@ -244,92 +244,69 @@ static RSFD r_open (RSET ct, int flag) logf (LOG_FATAL, "multior set type is read-only"); return NULL; } - rfd = (struct rset_multior_rfd *) xmalloc (sizeof(*rfd)); - rfd->flag = flag; - rfd->next = info->rfd_list; - rfd->info = info; - info->rfd_list = rfd; - 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)); + rfd=rfd_create_base(ct); + if (rfd->priv) { + p=(struct rset_multior_rfd *)rfd->priv; + heap_clear(p->h); + assert(p->items); + /* all other pointers shouls already be allocated, in right sizes! */ + } + else { + p = (struct rset_multior_rfd *) nmem_malloc (ct->nmem,sizeof(*p)); + rfd->priv=p; + p->h = heap_create( ct->nmem, info->no_rsets, kctrl); + p->items=(struct heap_item *) nmem_malloc(ct->nmem, + info->no_rsets*sizeof(*p->items)); + for (i=0; ino_rsets; i++){ + p->items[i].rset=info->rsets[i]; + p->items[i].buf=nmem_malloc(ct->nmem,kctrl->key_size); + } + } + p->flag = flag; + p->hits=0; for (i=0; ino_rsets; i++){ - rfd->items[i].rset=info->rsets[i]; - rfd->items[i].buf=xmalloc(info->key_size); - 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) ) - heap_insert(rfd->h, &(rfd->items[i])); + p->items[i].fd=rset_open(info->rsets[i],RSETF_READ); + if ( rset_read(p->items[i].fd, p->items[i].buf) ) + heap_insert(p->h, &(p->items[i])); } return rfd; } static void r_close (RSFD rfd) { - struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd; - struct rset_multior_info *info = mrfd->info; - struct rset_multior_rfd **rfdp; + struct rset_multior_info *info=(struct rset_multior_info *)(rfd->rset->priv); + struct rset_multior_rfd *p=(struct rset_multior_rfd *)(rfd->priv); int i; - - for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next) - if (*rfdp == rfd) - { - *rfdp = (*rfdp)->next; - - heap_destroy (mrfd->h); - for (i = 0; ino_rsets; i++) { - if (mrfd->items[i].fd) - rset_close(info->rsets[i],mrfd->items[i].fd); - xfree(mrfd->items[i].buf); - } - xfree(mrfd->items); - if (mrfd->prevvalue) - xfree(mrfd->prevvalue); - xfree(mrfd); - return; - } - logf (LOG_FATAL, "r_close but no rfd match!"); - 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); + heap_destroy (p->h); + for (i = 0; ino_rsets; i++) + if (p->items[i].fd) + rset_close(p->items[i].fd); + rfd_delete_base(rfd); } + static void r_rewind (RSFD rfd) { assert(!"rewind not implemented yet"); } -static int r_forward(RSET ct, RSFD rfd, void *buf, - int (*cmpfunc)(const void *p1, const void *p2), - const void *untilbuf) +static int r_forward(RSFD rfd, void *buf, const void *untilbuf) { - struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd; - struct rset_multior_info *info = mrfd->info; + struct rset_multior_rfd *mrfd=rfd->priv; + const struct key_control *kctrl=rfd->rset->keycontrol; struct heap_item it; int rdres; if (heap_empty(mrfd->h)) return 0; - if (cmpfunc) - assert(cmpfunc==mrfd->info->cmp); it = *(mrfd->h->heap[1]); - memcpy(buf,it.buf, info->key_size); + memcpy(buf,it.buf, kctrl->key_size); (mrfd->hits)++; if (untilbuf) - rdres=rset_forward(it.rset, it.fd, it.buf, cmpfunc,untilbuf); + rdres=rset_forward(it.fd, it.buf, untilbuf); else - rdres=rset_read(it.rset, it.fd, it.buf); + rdres=rset_read(it.fd, it.buf); if ( rdres ) heap_balance(mrfd->h); else @@ -340,18 +317,18 @@ static int r_forward(RSET ct, RSFD rfd, void *buf, static int r_read (RSFD rfd, void *buf) { - return r_forward(0,rfd, buf,0,0); + return r_forward(rfd, buf,0); } static void r_pos (RSFD rfd, double *current, double *total) { - struct rset_multior_rfd *mrfd = (struct rset_multior_rfd *) rfd; - struct rset_multior_info *info = mrfd->info; + struct rset_multior_info *info=(struct rset_multior_info *)(rfd->rset->priv); + struct rset_multior_rfd *mrfd=(struct rset_multior_rfd *)(rfd->priv); 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); + rset_pos(mrfd->items[i].fd, &cur, &tot); logf(LOG_LOG, "r_pos: %d %0.1f %0.1f", i, cur,tot); scur += cur; stot += tot;