-/* $Id: zsets.c,v 1.67 2004-10-22 10:58:28 heikki Exp $
- Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
- Index Data Aps
+/* $Id: zsets.c,v 1.102 2006-05-18 12:03:05 adam Exp $
+ Copyright (C) 1995-2006
+ Index Data ApS
This file is part of the Zebra server.
#endif
#include "index.h"
+#include "rank.h"
+#include <yaz/diagbib1.h>
#include <rset.h>
#define SORT_IDX_ENTRYSIZE 64
int use;
char *term;
};
+
struct zebra_set {
char *name;
RSET rset;
int num_bases;
char **basenames;
Z_RPNQuery *rpn;
+ Z_SortKeySpecList *sortSpec;
struct zset_sort_info *sort_info;
struct zebra_set_term_entry *term_entries;
int term_entries_max;
struct zebra_set *next;
int locked;
+
+ zint cache_position; /* last position */
+ RSFD cache_rfd; /* rfd (NULL if not existing) */
+ zint cache_psysno; /* sysno for last position */
+ zint approx_limit; /* limit before we do approx */
};
struct zset_sort_entry {
struct zset_sort_entry **entries;
};
-ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m,
- Z_RPNQuery *rpn, int num_bases,
- char **basenames,
- const char *setname)
+static int log_level_set=0;
+static int log_level_sort=0;
+static int log_level_searchhits=0;
+static int log_level_searchterms=0;
+static int log_level_resultsets=0;
+
+static void loglevels()
+{
+ if (log_level_set)
+ return;
+ log_level_sort = yaz_log_module_level("sorting");
+ log_level_searchhits = yaz_log_module_level("searchhits");
+ log_level_searchterms = yaz_log_module_level("searchterms");
+ log_level_resultsets = yaz_log_module_level("resultsets");
+ log_level_set = 1;
+}
+
+ZEBRA_RES resultSetSearch(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
+ Z_RPNQuery *rpn, ZebraSet sset)
+{
+ RSET rset = 0;
+ oident *attrset;
+ Z_SortKeySpecList *sort_sequence;
+ int sort_status, i;
+ ZEBRA_RES res = ZEBRA_OK;
+
+ zh->hits = 0;
+
+ sort_sequence = (Z_SortKeySpecList *)
+ nmem_malloc(nmem, sizeof(*sort_sequence));
+ sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
+ sort_sequence->specs = (Z_SortKeySpec **)
+ nmem_malloc(nmem, sort_sequence->num_specs *
+ sizeof(*sort_sequence->specs));
+ for (i = 0; i<sort_sequence->num_specs; i++)
+ sort_sequence->specs[i] = 0;
+
+ attrset = oid_getentbyoid (rpn->attributeSetId);
+ res = rpn_search_top(zh, rpn->RPNStructure, attrset->value,
+ nmem, rset_nmem,
+ sort_sequence,
+ sset->num_bases, sset->basenames,
+ &rset);
+ if (res != ZEBRA_OK)
+ {
+ sset->rset = 0;
+ return res;
+ }
+ for (i = 0; sort_sequence->specs[i]; i++)
+ ;
+ sort_sequence->num_specs = i;
+ rset->hits_limit = sset->approx_limit;
+ if (!i)
+ {
+ res = resultSetRank (zh, sset, rset, rset_nmem);
+ }
+ else
+ {
+ res = resultSetSortSingle (zh, nmem, sset, rset,
+ sort_sequence, &sort_status);
+ }
+ sset->rset = rset;
+ return res;
+}
+
+
+ZEBRA_RES resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
+ int num_bases, char **basenames,
+ const char *setname)
{
ZebraSet zebraSet;
int i;
+ ZEBRA_RES res;
- zh->errCode = 0;
- zh->errString = NULL;
zh->hits = 0;
- zebraSet = resultSetAdd (zh, setname, 1);
+ zebraSet = resultSetAdd(zh, setname, 1);
if (!zebraSet)
- return 0;
+ return ZEBRA_FAIL;
zebraSet->locked = 1;
zebraSet->rpn = 0;
zebraSet->nmem = m;
- zebraSet->rset_nmem=nmem_create();
+ zebraSet->rset_nmem = nmem_create();
zebraSet->num_bases = num_bases;
zebraSet->basenames =
nmem_malloc (zebraSet->nmem, num_bases * sizeof(*zebraSet->basenames));
for (i = 0; i<num_bases; i++)
- zebraSet->basenames[i] = nmem_strdup (zebraSet->nmem, basenames[i]);
+ zebraSet->basenames[i] = nmem_strdup(zebraSet->nmem, basenames[i]);
-
- zebraSet->rset = rpn_search (zh, zebraSet->nmem, zebraSet->rset_nmem,
- rpn, zebraSet->num_bases,
- zebraSet->basenames, zebraSet->name,
- zebraSet);
+ res = resultSetSearch(zh, zebraSet->nmem, zebraSet->rset_nmem,
+ rpn, zebraSet);
zh->hits = zebraSet->hits;
if (zebraSet->rset)
zebraSet->rpn = rpn;
zebraSet->locked = 0;
- return zebraSet;
+ if (!zebraSet->rset)
+ return ZEBRA_FAIL;
+ return res;
}
void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
(s->hits)++;
}
-#if 0 /* FIXME - Delete this, we don't count terms no more */
-int zebra_resultSetTerms (ZebraHandle zh, const char *setname,
- int no, zint *count,
- int *type, char *out, size_t *len)
-{
- ZebraSet s = resultSetGet (zh, setname);
- int no_max = 0;
-
- if (count)
- *count = 0;
- if (!s || !s->rset)
- return 0;
- no_max = s->rset->no_rset_terms;
- if (no < 0 || no >= no_max)
- return 0;
- if (count)
- *count = s->rset->rset_terms[no]->count;
- if (type)
- *type = s->rset->rset_terms[no]->type;
-
- if (out)
- {
- char *inbuf = s->rset->rset_terms[no]->name;
- size_t inleft = strlen(inbuf);
- size_t outleft = *len - 1;
- int converted = 0;
-
- if (zh->iconv_from_utf8 != 0)
- {
- char *outbuf = out;
- size_t ret;
-
- ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
- &outbuf, &outleft);
- if (ret == (size_t)(-1))
- *len = 0;
- else
- *len = outbuf - out;
- converted = 1;
- }
- if (!converted)
- {
- if (inleft > outleft)
- inleft = outleft;
- *len = inleft;
- memcpy (out, inbuf, *len);
- }
- out[*len] = 0;
- }
- return no_max;
-}
-
-#endif
-
-ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
+ZebraSet resultSetAdd(ZebraHandle zh, const char *name, int ov)
{
ZebraSet s;
int i;
for (s = zh->sets; s; s = s->next)
if (!strcmp (s->name, name))
break;
+
+ if (!log_level_set)
+ loglevels();
if (s)
{
- yaz_log (LOG_DEBUG, "updating result set %s", name);
+ yaz_log(log_level_resultsets, "updating result set %s", name);
if (!ov || s->locked)
return NULL;
if (s->rset)
+ {
+ if (s->cache_rfd)
+ rset_close(s->cache_rfd);
rset_delete (s->rset);
+ }
if (s->rset_nmem)
nmem_destroy (s->rset_nmem);
if (s->nmem)
{
const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
- yaz_log (LOG_DEBUG, "adding result set %s", name);
+ yaz_log(log_level_resultsets, "adding result set %s", name);
s = (ZebraSet) xmalloc (sizeof(*s));
s->next = zh->sets;
zh->sets = s;
s->term_entries = 0;
s->hits = 0;
s->rset = 0;
- s->rset_nmem=0;
+ s->rset_nmem = 0;
s->nmem = 0;
s->rpn = 0;
+ s->sortSpec = 0;
+ s->cache_position = 0;
+ s->cache_rfd = 0;
+ s->approx_limit = zh->approx_limit;
return s;
}
-ZebraSet resultSetGet (ZebraHandle zh, const char *name)
+ZebraSet resultSetGet(ZebraHandle zh, const char *name)
{
ZebraSet s;
if (!s->term_entries && !s->rset && s->rpn)
{
NMEM nmem = nmem_create ();
- yaz_log (LOG_LOG, "research %s", name);
+ yaz_log(log_level_resultsets, "research %s", name);
if (!s->rset_nmem)
s->rset_nmem=nmem_create();
- s->rset =
- rpn_search (zh, nmem, s->rset_nmem, s->rpn, s->num_bases,
- s->basenames, s->name, s);
+ resultSetSearch(zh, nmem, s->rset_nmem, s->rpn, s);
+ if (s->rset && s->sortSpec)
+ {
+ int sort_status;
+ yaz_log(log_level_resultsets, "resort %s", name);
+ resultSetSortSingle (zh, nmem, s, s->rset, s->sortSpec,
+ &sort_status);
+ }
nmem_destroy (nmem);
}
return s;
{
ZebraSet s = zh->sets;
+ yaz_log(log_level_resultsets, "invalidating result sets");
for (; s; s = s->next)
{
if (s->rset)
+ {
+ if (s->cache_rfd)
+ rset_close(s->cache_rfd);
rset_delete (s->rset);
+ }
s->rset = 0;
+ s->cache_rfd = 0;
+ s->cache_position = 0;
if (s->rset_nmem)
nmem_destroy(s->rset_nmem);
s->rset_nmem=0;
}
}
-void resultSetDestroy (ZebraHandle zh, int num, char **names,int *statuses)
+void resultSetDestroy(ZebraHandle zh, int num, char **names,int *statuses)
{
ZebraSet * ss = &zh->sets;
int i;
if (statuses)
for (i = 0; i<num; i++)
statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
- zh->errCode = 0;
- zh->errString = NULL;
while (*ss)
{
int i = -1;
if (s->nmem)
nmem_destroy (s->nmem);
if (s->rset)
+ {
+ if (s->cache_rfd)
+ rset_close(s->cache_rfd);
rset_delete (s->rset);
+ }
if (s->rset_nmem)
nmem_destroy(s->rset_nmem);
xfree (s->name);
}
}
-ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
- int num, int *positions)
+ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
+ const char *name,
+ zint start, int num)
+{
+ zint pos_small[10];
+ zint *pos = pos_small;
+ ZebraMetaRecord *mr;
+ int i;
+
+ if (num > 10000 || num <= 0)
+ return 0;
+
+ if (num > 10)
+ pos = xmalloc(sizeof(*pos) * num);
+
+ for (i = 0; i<num; i++)
+ pos[i] = start+i;
+
+ mr = zebra_meta_records_create(zh, name, num, pos);
+
+ if (num > 10)
+ xfree(pos);
+ return mr;
+}
+
+ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh, const char *name,
+ int num, zint *positions)
{
ZebraSet sset;
- ZebraPosSet sr = 0;
+ ZebraMetaRecord *sr = 0;
RSET rset;
int i;
struct zset_sort_info *sort_info;
+ size_t sysno_mem_index = 0;
+
+ if (zh->m_staticrank)
+ sysno_mem_index = 1;
+ if (!log_level_set)
+ loglevels();
if (!(sset = resultSetGet (zh, name)))
return NULL;
if (!(rset = sset->rset))
{
if (!sset->term_entries)
return 0;
- sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
+ sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
for (i = 0; i<num; i++)
{
sr[i].sysno = 0;
}
else
{
- sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
+ sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
for (i = 0; i<num; i++)
{
sr[i].sysno = 0;
sort_info = sset->sort_info;
if (sort_info)
{
- int position;
+ zint position;
for (i = 0; i<num; i++)
{
position = positions[i];
if (position > 0 && position <= sort_info->num_entries)
{
- yaz_log (LOG_DEBUG, "got pos=%d (sorted)", position);
+ yaz_log(log_level_sort, "got pos=" ZINT_FORMAT
+ " (sorted)", position);
sr[i].sysno = sort_info->entries[position-1]->sysno;
sr[i].score = sort_info->entries[position-1]->score;
}
}
if (i < num) /* nope, get the rest, unsorted - sorry */
{
- int position = 0;
+ zint position = 0;
int num_i = 0;
zint psysno = 0;
RSFD rfd;
if (sort_info)
position = sort_info->num_entries;
- while (num_i < num && positions[num_i] < position)
+ while (num_i < num && positions[num_i] <= position)
num_i++;
- rfd = rset_open (rset, RSETF_READ);
+
+ if (sset->cache_rfd &&
+ num_i < num && positions[num_i] > sset->cache_position)
+ {
+ position = sset->cache_position;
+ rfd = sset->cache_rfd;
+ psysno = sset->cache_psysno;
+ }
+ else
+ {
+ if (sset->cache_rfd)
+ rset_close(sset->cache_rfd);
+ rfd = rset_open (rset, RSETF_READ);
+ }
while (num_i < num && rset_read (rfd, &key, 0))
{
- zint this_sys = key.mem[0];
+ zint this_sys = key.mem[sysno_mem_index];
if (this_sys != psysno)
{
psysno = this_sys;
if (position == positions[num_i])
{
sr[num_i].sysno = psysno;
- yaz_log (LOG_DEBUG, "got pos=%d (unsorted)", position);
+ yaz_log(log_level_sort, "got pos=" ZINT_FORMAT " (unsorted)", position);
sr[num_i].score = -1;
num_i++;
}
}
}
- rset_close (rfd);
+ sset->cache_position = position;
+ sset->cache_psysno = psysno;
+ sset->cache_rfd = rfd;
}
}
return sr;
}
-void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
+void zebra_meta_records_destroy (ZebraHandle zh, ZebraMetaRecord *records,
+ int num)
{
assert(zh); /* compiler shut up about unused arg */
xfree (records);
struct sortKeyInfo {
int relation;
+#if 0
int attrUse;
+#else
+ int ord;
+#endif
int numerical;
};
sortIdx_sysno (zh->reg->sortIdx, sysno);
for (i = 0; i<num_criteria; i++)
{
- sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
+ sortIdx_type (zh->reg->sortIdx, criteria[i].ord);
sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
}
i = sort_info->num_entries;
new_entry->score = score;
}
-void resultSetSort (ZebraHandle zh, NMEM nmem,
- int num_input_setnames, const char **input_setnames,
- const char *output_setname,
- Z_SortKeySpecList *sort_sequence, int *sort_status)
+static Z_RPNQuery *copy_RPNQuery(Z_RPNQuery *src, NMEM nmem)
+{
+ Z_RPNQuery *dst = 0;
+ ODR encode = odr_createmem(ODR_ENCODE);
+ ODR decode = odr_createmem(ODR_DECODE);
+
+ if (z_RPNQuery(encode, &src, 0, 0))
+ {
+ int len;
+ char *buf = odr_getbuf(encode, &len, 0);
+
+ if (buf)
+ {
+ odr_setbuf(decode, buf, len, 0);
+ z_RPNQuery(decode, &dst, 0, 0);
+ }
+ }
+ nmem_transfer(nmem, decode->mem);
+ odr_destroy(encode);
+ odr_destroy(decode);
+ return dst;
+}
+
+static Z_SortKeySpecList *copy_SortKeySpecList(Z_SortKeySpecList *src, NMEM nmem)
+{
+ Z_SortKeySpecList *dst = 0;
+ ODR encode = odr_createmem(ODR_ENCODE);
+ ODR decode = odr_createmem(ODR_DECODE);
+
+ if (z_SortKeySpecList(encode, &src, 0, 0))
+ {
+ int len;
+ char *buf = odr_getbuf(encode, &len, 0);
+
+ if (buf)
+ {
+ odr_setbuf(decode, buf, len, 0);
+ z_SortKeySpecList(decode, &dst, 0, 0);
+ }
+ }
+ nmem_transfer(nmem, decode->mem);
+ odr_destroy(encode);
+ odr_destroy(decode);
+ return dst;
+}
+
+ZebraSet resultSetClone(ZebraHandle zh, const char *setname,
+ ZebraSet rset)
+{
+ ZebraSet nset;
+ int i;
+
+ nset = resultSetAdd(zh, setname, 1);
+ if (!nset)
+ return 0;
+
+ nset->nmem = nmem_create();
+
+ nset->num_bases = rset->num_bases;
+ nset->basenames =
+ nmem_malloc (nset->nmem, nset->num_bases * sizeof(*rset->basenames));
+ for (i = 0; i<rset->num_bases; i++)
+ nset->basenames[i] = nmem_strdup(nset->nmem, rset->basenames[i]);
+
+ if (rset->rset)
+ nset->rset = rset_dup(rset->rset);
+ if (rset->rpn)
+ nset->rpn = copy_RPNQuery(rset->rpn, nset->nmem);
+ return nset;
+}
+
+ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
+ int num_input_setnames, const char **input_setnames,
+ const char *output_setname,
+ Z_SortKeySpecList *sort_sequence, int *sort_status)
{
ZebraSet sset;
RSET rset;
if (num_input_setnames == 0)
{
- zh->errCode = 208;
- return ;
+ zebra_setError(zh, YAZ_BIB1_NO_RESULT_SET_NAME_SUPPLIED_ON_SORT, 0);
+ return ZEBRA_FAIL;
}
if (num_input_setnames > 1)
{
- zh->errCode = 230;
- return;
+ zebra_setError(zh, YAZ_BIB1_SORT_TOO_MANY_INPUT_RESULTS, 0);
+ return ZEBRA_FAIL;
}
- yaz_log (LOG_DEBUG, "result set sort input=%s output=%s",
+ if (!log_level_set)
+ loglevels();
+ yaz_log(log_level_sort, "result set sort input=%s output=%s",
*input_setnames, output_setname);
sset = resultSetGet (zh, input_setnames[0]);
if (!sset)
{
- zh->errCode = 30;
- zh->errString = nmem_strdup (nmem, input_setnames[0]);
- return;
+ zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
+ input_setnames[0]);
+ return ZEBRA_FAIL;
}
if (!(rset = sset->rset))
{
- zh->errCode = 30;
- zh->errString = nmem_strdup (nmem, input_setnames[0]);
- return;
+ zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
+ input_setnames[0]);
+ return ZEBRA_FAIL;
}
if (strcmp (output_setname, input_setnames[0]))
- {
- rset = rset_dup (rset);
- sset = resultSetAdd (zh, output_setname, 1);
- sset->rset = rset;
- }
- resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
+ sset = resultSetClone(zh, output_setname, sset);
+ sset->sortSpec = copy_SortKeySpecList(sort_sequence, sset->nmem);
+ return resultSetSortSingle (zh, nmem, sset, rset, sort_sequence,
+ sort_status);
}
-void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
- ZebraSet sset, RSET rset,
- Z_SortKeySpecList *sort_sequence, int *sort_status)
+ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
+ ZebraSet sset, RSET rset,
+ Z_SortKeySpecList *sort_sequence,
+ int *sort_status)
{
int i;
+ int n = 0;
+ zint kno = 0;
zint psysno = 0;
struct it_key key;
struct sortKeyInfo sort_criteria[3];
int num_criteria;
RSFD rfd;
TERMID termid;
+ TERMID *terms;
+ int numTerms = 0;
+ size_t sysno_mem_index = 0;
+
+ if (zh->m_staticrank)
+ sysno_mem_index = 1;
+
- yaz_log (LOG_LOG, "resultSetSortSingle start");
assert(nmem); /* compiler shut up about unused param */
sset->sort_info->num_entries = 0;
+ rset_getterms(rset, 0, 0, &n);
+ terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
+ rset_getterms(rset, terms, n, &numTerms);
+
sset->hits = 0;
num_criteria = sort_sequence->num_specs;
if (num_criteria > 3)
sort_criteria[i].relation = 'D';
else
{
- zh->errCode = 214;
- return;
+ zebra_setError(zh, YAZ_BIB1_ILLEGAL_SORT_RELATION, 0);
+ return ZEBRA_FAIL;
}
if (sks->sortElement->which == Z_SortElement_databaseSpecific)
{
- zh->errCode = 210;
- return;
+ zebra_setError(zh, YAZ_BIB1_DATABASE_SPECIFIC_SORT_UNSUPP, 0);
+ return ZEBRA_FAIL;
}
else if (sks->sortElement->which != Z_SortElement_generic)
{
- zh->errCode = 237;
- return;
+ zebra_setError(zh, YAZ_BIB1_SORT_ILLEGAL_SORT, 0);
+ return ZEBRA_FAIL;
}
sk = sks->sortElement->u.generic;
switch (sk->which)
{
case Z_SortKey_sortField:
- yaz_log (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
- zh->errCode = 207;
- return;
+ yaz_log(log_level_sort, "key %d is of type sortField",
+ i+1);
+ zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
+ return ZEBRA_FAIL;
case Z_SortKey_elementSpec:
- yaz_log (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
- zh->errCode = 207;
- return;
+ yaz_log(log_level_sort, "key %d is of type elementSpec",
+ i+1);
+ zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
+ return ZEBRA_FAIL;
case Z_SortKey_sortAttributes:
- yaz_log (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
- sort_criteria[i].attrUse =
- zebra_maps_sort (zh->reg->zebra_maps,
- sk->u.sortAttributes,
- &sort_criteria[i].numerical);
- yaz_log (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
- if (sort_criteria[i].attrUse == -1)
+ yaz_log(log_level_sort, "key %d is of type sortAttributes", i+1);
+ if (1)
{
- zh->errCode = 116;
- return;
- }
- if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
- {
- zh->errCode = 207;
- return;
+ int ord;
+ int use = zebra_maps_sort(zh->reg->zebra_maps,
+ sk->u.sortAttributes,
+ &sort_criteria[i].numerical);
+ yaz_log(log_level_sort, "use value = %d", use);
+ if (use == -1)
+ {
+ zebra_setError(
+ zh, YAZ_BIB1_USE_ATTRIBUTE_REQUIRED_BUT_NOT_SUPPLIED, 0);
+ return ZEBRA_FAIL;
+ }
+ ord = zebraExplain_lookup_attr_su_any_index(zh->reg->zei,
+ VAL_IDXPATH, use);
+ if (ord == -1)
+ {
+ zebra_setError(
+ zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
+ return ZEBRA_FAIL;
+ }
+ sort_criteria[i].ord = ord;
}
break;
}
}
rfd = rset_open (rset, RSETF_READ);
- while (rset_read (rfd, &key,&termid))
- /* FIXME - pass a TERMID *, and use it for something below !! */
+ while (rset_read (rfd, &key, &termid))
{
- zint this_sys = key.mem[0];
+ zint this_sys = key.mem[sysno_mem_index];
+ if (log_level_searchhits)
+ key_logdump_txt(log_level_searchhits, &key, termid->name);
+ kno++;
if (this_sys != psysno)
{
(sset->hits)++;
}
}
rset_close (rfd);
-
-#if 0
- for (i = 0; i < rset->no_rset_terms; i++)
- yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT
- " type=%s count=" ZINT_FORMAT,
- rset->rset_terms[i]->name,
- rset->rset_terms[i]->nn,
- rset->rset_terms[i]->flags,
- rset->rset_terms[i]->count);
-#endif
+ yaz_log(log_level_sort, ZINT_FORMAT " keys, " ZINT_FORMAT " sysnos, sort",
+ kno, sset->hits);
+ for (i = 0; i < numTerms; i++)
+ yaz_log(log_level_sort, "term=\"%s\" type=%s count=" ZINT_FORMAT,
+ terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
*sort_status = Z_SortResponse_success;
- yaz_log (LOG_LOG, "resultSetSortSingle end");
+ return ZEBRA_OK;
}
-RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
+RSET resultSetRef(ZebraHandle zh, const char *resultSetId)
{
ZebraSet s;
return NULL;
}
-void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
+ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
+ RSET rset, NMEM nmem)
{
- zint kno = 0;
struct it_key key;
- RSFD rfd;
TERMID termid;
TERMID *terms;
- int i,n;
+ zint kno = 0;
+ int numTerms = 0;
+ int n = 0;
+ int i;
ZebraRankClass rank_class;
- struct rank_control *rc;
struct zset_sort_info *sort_info;
const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
- double cur,tot;
- zint est=-2; /* -2 not done, -1 can't do, >0 actual estimate*/
- zint esthits;
- double ratio;
+ size_t sysno_mem_index = 0;
+ if (zh->m_staticrank)
+ sysno_mem_index = 1;
+
+ if (!log_level_set)
+ loglevels();
sort_info = zebraSet->sort_info;
sort_info->num_entries = 0;
zebraSet->hits = 0;
- rfd = rset_open (rset, RSETF_READ);
+ rset_getterms(rset, 0, 0, &n);
+ terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
+ rset_getterms(rset, terms, n, &numTerms);
- rank_class = zebraRankLookup (zh, rank_handler_name);
+
+ rank_class = zebraRankLookup(zh, rank_handler_name);
if (!rank_class)
{
- yaz_log (LOG_WARN, "No such rank handler: %s", rank_handler_name);
- return;
+ yaz_log(YLOG_WARN, "No such rank handler: %s", rank_handler_name);
+ zebra_setError(zh, YAZ_BIB1_UNSUPP_SEARCH, "Cannot find rank handler");
+ return ZEBRA_FAIL;
}
- rc = rank_class->control;
-
- if (rset_read (rfd, &key, &termid))
+ else
{
- zint psysno = key.mem[0];
- int score;
- void *handle =
- (*rc->begin) (zh->reg, rank_class->class_handle, rset);
- (zebraSet->hits)++;
- esthits=atoi(res_get_def(zh->res,"estimatehits","0"));
- if (!esthits)
- est=-1; /* can not do */
- do
- {
- zint this_sys = key.mem[0];
- kno++;
- if (this_sys != psysno)
- {
- score = (*rc->calc) (handle, psysno);
+ RSFD rfd = rset_open(rset, RSETF_READ);
+ struct rank_control *rc = rank_class->control;
+ double score;
+ zint count = 0;
+
+ void *handle =
+ (*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
+ terms, numTerms);
+ zint psysno = 0; /* previous doc id / sys no */
+ zint pstaticrank = 0; /* previous static rank */
+ int stop_flag = 0;
+ while (rset_read(rfd, &key, &termid))
+ {
+ zint this_sys = key.mem[sysno_mem_index];
- resultSetInsertRank (zh, sort_info, psysno, score, 'A');
- (zebraSet->hits)++;
- psysno = this_sys;
- }
- /* FIXME - Ranking is broken, since rsets no longer have */
- /* term lists! */
- /* (*rc->add) (handle, this_sys, term_index); */
-
- if ( (est==-2) && (zebraSet->hits==esthits))
- { /* time to estimate the hits */
- rset_pos(rfd,&cur,&tot);
- if (tot>0) {
- ratio=cur/tot;
- est=(zint)(0.5+zebraSet->hits/ratio);
- logf(LOG_LOG, "Estimating hits (%s) "
- "%0.1f->"ZINT_FORMAT
- "; %0.1f->"ZINT_FORMAT,
- rset->control->desc,
- cur, zebraSet->hits,
- tot,est);
- i=0; /* round to 3 significant digits */
- while (est>1000) {
- est/=10;
- i++;
- }
- while (i--) est*=10;
- zebraSet->hits=est;
- }
- }
- }
- while (rset_read (rfd, &key,&termid) && (est<0) );
- score = (*rc->calc) (handle, psysno);
- resultSetInsertRank (zh, sort_info, psysno, score, 'A');
- (*rc->end) (zh->reg, handle);
+ zint seqno = key.mem[key.len-1];
+ kno++;
+ if (log_level_searchhits)
+ key_logdump_txt(log_level_searchhits, &key, termid->name);
+ if (this_sys != psysno)
+ { /* new record .. */
+ if (rfd->counted_items > rset->hits_limit)
+ break;
+ if (psysno)
+ { /* only if we did have a previous record */
+ score = (*rc->calc) (handle, psysno, pstaticrank,
+ &stop_flag);
+ /* insert the hit. A=Ascending */
+ resultSetInsertRank (zh, sort_info, psysno, score, 'A');
+ count++;
+ if (stop_flag)
+ break;
+ }
+ psysno = this_sys;
+ if (zh->m_staticrank)
+ pstaticrank = key.mem[0];
+ }
+ (*rc->add) (handle, CAST_ZINT_TO_INT(seqno), termid);
+ }
+ /* no more items */
+ if (psysno)
+ { /* we had - at least - one record */
+ score = (*rc->calc)(handle, psysno, pstaticrank, &stop_flag);
+ /* insert the hit. A=Ascending */
+ resultSetInsertRank(zh, sort_info, psysno, score, 'A');
+ count++;
+ }
+ (*rc->end) (zh->reg, handle);
+ rset_close (rfd);
}
- rset_close (rfd);
- n=0;
- rset_getterms(rset,0,0,&n);
- terms=xmalloc( sizeof(*terms)*n);
- i=n;
- n=0;
- rset_getterms(rset,terms,i,&n);
-
-
- for (i = 0; i < n; i++)
- {
- if (est>0)
- terms[i]->count =
- est=(zint)(terms[i]->count/ratio);
- yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT
- " type=%s count=" ZINT_FORMAT,
- terms[i]->name,
- terms[i]->nn,
- terms[i]->flags,
- terms[i]->count);
- }
- xfree(terms);
- yaz_log (LOG_DEBUG, ZINT_FORMAT " keys, "ZINT_FORMAT" distinct sysnos",
- kno, zebraSet->hits);
+ zebraSet->hits = rset->hits_count;
+
+ yaz_log(log_level_searchterms, ZINT_FORMAT " keys, "
+ ZINT_FORMAT " sysnos, rank", kno, zebraSet->hits);
+ for (i = 0; i < numTerms; i++)
+ {
+ yaz_log(log_level_searchterms, "term=\"%s\" type=%s count="
+ ZINT_FORMAT,
+ terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
+ }
+ return ZEBRA_OK;
}
-ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
+ZebraRankClass zebraRankLookup(ZebraHandle zh, const char *name)
{
ZebraRankClass p = zh->reg->rank_classes;
while (p && strcmp (p->control->name, name))
return p;
}
-void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
+void zebraRankInstall(struct zebra_register *reg, struct rank_control *ctrl)
{
ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
reg->rank_classes = p;
}
-void zebraRankDestroy (struct zebra_register *reg)
+void zebraRankDestroy(struct zebra_register *reg)
{
ZebraRankClass p = reg->rank_classes;
while (p)
ZebraRankClass p_next = p->next;
if (p->init_flag && p->control->destroy)
(*p->control->destroy)(reg, p->class_handle);
- xfree (p->control->name);
- xfree (p->control);
- xfree (p);
+ xfree(p->control->name);
+ xfree(p->control);
+ xfree(p);
p = p_next;
}
reg->rank_classes = NULL;
}
+
+static int trav_rset_for_termids(RSET rset, TERMID *termid_array,
+ zint *hits_array, int *approx_array)
+{
+ int no = 0;
+ int i;
+ for (i = 0; i<rset->no_children; i++)
+ no += trav_rset_for_termids(rset->children[i],
+ (termid_array ? termid_array + no : 0),
+ (hits_array ? hits_array + no : 0),
+ (approx_array ? approx_array + no : 0));
+ if (rset->term)
+ {
+ if (termid_array)
+ termid_array[no] = rset->term;
+ if (hits_array)
+ hits_array[no] = rset->hits_count;
+ if (approx_array)
+ approx_array[no] = rset->hits_approx;
+#if 0
+ yaz_log(YLOG_LOG, "rset=%p term=%s limit=" ZINT_FORMAT
+ " count=" ZINT_FORMAT,
+ rset, rset->term->name, rset->hits_limit, rset->hits_count);
+#endif
+ no++;
+ }
+ return no;
+}
+
+ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
+ int *num_terms)
+{
+ ZebraSet sset = resultSetGet(zh, setname);
+ *num_terms = 0;
+ if (sset)
+ {
+ *num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
+ return ZEBRA_OK;
+ }
+ return ZEBRA_FAIL;
+}
+
+ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
+ int no, zint *count, int *approx,
+ char *termbuf, size_t *termlen,
+ const char **term_ref_id)
+{
+ ZebraSet sset = resultSetGet(zh, setname);
+ if (sset)
+ {
+ int num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
+ if (no >= 0 && no < num_terms)
+ {
+ TERMID *term_array = xmalloc(num_terms * sizeof(*term_array));
+ zint *hits_array = xmalloc(num_terms * sizeof(*hits_array));
+ int *approx_array = xmalloc(num_terms * sizeof(*approx_array));
+
+ trav_rset_for_termids(sset->rset, term_array,
+ hits_array, approx_array);
+
+ if (count)
+ *count = hits_array[no];
+ if (approx)
+ *approx = approx_array[no];
+ if (termbuf)
+ {
+ char *inbuf = term_array[no]->name;
+ size_t inleft = strlen(inbuf);
+ size_t outleft = *termlen - 1;
+
+ if (zh->iconv_from_utf8 != 0)
+ {
+ char *outbuf = termbuf;
+ size_t ret;
+
+ ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
+ &outbuf, &outleft);
+ if (ret == (size_t)(-1))
+ *termlen = 0;
+ else
+ *termlen = outbuf - termbuf;
+ }
+ else
+ {
+ if (inleft > outleft)
+ inleft = outleft;
+ *termlen = inleft;
+ memcpy(termbuf, inbuf, *termlen);
+ }
+ termbuf[*termlen] = '\0';
+ }
+ if (term_ref_id)
+ *term_ref_id = term_array[no]->ref_id;
+
+ xfree(term_array);
+ xfree(hits_array);
+ xfree(approx_array);
+ return ZEBRA_OK;
+ }
+ }
+ return ZEBRA_FAIL;
+}
+
+ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
+ zint sysno, zebra_snippets *snippets)
+{
+ ZebraSet sset = resultSetGet(zh, setname);
+ yaz_log(YLOG_DEBUG, "zebra_get_hit_vector setname=%s zysno=" ZINT_FORMAT,
+ setname, sysno);
+ if (!sset)
+ return ZEBRA_FAIL;
+ else
+ {
+ struct rset_key_control *kc = zebra_key_control_create(zh);
+ NMEM nmem = nmem_create();
+ struct it_key key;
+ RSET rsets[2], rset_comb;
+ RSET rset_temp = rstemp_create(nmem, kc, kc->scope,
+ res_get (zh->res, "setTmpDir"),0 );
+
+ TERMID termid;
+ RSFD rsfd = rset_open(rset_temp, RSETF_WRITE);
+
+ key.mem[0] = sysno;
+ key.mem[1] = 0;
+ key.mem[2] = 0;
+ key.mem[3] = 0;
+ key.len = 2;
+ rset_write (rsfd, &key);
+ rset_close (rsfd);
+
+ rsets[0] = rset_temp;
+ rsets[1] = rset_dup(sset->rset);
+
+ rset_comb = rsmulti_and_create(nmem, kc, kc->scope, 2, rsets);
+
+ rsfd = rset_open(rset_comb, RSETF_READ);
+
+ while (rset_read(rsfd, &key, &termid))
+ {
+ if (termid)
+ {
+ struct ord_list *ol;
+ for (ol = termid->ol; ol; ol = ol->next)
+ {
+ zebra_snippets_append(snippets, key.mem[key.len-1],
+ ol->ord, termid->name);
+ }
+ }
+ }
+ rset_close(rsfd);
+
+ rset_delete(rset_comb);
+ nmem_destroy(nmem);
+ kc->dec(kc);
+ }
+ return ZEBRA_OK;
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+