1 /* $Id: zsets.c,v 1.55 2004-08-10 08:19:15 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 {
53 struct zset_sort_info *sort_info;
54 struct zebra_set_term_entry *term_entries;
56 struct zebra_set *next;
60 struct zset_sort_entry {
63 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
66 struct zset_sort_info {
69 struct zset_sort_entry *all_entries;
70 struct zset_sort_entry **entries;
73 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m,
74 Z_RPNQuery *rpn, int num_bases,
85 zebraSet = resultSetAdd (zh, setname, 1);
92 zebraSet->num_bases = num_bases;
94 nmem_malloc (zebraSet->nmem, num_bases * sizeof(*zebraSet->basenames));
95 for (i = 0; i<num_bases; i++)
96 zebraSet->basenames[i] = nmem_strdup (zebraSet->nmem, basenames[i]);
99 zebraSet->rset = rpn_search (zh, zebraSet->nmem, rpn,
101 zebraSet->basenames, zebraSet->name,
103 zh->hits = zebraSet->hits;
106 zebraSet->locked = 0;
110 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
111 const char *db, int set,
112 int use, const char *term)
114 assert(zh); /* compiler shut up */
116 s->nmem = nmem_create ();
117 if (!s->term_entries)
120 s->term_entries_max = 1000;
122 nmem_malloc (s->nmem, s->term_entries_max *
123 sizeof(*s->term_entries));
124 for (i = 0; i < s->term_entries_max; i++)
125 s->term_entries[i].term = 0;
127 if (s->hits < s->term_entries_max)
129 s->term_entries[s->hits].reg_type = reg_type;
130 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
131 s->term_entries[s->hits].set = set;
132 s->term_entries[s->hits].use = use;
133 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
139 int zebra_resultSetTerms (ZebraHandle zh, const char *setname,
141 int *type, char *out, size_t *len)
143 ZebraSet s = resultSetGet (zh, setname);
150 no_max = s->rset->no_rset_terms;
151 if (no < 0 || no >= no_max)
154 *count = s->rset->rset_terms[no]->count;
156 *type = s->rset->rset_terms[no]->type;
160 char *inbuf = s->rset->rset_terms[no]->name;
161 size_t inleft = strlen(inbuf);
162 size_t outleft = *len - 1;
165 if (zh->iconv_from_utf8 != 0)
170 ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
172 if (ret == (size_t)(-1))
180 if (inleft > outleft)
183 memcpy (out, inbuf, *len);
191 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
196 for (s = zh->sets; s; s = s->next)
197 if (!strcmp (s->name, name))
201 yaz_log (LOG_DEBUG, "updating result set %s", name);
202 if (!ov || s->locked)
205 rset_delete (s->rset);
207 nmem_destroy (s->nmem);
211 const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
213 yaz_log (LOG_DEBUG, "adding result set %s", name);
214 s = (ZebraSet) xmalloc (sizeof(*s));
217 s->name = (char *) xmalloc (strlen(name)+1);
218 strcpy (s->name, name);
220 s->sort_info = (struct zset_sort_info *)
221 xmalloc (sizeof(*s->sort_info));
222 s->sort_info->max_entries = atoi(sort_max_str);
223 if (s->sort_info->max_entries < 2)
224 s->sort_info->max_entries = 2;
226 s->sort_info->entries = (struct zset_sort_entry **)
227 xmalloc (sizeof(*s->sort_info->entries) *
228 s->sort_info->max_entries);
229 s->sort_info->all_entries = (struct zset_sort_entry *)
230 xmalloc (sizeof(*s->sort_info->all_entries) *
231 s->sort_info->max_entries);
232 for (i = 0; i < s->sort_info->max_entries; i++)
233 s->sort_info->entries[i] = s->sort_info->all_entries + i;
244 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
248 for (s = zh->sets; s; s = s->next)
249 if (!strcmp (s->name, name))
251 if (!s->term_entries && !s->rset && s->rpn)
253 NMEM nmem = nmem_create ();
254 yaz_log (LOG_LOG, "research %s", name);
256 rpn_search (zh, nmem, s->rpn, s->num_bases,
257 s->basenames, s->name, s);
265 void resultSetInvalidate (ZebraHandle zh)
267 ZebraSet s = zh->sets;
269 for (; s; s = s->next)
272 rset_delete (s->rset);
277 void resultSetDestroy (ZebraHandle zh, int num, char **names,int *statuses)
279 ZebraSet * ss = &zh->sets;
283 for (i = 0; i<num; i++)
284 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
286 zh->errString = NULL;
293 for (i = 0; i<num; i++)
294 if (!strcmp (s->name, names[i]))
297 statuses[i] = Z_DeleteStatus_success;
306 xfree (s->sort_info->all_entries);
307 xfree (s->sort_info->entries);
308 xfree (s->sort_info);
311 nmem_destroy (s->nmem);
313 rset_delete (s->rset);
322 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
323 int num, int *positions)
329 struct zset_sort_info *sort_info;
331 if (!(sset = resultSetGet (zh, name)))
333 if (!(rset = sset->rset))
335 if (!sset->term_entries)
337 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
338 for (i = 0; i<num; i++)
345 if (positions[i] <= sset->term_entries_max)
347 sr[i].term = sset->term_entries[positions[i]-1].term;
348 sr[i].db = sset->term_entries[positions[i]-1].db;
354 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
355 for (i = 0; i<num; i++)
362 sort_info = sset->sort_info;
367 for (i = 0; i<num; i++)
369 position = positions[i];
370 if (position > 0 && position <= sort_info->num_entries)
372 yaz_log (LOG_DEBUG, "got pos=%d (sorted)", position);
373 sr[i].sysno = sort_info->entries[position-1]->sysno;
374 sr[i].score = sort_info->entries[position-1]->score;
378 /* did we really get all entries using sort ? */
379 for (i = 0; i<num; i++)
384 if (i < num) /* nope, get the rest, unsorted - sorry */
394 position = sort_info->num_entries;
395 while (num_i < num && positions[num_i] < position)
397 rfd = rset_open (rset, RSETF_READ);
398 while (num_i < num && rset_read (rset, rfd, &key, &term_index))
401 zint this_sys = key.mem[0];
403 zint this_sys = key.sysno;
405 if (this_sys != psysno)
410 /* determine we alreay have this in our set */
411 for (i = sort_info->num_entries; --i >= 0; )
412 if (psysno == sort_info->entries[i]->sysno)
418 assert (num_i < num);
419 if (position == positions[num_i])
421 sr[num_i].sysno = psysno;
422 yaz_log (LOG_DEBUG, "got pos=%d (unsorted)", position);
423 sr[num_i].score = -1;
428 rset_close (rset, rfd);
434 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
436 assert(zh); /* compiler shut up about unused arg */
446 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
447 struct sortKeyInfo *criteria, int num_criteria,
450 struct zset_sort_entry this_entry;
451 struct zset_sort_entry *new_entry = NULL;
452 struct zset_sort_info *sort_info = sset->sort_info;
455 sortIdx_sysno (zh->reg->sortIdx, sysno);
456 for (i = 0; i<num_criteria; i++)
458 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
459 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
461 i = sort_info->num_entries;
465 for (j = 0; j<num_criteria; j++)
467 if (criteria[j].numerical)
469 double diff = atof(this_entry.buf[j]) -
470 atof(sort_info->entries[i]->buf[j]);
479 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
487 if (criteria[j].relation == 'A')
492 else if (criteria[j].relation == 'D')
499 j = sort_info->max_entries;
503 if (sort_info->num_entries == j)
506 j = (sort_info->num_entries)++;
507 new_entry = sort_info->entries[j];
510 sort_info->entries[j] = sort_info->entries[j-1];
513 sort_info->entries[i] = new_entry;
515 for (i = 0; i<num_criteria; i++)
516 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
517 new_entry->sysno = sysno;
518 new_entry->score = -1;
521 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
522 zint sysno, int score, int relation)
524 struct zset_sort_entry *new_entry = NULL;
526 assert(zh); /* compiler shut up about unused arg */
528 i = sort_info->num_entries;
533 rel = score - sort_info->entries[i]->score;
540 else if (relation == 'A')
547 j = sort_info->max_entries;
551 if (sort_info->num_entries == j)
554 j = (sort_info->num_entries)++;
556 new_entry = sort_info->entries[j];
559 sort_info->entries[j] = sort_info->entries[j-1];
562 sort_info->entries[i] = new_entry;
564 new_entry->sysno = sysno;
565 new_entry->score = score;
568 void resultSetSort (ZebraHandle zh, NMEM nmem,
569 int num_input_setnames, const char **input_setnames,
570 const char *output_setname,
571 Z_SortKeySpecList *sort_sequence, int *sort_status)
576 if (num_input_setnames == 0)
581 if (num_input_setnames > 1)
586 yaz_log (LOG_DEBUG, "result set sort input=%s output=%s",
587 *input_setnames, output_setname);
588 sset = resultSetGet (zh, input_setnames[0]);
592 zh->errString = nmem_strdup (nmem, input_setnames[0]);
595 if (!(rset = sset->rset))
598 zh->errString = nmem_strdup (nmem, input_setnames[0]);
601 if (strcmp (output_setname, input_setnames[0]))
603 rset = rset_dup (rset);
604 sset = resultSetAdd (zh, output_setname, 1);
607 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
610 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
611 ZebraSet sset, RSET rset,
612 Z_SortKeySpecList *sort_sequence, int *sort_status)
617 struct sortKeyInfo sort_criteria[3];
622 yaz_log (LOG_LOG, "resultSetSortSingle start");
623 assert(nmem); /* compiler shut up about unused param */
624 sset->sort_info->num_entries = 0;
627 num_criteria = sort_sequence->num_specs;
628 if (num_criteria > 3)
630 for (i = 0; i < num_criteria; i++)
632 Z_SortKeySpec *sks = sort_sequence->specs[i];
635 if (*sks->sortRelation == Z_SortKeySpec_ascending)
636 sort_criteria[i].relation = 'A';
637 else if (*sks->sortRelation == Z_SortKeySpec_descending)
638 sort_criteria[i].relation = 'D';
644 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
649 else if (sks->sortElement->which != Z_SortElement_generic)
654 sk = sks->sortElement->u.generic;
657 case Z_SortKey_sortField:
658 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
661 case Z_SortKey_elementSpec:
662 yaz_log (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
665 case Z_SortKey_sortAttributes:
666 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
667 sort_criteria[i].attrUse =
668 zebra_maps_sort (zh->reg->zebra_maps,
669 sk->u.sortAttributes,
670 &sort_criteria[i].numerical);
671 yaz_log (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
672 if (sort_criteria[i].attrUse == -1)
677 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
685 rfd = rset_open (rset, RSETF_READ);
686 while (rset_read (rset, rfd, &key, &term_index))
689 zint this_sys = key.mem[0];
691 zint this_sys = key.sysno;
693 if (this_sys != psysno)
697 resultSetInsertSort (zh, sset,
698 sort_criteria, num_criteria, psysno);
701 rset_close (rset, rfd);
703 for (i = 0; i < rset->no_rset_terms; i++)
704 yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT " type=%s count=" ZINT_FORMAT,
705 rset->rset_terms[i]->name,
706 rset->rset_terms[i]->nn,
707 rset->rset_terms[i]->flags,
708 rset->rset_terms[i]->count);
710 *sort_status = Z_SortResponse_success;
711 yaz_log (LOG_LOG, "resultSetSortSingle end");
714 RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
718 if ((s = resultSetGet (zh, resultSetId)))
723 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
729 ZebraRankClass rank_class;
730 struct rank_control *rc;
731 struct zset_sort_info *sort_info;
732 const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
734 zint est=-2; /* -2 not done, -1 can't do, >0 actual estimate*/
737 sort_info = zebraSet->sort_info;
738 sort_info->num_entries = 0;
740 rfd = rset_open (rset, RSETF_READ);
742 yaz_log (LOG_LOG, "resultSetRank");
744 rank_class = zebraRankLookup (zh, rank_handler_name);
747 yaz_log (LOG_WARN, "No such rank handler: %s", rank_handler_name);
750 rc = rank_class->control;
752 if (rset_read (rset, rfd, &key, &term_index))
755 zint psysno = key.mem[0];
757 zint psysno = key.sysno;
761 (*rc->begin) (zh->reg, rank_class->class_handle, rset);
763 esthits=atoi(res_get_def(zh->res,"estimatehits","0"));
764 if (!esthits) est=-1; /* can not do */
768 zint this_sys = key.mem[0];
770 zint this_sys = key.sysno;
773 if (this_sys != psysno)
775 score = (*rc->calc) (handle, psysno);
777 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
781 (*rc->add) (handle, this_sys, term_index);
782 if ( (est==-2) && (zebraSet->hits==esthits))
783 { /* time to estimate the hits */
785 rset_pos(rset,rfd,&cur,&tot);
788 est=(zint)(0.5+zebraSet->hits/f);
789 logf(LOG_LOG, "Estimating hits (%s) "
791 "; %0.1f->"ZINT_FORMAT,
795 i=0; /* round to 3 significant digits */
805 while (rset_read (rset, rfd, &key, &term_index) && (est<0) );
807 score = (*rc->calc) (handle, psysno);
808 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
809 (*rc->end) (zh->reg, handle);
811 rset_close (rset, rfd);
813 for (i = 0; i < rset->no_rset_terms; i++)
814 yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT " type=%s count=" ZINT_FORMAT,
815 rset->rset_terms[i]->name,
816 rset->rset_terms[i]->nn,
817 rset->rset_terms[i]->flags,
818 rset->rset_terms[i]->count);
820 yaz_log (LOG_LOG, ZINT_FORMAT " keys, "ZINT_FORMAT" distinct sysnos",
821 kno, zebraSet->hits);
824 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
826 ZebraRankClass p = zh->reg->rank_classes;
827 while (p && strcmp (p->control->name, name))
829 if (p && !p->init_flag)
831 if (p->control->create)
832 p->class_handle = (*p->control->create)(zh);
838 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
840 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
841 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
842 memcpy (p->control, ctrl, sizeof(*p->control));
843 p->control->name = xstrdup (ctrl->name);
845 p->next = reg->rank_classes;
846 reg->rank_classes = p;
849 void zebraRankDestroy (struct zebra_register *reg)
851 ZebraRankClass p = reg->rank_classes;
854 ZebraRankClass p_next = p->next;
855 if (p->init_flag && p->control->destroy)
856 (*p->control->destroy)(reg, p->class_handle);
857 xfree (p->control->name);
862 reg->rank_classes = NULL;