1 /* $Id: zsets.c,v 1.124 2007-10-31 16:56:14 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <yaz/diagbib1.h>
37 #define ZSET_SORT_MAX_LEVEL 10
39 struct zebra_set_term_entry {
50 NMEM rset_nmem; /* for creating the rsets in */
55 Z_SortKeySpecList *sortSpec;
56 struct zset_sort_info *sort_info;
57 struct zebra_set_term_entry *term_entries;
59 struct zebra_set *next;
61 int estimated_hit_count;
63 zint cache_position; /* last position */
64 RSFD cache_rfd; /* rfd (NULL if not existing) */
65 zint cache_psysno; /* sysno for last position */
66 zint approx_limit; /* limit before we do approx */
69 struct zset_sort_entry {
74 struct zset_sort_info {
77 struct zset_sort_entry *all_entries;
78 struct zset_sort_entry **entries;
81 static int log_level_set=0;
82 static int log_level_sort=0;
83 static int log_level_searchhits=0;
84 static int log_level_searchterms=0;
85 static int log_level_resultsets=0;
87 static void loglevels(void)
91 log_level_sort = yaz_log_module_level("sorting");
92 log_level_searchhits = yaz_log_module_level("searchhits");
93 log_level_searchterms = yaz_log_module_level("searchterms");
94 log_level_resultsets = yaz_log_module_level("resultsets");
99 static ZEBRA_RES resultSetSearch(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
100 Z_RPNQuery *rpn, ZebraSet sset)
103 Z_SortKeySpecList *sort_sequence;
105 ZEBRA_RES res = ZEBRA_OK;
107 sort_sequence = (Z_SortKeySpecList *)
108 nmem_malloc(nmem, sizeof(*sort_sequence));
109 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
110 sort_sequence->specs = (Z_SortKeySpec **)
111 nmem_malloc(nmem, sort_sequence->num_specs *
112 sizeof(*sort_sequence->specs));
113 for (i = 0; i<sort_sequence->num_specs; i++)
114 sort_sequence->specs[i] = 0;
116 rpn_get_top_approx_limit(zh, rpn->RPNStructure, &sset->approx_limit);
118 res = rpn_search_top(zh, rpn->RPNStructure, rpn->attributeSetId,
121 sset->num_bases, sset->basenames,
128 for (i = 0; sort_sequence->specs[i]; i++)
130 sort_sequence->num_specs = i;
131 rset->hits_limit = sset->approx_limit;
134 res = resultSetRank (zh, sset, rset, rset_nmem);
138 res = resultSetSortSingle (zh, nmem, sset, rset,
139 sort_sequence, &sort_status);
146 ZEBRA_RES resultSetAddRPN(ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
147 int num_bases, char **basenames,
149 zint *hits, int *estimated_hit_count)
156 *estimated_hit_count = 0;
158 zebraSet = resultSetAdd(zh, setname, 1);
161 zebraSet->locked = 1;
164 zebraSet->rset_nmem = nmem_create();
166 zebraSet->num_bases = num_bases;
167 zebraSet->basenames =
168 nmem_malloc (zebraSet->nmem, num_bases * sizeof(*zebraSet->basenames));
169 for (i = 0; i<num_bases; i++)
170 zebraSet->basenames[i] = nmem_strdup(zebraSet->nmem, basenames[i]);
172 res = resultSetSearch(zh, zebraSet->nmem, zebraSet->rset_nmem,
174 *hits = zebraSet->hits;
175 if (zebraSet->estimated_hit_count)
176 *estimated_hit_count = 1;
180 zebraSet->locked = 0;
186 void resultSetAddTerm(ZebraHandle zh, ZebraSet s, int reg_type,
187 const char *db, const char *index_name,
190 assert(zh); /* compiler shut up */
192 s->nmem = nmem_create ();
193 if (!s->term_entries)
196 s->term_entries_max = 1000;
198 nmem_malloc (s->nmem, s->term_entries_max *
199 sizeof(*s->term_entries));
200 for (i = 0; i < s->term_entries_max; i++)
201 s->term_entries[i].term = 0;
203 if (s->hits < s->term_entries_max)
205 s->term_entries[s->hits].reg_type = reg_type;
206 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
207 s->term_entries[s->hits].index_name = nmem_strdup(s->nmem, index_name);
208 s->term_entries[s->hits].term = nmem_strdup(s->nmem, term);
213 ZebraSet resultSetAdd(ZebraHandle zh, const char *name, int ov)
218 for (s = zh->sets; s; s = s->next)
219 if (!strcmp (s->name, name))
226 yaz_log(log_level_resultsets, "updating result set %s", name);
227 if (!ov || s->locked)
232 rset_close(s->cache_rfd);
233 rset_delete (s->rset);
236 nmem_destroy (s->rset_nmem);
238 nmem_destroy (s->nmem);
242 const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
244 yaz_log(log_level_resultsets, "adding result set %s", name);
245 s = (ZebraSet) xmalloc (sizeof(*s));
248 s->name = (char *) xmalloc (strlen(name)+1);
249 strcpy (s->name, name);
251 s->sort_info = (struct zset_sort_info *)
252 xmalloc (sizeof(*s->sort_info));
253 s->sort_info->max_entries = atoi(sort_max_str);
254 if (s->sort_info->max_entries < 2)
255 s->sort_info->max_entries = 2;
257 s->sort_info->entries = (struct zset_sort_entry **)
258 xmalloc (sizeof(*s->sort_info->entries) *
259 s->sort_info->max_entries);
260 s->sort_info->all_entries = (struct zset_sort_entry *)
261 xmalloc (sizeof(*s->sort_info->all_entries) *
262 s->sort_info->max_entries);
263 for (i = 0; i < s->sort_info->max_entries; i++)
264 s->sort_info->entries[i] = s->sort_info->all_entries + i;
274 s->cache_position = 0;
276 s->approx_limit = zh->approx_limit;
277 s->estimated_hit_count = 0;
281 ZebraSet resultSetGet(ZebraHandle zh, const char *name)
285 for (s = zh->sets; s; s = s->next)
286 if (!strcmp (s->name, name))
288 if (!s->term_entries && !s->rset && s->rpn)
290 NMEM nmem = nmem_create ();
291 yaz_log(log_level_resultsets, "research %s", name);
293 s->rset_nmem=nmem_create();
294 resultSetSearch(zh, nmem, s->rset_nmem, s->rpn, s);
295 if (s->rset && s->sortSpec)
298 yaz_log(log_level_resultsets, "resort %s", name);
299 resultSetSortSingle (zh, nmem, s, s->rset, s->sortSpec,
309 void resultSetInvalidate (ZebraHandle zh)
311 ZebraSet s = zh->sets;
313 yaz_log(log_level_resultsets, "invalidating result sets");
314 for (; s; s = s->next)
319 rset_close(s->cache_rfd);
320 rset_delete (s->rset);
324 s->cache_position = 0;
326 nmem_destroy(s->rset_nmem);
331 void resultSetDestroy(ZebraHandle zh, int num, char **names,int *statuses)
333 ZebraSet * ss = &zh->sets;
337 for (i = 0; i<num; i++)
338 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
345 for (i = 0; i<num; i++)
346 if (!strcmp (s->name, names[i]))
349 statuses[i] = Z_DeleteStatus_success;
358 xfree (s->sort_info->all_entries);
359 xfree (s->sort_info->entries);
360 xfree (s->sort_info);
363 nmem_destroy (s->nmem);
367 rset_close(s->cache_rfd);
368 rset_delete (s->rset);
371 nmem_destroy(s->rset_nmem);
380 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
385 zint *pos = pos_small;
389 if (num > 10000 || num <= 0)
393 pos = xmalloc(sizeof(*pos) * num);
395 for (i = 0; i<num; i++)
398 mr = zebra_meta_records_create(zh, name, num, pos);
405 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh, const char *name,
406 int num, zint *positions)
409 ZebraMetaRecord *sr = 0;
412 struct zset_sort_info *sort_info;
413 size_t sysno_mem_index = 0;
415 if (zh->m_staticrank)
420 if (!(sset = resultSetGet (zh, name)))
422 if (!(rset = sset->rset))
424 if (!sset->term_entries)
426 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
427 for (i = 0; i<num; i++)
434 if (positions[i] <= sset->term_entries_max)
436 sr[i].term = sset->term_entries[positions[i]-1].term;
437 sr[i].db = sset->term_entries[positions[i]-1].db;
443 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
444 for (i = 0; i<num; i++)
451 sort_info = sset->sort_info;
456 for (i = 0; i<num; i++)
458 position = positions[i];
459 if (position > 0 && position <= sort_info->num_entries)
461 yaz_log(log_level_sort, "got pos=" ZINT_FORMAT
462 " (sorted)", position);
463 sr[i].sysno = sort_info->entries[position-1]->sysno;
464 sr[i].score = sort_info->entries[position-1]->score;
468 /* did we really get all entries using sort ? */
469 for (i = 0; i<num; i++)
474 if (i < num) /* nope, get the rest, unsorted - sorry */
483 position = sort_info->num_entries;
484 while (num_i < num && positions[num_i] <= position)
487 if (sset->cache_rfd &&
488 num_i < num && positions[num_i] > sset->cache_position)
490 position = sset->cache_position;
491 rfd = sset->cache_rfd;
492 psysno = sset->cache_psysno;
497 rset_close(sset->cache_rfd);
498 rfd = rset_open (rset, RSETF_READ);
500 while (num_i < num && rset_read (rfd, &key, 0))
502 zint this_sys = key.mem[sysno_mem_index];
503 if (this_sys != psysno)
508 /* determine we alreay have this in our set */
509 for (i = sort_info->num_entries; --i >= 0; )
510 if (psysno == sort_info->entries[i]->sysno)
516 assert (num_i < num);
517 if (position == positions[num_i])
519 sr[num_i].sysno = psysno;
520 yaz_log(log_level_sort, "got pos=" ZINT_FORMAT " (unsorted)", position);
521 sr[num_i].score = -1;
526 sset->cache_position = position;
527 sset->cache_psysno = psysno;
528 sset->cache_rfd = rfd;
534 void zebra_meta_records_destroy (ZebraHandle zh, ZebraMetaRecord *records,
537 assert(zh); /* compiler shut up about unused arg */
545 const char *index_type;
548 void resultSetInsertSort(ZebraHandle zh, ZebraSet sset,
549 struct sortKeyInfo *criteria, int num_criteria,
551 char *cmp_buf[], char *tmp_cmp_buf[])
553 struct zset_sort_entry *new_entry = NULL;
554 struct zset_sort_info *sort_info = sset->sort_info;
557 zebra_sort_sysno(zh->reg->sort_index, sysno);
558 for (i = 0; i<num_criteria; i++)
560 char *this_entry_buf = tmp_cmp_buf[i];
561 memset(this_entry_buf, '\0', SORT_IDX_ENTRYSIZE);
562 if (criteria[i].ord != -1)
564 zebra_sort_type(zh->reg->sort_index, criteria[i].ord);
565 zebra_sort_read(zh->reg->sort_index, this_entry_buf);
568 i = sort_info->num_entries;
572 for (j = 0; j<num_criteria; j++)
574 char *this_entry_buf = tmp_cmp_buf[j];
575 char *other_entry_buf =
576 cmp_buf[j] + i * SORT_IDX_ENTRYSIZE;
577 if (criteria[j].numerical)
579 char this_entry_org[1024];
580 char other_entry_org[1024];
582 const char *index_type = criteria[j].index_type;
583 zebra_term_untrans(zh, index_type, this_entry_org,
585 zebra_term_untrans(zh, index_type, other_entry_org,
587 diff = atof(this_entry_org) - atof(other_entry_org);
598 rel = memcmp(this_entry_buf, other_entry_buf,
606 if (criteria[j].relation == 'A')
611 else if (criteria[j].relation == 'D')
618 j = sort_info->max_entries;
622 if (sort_info->num_entries == j)
625 j = (sort_info->num_entries)++;
626 new_entry = sort_info->entries[j];
630 for (k = 0; k<num_criteria; k++)
632 char *j_buf = cmp_buf[k] + j * SORT_IDX_ENTRYSIZE;
633 char *j_1_buf = cmp_buf[k] + (j-1) * SORT_IDX_ENTRYSIZE;
634 memcpy(j_buf, j_1_buf, SORT_IDX_ENTRYSIZE);
636 sort_info->entries[j] = sort_info->entries[j-1];
639 sort_info->entries[i] = new_entry;
641 for (i = 0; i<num_criteria; i++)
643 char *new_entry_buf = cmp_buf[i] + j * SORT_IDX_ENTRYSIZE;
644 char *this_entry_buf = tmp_cmp_buf[i];
645 memcpy(new_entry_buf, this_entry_buf, SORT_IDX_ENTRYSIZE);
647 new_entry->sysno = sysno;
648 new_entry->score = -1;
651 void resultSetInsertRank(ZebraHandle zh, struct zset_sort_info *sort_info,
652 zint sysno, int score, int relation)
654 struct zset_sort_entry *new_entry = NULL;
656 assert(zh); /* compiler shut up about unused arg */
658 i = sort_info->num_entries;
663 rel = score - sort_info->entries[i]->score;
670 else if (relation == 'A')
677 j = sort_info->max_entries;
681 if (sort_info->num_entries == j)
684 j = (sort_info->num_entries)++;
686 new_entry = sort_info->entries[j];
689 sort_info->entries[j] = sort_info->entries[j-1];
692 sort_info->entries[i] = new_entry;
694 new_entry->sysno = sysno;
695 new_entry->score = score;
698 static Z_RPNQuery *copy_RPNQuery(Z_RPNQuery *src, NMEM nmem)
701 ODR encode = odr_createmem(ODR_ENCODE);
702 ODR decode = odr_createmem(ODR_DECODE);
704 if (z_RPNQuery(encode, &src, 0, 0))
707 char *buf = odr_getbuf(encode, &len, 0);
711 odr_setbuf(decode, buf, len, 0);
712 z_RPNQuery(decode, &dst, 0, 0);
715 nmem_transfer(nmem, decode->mem);
721 static Z_SortKeySpecList *copy_SortKeySpecList(Z_SortKeySpecList *src, NMEM nmem)
723 Z_SortKeySpecList *dst = 0;
724 ODR encode = odr_createmem(ODR_ENCODE);
725 ODR decode = odr_createmem(ODR_DECODE);
727 if (z_SortKeySpecList(encode, &src, 0, 0))
730 char *buf = odr_getbuf(encode, &len, 0);
734 odr_setbuf(decode, buf, len, 0);
735 z_SortKeySpecList(decode, &dst, 0, 0);
738 nmem_transfer(nmem, decode->mem);
744 ZebraSet resultSetClone(ZebraHandle zh, const char *setname,
750 nset = resultSetAdd(zh, setname, 1);
754 nset->nmem = nmem_create();
756 nset->num_bases = rset->num_bases;
758 nmem_malloc (nset->nmem, nset->num_bases * sizeof(*rset->basenames));
759 for (i = 0; i<rset->num_bases; i++)
760 nset->basenames[i] = nmem_strdup(nset->nmem, rset->basenames[i]);
763 nset->rset = rset_dup(rset->rset);
765 nset->rpn = copy_RPNQuery(rset->rpn, nset->nmem);
769 ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
770 int num_input_setnames, const char **input_setnames,
771 const char *output_setname,
772 Z_SortKeySpecList *sort_sequence, int *sort_status)
777 if (num_input_setnames == 0)
779 zebra_setError(zh, YAZ_BIB1_NO_RESULT_SET_NAME_SUPPLIED_ON_SORT, 0);
782 if (num_input_setnames > 1)
784 zebra_setError(zh, YAZ_BIB1_SORT_TOO_MANY_INPUT_RESULTS, 0);
789 yaz_log(log_level_sort, "result set sort input=%s output=%s",
790 *input_setnames, output_setname);
791 sset = resultSetGet (zh, input_setnames[0]);
794 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
798 if (!(rset = sset->rset))
800 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
804 if (strcmp (output_setname, input_setnames[0]))
805 sset = resultSetClone(zh, output_setname, sset);
806 sset->sortSpec = copy_SortKeySpecList(sort_sequence, sset->nmem);
807 return resultSetSortSingle (zh, nmem, sset, rset, sort_sequence,
811 ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
812 ZebraSet sset, RSET rset,
813 Z_SortKeySpecList *sort_sequence,
821 struct sortKeyInfo sort_criteria[ZSET_SORT_MAX_LEVEL];
822 char *cmp_buf[ZSET_SORT_MAX_LEVEL];
823 char *tmp_cmp_buf[ZSET_SORT_MAX_LEVEL];
829 size_t sysno_mem_index = 0;
831 if (zh->m_staticrank)
834 assert(nmem); /* compiler shut up about unused param */
835 sset->sort_info->num_entries = 0;
837 rset_getterms(rset, 0, 0, &n);
838 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
839 rset_getterms(rset, terms, n, &numTerms);
842 num_criteria = sort_sequence->num_specs;
843 if (num_criteria > ZSET_SORT_MAX_LEVEL)
844 num_criteria = ZSET_SORT_MAX_LEVEL;
845 for (i = 0; i < num_criteria; i++)
847 Z_SortKeySpec *sks = sort_sequence->specs[i];
851 sort_criteria[i].ord = -1;
852 sort_criteria[i].numerical = 0;
854 if (sks->which == Z_SortKeySpec_missingValueData)
856 zebra_setError(zh, YAZ_BIB1_UNSUPP_MISSING_DATA_ACTION, 0);
859 if (*sks->sortRelation == Z_SortKeySpec_ascending)
860 sort_criteria[i].relation = 'A';
861 else if (*sks->sortRelation == Z_SortKeySpec_descending)
862 sort_criteria[i].relation = 'D';
865 zebra_setError(zh, YAZ_BIB1_ILLEGAL_SORT_RELATION, 0);
868 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
870 zebra_setError(zh, YAZ_BIB1_DATABASE_SPECIFIC_SORT_UNSUPP, 0);
873 else if (sks->sortElement->which != Z_SortElement_generic)
875 zebra_setError(zh, YAZ_BIB1_SORT_ILLEGAL_SORT, 0);
878 sk = sks->sortElement->u.generic;
881 case Z_SortKey_sortField:
882 yaz_log(log_level_sort, "key %d is of type sortField",
884 sort_criteria[i].numerical = 0;
885 sort_criteria[i].ord =
886 zebraExplain_lookup_attr_str(zh->reg->zei,
887 zinfo_index_category_sort,
889 if (sks->which != Z_SortKeySpec_null
890 && sort_criteria[i].ord == -1)
893 YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
897 case Z_SortKey_elementSpec:
898 yaz_log(log_level_sort, "key %d is of type elementSpec",
900 zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
902 case Z_SortKey_sortAttributes:
903 yaz_log(log_level_sort, "key %d is of type sortAttributes", i+1);
904 res = zebra_sort_get_ord(zh, sk->u.sortAttributes,
906 &sort_criteria[i].ord,
907 &sort_criteria[i].numerical);
908 if (sks->which != Z_SortKeySpec_null && res != ZEBRA_OK)
912 if (zebraExplain_lookup_ord(zh->reg->zei, sort_criteria[i].ord,
913 &sort_criteria[i].index_type,
916 zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
920 /* allocate space for each cmpare buf + one extra for tmp comparison */
921 for (i = 0; i<num_criteria; i++)
923 cmp_buf[i] = xmalloc(sset->sort_info->max_entries
924 * SORT_IDX_ENTRYSIZE);
925 tmp_cmp_buf[i] = xmalloc(SORT_IDX_ENTRYSIZE);
927 rfd = rset_open (rset, RSETF_READ);
928 while (rset_read (rfd, &key, &termid))
930 zint this_sys = key.mem[sysno_mem_index];
931 if (log_level_searchhits)
932 key_logdump_txt(log_level_searchhits, &key, termid->name);
934 if (this_sys != psysno)
936 if ((sset->hits & 255) == 0 && zh->break_handler_func)
938 if (zh->break_handler_func(zh->break_handler_data))
940 rset_set_hits_limit(rset, 0);
946 resultSetInsertSort(zh, sset,
947 sort_criteria, num_criteria, psysno, cmp_buf,
953 for (i = 0; i<num_criteria; i++)
956 xfree(tmp_cmp_buf[i]);
959 yaz_log(log_level_sort, ZINT_FORMAT " keys, " ZINT_FORMAT " sysnos, sort",
961 for (i = 0; i < numTerms; i++)
962 yaz_log(log_level_sort, "term=\"%s\" type=%s count=" ZINT_FORMAT,
963 terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
964 *sort_status = Z_SortResponse_success;
968 RSET resultSetRef(ZebraHandle zh, const char *resultSetId)
972 if ((s = resultSetGet (zh, resultSetId)))
977 ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
978 RSET rset, NMEM nmem)
987 ZebraRankClass rank_class;
988 struct zset_sort_info *sort_info;
989 const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
990 size_t sysno_mem_index = 0;
992 if (zh->m_staticrank)
997 sort_info = zebraSet->sort_info;
998 sort_info->num_entries = 0;
1000 zebraSet->estimated_hit_count = 0;
1001 rset_getterms(rset, 0, 0, &n);
1002 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
1003 rset_getterms(rset, terms, n, &numTerms);
1005 rank_class = zebraRankLookup(zh, rank_handler_name);
1008 yaz_log(YLOG_WARN, "No such rank handler: %s", rank_handler_name);
1009 zebra_setError(zh, YAZ_BIB1_UNSUPP_SEARCH, "Cannot find rank handler");
1014 RSFD rfd = rset_open(rset, RSETF_READ);
1015 struct rank_control *rc = rank_class->control;
1018 void *handle = (*rc->begin) (zh->reg, rank_class->class_handle, rset,
1019 nmem, terms, numTerms);
1020 zint psysno = 0; /* previous doc id / sys no */
1021 zint pstaticrank = 0; /* previous static rank */
1023 while (rset_read(rfd, &key, &termid))
1025 zint this_sys = key.mem[sysno_mem_index];
1027 zint seqno = key.mem[key.len-1];
1029 if (log_level_searchhits)
1030 key_logdump_txt(log_level_searchhits, &key, termid->name);
1031 if (this_sys != psysno)
1032 { /* new record .. */
1033 if (!(rfd->counted_items & 255) && zh->break_handler_func)
1035 if (zh->break_handler_func(zh->break_handler_data))
1037 yaz_log(YLOG_LOG, "Aborted search");
1041 if (rfd->counted_items > rset->hits_limit)
1044 { /* only if we did have a previous record */
1045 score = (*rc->calc) (handle, psysno, pstaticrank,
1047 /* insert the hit. A=Ascending */
1048 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
1053 zebraSet->estimated_hit_count = 1;
1054 rset_set_hits_limit(rset, 0);
1058 if (zh->m_staticrank)
1059 pstaticrank = key.mem[0];
1061 (*rc->add) (handle, CAST_ZINT_TO_INT(seqno), termid);
1065 { /* we had - at least - one record */
1066 score = (*rc->calc)(handle, psysno, pstaticrank, &stop_flag);
1067 /* insert the hit. A=Ascending */
1068 resultSetInsertRank(zh, sort_info, psysno, score, 'A');
1071 (*rc->end) (zh->reg, handle);
1074 zebraSet->hits = rset->hits_count;
1076 yaz_log(log_level_searchterms, ZINT_FORMAT " keys, "
1077 ZINT_FORMAT " sysnos, rank", kno, zebraSet->hits);
1078 for (i = 0; i < numTerms; i++)
1080 yaz_log(log_level_searchterms, "term=\"%s\" type=%s count="
1082 terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
1087 ZebraRankClass zebraRankLookup(ZebraHandle zh, const char *name)
1089 ZebraRankClass p = zh->reg->rank_classes;
1090 while (p && strcmp (p->control->name, name))
1092 if (p && !p->init_flag)
1094 if (p->control->create)
1095 p->class_handle = (*p->control->create)(zh);
1101 void zebraRankInstall(struct zebra_register *reg, struct rank_control *ctrl)
1103 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
1104 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
1105 memcpy (p->control, ctrl, sizeof(*p->control));
1106 p->control->name = xstrdup (ctrl->name);
1108 p->next = reg->rank_classes;
1109 reg->rank_classes = p;
1112 void zebraRankDestroy(struct zebra_register *reg)
1114 ZebraRankClass p = reg->rank_classes;
1117 ZebraRankClass p_next = p->next;
1118 if (p->init_flag && p->control->destroy)
1119 (*p->control->destroy)(reg, p->class_handle);
1120 xfree(p->control->name);
1125 reg->rank_classes = NULL;
1128 static int trav_rset_for_termids(RSET rset, TERMID *termid_array,
1129 zint *hits_array, int *approx_array)
1133 for (i = 0; i<rset->no_children; i++)
1134 no += trav_rset_for_termids(rset->children[i],
1135 (termid_array ? termid_array + no : 0),
1136 (hits_array ? hits_array + no : 0),
1137 (approx_array ? approx_array + no : 0));
1141 termid_array[no] = rset->term;
1143 hits_array[no] = rset->hits_count;
1145 approx_array[no] = rset->hits_approx;
1147 yaz_log(YLOG_LOG, "rset=%p term=%s limit=" ZINT_FORMAT
1148 " count=" ZINT_FORMAT,
1149 rset, rset->term->name, rset->hits_limit, rset->hits_count);
1156 ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
1159 ZebraSet sset = resultSetGet(zh, setname);
1163 *num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
1169 ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
1170 int no, zint *count, int *approx,
1171 char *termbuf, size_t *termlen,
1172 const char **term_ref_id)
1174 ZebraSet sset = resultSetGet(zh, setname);
1177 int num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
1178 if (no >= 0 && no < num_terms)
1180 TERMID *term_array = xmalloc(num_terms * sizeof(*term_array));
1181 zint *hits_array = xmalloc(num_terms * sizeof(*hits_array));
1182 int *approx_array = xmalloc(num_terms * sizeof(*approx_array));
1184 trav_rset_for_termids(sset->rset, term_array,
1185 hits_array, approx_array);
1188 *count = hits_array[no];
1190 *approx = approx_array[no];
1193 char *inbuf = term_array[no]->name;
1194 size_t inleft = strlen(inbuf);
1195 size_t outleft = *termlen - 1;
1197 if (zh->iconv_from_utf8 != 0)
1199 char *outbuf = termbuf;
1202 ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
1204 if (ret == (size_t)(-1))
1208 yaz_iconv(zh->iconv_from_utf8, 0, 0,
1210 *termlen = outbuf - termbuf;
1215 if (inleft > outleft)
1218 memcpy(termbuf, inbuf, *termlen);
1220 termbuf[*termlen] = '\0';
1223 *term_ref_id = term_array[no]->ref_id;
1227 xfree(approx_array);
1234 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
1235 zint sysno, zebra_snippets *snippets)
1237 ZebraSet sset = resultSetGet(zh, setname);
1238 yaz_log(YLOG_DEBUG, "zebra_get_hit_vector setname=%s zysno=" ZINT_FORMAT,
1244 struct rset_key_control *kc = zebra_key_control_create(zh);
1245 NMEM nmem = nmem_create();
1247 RSET rsets[2], rset_comb;
1248 RSET rset_temp = rset_create_temp(nmem, kc, kc->scope,
1249 res_get (zh->res, "setTmpDir"),0 );
1252 RSFD rsfd = rset_open(rset_temp, RSETF_WRITE);
1259 rset_write (rsfd, &key);
1262 rsets[0] = rset_temp;
1263 rsets[1] = rset_dup(sset->rset);
1265 rset_comb = rset_create_and(nmem, kc, kc->scope, 2, rsets);
1267 rsfd = rset_open(rset_comb, RSETF_READ);
1269 while (rset_read(rsfd, &key, &termid))
1273 struct ord_list *ol;
1274 for (ol = termid->ol; ol; ol = ol->next)
1276 zebra_snippets_append(snippets, key.mem[key.len-1], 0,
1277 ol->ord, termid->name);
1283 rset_delete(rset_comb);
1293 * indent-tabs-mode: nil
1295 * vim: shiftwidth=4 tabstop=8 expandtab