-/* $Id: zsets.c,v 1.91 2005-08-18 12:50:18 adam Exp $
- Copyright (C) 1995-2005
+/* $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>
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;
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;
if (!s->rset_nmem)
s->rset_nmem=nmem_create();
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;
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;
}
+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,
return ZEBRA_FAIL;
}
if (strcmp (output_setname, input_setnames[0]))
- {
- rset = rset_dup (rset);
- sset = resultSetAdd (zh, output_setname, 1);
- sset->rset = rset;
- }
+ sset = resultSetClone(zh, output_setname, sset);
+ sset->sortSpec = copy_SortKeySpecList(sort_sequence, sset->nmem);
return resultSetSortSingle (zh, nmem, sset, rset, sort_sequence,
sort_status);
}
return ZEBRA_FAIL;
case Z_SortKey_sortAttributes:
yaz_log(log_level_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_level_sort, "use value = %d", sort_criteria[i].attrUse);
- if (sort_criteria[i].attrUse == -1)
- {
- zebra_setError(
- zh, YAZ_BIB1_USE_ATTRIBUTE_REQUIRED_BUT_NOT_SUPPLIED, 0);
- return ZEBRA_FAIL;
- }
- if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
+ if (1)
{
- zebra_setError(
- zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
- return ZEBRA_FAIL;
+ 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;
}
ZebraRankClass rank_class;
struct zset_sort_info *sort_info;
const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
+ size_t sysno_mem_index = 0;
+
+ if (zh->m_staticrank)
+ sysno_mem_index = 1;
if (!log_level_set)
loglevels();
rset_getterms(rset, 0, 0, &n);
terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
rset_getterms(rset, terms, n, &numTerms);
- size_t sysno_mem_index = 0;
- if (zh->m_staticrank)
- sysno_mem_index = 1;
rank_class = zebraRankLookup(zh, rank_handler_name);
if (!rank_class)
void *handle =
(*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
terms, numTerms);
- zint psysno = 0;
+ 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];
+
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)
- {
+ if (this_sys != psysno)
+ { /* new record .. */
if (rfd->counted_items > rset->hits_limit)
break;
if (psysno)
- {
- score = (*rc->calc) (handle, 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)
- {
- score = (*rc->calc)(handle, 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++;
}
zint sysno, zebra_snippets *snippets)
{
ZebraSet sset = resultSetGet(zh, setname);
- yaz_log(YLOG_LOG, "zebra_get_hit_vector setname=%s zysno=" ZINT_FORMAT,
+ yaz_log(YLOG_DEBUG, "zebra_get_hit_vector setname=%s zysno=" ZINT_FORMAT,
setname, sysno);
if (!sset)
return ZEBRA_FAIL;
for (ol = termid->ol; ol; ol = ol->next)
{
zebra_snippets_append(snippets, key.mem[key.len-1],
- termid->reg_type,
ol->ord, termid->name);
}
}
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
+ */
+