1 /* $Id: zsets.c,v 1.69 2004-10-26 15:32:11 heikki 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();
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->rset_nmem);
212 nmem_destroy (s->nmem);
216 const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
218 yaz_log (LOG_DEBUG, "adding result set %s", name);
219 s = (ZebraSet) xmalloc (sizeof(*s));
222 s->name = (char *) xmalloc (strlen(name)+1);
223 strcpy (s->name, name);
225 s->sort_info = (struct zset_sort_info *)
226 xmalloc (sizeof(*s->sort_info));
227 s->sort_info->max_entries = atoi(sort_max_str);
228 if (s->sort_info->max_entries < 2)
229 s->sort_info->max_entries = 2;
231 s->sort_info->entries = (struct zset_sort_entry **)
232 xmalloc (sizeof(*s->sort_info->entries) *
233 s->sort_info->max_entries);
234 s->sort_info->all_entries = (struct zset_sort_entry *)
235 xmalloc (sizeof(*s->sort_info->all_entries) *
236 s->sort_info->max_entries);
237 for (i = 0; i < s->sort_info->max_entries; i++)
238 s->sort_info->entries[i] = s->sort_info->all_entries + i;
250 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
254 for (s = zh->sets; s; s = s->next)
255 if (!strcmp (s->name, name))
257 if (!s->term_entries && !s->rset && s->rpn)
259 NMEM nmem = nmem_create ();
260 yaz_log (LOG_LOG, "research %s", name);
262 s->rset_nmem=nmem_create();
264 rpn_search (zh, nmem, s->rset_nmem, s->rpn, s->num_bases,
265 s->basenames, s->name, s);
273 void resultSetInvalidate (ZebraHandle zh)
275 ZebraSet s = zh->sets;
277 for (; s; s = s->next)
280 rset_delete (s->rset);
283 nmem_destroy(s->rset_nmem);
288 void resultSetDestroy (ZebraHandle zh, int num, char **names,int *statuses)
290 ZebraSet * ss = &zh->sets;
294 for (i = 0; i<num; i++)
295 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
297 zh->errString = NULL;
304 for (i = 0; i<num; i++)
305 if (!strcmp (s->name, names[i]))
308 statuses[i] = Z_DeleteStatus_success;
317 xfree (s->sort_info->all_entries);
318 xfree (s->sort_info->entries);
319 xfree (s->sort_info);
322 nmem_destroy (s->nmem);
324 rset_delete (s->rset);
326 nmem_destroy(s->rset_nmem);
335 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
336 int num, int *positions)
342 struct zset_sort_info *sort_info;
344 if (!(sset = resultSetGet (zh, name)))
346 if (!(rset = sset->rset))
348 if (!sset->term_entries)
350 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
351 for (i = 0; i<num; i++)
358 if (positions[i] <= sset->term_entries_max)
360 sr[i].term = sset->term_entries[positions[i]-1].term;
361 sr[i].db = sset->term_entries[positions[i]-1].db;
367 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
368 for (i = 0; i<num; i++)
375 sort_info = sset->sort_info;
380 for (i = 0; i<num; i++)
382 position = positions[i];
383 if (position > 0 && position <= sort_info->num_entries)
385 yaz_log (LOG_DEBUG, "got pos=%d (sorted)", position);
386 sr[i].sysno = sort_info->entries[position-1]->sysno;
387 sr[i].score = sort_info->entries[position-1]->score;
391 /* did we really get all entries using sort ? */
392 for (i = 0; i<num; i++)
397 if (i < num) /* nope, get the rest, unsorted - sorry */
406 position = sort_info->num_entries;
407 while (num_i < num && positions[num_i] < position)
409 rfd = rset_open (rset, RSETF_READ);
410 while (num_i < num && rset_read (rfd, &key, 0))
412 zint this_sys = key.mem[0];
413 if (this_sys != psysno)
418 /* determine we alreay have this in our set */
419 for (i = sort_info->num_entries; --i >= 0; )
420 if (psysno == sort_info->entries[i]->sysno)
426 assert (num_i < num);
427 if (position == positions[num_i])
429 sr[num_i].sysno = psysno;
430 yaz_log (LOG_DEBUG, "got pos=%d (unsorted)", position);
431 sr[num_i].score = -1;
442 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
444 assert(zh); /* compiler shut up about unused arg */
454 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
455 struct sortKeyInfo *criteria, int num_criteria,
458 struct zset_sort_entry this_entry;
459 struct zset_sort_entry *new_entry = NULL;
460 struct zset_sort_info *sort_info = sset->sort_info;
463 sortIdx_sysno (zh->reg->sortIdx, sysno);
464 for (i = 0; i<num_criteria; i++)
466 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
467 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
469 i = sort_info->num_entries;
473 for (j = 0; j<num_criteria; j++)
475 if (criteria[j].numerical)
477 double diff = atof(this_entry.buf[j]) -
478 atof(sort_info->entries[i]->buf[j]);
487 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
495 if (criteria[j].relation == 'A')
500 else if (criteria[j].relation == 'D')
507 j = sort_info->max_entries;
511 if (sort_info->num_entries == j)
514 j = (sort_info->num_entries)++;
515 new_entry = sort_info->entries[j];
518 sort_info->entries[j] = sort_info->entries[j-1];
521 sort_info->entries[i] = new_entry;
523 for (i = 0; i<num_criteria; i++)
524 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
525 new_entry->sysno = sysno;
526 new_entry->score = -1;
529 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
530 zint sysno, int score, int relation)
532 struct zset_sort_entry *new_entry = NULL;
534 assert(zh); /* compiler shut up about unused arg */
536 i = sort_info->num_entries;
541 rel = score - sort_info->entries[i]->score;
548 else if (relation == 'A')
555 j = sort_info->max_entries;
559 if (sort_info->num_entries == j)
562 j = (sort_info->num_entries)++;
564 new_entry = sort_info->entries[j];
567 sort_info->entries[j] = sort_info->entries[j-1];
570 sort_info->entries[i] = new_entry;
572 new_entry->sysno = sysno;
573 new_entry->score = score;
576 void resultSetSort (ZebraHandle zh, NMEM nmem,
577 int num_input_setnames, const char **input_setnames,
578 const char *output_setname,
579 Z_SortKeySpecList *sort_sequence, int *sort_status)
584 if (num_input_setnames == 0)
589 if (num_input_setnames > 1)
594 yaz_log (LOG_DEBUG, "result set sort input=%s output=%s",
595 *input_setnames, output_setname);
596 sset = resultSetGet (zh, input_setnames[0]);
600 zh->errString = nmem_strdup (nmem, input_setnames[0]);
603 if (!(rset = sset->rset))
606 zh->errString = nmem_strdup (nmem, input_setnames[0]);
609 if (strcmp (output_setname, input_setnames[0]))
611 rset = rset_dup (rset);
612 sset = resultSetAdd (zh, output_setname, 1);
615 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
618 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
619 ZebraSet sset, RSET rset,
620 Z_SortKeySpecList *sort_sequence, int *sort_status)
625 struct sortKeyInfo sort_criteria[3];
630 yaz_log (LOG_LOG, "resultSetSortSingle start");
631 assert(nmem); /* compiler shut up about unused param */
632 sset->sort_info->num_entries = 0;
635 num_criteria = sort_sequence->num_specs;
636 if (num_criteria > 3)
638 for (i = 0; i < num_criteria; i++)
640 Z_SortKeySpec *sks = sort_sequence->specs[i];
643 if (*sks->sortRelation == Z_SortKeySpec_ascending)
644 sort_criteria[i].relation = 'A';
645 else if (*sks->sortRelation == Z_SortKeySpec_descending)
646 sort_criteria[i].relation = 'D';
652 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
657 else if (sks->sortElement->which != Z_SortElement_generic)
662 sk = sks->sortElement->u.generic;
665 case Z_SortKey_sortField:
666 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
669 case Z_SortKey_elementSpec:
670 yaz_log (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
673 case Z_SortKey_sortAttributes:
674 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
675 sort_criteria[i].attrUse =
676 zebra_maps_sort (zh->reg->zebra_maps,
677 sk->u.sortAttributes,
678 &sort_criteria[i].numerical);
679 yaz_log (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
680 if (sort_criteria[i].attrUse == -1)
685 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
693 rfd = rset_open (rset, RSETF_READ);
694 while (rset_read (rfd, &key,&termid))
695 /* FIXME - pass a TERMID *, and use it for something below !! */
697 zint this_sys = key.mem[0];
698 if (this_sys != psysno)
702 resultSetInsertSort (zh, sset,
703 sort_criteria, num_criteria, psysno);
709 for (i = 0; i < rset->no_rset_terms; i++)
710 yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT
711 " type=%s count=" ZINT_FORMAT,
712 rset->rset_terms[i]->name,
713 rset->rset_terms[i]->nn,
714 rset->rset_terms[i]->flags,
715 rset->rset_terms[i]->count);
717 *sort_status = Z_SortResponse_success;
718 yaz_log (LOG_LOG, "resultSetSortSingle end");
721 RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
725 if ((s = resultSetGet (zh, resultSetId)))
730 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset, NMEM nmem)
739 ZebraRankClass rank_class;
740 struct rank_control *rc;
741 struct zset_sort_info *sort_info;
742 const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
744 zint est=-2; /* -2 not done, -1 can't do, >0 actual estimate*/
748 sort_info = zebraSet->sort_info;
749 sort_info->num_entries = 0;
752 rset_getterms(rset,0,0,&n);
753 terms=malloc( sizeof(*terms)*n);
755 rset_getterms(rset,terms,n,&numterms);
757 rfd = rset_open (rset, RSETF_READ);
759 rank_class = zebraRankLookup (zh, rank_handler_name);
762 yaz_log (LOG_WARN, "No such rank handler: %s", rank_handler_name);
765 rc = rank_class->control;
767 if (rset_read (rfd, &key, &termid))
769 zint psysno = key.mem[0];
772 (*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
775 esthits=atoi(res_get_def(zh->res,"estimatehits","0"));
777 est=-1; /* can not do */
780 zint this_sys = key.mem[0]; /* FIXME - assumes scope==2 */
781 zint seqno = key.mem[1]; /* FIXME - assumes scope==2 */
783 if (this_sys != psysno)
785 score = (*rc->calc) (handle, psysno);
787 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
791 (*rc->add) (handle, seqno, termid);
793 if ( (est==-2) && (zebraSet->hits==esthits))
794 { /* time to estimate the hits */
795 rset_pos(rfd,&cur,&tot);
798 est=(zint)(0.5+zebraSet->hits/ratio);
799 logf(LOG_LOG, "Estimating hits (%s) "
801 "; %0.1f->"ZINT_FORMAT,
805 i=0; /* round to 3 significant digits */
815 while (rset_read (rfd, &key,&termid) && (est<0) );
816 score = (*rc->calc) (handle, psysno);
817 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
818 (*rc->end) (zh->reg, handle);
823 for (i = 0; i < numterms; i++)
825 yaz_log (LOG_LOG, "term=\"%s\" "
826 " type=%s count=" ZINT_FORMAT,
829 rset_count(terms[i]->rset));
831 yaz_log (LOG_DEBUG, ZINT_FORMAT " keys, "ZINT_FORMAT" distinct sysnos",
832 kno, zebraSet->hits);
835 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
837 ZebraRankClass p = zh->reg->rank_classes;
838 while (p && strcmp (p->control->name, name))
840 if (p && !p->init_flag)
842 if (p->control->create)
843 p->class_handle = (*p->control->create)(zh);
849 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
851 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
852 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
853 memcpy (p->control, ctrl, sizeof(*p->control));
854 p->control->name = xstrdup (ctrl->name);
856 p->next = reg->rank_classes;
857 reg->rank_classes = p;
860 void zebraRankDestroy (struct zebra_register *reg)
862 ZebraRankClass p = reg->rank_classes;
865 ZebraRankClass p_next = p->next;
866 if (p->init_flag && p->control->destroy)
867 (*p->control->destroy)(reg, p->class_handle);
868 xfree (p->control->name);
873 reg->rank_classes = NULL;