1 /* $Id: zsets.c,v 1.62 2004-09-15 08:13:51 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
35 #define SORT_IDX_ENTRYSIZE 64
36 #define ZSET_SORT_MAX_LEVEL 3
38 struct zebra_set_term_entry {
49 NMEM rset_nmem; /* for creating the rsets in */
54 struct zset_sort_info *sort_info;
55 struct zebra_set_term_entry *term_entries;
57 struct zebra_set *next;
61 struct zset_sort_entry {
64 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
67 struct zset_sort_info {
70 struct zset_sort_entry *all_entries;
71 struct zset_sort_entry **entries;
74 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m,
75 Z_RPNQuery *rpn, int num_bases,
86 zebraSet = resultSetAdd (zh, setname, 1);
92 zebraSet->rset_nmem=nmem_create(); /* FIXME - where to free this ?? */
94 zebraSet->num_bases = num_bases;
96 nmem_malloc (zebraSet->nmem, num_bases * sizeof(*zebraSet->basenames));
97 for (i = 0; i<num_bases; i++)
98 zebraSet->basenames[i] = nmem_strdup (zebraSet->nmem, basenames[i]);
101 zebraSet->rset = rpn_search (zh, zebraSet->nmem, zebraSet->rset_nmem,
102 rpn, zebraSet->num_bases,
103 zebraSet->basenames, zebraSet->name,
105 zh->hits = zebraSet->hits;
108 zebraSet->locked = 0;
112 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
113 const char *db, int set,
114 int use, const char *term)
116 assert(zh); /* compiler shut up */
118 s->nmem = nmem_create ();
119 if (!s->term_entries)
122 s->term_entries_max = 1000;
124 nmem_malloc (s->nmem, s->term_entries_max *
125 sizeof(*s->term_entries));
126 for (i = 0; i < s->term_entries_max; i++)
127 s->term_entries[i].term = 0;
129 if (s->hits < s->term_entries_max)
131 s->term_entries[s->hits].reg_type = reg_type;
132 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
133 s->term_entries[s->hits].set = set;
134 s->term_entries[s->hits].use = use;
135 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
140 #if 0 /* FIXME - Delete this, we don't count terms no more */
141 int zebra_resultSetTerms (ZebraHandle zh, const char *setname,
143 int *type, char *out, size_t *len)
145 ZebraSet s = resultSetGet (zh, setname);
152 no_max = s->rset->no_rset_terms;
153 if (no < 0 || no >= no_max)
156 *count = s->rset->rset_terms[no]->count;
158 *type = s->rset->rset_terms[no]->type;
162 char *inbuf = s->rset->rset_terms[no]->name;
163 size_t inleft = strlen(inbuf);
164 size_t outleft = *len - 1;
167 if (zh->iconv_from_utf8 != 0)
172 ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
174 if (ret == (size_t)(-1))
182 if (inleft > outleft)
185 memcpy (out, inbuf, *len);
194 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
199 for (s = zh->sets; s; s = s->next)
200 if (!strcmp (s->name, name))
204 yaz_log (LOG_DEBUG, "updating result set %s", name);
205 if (!ov || s->locked)
208 rset_delete (s->rset);
210 nmem_destroy (s->nmem);
214 const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
216 yaz_log (LOG_DEBUG, "adding result set %s", name);
217 s = (ZebraSet) xmalloc (sizeof(*s));
220 s->name = (char *) xmalloc (strlen(name)+1);
221 strcpy (s->name, name);
223 s->sort_info = (struct zset_sort_info *)
224 xmalloc (sizeof(*s->sort_info));
225 s->sort_info->max_entries = atoi(sort_max_str);
226 if (s->sort_info->max_entries < 2)
227 s->sort_info->max_entries = 2;
229 s->sort_info->entries = (struct zset_sort_entry **)
230 xmalloc (sizeof(*s->sort_info->entries) *
231 s->sort_info->max_entries);
232 s->sort_info->all_entries = (struct zset_sort_entry *)
233 xmalloc (sizeof(*s->sort_info->all_entries) *
234 s->sort_info->max_entries);
235 for (i = 0; i < s->sort_info->max_entries; i++)
236 s->sort_info->entries[i] = s->sort_info->all_entries + i;
248 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
252 for (s = zh->sets; s; s = s->next)
253 if (!strcmp (s->name, name))
255 if (!s->term_entries && !s->rset && s->rpn)
257 NMEM nmem = nmem_create ();
258 yaz_log (LOG_LOG, "research %s", name);
260 rpn_search (zh, nmem, s->rset_nmem, s->rpn, s->num_bases,
261 s->basenames, s->name, s);
269 void resultSetInvalidate (ZebraHandle zh)
271 ZebraSet s = zh->sets;
273 for (; s; s = s->next)
276 rset_delete (s->rset);
279 nmem_destroy(s->rset_nmem);
284 void resultSetDestroy (ZebraHandle zh, int num, char **names,int *statuses)
286 ZebraSet * ss = &zh->sets;
290 for (i = 0; i<num; i++)
291 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
293 zh->errString = NULL;
300 for (i = 0; i<num; i++)
301 if (!strcmp (s->name, names[i]))
304 statuses[i] = Z_DeleteStatus_success;
313 xfree (s->sort_info->all_entries);
314 xfree (s->sort_info->entries);
315 xfree (s->sort_info);
318 nmem_destroy (s->nmem);
320 rset_delete (s->rset);
322 nmem_destroy(s->rset_nmem);
331 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
332 int num, int *positions)
338 struct zset_sort_info *sort_info;
340 if (!(sset = resultSetGet (zh, name)))
342 if (!(rset = sset->rset))
344 if (!sset->term_entries)
346 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
347 for (i = 0; i<num; i++)
354 if (positions[i] <= sset->term_entries_max)
356 sr[i].term = sset->term_entries[positions[i]-1].term;
357 sr[i].db = sset->term_entries[positions[i]-1].db;
363 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
364 for (i = 0; i<num; i++)
371 sort_info = sset->sort_info;
376 for (i = 0; i<num; i++)
378 position = positions[i];
379 if (position > 0 && position <= sort_info->num_entries)
381 yaz_log (LOG_DEBUG, "got pos=%d (sorted)", position);
382 sr[i].sysno = sort_info->entries[position-1]->sysno;
383 sr[i].score = sort_info->entries[position-1]->score;
387 /* did we really get all entries using sort ? */
388 for (i = 0; i<num; i++)
393 if (i < num) /* nope, get the rest, unsorted - sorry */
402 position = sort_info->num_entries;
403 while (num_i < num && positions[num_i] < position)
405 rfd = rset_open (rset, RSETF_READ);
406 while (num_i < num && rset_read (rfd, &key))
408 zint this_sys = key.mem[0];
409 if (this_sys != psysno)
414 /* determine we alreay have this in our set */
415 for (i = sort_info->num_entries; --i >= 0; )
416 if (psysno == sort_info->entries[i]->sysno)
422 assert (num_i < num);
423 if (position == positions[num_i])
425 sr[num_i].sysno = psysno;
426 yaz_log (LOG_DEBUG, "got pos=%d (unsorted)", position);
427 sr[num_i].score = -1;
438 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
440 assert(zh); /* compiler shut up about unused arg */
450 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
451 struct sortKeyInfo *criteria, int num_criteria,
454 struct zset_sort_entry this_entry;
455 struct zset_sort_entry *new_entry = NULL;
456 struct zset_sort_info *sort_info = sset->sort_info;
459 sortIdx_sysno (zh->reg->sortIdx, sysno);
460 for (i = 0; i<num_criteria; i++)
462 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
463 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
465 i = sort_info->num_entries;
469 for (j = 0; j<num_criteria; j++)
471 if (criteria[j].numerical)
473 double diff = atof(this_entry.buf[j]) -
474 atof(sort_info->entries[i]->buf[j]);
483 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
491 if (criteria[j].relation == 'A')
496 else if (criteria[j].relation == 'D')
503 j = sort_info->max_entries;
507 if (sort_info->num_entries == j)
510 j = (sort_info->num_entries)++;
511 new_entry = sort_info->entries[j];
514 sort_info->entries[j] = sort_info->entries[j-1];
517 sort_info->entries[i] = new_entry;
519 for (i = 0; i<num_criteria; i++)
520 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
521 new_entry->sysno = sysno;
522 new_entry->score = -1;
525 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
526 zint sysno, int score, int relation)
528 struct zset_sort_entry *new_entry = NULL;
530 assert(zh); /* compiler shut up about unused arg */
532 i = sort_info->num_entries;
537 rel = score - sort_info->entries[i]->score;
544 else if (relation == 'A')
551 j = sort_info->max_entries;
555 if (sort_info->num_entries == j)
558 j = (sort_info->num_entries)++;
560 new_entry = sort_info->entries[j];
563 sort_info->entries[j] = sort_info->entries[j-1];
566 sort_info->entries[i] = new_entry;
568 new_entry->sysno = sysno;
569 new_entry->score = score;
572 void resultSetSort (ZebraHandle zh, NMEM nmem,
573 int num_input_setnames, const char **input_setnames,
574 const char *output_setname,
575 Z_SortKeySpecList *sort_sequence, int *sort_status)
580 if (num_input_setnames == 0)
585 if (num_input_setnames > 1)
590 yaz_log (LOG_DEBUG, "result set sort input=%s output=%s",
591 *input_setnames, output_setname);
592 sset = resultSetGet (zh, input_setnames[0]);
596 zh->errString = nmem_strdup (nmem, input_setnames[0]);
599 if (!(rset = sset->rset))
602 zh->errString = nmem_strdup (nmem, input_setnames[0]);
605 if (strcmp (output_setname, input_setnames[0]))
607 rset = rset_dup (rset);
608 sset = resultSetAdd (zh, output_setname, 1);
611 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
614 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
615 ZebraSet sset, RSET rset,
616 Z_SortKeySpecList *sort_sequence, int *sort_status)
621 struct sortKeyInfo sort_criteria[3];
625 yaz_log (LOG_LOG, "resultSetSortSingle start");
626 assert(nmem); /* compiler shut up about unused param */
627 sset->sort_info->num_entries = 0;
630 num_criteria = sort_sequence->num_specs;
631 if (num_criteria > 3)
633 for (i = 0; i < num_criteria; i++)
635 Z_SortKeySpec *sks = sort_sequence->specs[i];
638 if (*sks->sortRelation == Z_SortKeySpec_ascending)
639 sort_criteria[i].relation = 'A';
640 else if (*sks->sortRelation == Z_SortKeySpec_descending)
641 sort_criteria[i].relation = 'D';
647 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
652 else if (sks->sortElement->which != Z_SortElement_generic)
657 sk = sks->sortElement->u.generic;
660 case Z_SortKey_sortField:
661 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
664 case Z_SortKey_elementSpec:
665 yaz_log (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
668 case Z_SortKey_sortAttributes:
669 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
670 sort_criteria[i].attrUse =
671 zebra_maps_sort (zh->reg->zebra_maps,
672 sk->u.sortAttributes,
673 &sort_criteria[i].numerical);
674 yaz_log (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
675 if (sort_criteria[i].attrUse == -1)
680 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
688 rfd = rset_open (rset, RSETF_READ);
689 while (rset_read (rfd, &key))
691 zint this_sys = key.mem[0];
692 if (this_sys != psysno)
696 resultSetInsertSort (zh, sset,
697 sort_criteria, num_criteria, psysno);
703 for (i = 0; i < rset->no_rset_terms; i++)
704 yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT
705 " type=%s count=" ZINT_FORMAT,
706 rset->rset_terms[i]->name,
707 rset->rset_terms[i]->nn,
708 rset->rset_terms[i]->flags,
709 rset->rset_terms[i]->count);
711 *sort_status = Z_SortResponse_success;
712 yaz_log (LOG_LOG, "resultSetSortSingle end");
715 RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
719 if ((s = resultSetGet (zh, resultSetId)))
724 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
729 /* int term_index; */
731 ZebraRankClass rank_class;
732 struct rank_control *rc;
733 struct zset_sort_info *sort_info;
734 const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
736 zint est=-2; /* -2 not done, -1 can't do, >0 actual estimate*/
740 sort_info = zebraSet->sort_info;
741 sort_info->num_entries = 0;
743 rfd = rset_open (rset, RSETF_READ);
745 rank_class = zebraRankLookup (zh, rank_handler_name);
748 yaz_log (LOG_WARN, "No such rank handler: %s", rank_handler_name);
751 rc = rank_class->control;
753 if (rset_read (rfd, &key))
755 zint psysno = key.mem[0];
758 (*rc->begin) (zh->reg, rank_class->class_handle, rset);
760 esthits=atoi(res_get_def(zh->res,"estimatehits","0"));
762 est=-1; /* can not do */
765 zint this_sys = key.mem[0];
767 if (this_sys != psysno)
769 score = (*rc->calc) (handle, psysno);
771 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
775 /* FIXME - Ranking is broken, since rsets no longer have */
777 /* (*rc->add) (handle, this_sys, term_index); */
779 if ( (est==-2) && (zebraSet->hits==esthits))
780 { /* time to estimate the hits */
781 rset_pos(rfd,&cur,&tot);
784 est=(zint)(0.5+zebraSet->hits/ratio);
785 logf(LOG_LOG, "Estimating hits (%s) "
787 "; %0.1f->"ZINT_FORMAT,
791 i=0; /* round to 3 significant digits */
801 while (rset_read (rfd, &key) && (est<0) );
803 score = (*rc->calc) (handle, psysno);
804 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
805 (*rc->end) (zh->reg, handle);
809 for (i = 0; i < rset->no_rset_terms; i++)
812 rset->rset_terms[i]->count =
813 est=(zint)(rset->rset_terms[i]->count/ratio);
814 yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT
815 " type=%s count=" ZINT_FORMAT,
816 rset->rset_terms[i]->name,
817 rset->rset_terms[i]->nn,
818 rset->rset_terms[i]->flags,
819 rset->rset_terms[i]->count);
822 yaz_log (LOG_DEBUG, ZINT_FORMAT " keys, "ZINT_FORMAT" distinct sysnos",
823 kno, zebraSet->hits);
826 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
828 ZebraRankClass p = zh->reg->rank_classes;
829 while (p && strcmp (p->control->name, name))
831 if (p && !p->init_flag)
833 if (p->control->create)
834 p->class_handle = (*p->control->create)(zh);
840 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
842 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
843 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
844 memcpy (p->control, ctrl, sizeof(*p->control));
845 p->control->name = xstrdup (ctrl->name);
847 p->next = reg->rank_classes;
848 reg->rank_classes = p;
851 void zebraRankDestroy (struct zebra_register *reg)
853 ZebraRankClass p = reg->rank_classes;
856 ZebraRankClass p_next = p->next;
857 if (p->init_flag && p->control->destroy)
858 (*p->control->destroy)(reg, p->class_handle);
859 xfree (p->control->name);
864 reg->rank_classes = NULL;