X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=rset%2Frsbetween.c;h=79b5a8aff2752efe37d15a29cf900efdef555581;hb=aa61d44969d09a45d04e8a6d30ab51875ad89e61;hp=83867fb7a258e7099569d797224d33375890933e;hpb=053c19fa30845e0d69fc55a1757bdbb652b37770;p=idzebra-moved-to-github.git
diff --git a/rset/rsbetween.c b/rset/rsbetween.c
index 83867fb..79b5a8a 100644
--- a/rset/rsbetween.c
+++ b/rset/rsbetween.c
@@ -1,297 +1,333 @@
-/*
- * Copyright (C) 1994-2002, Index Data
- * All rights reserved.
- * Heikki Levanto
+/* $Id: rsbetween.c,v 1.46 2007-01-15 15:10:19 adam Exp $
+ Copyright (C) 1995-2007
+ Index Data ApS
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+*/
+
+
+/* rsbetween is (mostly) used for xml searches. It returns the hits of the
+ * "middle" rset, that are in between the "left" and "right" rsets. For
+ * example "Shakespeare" in between "" and . The thing is
+ * complicated by the inclusion of attributes (from their own rset). If attrs
+ * specified, they must match the "left" rset (start tag). "Hamlet" between
+ * "
" and "". (This assumes that the attributes are
+ * indexed to the same seqno as the tags).
*
- * $Id: rsbetween.c,v 1.5 2002-04-12 15:25:03 heikki Exp $
- */
+*/
#include
#include
#include
#include
-#include
-#include
+#include
+#include
-static void *r_create_between(RSET ct, const struct rset_control *sel, void *parms);
-static RSFD r_open_between (RSET ct, int flag);
-static void r_close_between (RSFD rfd);
-static void r_delete_between (RSET ct);
-static void r_rewind_between (RSFD rfd);
-static int r_count_between (RSET ct);
-static int r_read_between (RSFD rfd, void *buf, int *term_index);
-static int r_write_between (RSFD rfd, const void *buf);
-static const struct rset_control control_between =
+static RSFD r_open(RSET ct, int flag);
+static void r_close(RSFD rfd);
+static void r_delete(RSET ct);
+static int r_forward(RSFD rfd, void *buf,
+ TERMID *term, const void *untilbuf);
+static int r_read(RSFD rfd, void *buf, TERMID *term );
+static int r_write(RSFD rfd, const void *buf);
+static void r_pos(RSFD rfd, double *current, double *total);
+static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
+
+static const struct rset_control control =
{
"between",
- r_create_between,
- r_open_between,
- r_close_between,
- r_delete_between,
- r_rewind_between,
- r_count_between,
- r_read_between,
- r_write_between,
+ r_delete,
+ r_get_terms,
+ r_open,
+ r_close,
+ r_forward,
+ r_pos,
+ r_read,
+ r_write,
};
-
-const struct rset_control *rset_kind_between = &control_between;
+#define STARTTAG 0
+#define HIT 1
+#define STOPTAG 2
+#define ATTRTAG 3
struct rset_between_info {
- int key_size;
- RSET rset_l;
- RSET rset_m;
- RSET rset_r;
- int term_index_s;
- int (*cmp)(const void *p1, const void *p2);
- char *(*printer)(const void *p1, char *buf);
- struct rset_between_rfd *rfd_list;
+ TERMID startterm; /* pseudo terms for detecting which one we read from */
+ TERMID stopterm;
+ TERMID attrterm;
};
struct rset_between_rfd {
- RSFD rfd_l;
- RSFD rfd_m;
- RSFD rfd_r;
- int more_l;
- int more_m;
- int more_r;
- int term_index_l;
- int term_index_m;
- int term_index_r;
- void *buf_l;
- void *buf_m;
- void *buf_r;
- int level;
- struct rset_between_rfd *next;
- struct rset_between_info *info;
+ RSFD andrfd;
+ void *recbuf; /* a key that tells which record we are in */
+ void *startbuf; /* the start tag */
+ int startbufok; /* we have seen the first start tag */
+ void *attrbuf; /* the attr tag. If these two match, we have attr match */
+ int attrbufok; /* we have seen the first attr tag, can compare */
+ int depth; /* number of start-tags without end-tags */
+ int attrdepth; /* on what depth the attr matched */
+ zint hits;
};
-static void *r_create_between (RSET ct, const struct rset_control *sel, void *parms)
+static int log_level = 0;
+static int log_level_initialized = 0;
+
+
+/* make sure that the rset has a term attached. If not, create one */
+/* we need these terms for the tags, to distinguish what we read */
+static void checkterm(RSET rs, char *tag, NMEM nmem)
{
- rset_between_parms *between_parms = (rset_between_parms *) parms;
- struct rset_between_info *info;
-
- info = (struct rset_between_info *) xmalloc (sizeof(*info));
- info->key_size = between_parms->key_size;
- info->rset_l = between_parms->rset_l;
- info->rset_m = between_parms->rset_m;
- info->rset_r = between_parms->rset_r;
- if (rset_is_volatile(info->rset_l) ||
- rset_is_volatile(info->rset_m) ||
- rset_is_volatile(info->rset_r))
- ct->flags |= RSET_FLAG_VOLATILE;
- info->cmp = between_parms->cmp;
- info->printer = between_parms->printer;
- info->rfd_list = NULL;
-
- info->term_index_s = info->rset_l->no_rset_terms;
- ct->no_rset_terms =
- info->rset_l->no_rset_terms +
- info->rset_m->no_rset_terms +
- info->rset_r->no_rset_terms;
- ct->rset_terms = (RSET_TERM *)
- xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
-
- memcpy (ct->rset_terms, info->rset_l->rset_terms,
- info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
- memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
- info->rset_m->rset_terms,
- info->rset_m->no_rset_terms * sizeof(*ct->rset_terms));
- memcpy (ct->rset_terms + info->rset_l->no_rset_terms +
- info->rset_m->no_rset_terms,
- info->rset_r->rset_terms,
- info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
- return info;
+ if (!rs->term)
+ {
+ rs->term = rset_term_create(tag, -1, "", 0, nmem, 0, 0, 0, 0);
+ rs->term->rset = rs;
+ }
}
-static RSFD r_open_between (RSET ct, int flag)
+
+RSET rset_create_between(NMEM nmem, struct rset_key_control *kcontrol,
+ int scope,
+ RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
{
- struct rset_between_info *info = (struct rset_between_info *) ct->buf;
- struct rset_between_rfd *rfd;
+ RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0, 0, 0);
+ struct rset_between_info *info=
+ (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
+ RSET rsetarray[4];
+ int n = 4;
+
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("rsbetween");
+ log_level_initialized = 1;
+ }
+ rsetarray[STARTTAG] = rset_l;
+ rsetarray[HIT] = rset_m;
+ rsetarray[STOPTAG] = rset_r;
+ rsetarray[ATTRTAG] = rset_attr;
- if (flag & RSETF_WRITE)
+ /* make sure we have decent terms for all rsets. Create dummies if needed*/
+ checkterm(rsetarray[STARTTAG], "(start)", nmem);
+ checkterm(rsetarray[STOPTAG], "(start)", nmem);
+ info->startterm = rsetarray[STARTTAG]->term;
+ info->stopterm = rsetarray[STOPTAG]->term;
+
+ if (rset_attr)
{
- logf (LOG_FATAL, "between set type is read-only");
- return NULL;
+ checkterm(rsetarray[ATTRTAG], "(start)", nmem);
+ info->attrterm = rsetarray[ATTRTAG]->term;
+ n = 4;
}
- rfd = (struct rset_between_rfd *) xmalloc (sizeof(*rfd));
- rfd->next = info->rfd_list;
- info->rfd_list = rfd;
- rfd->info = info;
-
- rfd->buf_l = xmalloc (info->key_size);
- rfd->buf_m = xmalloc (info->key_size);
- rfd->buf_r = xmalloc (info->key_size);
- rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
- rfd->rfd_m = rset_open (info->rset_m, RSETF_READ);
- rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
- rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
- &rfd->term_index_l);
- rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m,
- &rfd->term_index_m);
- rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
- &rfd->term_index_r);
- rfd->level=0;
- return rfd;
+ else
+ {
+ info->attrterm = NULL;
+ n = 3;
+ }
+ rnew->no_children = 1;
+ rnew->children = nmem_malloc(rnew->nmem, sizeof(RSET *));
+ rnew->children[0] = rset_create_and(nmem, kcontrol,
+ scope, n, rsetarray);
+ rnew->priv = info;
+ yaz_log(log_level, "create rset at %p", rnew);
+ return rnew;
}
-static void r_close_between (RSFD rfd)
+static void r_delete(RSET ct)
{
- struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
- struct rset_between_rfd **rfdp;
-
- for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
- if (*rfdp == rfd)
- {
- xfree ((*rfdp)->buf_l);
- xfree ((*rfdp)->buf_m);
- xfree ((*rfdp)->buf_r);
- rset_close (info->rset_l, (*rfdp)->rfd_l);
- rset_close (info->rset_m, (*rfdp)->rfd_m);
- rset_close (info->rset_r, (*rfdp)->rfd_r);
- *rfdp = (*rfdp)->next;
- xfree (rfd);
- return;
- }
- logf (LOG_FATAL, "r_close_between but no rfd match!");
- assert (0);
}
-static void r_delete_between (RSET ct)
+
+static RSFD r_open(RSET ct, int flag)
{
- struct rset_between_info *info = (struct rset_between_info *) ct->buf;
-
- assert (info->rfd_list == NULL);
- xfree (ct->rset_terms);
- rset_delete (info->rset_l);
- rset_delete (info->rset_m);
- rset_delete (info->rset_r);
- xfree (info);
+ RSFD rfd;
+ struct rset_between_rfd *p;
+
+ if (flag & RSETF_WRITE)
+ {
+ yaz_log(YLOG_FATAL, "between set type is read-only");
+ return NULL;
+ }
+ rfd = rfd_create_base(ct);
+ if (rfd->priv)
+ p=(struct rset_between_rfd *)rfd->priv;
+ else {
+ p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
+ rfd->priv = p;
+ p->recbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
+ p->startbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
+ p->attrbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
+ }
+ p->andrfd = rset_open(ct->children[0], RSETF_READ);
+ p->hits = -1;
+ p->depth = 0;
+ p->attrdepth = 0;
+ p->attrbufok = 0;
+ p->startbufok = 0;
+ yaz_log(log_level, "open rset=%p rfd=%p", ct, rfd);
+ return rfd;
}
-static void r_rewind_between (RSFD rfd)
+static void r_close(RSFD rfd)
{
- struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
- struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
-
- logf (LOG_DEBUG, "rsbetween_rewind");
- rset_rewind (info->rset_l, p->rfd_l);
- rset_rewind (info->rset_m, p->rfd_m);
- rset_rewind (info->rset_r, p->rfd_r);
- p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
- p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m, &p->term_index_m);
- p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
- p->level=0;
+ struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
+ yaz_log(log_level,"close rfd=%p", rfd);
+ rset_close(p->andrfd);
}
-static int r_count_between (RSET ct)
+static int r_forward(RSFD rfd, void *buf,
+ TERMID *term, const void *untilbuf)
{
- return 0;
+ struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
+ int rc;
+ yaz_log(log_level, "forwarding ");
+ rc = rset_forward(p->andrfd,buf,term,untilbuf);
+ return rc;
}
-static void logit( struct rset_between_info *info, char *prefix, void *l, void *m, void *r)
+static void checkattr(RSFD rfd)
{
- char buf_l[32];
- char buf_m[32];
- char buf_r[32];
- logf(LOG_DEBUG,"btw: %s l=%s m=%s r=%s",
- prefix,
- (*info->printer)(l, buf_l),
- (*info->printer)(m, buf_m),
- (*info->printer)(r, buf_r) );
+ struct rset_between_info *info =(struct rset_between_info *)
+ rfd->rset->priv;
+ struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
+ const struct rset_key_control *kctrl = rfd->rset->keycontrol;
+ int cmp;
+ if (p->attrdepth)
+ return; /* already found one */
+ if (!info->attrterm)
+ {
+ p->attrdepth=-1; /* matches always */
+ return;
+ }
+ if ( p->startbufok && p->attrbufok )
+ { /* have buffers to compare */
+ cmp=(kctrl->cmp)(p->startbuf,p->attrbuf);
+ if (0==cmp) /* and the keys match */
+ {
+ p->attrdepth = p->depth;
+ yaz_log(log_level, "found attribute match at depth %d",p->attrdepth);
+ }
+ }
}
-static int r_read_between (RSFD rfd, void *buf, int *term_index)
+static int r_read(RSFD rfd, void *buf, TERMID *term)
{
- struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
- struct rset_between_info *info = p->info;
- int cmp_l;
- int cmp_r;
-
- while (p->more_m)
+ struct rset_between_info *info =
+ (struct rset_between_info *)rfd->rset->priv;
+ struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
+ const struct rset_key_control *kctrl = rfd->rset->keycontrol;
+ int cmp;
+ TERMID dummyterm = 0;
+ yaz_log(log_level, "== read: term=%p",term);
+ if (!term)
+ term = &dummyterm;
+ while (rset_read(p->andrfd, buf, term))
{
- logit( info, "start of loop", p->buf_l, p->buf_m, p->buf_r);
-
- /* forward L until past m, count levels, note rec boundaries */
- if (p->more_l)
- cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
- else
- cmp_l=2; /* past this record */
- logf(LOG_DEBUG, "cmp_l=%d", cmp_l);
-
- while (cmp_l < 0) /* l before m */
- {
- if (cmp_l == -2)
- p->level=0; /* earlier record */
- if (cmp_l == -1)
- p->level++; /* relevant start tag */
- if (p->more_l)
- {
- p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
- &p->term_index_l);
- cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
- logit( info, "forwarded L", p->buf_l, p->buf_m, p->buf_r);
- logf(LOG_DEBUG, " cmp_l=%d", cmp_l);
- }
- else
- cmp_l=2;
- } /* forward L */
-
- /* forward R until past m, count levels */
- if (p->more_r)
- cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
- else
- cmp_r=2;
- logf(LOG_DEBUG, "cmp_r=%d", cmp_r);
- while (cmp_r < 0) /* r before m */
- {
- /* -2, earlier record, doesn't matter */
- if (cmp_r == -1)
- p->level--; /* relevant end tag */
- if (p->more_r)
+ yaz_log(log_level,"read loop term=%p d=%d ad=%d",
+ *term, p->depth, p->attrdepth);
+ if (p->hits<0)
+ {/* first time? */
+ memcpy(p->recbuf, buf, kctrl->key_size);
+ p->hits = 0;
+ cmp = rfd->rset->scope; /* force newrecord */
+ }
+ else {
+ cmp = (kctrl->cmp)(buf, p->recbuf);
+ yaz_log(log_level, "cmp=%d", cmp);
+ }
+
+ if (cmp>=rfd->rset->scope)
+ {
+ yaz_log(log_level, "new record");
+ p->depth = 0;
+ p->attrdepth = 0;
+ memcpy(p->recbuf, buf, kctrl->key_size);
+ }
+
+ if (*term)
+ yaz_log(log_level, " term: '%s'", (*term)->name);
+ if (*term==info->startterm)
+ {
+ p->depth++;
+ yaz_log(log_level, "read start tag. d=%d", p->depth);
+ memcpy(p->startbuf, buf, kctrl->key_size);
+ p->startbufok = 1;
+ checkattr(rfd); /* in case we already saw the attr here */
+ }
+ else if (*term==info->stopterm)
+ {
+ if (p->depth == p->attrdepth)
+ p->attrdepth = 0; /* ending the tag with attr match */
+ p->depth--;
+ yaz_log(log_level,"read end tag. d=%d ad=%d", p->depth,
+ p->attrdepth);
+ }
+ else if (*term==info->attrterm)
+ {
+ yaz_log(log_level,"read attr");
+ memcpy(p->attrbuf, buf, kctrl->key_size);
+ p->attrbufok = 1;
+ checkattr(rfd); /* in case the start tag came first */
+ }
+ else
+ { /* mut be a real hit */
+ if (p->depth && p->attrdepth)
{
- p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
- &p->term_index_r);
- cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
- logit( info, "forwarded R", p->buf_l, p->buf_m, p->buf_r);
- logf(LOG_DEBUG, " cmp_r=%d", cmp_r);
- }
- else
- cmp_r=2;
- } /* forward R */
-
- if ( ( p->level <= 0 ) && ! p->more_l)
- return 0; /* no more start tags, nothing more to find */
-
- if ( p->level > 0) /* within a tag pair (or deeper) */
- {
- memcpy (buf, p->buf_m, info->key_size);
- *term_index = p->term_index_m;
- logit( info, "Returning a hit (m)", p->buf_l, p->buf_m, p->buf_r);
- p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
- &p->term_index_m);
- return 1;
- }
- else
- if ( ! p->more_l ) /* not in data, no more starts */
- return 0; /* ergo, nothing can be found. stop scanning */
-
- p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m,
- &p->term_index_m);
- } /* while more_m */
-
- logf(LOG_DEBUG,"Exiting, no more stuff in m");
- return 0; /* no more data possible */
+ p->hits++;
+ yaz_log(log_level,"got a hit h="ZINT_FORMAT" d=%d ad=%d",
+ p->hits, p->depth, p->attrdepth);
+ return 1; /* we have everything in place already! */
+ } else
+ yaz_log(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
+ p->hits, p->depth, p->attrdepth);
+ }
+ } /* while read */
+ return 0;
} /* r_read */
-static int r_write_between (RSFD rfd, const void *buf)
+static int r_write(RSFD rfd, const void *buf)
{
- logf (LOG_FATAL, "between set type is read-only");
+ yaz_log(YLOG_FATAL, "between set type is read-only");
return -1;
}
+
+static void r_pos(RSFD rfd, double *current, double *total)
+{
+ struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
+ rset_pos(p->andrfd, current, total);
+ yaz_log(log_level, "pos: %0.1f/%0.1f ", *current, *total);
+}
+
+static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
+{
+ rset_getterms(ct->children[0], terms, maxterms, curterm);
+}
+
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+