1 /* $Id: zsets.c,v 1.77 2005-01-15 19:38:30 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
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;
60 zint cache_position; /* last position */
61 RSFD cache_rfd; /* rfd (NULL if not existing) */
62 zint cache_psysno; /* sysno for last position */
65 struct zset_sort_entry {
68 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
71 struct zset_sort_info {
74 struct zset_sort_entry *all_entries;
75 struct zset_sort_entry **entries;
78 static int log_level_set=0;
79 static int log_level_sorting=0;
80 static int log_level_searchhits=0;
81 static int log_level_searchterms=0;
82 static int log_level_resultsets=0;
84 static void loglevels()
88 log_level_sorting = yaz_log_module_level("sorting");
89 log_level_searchhits = yaz_log_module_level("searchhits");
90 log_level_searchterms = yaz_log_module_level("searchterms");
91 log_level_resultsets = yaz_log_module_level("resultsets");
95 ZebraSet resultSetAddRPN (ZebraHandle zh, NMEM m,
96 Z_RPNQuery *rpn, int num_bases,
104 zh->errString = NULL;
107 zebraSet = resultSetAdd (zh, setname, 1);
110 zebraSet->locked = 1;
113 zebraSet->rset_nmem=nmem_create();
115 zebraSet->num_bases = num_bases;
116 zebraSet->basenames =
117 nmem_malloc (zebraSet->nmem, num_bases * sizeof(*zebraSet->basenames));
118 for (i = 0; i<num_bases; i++)
119 zebraSet->basenames[i] = nmem_strdup (zebraSet->nmem, basenames[i]);
122 zebraSet->rset = rpn_search (zh, zebraSet->nmem, zebraSet->rset_nmem,
123 rpn, zebraSet->num_bases,
124 zebraSet->basenames, zebraSet->name,
126 zh->hits = zebraSet->hits;
129 zebraSet->locked = 0;
133 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
134 const char *db, int set,
135 int use, const char *term)
137 assert(zh); /* compiler shut up */
139 s->nmem = nmem_create ();
140 if (!s->term_entries)
143 s->term_entries_max = 1000;
145 nmem_malloc (s->nmem, s->term_entries_max *
146 sizeof(*s->term_entries));
147 for (i = 0; i < s->term_entries_max; i++)
148 s->term_entries[i].term = 0;
150 if (s->hits < s->term_entries_max)
152 s->term_entries[s->hits].reg_type = reg_type;
153 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
154 s->term_entries[s->hits].set = set;
155 s->term_entries[s->hits].use = use;
156 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
161 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
166 for (s = zh->sets; s; s = s->next)
167 if (!strcmp (s->name, name))
174 yaz_log(log_level_resultsets, "updating result set %s", name);
175 if (!ov || s->locked)
180 rset_close(s->cache_rfd);
181 rset_delete (s->rset);
184 nmem_destroy (s->rset_nmem);
186 nmem_destroy (s->nmem);
190 const char *sort_max_str = zebra_get_resource(zh, "sortmax", "1000");
192 yaz_log(log_level_resultsets, "adding result set %s", name);
193 s = (ZebraSet) xmalloc (sizeof(*s));
196 s->name = (char *) xmalloc (strlen(name)+1);
197 strcpy (s->name, name);
199 s->sort_info = (struct zset_sort_info *)
200 xmalloc (sizeof(*s->sort_info));
201 s->sort_info->max_entries = atoi(sort_max_str);
202 if (s->sort_info->max_entries < 2)
203 s->sort_info->max_entries = 2;
205 s->sort_info->entries = (struct zset_sort_entry **)
206 xmalloc (sizeof(*s->sort_info->entries) *
207 s->sort_info->max_entries);
208 s->sort_info->all_entries = (struct zset_sort_entry *)
209 xmalloc (sizeof(*s->sort_info->all_entries) *
210 s->sort_info->max_entries);
211 for (i = 0; i < s->sort_info->max_entries; i++)
212 s->sort_info->entries[i] = s->sort_info->all_entries + i;
221 s->cache_position = 0;
226 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
230 for (s = zh->sets; s; s = s->next)
231 if (!strcmp (s->name, name))
233 if (!s->term_entries && !s->rset && s->rpn)
235 NMEM nmem = nmem_create ();
236 yaz_log(log_level_resultsets, "research %s", name);
238 s->rset_nmem=nmem_create();
240 rpn_search (zh, nmem, s->rset_nmem, s->rpn, s->num_bases,
241 s->basenames, s->name, s);
249 void resultSetInvalidate (ZebraHandle zh)
251 ZebraSet s = zh->sets;
253 yaz_log(log_level_resultsets, "invalidating result sets");
254 for (; s; s = s->next)
259 rset_close(s->cache_rfd);
260 rset_delete (s->rset);
264 s->cache_position = 0;
266 nmem_destroy(s->rset_nmem);
271 void resultSetDestroy (ZebraHandle zh, int num, char **names,int *statuses)
273 ZebraSet * ss = &zh->sets;
277 for (i = 0; i<num; i++)
278 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
280 zh->errString = NULL;
287 for (i = 0; i<num; i++)
288 if (!strcmp (s->name, names[i]))
291 statuses[i] = Z_DeleteStatus_success;
300 xfree (s->sort_info->all_entries);
301 xfree (s->sort_info->entries);
302 xfree (s->sort_info);
305 nmem_destroy (s->nmem);
309 rset_close(s->cache_rfd);
310 rset_delete (s->rset);
313 nmem_destroy(s->rset_nmem);
322 ZebraMetaRecord *zebra_meta_records_create_range (ZebraHandle zh,
327 zint *pos = pos_small;
331 if (num > 10000 || num <= 0)
335 pos = xmalloc(sizeof(*pos) * num);
337 for (i = 0; i<num; i++)
340 mr = zebra_meta_records_create(zh, name, num, pos);
347 ZebraMetaRecord *zebra_meta_records_create (ZebraHandle zh, const char *name,
348 int num, zint *positions)
351 ZebraMetaRecord *sr = 0;
354 struct zset_sort_info *sort_info;
358 if (!(sset = resultSetGet (zh, name)))
360 if (!(rset = sset->rset))
362 if (!sset->term_entries)
364 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
365 for (i = 0; i<num; i++)
372 if (positions[i] <= sset->term_entries_max)
374 sr[i].term = sset->term_entries[positions[i]-1].term;
375 sr[i].db = sset->term_entries[positions[i]-1].db;
381 sr = (ZebraMetaRecord *) xmalloc (sizeof(*sr) * num);
382 for (i = 0; i<num; i++)
389 sort_info = sset->sort_info;
394 for (i = 0; i<num; i++)
396 position = positions[i];
397 if (position > 0 && position <= sort_info->num_entries)
399 yaz_log(log_level_sorting, "got pos=" ZINT_FORMAT
400 " (sorted)", position);
401 sr[i].sysno = sort_info->entries[position-1]->sysno;
402 sr[i].score = sort_info->entries[position-1]->score;
406 /* did we really get all entries using sort ? */
407 for (i = 0; i<num; i++)
412 if (i < num) /* nope, get the rest, unsorted - sorry */
421 position = sort_info->num_entries;
422 while (num_i < num && positions[num_i] < position)
425 if (sset->cache_rfd &&
426 num_i < num && positions[num_i] > sset->cache_position)
428 position = sset->cache_position;
429 rfd = sset->cache_rfd;
430 psysno = sset->cache_psysno;
435 rset_close(sset->cache_rfd);
436 rfd = rset_open (rset, RSETF_READ);
438 while (num_i < num && rset_read (rfd, &key, 0))
440 zint this_sys = key.mem[0];
441 if (this_sys != psysno)
446 /* determine we alreay have this in our set */
447 for (i = sort_info->num_entries; --i >= 0; )
448 if (psysno == sort_info->entries[i]->sysno)
454 assert (num_i < num);
455 if (position == positions[num_i])
457 sr[num_i].sysno = psysno;
458 yaz_log(log_level_sorting, "got pos=" ZINT_FORMAT " (unsorted)", position);
459 sr[num_i].score = -1;
464 sset->cache_position = position;
465 sset->cache_psysno = psysno;
466 sset->cache_rfd = rfd;
472 void zebra_meta_records_destroy (ZebraHandle zh, ZebraMetaRecord *records,
475 assert(zh); /* compiler shut up about unused arg */
485 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
486 struct sortKeyInfo *criteria, int num_criteria,
489 struct zset_sort_entry this_entry;
490 struct zset_sort_entry *new_entry = NULL;
491 struct zset_sort_info *sort_info = sset->sort_info;
494 sortIdx_sysno (zh->reg->sortIdx, sysno);
495 for (i = 0; i<num_criteria; i++)
497 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
498 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
500 i = sort_info->num_entries;
504 for (j = 0; j<num_criteria; j++)
506 if (criteria[j].numerical)
508 double diff = atof(this_entry.buf[j]) -
509 atof(sort_info->entries[i]->buf[j]);
518 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
526 if (criteria[j].relation == 'A')
531 else if (criteria[j].relation == 'D')
538 j = sort_info->max_entries;
542 if (sort_info->num_entries == j)
545 j = (sort_info->num_entries)++;
546 new_entry = sort_info->entries[j];
549 sort_info->entries[j] = sort_info->entries[j-1];
552 sort_info->entries[i] = new_entry;
554 for (i = 0; i<num_criteria; i++)
555 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
556 new_entry->sysno = sysno;
557 new_entry->score = -1;
560 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
561 zint sysno, int score, int relation)
563 struct zset_sort_entry *new_entry = NULL;
565 assert(zh); /* compiler shut up about unused arg */
567 i = sort_info->num_entries;
572 rel = score - sort_info->entries[i]->score;
579 else if (relation == 'A')
586 j = sort_info->max_entries;
590 if (sort_info->num_entries == j)
593 j = (sort_info->num_entries)++;
595 new_entry = sort_info->entries[j];
598 sort_info->entries[j] = sort_info->entries[j-1];
601 sort_info->entries[i] = new_entry;
603 new_entry->sysno = sysno;
604 new_entry->score = score;
607 void resultSetSort (ZebraHandle zh, NMEM nmem,
608 int num_input_setnames, const char **input_setnames,
609 const char *output_setname,
610 Z_SortKeySpecList *sort_sequence, int *sort_status)
615 if (num_input_setnames == 0)
620 if (num_input_setnames > 1)
627 yaz_log(log_level_sorting, "result set sort input=%s output=%s",
628 *input_setnames, output_setname);
629 sset = resultSetGet (zh, input_setnames[0]);
633 zh->errString = nmem_strdup (nmem, input_setnames[0]);
636 if (!(rset = sset->rset))
639 zh->errString = nmem_strdup (nmem, input_setnames[0]);
642 if (strcmp (output_setname, input_setnames[0]))
644 rset = rset_dup (rset);
645 sset = resultSetAdd (zh, output_setname, 1);
648 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
651 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
652 ZebraSet sset, RSET rset,
653 Z_SortKeySpecList *sort_sequence, int *sort_status)
660 struct sortKeyInfo sort_criteria[3];
667 assert(nmem); /* compiler shut up about unused param */
668 sset->sort_info->num_entries = 0;
670 rset_getterms(rset, 0, 0, &n);
671 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
672 rset_getterms(rset, terms, n, &numTerms);
675 num_criteria = sort_sequence->num_specs;
676 if (num_criteria > 3)
678 for (i = 0; i < num_criteria; i++)
680 Z_SortKeySpec *sks = sort_sequence->specs[i];
683 if (*sks->sortRelation == Z_SortKeySpec_ascending)
684 sort_criteria[i].relation = 'A';
685 else if (*sks->sortRelation == Z_SortKeySpec_descending)
686 sort_criteria[i].relation = 'D';
692 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
697 else if (sks->sortElement->which != Z_SortElement_generic)
702 sk = sks->sortElement->u.generic;
705 case Z_SortKey_sortField:
706 yaz_log(log_level_sorting, "Sort: key %d is of type sortField", i+1);
709 case Z_SortKey_elementSpec:
710 yaz_log(log_level_sorting, "Sort: key %d is of type elementSpec", i+1);
713 case Z_SortKey_sortAttributes:
714 yaz_log(log_level_sorting, "Sort: key %d is of type sortAttributes", i+1);
715 sort_criteria[i].attrUse =
716 zebra_maps_sort (zh->reg->zebra_maps,
717 sk->u.sortAttributes,
718 &sort_criteria[i].numerical);
719 yaz_log(log_level_sorting, "use value = %d", sort_criteria[i].attrUse);
720 if (sort_criteria[i].attrUse == -1)
725 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
733 rfd = rset_open (rset, RSETF_READ);
734 while (rset_read (rfd, &key, &termid))
735 /* FIXME - pass a TERMID *, and use it for something below !! */
737 zint this_sys = key.mem[0];
739 if (this_sys != psysno)
743 resultSetInsertSort (zh, sset,
744 sort_criteria, num_criteria, psysno);
748 yaz_log(log_level_sorting, ZINT_FORMAT " keys, " ZINT_FORMAT " sysnos, sort",
750 for (i = 0; i < numTerms; i++)
751 yaz_log(log_level_sorting, "term=\"%s\" type=%s count=" ZINT_FORMAT,
752 terms[i]->name, terms[i]->flags, rset_count(terms[i]->rset));
753 *sort_status = Z_SortResponse_success;
756 RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
760 if ((s = resultSetGet (zh, resultSetId)))
765 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset, NMEM nmem)
775 ZebraRankClass rank_class;
776 struct rank_control *rc;
777 struct zset_sort_info *sort_info;
778 const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
780 zint est=-2; /* -2 not done, -1 can't do, >0 actual estimate*/
786 sort_info = zebraSet->sort_info;
787 sort_info->num_entries = 0;
789 rset_getterms(rset, 0, 0, &n);
790 terms = (TERMID *) nmem_malloc(nmem, sizeof(*terms)*n);
791 rset_getterms(rset, terms, n, &numTerms);
793 rfd = rset_open (rset, RSETF_READ);
795 rank_class = zebraRankLookup (zh, rank_handler_name);
798 yaz_log(YLOG_WARN, "No such rank handler: %s", rank_handler_name);
801 rc = rank_class->control;
803 if (rset_read (rfd, &key, &termid))
805 zint psysno = key.mem[0];
808 (*rc->begin) (zh->reg, rank_class->class_handle, rset, nmem,
811 esthits = atoi(res_get_def(zh->res, "estimatehits","0"));
813 est = -1; /* can not do */
816 zint this_sys = key.mem[0]; /* FIXME - assumes scope==2 */
817 zint seqno = key.mem[key.len-1]; /* FIXME - assumes scope==2 */
819 key_logdump_txt(log_level_searchhits,&key," Got hit");
820 if (this_sys != psysno)
822 score = (*rc->calc) (handle, psysno);
824 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
828 (*rc->add) (handle, seqno, termid);
830 if ((est==-2) && (zebraSet->hits==esthits))
831 { /* time to estimate the hits */
832 rset_pos(rfd,&cur,&tot);
835 est = (zint)(0.5+zebraSet->hits/ratio);
836 yaz_log(log_level_searchhits, "Estimating hits (%s) "
837 "%0.1f->" ZINT_FORMAT
838 "; %0.1f->" ZINT_FORMAT,
842 i = 0; /* round to 3 significant digits */
849 zebraSet->hits = est;
853 while (rset_read (rfd, &key,&termid) && (est<0) );
854 score = (*rc->calc)(handle, psysno);
855 resultSetInsertRank(zh, sort_info, psysno, score, 'A');
856 (*rc->end) (zh->reg, handle);
860 yaz_log(log_level_searchterms, ZINT_FORMAT " keys, " ZINT_FORMAT " sysnos, rank",
861 kno, zebraSet->hits);
862 for (i = 0; i < numTerms; i++)
864 yaz_log(log_level_searchterms, "term=\"%s\" type=%s count=" ZINT_FORMAT,
865 terms[i]->name, terms[i]->flags, rset_count(terms[i]->rset));
869 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
871 ZebraRankClass p = zh->reg->rank_classes;
872 while (p && strcmp (p->control->name, name))
874 if (p && !p->init_flag)
876 if (p->control->create)
877 p->class_handle = (*p->control->create)(zh);
883 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
885 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
886 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
887 memcpy (p->control, ctrl, sizeof(*p->control));
888 p->control->name = xstrdup (ctrl->name);
890 p->next = reg->rank_classes;
891 reg->rank_classes = p;
894 void zebraRankDestroy (struct zebra_register *reg)
896 ZebraRankClass p = reg->rank_classes;
899 ZebraRankClass p_next = p->next;
900 if (p->init_flag && p->control->destroy)
901 (*p->control->destroy)(reg, p->class_handle);
902 xfree(p->control->name);
907 reg->rank_classes = NULL;