1 /* $Id: zsets.c,v 1.101 2006-05-10 08:13:26 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
34 #include <yaz/diagbib1.h>
37 #define SORT_IDX_ENTRYSIZE 64
38 #define ZSET_SORT_MAX_LEVEL 3
40 struct zebra_set_term_entry {
52 NMEM rset_nmem; /* for creating the rsets in */
57 Z_SortKeySpecList *sortSpec;
58 struct zset_sort_info *sort_info;
59 struct zebra_set_term_entry *term_entries;
61 struct zebra_set *next;
64 zint cache_position; /* last position */
65 RSFD cache_rfd; /* rfd (NULL if not existing) */
66 zint cache_psysno; /* sysno for last position */
67 zint approx_limit; /* limit before we do approx */
70 struct zset_sort_entry {
73 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
76 struct zset_sort_info {
79 struct zset_sort_entry *all_entries;
80 struct zset_sort_entry **entries;
83 static int log_level_set=0;
84 static int log_level_sort=0;
85 static int log_level_searchhits=0;
86 static int log_level_searchterms=0;
87 static int log_level_resultsets=0;
89 static void loglevels()
93 log_level_sort = yaz_log_module_level("sorting");
94 log_level_searchhits = yaz_log_module_level("searchhits");
95 log_level_searchterms = yaz_log_module_level("searchterms");
96 log_level_resultsets = yaz_log_module_level("resultsets");
100 ZEBRA_RES resultSetSearch(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
101 Z_RPNQuery *rpn, ZebraSet sset)
105 Z_SortKeySpecList *sort_sequence;
107 ZEBRA_RES res = ZEBRA_OK;
111 sort_sequence = (Z_SortKeySpecList *)
112 nmem_malloc(nmem, sizeof(*sort_sequence));
113 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
114 sort_sequence->specs = (Z_SortKeySpec **)
115 nmem_malloc(nmem, sort_sequence->num_specs *
116 sizeof(*sort_sequence->specs));
117 for (i = 0; i<sort_sequence->num_specs; i++)
118 sort_sequence->specs[i] = 0;
120 attrset = oid_getentbyoid (rpn->attributeSetId);
121 res = rpn_search_top(zh, rpn->RPNStructure, attrset->value,
124 sset->num_bases, sset->basenames,
131 for (i = 0; sort_sequence->specs[i]; i++)
133 sort_sequence->num_specs = i;
134 rset->hits_limit = sset->approx_limit;
137 res = resultSetRank (zh, sset, rset, rset_nmem);
141 res = resultSetSortSingle (zh, nmem, sset, rset,
142 sort_sequence, &sort_status);
149 ZEBRA_RES resultSetAddRPN (ZebraHandle zh, NMEM m, Z_RPNQuery *rpn,
150 int num_bases, char **basenames,
159 zebraSet = resultSetAdd(zh, setname, 1);
162 zebraSet->locked = 1;
165 zebraSet->rset_nmem = nmem_create();
167 zebraSet->num_bases = num_bases;
168 zebraSet->basenames =
169 nmem_malloc (zebraSet->nmem, num_bases * sizeof(*zebraSet->basenames));
170 for (i = 0; i<num_bases; i++)
171 zebraSet->basenames[i] = nmem_strdup(zebraSet->nmem, basenames[i]);
173 res = resultSetSearch(zh, zebraSet->nmem, zebraSet->rset_nmem,
175 zh->hits = zebraSet->hits;
178 zebraSet->locked = 0;
184 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
185 const char *db, int set,
186 int use, const char *term)
188 assert(zh); /* compiler shut up */
190 s->nmem = nmem_create ();
191 if (!s->term_entries)
194 s->term_entries_max = 1000;
196 nmem_malloc (s->nmem, s->term_entries_max *
197 sizeof(*s->term_entries));
198 for (i = 0; i < s->term_entries_max; i++)
199 s->term_entries[i].term = 0;
201 if (s->hits < s->term_entries_max)
203 s->term_entries[s->hits].reg_type = reg_type;
204 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
205 s->term_entries[s->hits].set = set;
206 s->term_entries[s->hits].use = use;
207 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
212 ZebraSet resultSetAdd(ZebraHandle zh, const char *name, int ov)
217 for (s = zh->sets; s; s = s->next)
218 if (!strcmp (s->name, name))
225 yaz_log(log_level_resultsets, "updating result set %s", name);
226 if (!ov || s->locked)
231 rset_close(s->cache_rfd);
232 rset_delete (s->rset);
235 nmem_destroy (s->rset_nmem);
237 nmem_destroy (s->nmem);
241 const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
243 yaz_log(log_level_resultsets, "adding result set %s", name);
244 s = (ZebraSet) xmalloc (sizeof(*s));
247 s->name = (char *) xmalloc (strlen(name)+1);
248 strcpy (s->name, name);
250 s->sort_info = (struct zset_sort_info *)
251 xmalloc (sizeof(*s->sort_info));
252 s->sort_info->max_entries = atoi(sort_max_str);
253 if (s->sort_info->max_entries < 2)
254 s->sort_info->max_entries = 2;
256 s->sort_info->entries = (struct zset_sort_entry **)
257 xmalloc (sizeof(*s->sort_info->entries) *
258 s->sort_info->max_entries);
259 s->sort_info->all_entries = (struct zset_sort_entry *)
260 xmalloc (sizeof(*s->sort_info->all_entries) *
261 s->sort_info->max_entries);
262 for (i = 0; i < s->sort_info->max_entries; i++)
263 s->sort_info->entries[i] = s->sort_info->all_entries + i;
273 s->cache_position = 0;
275 s->approx_limit = zh->approx_limit;
279 ZebraSet resultSetGet(ZebraHandle zh, const char *name)
283 for (s = zh->sets; s; s = s->next)
284 if (!strcmp (s->name, name))
286 if (!s->term_entries && !s->rset && s->rpn)
288 NMEM nmem = nmem_create ();
289 yaz_log(log_level_resultsets, "research %s", name);
291 s->rset_nmem=nmem_create();
292 resultSetSearch(zh, nmem, s->rset_nmem, s->rpn, s);
293 if (s->rset && s->sortSpec)
296 yaz_log(log_level_resultsets, "resort %s", name);
297 resultSetSortSingle (zh, nmem, s, s->rset, s->sortSpec,
307 void resultSetInvalidate (ZebraHandle zh)
309 ZebraSet s = zh->sets;
311 yaz_log(log_level_resultsets, "invalidating result sets");
312 for (; s; s = s->next)
317 rset_close(s->cache_rfd);
318 rset_delete (s->rset);
322 s->cache_position = 0;
324 nmem_destroy(s->rset_nmem);
329 void resultSetDestroy(ZebraHandle zh, int num, char **names,int *statuses)
331 ZebraSet * ss = &zh->sets;
335 for (i = 0; i<num; i++)
336 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
343 for (i = 0; i<num; i++)
344 if (!strcmp (s->name, names[i]))
347 statuses[i] = Z_DeleteStatus_success;
356 xfree (s->sort_info->all_entries);
357 xfree (s->sort_info->entries);
358 xfree (s->sort_info);
361 nmem_destroy (s->nmem);
365 rset_close(s->cache_rfd);
366 rset_delete (s->rset);
369 nmem_destroy(s->rset_nmem);
378 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
383 zint *pos = pos_small;
387 if (num > 10000 || num <= 0)
391 pos = xmalloc(sizeof(*pos) * num);
393 for (i = 0; i<num; i++)
396 mr = zebra_meta_records_create(zh, name, num, pos);
403 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh, const char *name,
404 int num, zint *positions)
407 ZebraMetaRecord *sr = 0;
410 struct zset_sort_info *sort_info;
411 size_t sysno_mem_index = 0;
413 if (zh->m_staticrank)
418 if (!(sset = resultSetGet (zh, name)))
420 if (!(rset = sset->rset))
422 if (!sset->term_entries)
424 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
425 for (i = 0; i<num; i++)
432 if (positions[i] <= sset->term_entries_max)
434 sr[i].term = sset->term_entries[positions[i]-1].term;
435 sr[i].db = sset->term_entries[positions[i]-1].db;
441 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
442 for (i = 0; i<num; i++)
449 sort_info = sset->sort_info;
454 for (i = 0; i<num; i++)
456 position = positions[i];
457 if (position > 0 && position <= sort_info->num_entries)
459 yaz_log(log_level_sort, "got pos=" ZINT_FORMAT
460 " (sorted)", position);
461 sr[i].sysno = sort_info->entries[position-1]->sysno;
462 sr[i].score = sort_info->entries[position-1]->score;
466 /* did we really get all entries using sort ? */
467 for (i = 0; i<num; i++)
472 if (i < num) /* nope, get the rest, unsorted - sorry */
481 position = sort_info->num_entries;
482 while (num_i < num && positions[num_i] <= position)
485 if (sset->cache_rfd &&
486 num_i < num && positions[num_i] > sset->cache_position)
488 position = sset->cache_position;
489 rfd = sset->cache_rfd;
490 psysno = sset->cache_psysno;
495 rset_close(sset->cache_rfd);
496 rfd = rset_open (rset, RSETF_READ);
498 while (num_i < num && rset_read (rfd, &key, 0))
500 zint this_sys = key.mem[sysno_mem_index];
501 if (this_sys != psysno)
506 /* determine we alreay have this in our set */
507 for (i = sort_info->num_entries; --i >= 0; )
508 if (psysno == sort_info->entries[i]->sysno)
514 assert (num_i < num);
515 if (position == positions[num_i])
517 sr[num_i].sysno = psysno;
518 yaz_log(log_level_sort, "got pos=" ZINT_FORMAT " (unsorted)", position);
519 sr[num_i].score = -1;
524 sset->cache_position = position;
525 sset->cache_psysno = psysno;
526 sset->cache_rfd = rfd;
532 void zebra_meta_records_destroy (ZebraHandle zh, ZebraMetaRecord *records,
535 assert(zh); /* compiler shut up about unused arg */
545 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
546 struct sortKeyInfo *criteria, int num_criteria,
549 struct zset_sort_entry this_entry;
550 struct zset_sort_entry *new_entry = NULL;
551 struct zset_sort_info *sort_info = sset->sort_info;
554 sortIdx_sysno (zh->reg->sortIdx, sysno);
555 for (i = 0; i<num_criteria; i++)
557 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
558 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
560 i = sort_info->num_entries;
564 for (j = 0; j<num_criteria; j++)
566 if (criteria[j].numerical)
568 double diff = atof(this_entry.buf[j]) -
569 atof(sort_info->entries[i]->buf[j]);
578 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
586 if (criteria[j].relation == 'A')
591 else if (criteria[j].relation == 'D')
598 j = sort_info->max_entries;
602 if (sort_info->num_entries == j)
605 j = (sort_info->num_entries)++;
606 new_entry = sort_info->entries[j];
609 sort_info->entries[j] = sort_info->entries[j-1];
612 sort_info->entries[i] = new_entry;
614 for (i = 0; i<num_criteria; i++)
615 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
616 new_entry->sysno = sysno;
617 new_entry->score = -1;
620 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
621 zint sysno, int score, int relation)
623 struct zset_sort_entry *new_entry = NULL;
625 assert(zh); /* compiler shut up about unused arg */
627 i = sort_info->num_entries;
632 rel = score - sort_info->entries[i]->score;
639 else if (relation == 'A')
646 j = sort_info->max_entries;
650 if (sort_info->num_entries == j)
653 j = (sort_info->num_entries)++;
655 new_entry = sort_info->entries[j];
658 sort_info->entries[j] = sort_info->entries[j-1];
661 sort_info->entries[i] = new_entry;
663 new_entry->sysno = sysno;
664 new_entry->score = score;
667 static Z_RPNQuery *copy_RPNQuery(Z_RPNQuery *src, NMEM nmem)
670 ODR encode = odr_createmem(ODR_ENCODE);
671 ODR decode = odr_createmem(ODR_DECODE);
673 if (z_RPNQuery(encode, &src, 0, 0))
676 char *buf = odr_getbuf(encode, &len, 0);
680 odr_setbuf(decode, buf, len, 0);
681 z_RPNQuery(decode, &dst, 0, 0);
684 nmem_transfer(nmem, decode->mem);
690 static Z_SortKeySpecList *copy_SortKeySpecList(Z_SortKeySpecList *src, NMEM nmem)
692 Z_SortKeySpecList *dst = 0;
693 ODR encode = odr_createmem(ODR_ENCODE);
694 ODR decode = odr_createmem(ODR_DECODE);
696 if (z_SortKeySpecList(encode, &src, 0, 0))
699 char *buf = odr_getbuf(encode, &len, 0);
703 odr_setbuf(decode, buf, len, 0);
704 z_SortKeySpecList(decode, &dst, 0, 0);
707 nmem_transfer(nmem, decode->mem);
713 ZebraSet resultSetClone(ZebraHandle zh, const char *setname,
719 nset = resultSetAdd(zh, setname, 1);
723 nset->nmem = nmem_create();
725 nset->num_bases = rset->num_bases;
727 nmem_malloc (nset->nmem, nset->num_bases * sizeof(*rset->basenames));
728 for (i = 0; i<rset->num_bases; i++)
729 nset->basenames[i] = nmem_strdup(nset->nmem, rset->basenames[i]);
732 nset->rset = rset_dup(rset->rset);
734 nset->rpn = copy_RPNQuery(rset->rpn, nset->nmem);
738 ZEBRA_RES resultSetSort(ZebraHandle zh, NMEM nmem,
739 int num_input_setnames, const char **input_setnames,
740 const char *output_setname,
741 Z_SortKeySpecList *sort_sequence, int *sort_status)
746 if (num_input_setnames == 0)
748 zebra_setError(zh, YAZ_BIB1_NO_RESULT_SET_NAME_SUPPLIED_ON_SORT, 0);
751 if (num_input_setnames > 1)
753 zebra_setError(zh, YAZ_BIB1_SORT_TOO_MANY_INPUT_RESULTS, 0);
758 yaz_log(log_level_sort, "result set sort input=%s output=%s",
759 *input_setnames, output_setname);
760 sset = resultSetGet (zh, input_setnames[0]);
763 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
767 if (!(rset = sset->rset))
769 zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
773 if (strcmp (output_setname, input_setnames[0]))
774 sset = resultSetClone(zh, output_setname, sset);
775 sset->sortSpec = copy_SortKeySpecList(sort_sequence, sset->nmem);
776 return resultSetSortSingle (zh, nmem, sset, rset, sort_sequence,
780 ZEBRA_RES resultSetSortSingle(ZebraHandle zh, NMEM nmem,
781 ZebraSet sset, RSET rset,
782 Z_SortKeySpecList *sort_sequence,
790 struct sortKeyInfo sort_criteria[3];
796 size_t sysno_mem_index = 0;
798 if (zh->m_staticrank)
802 assert(nmem); /* compiler shut up about unused param */
803 sset->sort_info->num_entries = 0;
805 rset_getterms(rset, 0, 0, &n);
806 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
807 rset_getterms(rset, terms, n, &numTerms);
810 num_criteria = sort_sequence->num_specs;
811 if (num_criteria > 3)
813 for (i = 0; i < num_criteria; i++)
815 Z_SortKeySpec *sks = sort_sequence->specs[i];
818 if (*sks->sortRelation == Z_SortKeySpec_ascending)
819 sort_criteria[i].relation = 'A';
820 else if (*sks->sortRelation == Z_SortKeySpec_descending)
821 sort_criteria[i].relation = 'D';
824 zebra_setError(zh, YAZ_BIB1_ILLEGAL_SORT_RELATION, 0);
827 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
829 zebra_setError(zh, YAZ_BIB1_DATABASE_SPECIFIC_SORT_UNSUPP, 0);
832 else if (sks->sortElement->which != Z_SortElement_generic)
834 zebra_setError(zh, YAZ_BIB1_SORT_ILLEGAL_SORT, 0);
837 sk = sks->sortElement->u.generic;
840 case Z_SortKey_sortField:
841 yaz_log(log_level_sort, "key %d is of type sortField",
843 zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
845 case Z_SortKey_elementSpec:
846 yaz_log(log_level_sort, "key %d is of type elementSpec",
848 zebra_setError(zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
850 case Z_SortKey_sortAttributes:
851 yaz_log(log_level_sort, "key %d is of type sortAttributes", i+1);
852 sort_criteria[i].attrUse =
853 zebra_maps_sort (zh->reg->zebra_maps,
854 sk->u.sortAttributes,
855 &sort_criteria[i].numerical);
856 yaz_log(log_level_sort, "use value = %d", sort_criteria[i].attrUse);
857 if (sort_criteria[i].attrUse == -1)
860 zh, YAZ_BIB1_USE_ATTRIBUTE_REQUIRED_BUT_NOT_SUPPLIED, 0);
863 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
866 zh, YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE, 0);
872 rfd = rset_open (rset, RSETF_READ);
873 while (rset_read (rfd, &key, &termid))
875 zint this_sys = key.mem[sysno_mem_index];
876 if (log_level_searchhits)
877 key_logdump_txt(log_level_searchhits, &key, termid->name);
879 if (this_sys != psysno)
883 resultSetInsertSort (zh, sset,
884 sort_criteria, num_criteria, psysno);
888 yaz_log(log_level_sort, ZINT_FORMAT " keys, " ZINT_FORMAT " sysnos, sort",
890 for (i = 0; i < numTerms; i++)
891 yaz_log(log_level_sort, "term=\"%s\" type=%s count=" ZINT_FORMAT,
892 terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
893 *sort_status = Z_SortResponse_success;
897 RSET resultSetRef(ZebraHandle zh, const char *resultSetId)
901 if ((s = resultSetGet (zh, resultSetId)))
906 ZEBRA_RES resultSetRank(ZebraHandle zh, ZebraSet zebraSet,
907 RSET rset, NMEM nmem)
916 ZebraRankClass rank_class;
917 struct zset_sort_info *sort_info;
918 const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
919 size_t sysno_mem_index = 0;
921 if (zh->m_staticrank)
926 sort_info = zebraSet->sort_info;
927 sort_info->num_entries = 0;
929 rset_getterms(rset, 0, 0, &n);
930 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
931 rset_getterms(rset, terms, n, &numTerms);
934 rank_class = zebraRankLookup(zh, rank_handler_name);
937 yaz_log(YLOG_WARN, "No such rank handler: %s", rank_handler_name);
938 zebra_setError(zh, YAZ_BIB1_UNSUPP_SEARCH, "Cannot find rank handler");
943 RSFD rfd = rset_open(rset, RSETF_READ);
944 struct rank_control *rc = rank_class->control;
949 (*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
951 zint psysno = 0; /* previous doc id / sys no */
952 zint pstaticrank = 0; /* previous static rank */
954 while (rset_read(rfd, &key, &termid))
956 zint this_sys = key.mem[sysno_mem_index];
958 zint seqno = key.mem[key.len-1];
960 if (log_level_searchhits)
961 key_logdump_txt(log_level_searchhits, &key, termid->name);
962 if (this_sys != psysno)
963 { /* new record .. */
964 if (rfd->counted_items > rset->hits_limit)
967 { /* only if we did have a previous record */
968 score = (*rc->calc) (handle, psysno, pstaticrank,
970 /* insert the hit. A=Ascending */
971 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
977 if (zh->m_staticrank)
978 pstaticrank = key.mem[0];
980 (*rc->add) (handle, CAST_ZINT_TO_INT(seqno), termid);
984 { /* we had - at least - one record */
985 score = (*rc->calc)(handle, psysno, pstaticrank, &stop_flag);
986 /* insert the hit. A=Ascending */
987 resultSetInsertRank(zh, sort_info, psysno, score, 'A');
990 (*rc->end) (zh->reg, handle);
993 zebraSet->hits = rset->hits_count;
995 yaz_log(log_level_searchterms, ZINT_FORMAT " keys, "
996 ZINT_FORMAT " sysnos, rank", kno, zebraSet->hits);
997 for (i = 0; i < numTerms; i++)
999 yaz_log(log_level_searchterms, "term=\"%s\" type=%s count="
1001 terms[i]->name, terms[i]->flags, terms[i]->rset->hits_count);
1006 ZebraRankClass zebraRankLookup(ZebraHandle zh, const char *name)
1008 ZebraRankClass p = zh->reg->rank_classes;
1009 while (p && strcmp (p->control->name, name))
1011 if (p && !p->init_flag)
1013 if (p->control->create)
1014 p->class_handle = (*p->control->create)(zh);
1020 void zebraRankInstall(struct zebra_register *reg, struct rank_control *ctrl)
1022 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
1023 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
1024 memcpy (p->control, ctrl, sizeof(*p->control));
1025 p->control->name = xstrdup (ctrl->name);
1027 p->next = reg->rank_classes;
1028 reg->rank_classes = p;
1031 void zebraRankDestroy(struct zebra_register *reg)
1033 ZebraRankClass p = reg->rank_classes;
1036 ZebraRankClass p_next = p->next;
1037 if (p->init_flag && p->control->destroy)
1038 (*p->control->destroy)(reg, p->class_handle);
1039 xfree(p->control->name);
1044 reg->rank_classes = NULL;
1047 static int trav_rset_for_termids(RSET rset, TERMID *termid_array,
1048 zint *hits_array, int *approx_array)
1052 for (i = 0; i<rset->no_children; i++)
1053 no += trav_rset_for_termids(rset->children[i],
1054 (termid_array ? termid_array + no : 0),
1055 (hits_array ? hits_array + no : 0),
1056 (approx_array ? approx_array + no : 0));
1060 termid_array[no] = rset->term;
1062 hits_array[no] = rset->hits_count;
1064 approx_array[no] = rset->hits_approx;
1066 yaz_log(YLOG_LOG, "rset=%p term=%s limit=" ZINT_FORMAT
1067 " count=" ZINT_FORMAT,
1068 rset, rset->term->name, rset->hits_limit, rset->hits_count);
1075 ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
1078 ZebraSet sset = resultSetGet(zh, setname);
1082 *num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
1088 ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
1089 int no, zint *count, int *approx,
1090 char *termbuf, size_t *termlen,
1091 const char **term_ref_id)
1093 ZebraSet sset = resultSetGet(zh, setname);
1096 int num_terms = trav_rset_for_termids(sset->rset, 0, 0, 0);
1097 if (no >= 0 && no < num_terms)
1099 TERMID *term_array = xmalloc(num_terms * sizeof(*term_array));
1100 zint *hits_array = xmalloc(num_terms * sizeof(*hits_array));
1101 int *approx_array = xmalloc(num_terms * sizeof(*approx_array));
1103 trav_rset_for_termids(sset->rset, term_array,
1104 hits_array, approx_array);
1107 *count = hits_array[no];
1109 *approx = approx_array[no];
1112 char *inbuf = term_array[no]->name;
1113 size_t inleft = strlen(inbuf);
1114 size_t outleft = *termlen - 1;
1116 if (zh->iconv_from_utf8 != 0)
1118 char *outbuf = termbuf;
1121 ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
1123 if (ret == (size_t)(-1))
1126 *termlen = outbuf - termbuf;
1130 if (inleft > outleft)
1133 memcpy(termbuf, inbuf, *termlen);
1135 termbuf[*termlen] = '\0';
1138 *term_ref_id = term_array[no]->ref_id;
1142 xfree(approx_array);
1149 ZEBRA_RES zebra_snippets_hit_vector(ZebraHandle zh, const char *setname,
1150 zint sysno, zebra_snippets *snippets)
1152 ZebraSet sset = resultSetGet(zh, setname);
1153 yaz_log(YLOG_DEBUG, "zebra_get_hit_vector setname=%s zysno=" ZINT_FORMAT,
1159 struct rset_key_control *kc = zebra_key_control_create(zh);
1160 NMEM nmem = nmem_create();
1162 RSET rsets[2], rset_comb;
1163 RSET rset_temp = rstemp_create(nmem, kc, kc->scope,
1164 res_get (zh->res, "setTmpDir"),0 );
1167 RSFD rsfd = rset_open(rset_temp, RSETF_WRITE);
1174 rset_write (rsfd, &key);
1177 rsets[0] = rset_temp;
1178 rsets[1] = rset_dup(sset->rset);
1180 rset_comb = rsmulti_and_create(nmem, kc, kc->scope, 2, rsets);
1182 rsfd = rset_open(rset_comb, RSETF_READ);
1184 while (rset_read(rsfd, &key, &termid))
1188 struct ord_list *ol;
1189 for (ol = termid->ol; ol; ol = ol->next)
1191 zebra_snippets_append(snippets, key.mem[key.len-1],
1192 ol->ord, termid->name);
1198 rset_delete(rset_comb);
1208 * indent-tabs-mode: nil
1210 * vim: shiftwidth=4 tabstop=8 expandtab