1 /* $Id: zsets.c,v 1.99 2006-01-26 22:17:16 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 #include <yaz/diagbib1.h>
36 #define SORT_IDX_ENTRYSIZE 64
37 #define ZSET_SORT_MAX_LEVEL 3
39 struct zebra_set_term_entry {
51 NMEM rset_nmem; /* for creating the rsets in */
56 Z_SortKeySpecList *sortSpec;
57 struct zset_sort_info *sort_info;
58 struct zebra_set_term_entry *term_entries;
60 struct zebra_set *next;
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 {
72 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
75 struct zset_sort_info {
78 struct zset_sort_entry *all_entries;
79 struct zset_sort_entry **entries;
82 static int log_level_set=0;
83 static int log_level_sort=0;
84 static int log_level_searchhits=0;
85 static int log_level_searchterms=0;
86 static int log_level_resultsets=0;
88 static void loglevels()
92 log_level_sort = yaz_log_module_level("sorting");
93 log_level_searchhits = yaz_log_module_level("searchhits");
94 log_level_searchterms = yaz_log_module_level("searchterms");
95 log_level_resultsets = yaz_log_module_level("resultsets");
99 ZEBRA_RES resultSetSearch(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
100 Z_RPNQuery *rpn, ZebraSet sset)
104 Z_SortKeySpecList *sort_sequence;
106 ZEBRA_RES res = ZEBRA_OK;
110 sort_sequence = (Z_SortKeySpecList *)
111 nmem_malloc(nmem, sizeof(*sort_sequence));
112 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
113 sort_sequence->specs = (Z_SortKeySpec **)
114 nmem_malloc(nmem, sort_sequence->num_specs *
115 sizeof(*sort_sequence->specs));
116 for (i = 0; i<sort_sequence->num_specs; i++)
117 sort_sequence->specs[i] = 0;
119 attrset = oid_getentbyoid (rpn->attributeSetId);
120 res = rpn_search_top(zh, rpn->RPNStructure, attrset->value,
123 sset->num_bases, sset->basenames,
130 for (i = 0; sort_sequence->specs[i]; i++)
132 sort_sequence->num_specs = i;
133 rset->hits_limit = sset->approx_limit;
136 res = resultSetRank (zh, sset, rset, rset_nmem);
140 res = resultSetSortSingle (zh, nmem, sset, rset,
141 sort_sequence, &sort_status);
148 ZEBRA_RES resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
149 int num_bases, char **basenames,
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 zh->hits = zebraSet->hits;
177 zebraSet->locked = 0;
183 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
184 const char *db, int set,
185 int use, const char *term)
187 assert(zh); /* compiler shut up */
189 s->nmem = nmem_create ();
190 if (!s->term_entries)
193 s->term_entries_max = 1000;
195 nmem_malloc (s->nmem, s->term_entries_max *
196 sizeof(*s->term_entries));
197 for (i = 0; i < s->term_entries_max; i++)
198 s->term_entries[i].term = 0;
200 if (s->hits < s->term_entries_max)
202 s->term_entries[s->hits].reg_type = reg_type;
203 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
204 s->term_entries[s->hits].set = set;
205 s->term_entries[s->hits].use = use;
206 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
211 ZebraSet resultSetAdd(ZebraHandle zh, const char *name, int ov)
216 for (s = zh->sets; s; s = s->next)
217 if (!strcmp (s->name, name))
224 yaz_log(log_level_resultsets, "updating result set %s", name);
225 if (!ov || s->locked)
230 rset_close(s->cache_rfd);
231 rset_delete (s->rset);
234 nmem_destroy (s->rset_nmem);
236 nmem_destroy (s->nmem);
240 const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
242 yaz_log(log_level_resultsets, "adding result set %s", name);
243 s = (ZebraSet) xmalloc (sizeof(*s));
246 s->name = (char *) xmalloc (strlen(name)+1);
247 strcpy (s->name, name);
249 s->sort_info = (struct zset_sort_info *)
250 xmalloc (sizeof(*s->sort_info));
251 s->sort_info->max_entries = atoi(sort_max_str);
252 if (s->sort_info->max_entries < 2)
253 s->sort_info->max_entries = 2;
255 s->sort_info->entries = (struct zset_sort_entry **)
256 xmalloc (sizeof(*s->sort_info->entries) *
257 s->sort_info->max_entries);
258 s->sort_info->all_entries = (struct zset_sort_entry *)
259 xmalloc (sizeof(*s->sort_info->all_entries) *
260 s->sort_info->max_entries);
261 for (i = 0; i < s->sort_info->max_entries; i++)
262 s->sort_info->entries[i] = s->sort_info->all_entries + i;
272 s->cache_position = 0;
274 s->approx_limit = zh->approx_limit;
278 ZebraSet resultSetGet(ZebraHandle zh, const char *name)
282 for (s = zh->sets; s; s = s->next)
283 if (!strcmp (s->name, name))
285 if (!s->term_entries && !s->rset && s->rpn)
287 NMEM nmem = nmem_create ();
288 yaz_log(log_level_resultsets, "research %s", name);
290 s->rset_nmem=nmem_create();
291 resultSetSearch(zh, nmem, s->rset_nmem, s->rpn, s);
292 if (s->rset && s->sortSpec)
295 yaz_log(log_level_resultsets, "resort %s", name);
296 resultSetSortSingle (zh, nmem, s, s->rset, s->sortSpec,
306 void resultSetInvalidate (ZebraHandle zh)
308 ZebraSet s = zh->sets;
310 yaz_log(log_level_resultsets, "invalidating result sets");
311 for (; s; s = s->next)
316 rset_close(s->cache_rfd);
317 rset_delete (s->rset);
321 s->cache_position = 0;
323 nmem_destroy(s->rset_nmem);
328 void resultSetDestroy(ZebraHandle zh, int num, char **names,int *statuses)
330 ZebraSet * ss = &zh->sets;
334 for (i = 0; i<num; i++)
335 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
342 for (i = 0; i<num; i++)
343 if (!strcmp (s->name, names[i]))
346 statuses[i] = Z_DeleteStatus_success;
355 xfree (s->sort_info->all_entries);
356 xfree (s->sort_info->entries);
357 xfree (s->sort_info);
360 nmem_destroy (s->nmem);
364 rset_close(s->cache_rfd);
365 rset_delete (s->rset);
368 nmem_destroy(s->rset_nmem);
377 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
382 zint *pos = pos_small;
386 if (num > 10000 || num <= 0)
390 pos = xmalloc(sizeof(*pos) * num);
392 for (i = 0; i<num; i++)
395 mr = zebra_meta_records_create(zh, name, num, pos);
402 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh, const char *name,
403 int num, zint *positions)
406 ZebraMetaRecord *sr = 0;
409 struct zset_sort_info *sort_info;
410 size_t sysno_mem_index = 0;
412 if (zh->m_staticrank)
417 if (!(sset = resultSetGet (zh, name)))
419 if (!(rset = sset->rset))
421 if (!sset->term_entries)
423 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
424 for (i = 0; i<num; i++)
431 if (positions[i] <= sset->term_entries_max)
433 sr[i].term = sset->term_entries[positions[i]-1].term;
434 sr[i].db = sset->term_entries[positions[i]-1].db;
440 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
441 for (i = 0; i<num; i++)
448 sort_info = sset->sort_info;
453 for (i = 0; i<num; i++)
455 position = positions[i];
456 if (position > 0 && position <= sort_info->num_entries)
458 yaz_log(log_level_sort, "got pos=" ZINT_FORMAT
459 " (sorted)", position);
460 sr[i].sysno = sort_info->entries[position-1]->sysno;
461 sr[i].score = sort_info->entries[position-1]->score;
465 /* did we really get all entries using sort ? */
466 for (i = 0; i<num; i++)
471 if (i < num) /* nope, get the rest, unsorted - sorry */
480 position = sort_info->num_entries;
481 while (num_i < num && positions[num_i] <= position)
484 if (sset->cache_rfd &&
485 num_i < num && positions[num_i] > sset->cache_position)
487 position = sset->cache_position;
488 rfd = sset->cache_rfd;
489 psysno = sset->cache_psysno;
494 rset_close(sset->cache_rfd);
495 rfd = rset_open (rset, RSETF_READ);
497 while (num_i < num && rset_read (rfd, &key, 0))
499 zint this_sys = key.mem[sysno_mem_index];
500 if (this_sys != psysno)
505 /* determine we alreay have this in our set */
506 for (i = sort_info->num_entries; --i >= 0; )
507 if (psysno == sort_info->entries[i]->sysno)
513 assert (num_i < num);
514 if (position == positions[num_i])
516 sr[num_i].sysno = psysno;
517 yaz_log(log_level_sort, "got pos=" ZINT_FORMAT " (unsorted)", position);
518 sr[num_i].score = -1;
523 sset->cache_position = position;
524 sset->cache_psysno = psysno;
525 sset->cache_rfd = rfd;
531 void zebra_meta_records_destroy (ZebraHandle zh, ZebraMetaRecord *records,
534 assert(zh); /* compiler shut up about unused arg */
544 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
545 struct sortKeyInfo *criteria, int num_criteria,
548 struct zset_sort_entry this_entry;
549 struct zset_sort_entry *new_entry = NULL;
550 struct zset_sort_info *sort_info = sset->sort_info;
553 sortIdx_sysno (zh->reg->sortIdx, sysno);
554 for (i = 0; i<num_criteria; i++)
556 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
557 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
559 i = sort_info->num_entries;
563 for (j = 0; j<num_criteria; j++)
565 if (criteria[j].numerical)
567 double diff = atof(this_entry.buf[j]) -
568 atof(sort_info->entries[i]->buf[j]);
577 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
585 if (criteria[j].relation == 'A')
590 else if (criteria[j].relation == 'D')
597 j = sort_info->max_entries;
601 if (sort_info->num_entries == j)
604 j = (sort_info->num_entries)++;
605 new_entry = sort_info->entries[j];
608 sort_info->entries[j] = sort_info->entries[j-1];
611 sort_info->entries[i] = new_entry;
613 for (i = 0; i<num_criteria; i++)
614 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
615 new_entry->sysno = sysno;
616 new_entry->score = -1;
619 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
620 zint sysno, int score, int relation)
622 struct zset_sort_entry *new_entry = NULL;
624 assert(zh); /* compiler shut up about unused arg */
626 i = sort_info->num_entries;
631 rel = score - sort_info->entries[i]->score;
638 else if (relation == 'A')
645 j = sort_info->max_entries;
649 if (sort_info->num_entries == j)
652 j = (sort_info->num_entries)++;
654 new_entry = sort_info->entries[j];
657 sort_info->entries[j] = sort_info->entries[j-1];
660 sort_info->entries[i] = new_entry;
662 new_entry->sysno = sysno;
663 new_entry->score = score;
666 static Z_RPNQuery *copy_RPNQuery(Z_RPNQuery *src, NMEM nmem)
669 ODR encode = odr_createmem(ODR_ENCODE);
670 ODR decode = odr_createmem(ODR_DECODE);
672 if (z_RPNQuery(encode, &src, 0, 0))
675 char *buf = odr_getbuf(encode, &len, 0);
679 odr_setbuf(decode, buf, len, 0);
680 z_RPNQuery(decode, &dst, 0, 0);
683 nmem_transfer(nmem, decode->mem);
689 static Z_SortKeySpecList *copy_SortKeySpecList(Z_SortKeySpecList *src, NMEM nmem)
691 Z_SortKeySpecList *dst = 0;
692 ODR encode = odr_createmem(ODR_ENCODE);
693 ODR decode = odr_createmem(ODR_DECODE);
695 if (z_SortKeySpecList(encode, &src, 0, 0))
698 char *buf = odr_getbuf(encode, &len, 0);
702 odr_setbuf(decode, buf, len, 0);
703 z_SortKeySpecList(decode, &dst, 0, 0);
706 nmem_transfer(nmem, decode->mem);
712 ZebraSet resultSetClone(ZebraHandle zh, const char *setname,
718 nset = resultSetAdd(zh, setname, 1);
722 nset->nmem = nmem_create();
724 nset->num_bases = rset->num_bases;
726 nmem_malloc (nset->nmem, nset->num_bases * sizeof(*rset->basenames));
727 for (i = 0; i<rset->num_bases; i++)
728 nset->basenames[i] = nmem_strdup(nset->nmem, rset->basenames[i]);
731 nset->rset = rset_dup(rset->rset);
733 nset->rpn = copy_RPNQuery(rset->rpn, nset->nmem);
737 ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
738 int num_input_setnames, const char **input_setnames,
739 const char *output_setname,
740 Z_SortKeySpecList *sort_sequence, int *sort_status)
745 if (num_input_setnames == 0)
747 zebra_setError(zh, YAZ_BIB1_NO_RESULT_SET_NAME_SUPPLIED_ON_SORT, 0);
750 if (num_input_setnames > 1)
752 zebra_setError(zh, YAZ_BIB1_SORT_TOO_MANY_INPUT_RESULTS, 0);
757 yaz_log(log_level_sort, "result set sort input=%s output=%s",
758 *input_setnames, output_setname);
759 sset = resultSetGet (zh, input_setnames[0]);
762 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
766 if (!(rset = sset->rset))
768 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
772 if (strcmp (output_setname, input_setnames[0]))
773 sset = resultSetClone(zh, output_setname, sset);
774 sset->sortSpec = copy_SortKeySpecList(sort_sequence, sset->nmem);
775 return resultSetSortSingle (zh, nmem, sset, rset, sort_sequence,
779 ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
780 ZebraSet sset, RSET rset,
781 Z_SortKeySpecList *sort_sequence,
789 struct sortKeyInfo sort_criteria[3];
795 size_t sysno_mem_index = 0;
797 if (zh->m_staticrank)
801 assert(nmem); /* compiler shut up about unused param */
802 sset->sort_info->num_entries = 0;
804 rset_getterms(rset, 0, 0, &n);
805 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
806 rset_getterms(rset, terms, n, &numTerms);
809 num_criteria = sort_sequence->num_specs;
810 if (num_criteria > 3)
812 for (i = 0; i < num_criteria; i++)
814 Z_SortKeySpec *sks = sort_sequence->specs[i];
817 if (*sks->sortRelation == Z_SortKeySpec_ascending)
818 sort_criteria[i].relation = 'A';
819 else if (*sks->sortRelation == Z_SortKeySpec_descending)
820 sort_criteria[i].relation = 'D';
823 zebra_setError(zh, YAZ_BIB1_ILLEGAL_SORT_RELATION, 0);
826 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
828 zebra_setError(zh, YAZ_BIB1_DATABASE_SPECIFIC_SORT_UNSUPP, 0);
831 else if (sks->sortElement->which != Z_SortElement_generic)
833 zebra_setError(zh, YAZ_BIB1_SORT_ILLEGAL_SORT, 0);
836 sk = sks->sortElement->u.generic;
839 case Z_SortKey_sortField:
840 yaz_log(log_level_sort, "key %d is of type sortField",
842 zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
844 case Z_SortKey_elementSpec:
845 yaz_log(log_level_sort, "key %d is of type elementSpec",
847 zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
849 case Z_SortKey_sortAttributes:
850 yaz_log(log_level_sort, "key %d is of type sortAttributes", i+1);
851 sort_criteria[i].attrUse =
852 zebra_maps_sort (zh->reg->zebra_maps,
853 sk->u.sortAttributes,
854 &sort_criteria[i].numerical);
855 yaz_log(log_level_sort, "use value = %d", sort_criteria[i].attrUse);
856 if (sort_criteria[i].attrUse == -1)
859 zh, YAZ_BIB1_USE_ATTRIBUTE_REQUIRED_BUT_NOT_SUPPLIED, 0);
862 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
865 zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
871 rfd = rset_open (rset, RSETF_READ);
872 while (rset_read (rfd, &key, &termid))
874 zint this_sys = key.mem[sysno_mem_index];
875 if (log_level_searchhits)
876 key_logdump_txt(log_level_searchhits, &key, termid->name);
878 if (this_sys != psysno)
882 resultSetInsertSort (zh, sset,
883 sort_criteria, num_criteria, psysno);
887 yaz_log(log_level_sort, ZINT_FORMAT " keys, " ZINT_FORMAT " sysnos, sort",
889 for (i = 0; i < numTerms; i++)
890 yaz_log(log_level_sort, "term=\"%s\" type=%s count=" ZINT_FORMAT,
891 terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
892 *sort_status = Z_SortResponse_success;
896 RSET resultSetRef(ZebraHandle zh, const char *resultSetId)
900 if ((s = resultSetGet (zh, resultSetId)))
905 ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
906 RSET rset, NMEM nmem)
915 ZebraRankClass rank_class;
916 struct zset_sort_info *sort_info;
917 const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
918 size_t sysno_mem_index = 0;
920 if (zh->m_staticrank)
925 sort_info = zebraSet->sort_info;
926 sort_info->num_entries = 0;
928 rset_getterms(rset, 0, 0, &n);
929 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
930 rset_getterms(rset, terms, n, &numTerms);
933 rank_class = zebraRankLookup(zh, rank_handler_name);
936 yaz_log(YLOG_WARN, "No such rank handler: %s", rank_handler_name);
937 zebra_setError(zh, YAZ_BIB1_UNSUPP_SEARCH, "Cannot find rank handler");
942 RSFD rfd = rset_open(rset, RSETF_READ);
943 struct rank_control *rc = rank_class->control;
948 (*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
950 zint psysno = 0; /* previous doc id / sys no */
951 zint pstaticrank = 0; /* previous static rank */
953 while (rset_read(rfd, &key, &termid))
955 zint this_sys = key.mem[sysno_mem_index];
957 zint seqno = key.mem[key.len-1];
959 if (log_level_searchhits)
960 key_logdump_txt(log_level_searchhits, &key, termid->name);
961 if (this_sys != psysno)
962 { /* new record .. */
963 if (rfd->counted_items > rset->hits_limit)
966 { /* only if we did have a previous record */
967 score = (*rc->calc) (handle, psysno, pstaticrank,
969 /* insert the hit. A=Ascending */
970 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
976 if (zh->m_staticrank)
977 pstaticrank = key.mem[0];
979 (*rc->add) (handle, CAST_ZINT_TO_INT(seqno), termid);
983 { /* we had - at least - one record */
984 score = (*rc->calc)(handle, psysno, pstaticrank, &stop_flag);
985 /* insert the hit. A=Ascending */
986 resultSetInsertRank(zh, sort_info, psysno, score, 'A');
989 (*rc->end) (zh->reg, handle);
992 zebraSet->hits = rset->hits_count;
994 yaz_log(log_level_searchterms, ZINT_FORMAT " keys, "
995 ZINT_FORMAT " sysnos, rank", kno, zebraSet->hits);
996 for (i = 0; i < numTerms; i++)
998 yaz_log(log_level_searchterms, "term=\"%s\" type=%s count="
1000 terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
1005 ZebraRankClass zebraRankLookup(ZebraHandle zh, const char *name)
1007 ZebraRankClass p = zh->reg->rank_classes;
1008 while (p && strcmp (p->control->name, name))
1010 if (p && !p->init_flag)
1012 if (p->control->create)
1013 p->class_handle = (*p->control->create)(zh);
1019 void zebraRankInstall(struct zebra_register *reg, struct rank_control *ctrl)
1021 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
1022 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
1023 memcpy (p->control, ctrl, sizeof(*p->control));
1024 p->control->name = xstrdup (ctrl->name);
1026 p->next = reg->rank_classes;
1027 reg->rank_classes = p;
1030 void zebraRankDestroy(struct zebra_register *reg)
1032 ZebraRankClass p = reg->rank_classes;
1035 ZebraRankClass p_next = p->next;
1036 if (p->init_flag && p->control->destroy)
1037 (*p->control->destroy)(reg, p->class_handle);
1038 xfree(p->control->name);
1043 reg->rank_classes = NULL;
1046 static int trav_rset_for_termids(RSET rset, TERMID *termid_array,
1047 zint *hits_array, int *approx_array)
1051 for (i = 0; i<rset->no_children; i++)
1052 no += trav_rset_for_termids(rset->children[i],
1053 (termid_array ? termid_array + no : 0),
1054 (hits_array ? hits_array + no : 0),
1055 (approx_array ? approx_array + no : 0));
1059 termid_array[no] = rset->term;
1061 hits_array[no] = rset->hits_count;
1063 approx_array[no] = rset->hits_approx;
1065 yaz_log(YLOG_LOG, "rset=%p term=%s limit=" ZINT_FORMAT
1066 " count=" ZINT_FORMAT,
1067 rset, rset->term->name, rset->hits_limit, rset->hits_count);
1074 ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
1077 ZebraSet sset = resultSetGet(zh, setname);
1081 *num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
1087 ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
1088 int no, zint *count, int *approx,
1089 char *termbuf, size_t *termlen,
1090 const char **term_ref_id)
1092 ZebraSet sset = resultSetGet(zh, setname);
1095 int num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
1096 if (no >= 0 && no < num_terms)
1098 TERMID *term_array = xmalloc(num_terms * sizeof(*term_array));
1099 zint *hits_array = xmalloc(num_terms * sizeof(*hits_array));
1100 int *approx_array = xmalloc(num_terms * sizeof(*approx_array));
1102 trav_rset_for_termids(sset->rset, term_array,
1103 hits_array, approx_array);
1106 *count = hits_array[no];
1108 *approx = approx_array[no];
1111 char *inbuf = term_array[no]->name;
1112 size_t inleft = strlen(inbuf);
1113 size_t outleft = *termlen - 1;
1115 if (zh->iconv_from_utf8 != 0)
1117 char *outbuf = termbuf;
1120 ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
1122 if (ret == (size_t)(-1))
1125 *termlen = outbuf - termbuf;
1129 if (inleft > outleft)
1132 memcpy(termbuf, inbuf, *termlen);
1134 termbuf[*termlen] = '\0';
1137 *term_ref_id = term_array[no]->ref_id;
1141 xfree(approx_array);
1148 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
1149 zint sysno, zebra_snippets *snippets)
1151 ZebraSet sset = resultSetGet(zh, setname);
1152 yaz_log(YLOG_DEBUG, "zebra_get_hit_vector setname=%s zysno=" ZINT_FORMAT,
1158 struct rset_key_control *kc = zebra_key_control_create(zh);
1159 NMEM nmem = nmem_create();
1161 RSET rsets[2], rset_comb;
1162 RSET rset_temp = rstemp_create(nmem, kc, kc->scope,
1163 res_get (zh->res, "setTmpDir"),0 );
1166 RSFD rsfd = rset_open(rset_temp, RSETF_WRITE);
1173 rset_write (rsfd, &key);
1176 rsets[0] = rset_temp;
1177 rsets[1] = rset_dup(sset->rset);
1179 rset_comb = rsmulti_and_create(nmem, kc, kc->scope, 2, rsets);
1181 rsfd = rset_open(rset_comb, RSETF_READ);
1183 while (rset_read(rsfd, &key, &termid))
1187 struct ord_list *ol;
1188 for (ol = termid->ol; ol; ol = ol->next)
1190 zebra_snippets_append(snippets, key.mem[key.len-1],
1191 ol->ord, termid->name);
1197 rset_delete(rset_comb);