1 /* $Id: zsets.c,v 1.47 2004-05-10 08:47:54 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 {
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)
115 s->nmem = nmem_create ();
116 if (!s->term_entries)
119 s->term_entries_max = 1000;
121 nmem_malloc (s->nmem, s->term_entries_max *
122 sizeof(*s->term_entries));
123 for (i = 0; i < s->term_entries_max; i++)
124 s->term_entries[i].term = 0;
126 if (s->hits < s->term_entries_max)
128 s->term_entries[s->hits].reg_type = reg_type;
129 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
130 s->term_entries[s->hits].set = set;
131 s->term_entries[s->hits].use = use;
132 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
138 int zebra_resultSetTerms (ZebraHandle zh, const char *setname,
140 int *type, char *out, size_t *len)
142 ZebraSet s = resultSetGet (zh, setname);
149 no_max = s->rset->no_rset_terms;
150 if (no < 0 || no >= no_max)
153 *count = s->rset->rset_terms[no]->count;
155 *type = s->rset->rset_terms[no]->type;
159 char *inbuf = s->rset->rset_terms[no]->name;
160 size_t inleft = strlen(inbuf);
161 size_t outleft = *len - 1;
164 if (zh->iconv_from_utf8 != 0)
169 ret = yaz_iconv(zh->iconv_from_utf8, &inbuf, &inleft,
171 if (ret == (size_t)(-1))
179 if (inleft > outleft)
182 memcpy (out, inbuf, *len);
190 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
195 for (s = zh->sets; s; s = s->next)
196 if (!strcmp (s->name, name))
200 yaz_log (LOG_DEBUG, "updating result set %s", name);
201 if (!ov || s->locked)
204 rset_delete (s->rset);
206 nmem_destroy (s->nmem);
210 yaz_log (LOG_DEBUG, "adding result set %s", name);
211 s = (ZebraSet) xmalloc (sizeof(*s));
214 s->name = (char *) xmalloc (strlen(name)+1);
215 strcpy (s->name, name);
217 s->sort_info = (struct zset_sort_info *)
218 xmalloc (sizeof(*s->sort_info));
219 s->sort_info->max_entries = 1000;
220 s->sort_info->entries = (struct zset_sort_entry **)
221 xmalloc (sizeof(*s->sort_info->entries) *
222 s->sort_info->max_entries);
223 s->sort_info->all_entries = (struct zset_sort_entry *)
224 xmalloc (sizeof(*s->sort_info->all_entries) *
225 s->sort_info->max_entries);
226 for (i = 0; i < s->sort_info->max_entries; i++)
227 s->sort_info->entries[i] = s->sort_info->all_entries + i;
238 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
242 for (s = zh->sets; s; s = s->next)
243 if (!strcmp (s->name, name))
245 if (!s->term_entries && !s->rset && s->rpn)
247 NMEM nmem = nmem_create ();
248 yaz_log (LOG_LOG, "research %s", name);
250 rpn_search (zh, nmem, s->rpn, s->num_bases,
251 s->basenames, s->name, s);
259 void resultSetInvalidate (ZebraHandle zh)
261 ZebraSet s = zh->sets;
263 for (; s; s = s->next)
266 rset_delete (s->rset);
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);
307 rset_delete (s->rset);
316 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
317 int num, int *positions)
323 struct zset_sort_info *sort_info;
325 if (!(sset = resultSetGet (zh, name)))
327 if (!(rset = sset->rset))
329 if (!sset->term_entries)
331 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
332 for (i = 0; i<num; i++)
339 if (positions[i] <= sset->term_entries_max)
341 sr[i].term = sset->term_entries[positions[i]-1].term;
342 sr[i].db = sset->term_entries[positions[i]-1].db;
348 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
349 for (i = 0; i<num; i++)
356 sort_info = sset->sort_info;
361 for (i = 0; i<num; i++)
363 position = positions[i];
364 if (position > 0 && position <= sort_info->num_entries)
366 yaz_log (LOG_DEBUG, "got pos=%d (sorted)", position);
367 sr[i].sysno = sort_info->entries[position-1]->sysno;
368 sr[i].score = sort_info->entries[position-1]->score;
372 /* did we really get all entries using sort ? */
373 for (i = 0; i<num; i++)
378 if (i < num) /* nope, get the rest, unsorted - sorry */
388 position = sort_info->num_entries;
389 while (num_i < num && positions[num_i] < position)
391 rfd = rset_open (rset, RSETF_READ);
392 while (num_i < num && rset_read (rset, rfd, &key, &term_index))
394 if (key.sysno != psysno)
399 /* determine we alreay have this in our set */
400 for (i = sort_info->num_entries; --i >= 0; )
401 if (psysno == sort_info->entries[i]->sysno)
407 assert (num_i < num);
408 if (position == positions[num_i])
410 sr[num_i].sysno = psysno;
411 yaz_log (LOG_DEBUG, "got pos=%d (unsorted)", position);
412 sr[num_i].score = -1;
417 rset_close (rset, rfd);
423 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
434 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
435 struct sortKeyInfo *criteria, int num_criteria,
438 struct zset_sort_entry this_entry;
439 struct zset_sort_entry *new_entry = NULL;
440 struct zset_sort_info *sort_info = sset->sort_info;
443 sortIdx_sysno (zh->reg->sortIdx, sysno);
444 for (i = 0; i<num_criteria; i++)
446 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
447 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
449 i = sort_info->num_entries;
453 for (j = 0; j<num_criteria; j++)
455 if (criteria[j].numerical)
457 double diff = atof(this_entry.buf[j]) -
458 atof(sort_info->entries[i]->buf[j]);
467 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
475 if (criteria[j].relation == 'A')
480 else if (criteria[j].relation == 'D')
487 j = sort_info->max_entries;
491 if (sort_info->num_entries == j)
494 j = (sort_info->num_entries)++;
495 new_entry = sort_info->entries[j];
498 sort_info->entries[j] = sort_info->entries[j-1];
501 sort_info->entries[i] = new_entry;
503 for (i = 0; i<num_criteria; i++)
504 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
505 new_entry->sysno = sysno;
506 new_entry->score = -1;
509 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
510 int sysno, int score, int relation)
512 struct zset_sort_entry *new_entry = NULL;
515 i = sort_info->num_entries;
520 rel = score - sort_info->entries[i]->score;
527 else if (relation == 'A')
534 j = sort_info->max_entries;
538 if (sort_info->num_entries == j)
541 j = (sort_info->num_entries)++;
543 new_entry = sort_info->entries[j];
546 sort_info->entries[j] = sort_info->entries[j-1];
549 sort_info->entries[i] = new_entry;
551 new_entry->sysno = sysno;
552 new_entry->score = score;
555 void resultSetSort (ZebraHandle zh, NMEM nmem,
556 int num_input_setnames, const char **input_setnames,
557 const char *output_setname,
558 Z_SortKeySpecList *sort_sequence, int *sort_status)
563 if (num_input_setnames == 0)
568 if (num_input_setnames > 1)
573 yaz_log (LOG_DEBUG, "result set sort input=%s output=%s",
574 *input_setnames, output_setname);
575 sset = resultSetGet (zh, input_setnames[0]);
579 zh->errString = nmem_strdup (nmem, input_setnames[0]);
582 if (!(rset = sset->rset))
585 zh->errString = nmem_strdup (nmem, input_setnames[0]);
588 if (strcmp (output_setname, input_setnames[0]))
590 rset = rset_dup (rset);
591 sset = resultSetAdd (zh, output_setname, 1);
594 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
597 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
598 ZebraSet sset, RSET rset,
599 Z_SortKeySpecList *sort_sequence, int *sort_status)
603 struct sortKeyInfo sort_criteria[3];
608 yaz_log (LOG_LOG, "resultSetSortSingle start");
609 sset->sort_info->num_entries = 0;
612 num_criteria = sort_sequence->num_specs;
613 if (num_criteria > 3)
615 for (i = 0; i < num_criteria; i++)
617 Z_SortKeySpec *sks = sort_sequence->specs[i];
620 if (*sks->sortRelation == Z_SortKeySpec_ascending)
621 sort_criteria[i].relation = 'A';
622 else if (*sks->sortRelation == Z_SortKeySpec_descending)
623 sort_criteria[i].relation = 'D';
629 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
634 else if (sks->sortElement->which != Z_SortElement_generic)
639 sk = sks->sortElement->u.generic;
642 case Z_SortKey_sortField:
643 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
646 case Z_SortKey_elementSpec:
647 yaz_log (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
650 case Z_SortKey_sortAttributes:
651 yaz_log (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
652 sort_criteria[i].attrUse =
653 zebra_maps_sort (zh->reg->zebra_maps,
654 sk->u.sortAttributes,
655 &sort_criteria[i].numerical);
656 yaz_log (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
657 if (sort_criteria[i].attrUse == -1)
662 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
670 rfd = rset_open (rset, RSETF_READ);
671 while (rset_read (rset, rfd, &key, &term_index))
673 if (key.sysno != psysno)
677 resultSetInsertSort (zh, sset,
678 sort_criteria, num_criteria, psysno);
681 rset_close (rset, rfd);
683 for (i = 0; i < rset->no_rset_terms; i++)
684 yaz_log (LOG_LOG, "term=\"%s\" nn=%d type=%s count=%d",
685 rset->rset_terms[i]->name,
686 rset->rset_terms[i]->nn,
687 rset->rset_terms[i]->flags,
688 rset->rset_terms[i]->count);
690 *sort_status = Z_SortResponse_success;
691 yaz_log (LOG_LOG, "resultSetSortSingle end");
694 RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
698 if ((s = resultSetGet (zh, resultSetId)))
703 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
709 ZebraRankClass rank_class;
710 struct rank_control *rc;
711 struct zset_sort_info *sort_info;
713 sort_info = zebraSet->sort_info;
714 sort_info->num_entries = 0;
716 rfd = rset_open (rset, RSETF_READ);
718 yaz_log (LOG_LOG, "resultSetRank");
720 rank_class = zebraRankLookup (zh, res_get_def(zh->res, "rank", "rank-1"));
721 rc = rank_class->control;
723 if (rset_read (rset, rfd, &key, &term_index))
725 int psysno = key.sysno;
728 (*rc->begin) (zh->reg, rank_class->class_handle, rset);
733 if (key.sysno != psysno)
735 score = (*rc->calc) (handle, psysno);
737 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
741 (*rc->add) (handle, key.seqno, term_index);
743 while (rset_read (rset, rfd, &key, &term_index));
744 score = (*rc->calc) (handle, psysno);
745 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
746 (*rc->end) (zh->reg, handle);
748 rset_close (rset, rfd);
750 for (i = 0; i < rset->no_rset_terms; i++)
751 yaz_log (LOG_LOG, "term=\"%s\" nn=%d type=%s count=%d",
752 rset->rset_terms[i]->name,
753 rset->rset_terms[i]->nn,
754 rset->rset_terms[i]->flags,
755 rset->rset_terms[i]->count);
757 yaz_log (LOG_LOG, "%d keys, %d distinct sysnos", kno, zebraSet->hits);
760 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
762 ZebraRankClass p = zh->reg->rank_classes;
763 while (p && strcmp (p->control->name, name))
765 if (p && !p->init_flag)
767 if (p->control->create)
768 p->class_handle = (*p->control->create)(zh);
774 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
776 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
777 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
778 memcpy (p->control, ctrl, sizeof(*p->control));
779 p->control->name = xstrdup (ctrl->name);
781 p->next = reg->rank_classes;
782 reg->rank_classes = p;
785 void zebraRankDestroy (struct zebra_register *reg)
787 ZebraRankClass p = reg->rank_classes;
790 ZebraRankClass p_next = p->next;
791 if (p->init_flag && p->control->destroy)
792 (*p->control->destroy)(reg, p->class_handle);
793 xfree (p->control->name);
798 reg->rank_classes = NULL;