2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: zsets.c,v 1.34 2002-04-04 14:14:13 adam Exp $
19 #define SORT_IDX_ENTRYSIZE 64
20 #define ZSET_SORT_MAX_LEVEL 3
22 struct zebra_set_term_entry {
37 struct zset_sort_info *sort_info;
38 struct zebra_set_term_entry *term_entries;
40 struct zebra_set *next;
44 struct zset_sort_entry {
47 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
50 struct zset_sort_info {
53 struct zset_sort_entry *all_entries;
54 struct zset_sort_entry **entries;
57 ZebraSet resultSetAddRPN (ZebraHandle zh, ODR input, ODR output,
58 Z_RPNQuery *rpn, int num_bases,
69 zebraSet = resultSetAdd (zh, setname, 1);
74 zebraSet->nmem = nmem_create ();
76 zebraSet->num_bases = num_bases;
78 nmem_malloc (zebraSet->nmem, num_bases * sizeof(*zebraSet->basenames));
79 for (i = 0; i<num_bases; i++)
80 zebraSet->basenames[i] = nmem_strdup (zebraSet->nmem, basenames[i]);
83 zebraSet->rset = rpn_search (zh, output->mem, rpn,
85 zebraSet->basenames, zebraSet->name,
87 zh->hits = zebraSet->hits;
94 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
95 const char *db, int set,
96 int use, const char *term)
99 s->nmem = nmem_create ();
100 if (!s->term_entries)
103 s->term_entries_max = 1000;
105 nmem_malloc (s->nmem, s->term_entries_max *
106 sizeof(*s->term_entries));
107 for (i = 0; i < s->term_entries_max; i++)
108 s->term_entries[i].term = 0;
110 if (s->hits < s->term_entries_max)
112 s->term_entries[s->hits].reg_type = reg_type;
113 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
114 s->term_entries[s->hits].set = set;
115 s->term_entries[s->hits].use = use;
116 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
122 const char *zebra_resultSetTerms (ZebraHandle zh, const char *setname,
123 int no, int *count, int *no_max)
125 ZebraSet s = resultSetGet (zh, setname);
131 *no_max = s->rset->no_rset_terms;
132 if (no < 0 || no >= *no_max)
134 *count = s->rset->rset_terms[no]->count;
135 return s->rset->rset_terms[no]->name;
139 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
144 for (s = zh->sets; s; s = s->next)
145 if (!strcmp (s->name, name))
149 logf (LOG_DEBUG, "updating result set %s", name);
150 if (!ov || s->locked)
153 rset_delete (s->rset);
155 nmem_destroy (s->nmem);
159 logf (LOG_DEBUG, "adding result set %s", name);
160 s = (ZebraSet) xmalloc (sizeof(*s));
163 s->name = (char *) xmalloc (strlen(name)+1);
164 strcpy (s->name, name);
166 s->sort_info = (struct zset_sort_info *)
167 xmalloc (sizeof(*s->sort_info));
168 s->sort_info->max_entries = 1000;
169 s->sort_info->entries = (struct zset_sort_entry **)
170 xmalloc (sizeof(*s->sort_info->entries) *
171 s->sort_info->max_entries);
172 s->sort_info->all_entries = (struct zset_sort_entry *)
173 xmalloc (sizeof(*s->sort_info->all_entries) *
174 s->sort_info->max_entries);
175 for (i = 0; i < s->sort_info->max_entries; i++)
176 s->sort_info->entries[i] = s->sort_info->all_entries + i;
187 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
191 for (s = zh->sets; s; s = s->next)
192 if (!strcmp (s->name, name))
194 if (!s->term_entries && !s->rset && s->rpn)
196 NMEM nmem = nmem_create ();
197 yaz_log (LOG_LOG, "research %s", name);
199 rpn_search (zh, nmem, s->rpn, s->num_bases,
200 s->basenames, s->name, s);
208 void resultSetInvalidate (ZebraHandle zh)
210 ZebraSet s = zh->sets;
212 for (; s; s = s->next)
215 rset_delete (s->rset);
220 void resultSetDestroy (ZebraHandle zh, int num, char **names,int *statuses)
222 ZebraSet * ss = &zh->sets;
226 for (i = 0; i<num; i++)
227 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
229 zh->errString = NULL;
236 for (i = 0; i<num; i++)
237 if (!strcmp (s->name, names[i]))
240 statuses[i] = Z_DeleteStatus_success;
249 xfree (s->sort_info->all_entries);
250 xfree (s->sort_info->entries);
251 xfree (s->sort_info);
254 nmem_destroy (s->nmem);
256 rset_delete (s->rset);
265 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
266 int num, int *positions)
272 struct zset_sort_info *sort_info;
274 if (!(sset = resultSetGet (zh, name)))
276 if (!(rset = sset->rset))
278 if (!sset->term_entries)
280 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
281 for (i = 0; i<num; i++)
288 if (positions[i] <= sset->term_entries_max)
290 sr[i].term = sset->term_entries[positions[i]-1].term;
291 sr[i].db = sset->term_entries[positions[i]-1].db;
297 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
298 for (i = 0; i<num; i++)
305 sort_info = sset->sort_info;
310 for (i = 0; i<num; i++)
312 position = positions[i];
313 if (position > 0 && position <= sort_info->num_entries)
315 logf (LOG_DEBUG, "got pos=%d (sorted)", position);
316 sr[i].sysno = sort_info->entries[position-1]->sysno;
317 sr[i].score = sort_info->entries[position-1]->score;
321 /* did we really get all entries using sort ? */
322 for (i = 0; i<num; i++)
327 if (i < num) /* nope, get the rest, unsorted - sorry */
337 position = sort_info->num_entries;
338 while (num_i < num && positions[num_i] < position)
340 rfd = rset_open (rset, RSETF_READ);
341 while (num_i < num && rset_read (rset, rfd, &key, &term_index))
343 if (key.sysno != psysno)
348 /* determine we alreay have this in our set */
349 for (i = sort_info->num_entries; --i >= 0; )
350 if (psysno == sort_info->entries[i]->sysno)
356 assert (num_i < num);
357 if (position == positions[num_i])
359 sr[num_i].sysno = psysno;
360 logf (LOG_DEBUG, "got pos=%d (unsorted)", position);
361 sr[num_i].score = -1;
366 rset_close (rset, rfd);
372 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
383 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
384 struct sortKeyInfo *criteria, int num_criteria,
387 struct zset_sort_entry this_entry;
388 struct zset_sort_entry *new_entry = NULL;
389 struct zset_sort_info *sort_info = sset->sort_info;
392 sortIdx_sysno (zh->reg->sortIdx, sysno);
393 for (i = 0; i<num_criteria; i++)
395 sortIdx_type (zh->reg->sortIdx, criteria[i].attrUse);
396 sortIdx_read (zh->reg->sortIdx, this_entry.buf[i]);
398 i = sort_info->num_entries;
402 for (j = 0; j<num_criteria; j++)
404 if (criteria[j].numerical)
406 double diff = atof(this_entry.buf[j]) -
407 atof(sort_info->entries[i]->buf[j]);
416 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
424 if (criteria[j].relation == 'A')
429 else if (criteria[j].relation == 'D')
436 j = sort_info->max_entries;
440 if (sort_info->num_entries == j)
443 j = (sort_info->num_entries)++;
444 new_entry = sort_info->entries[j];
447 sort_info->entries[j] = sort_info->entries[j-1];
450 sort_info->entries[i] = new_entry;
452 for (i = 0; i<num_criteria; i++)
453 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
454 new_entry->sysno = sysno;
455 new_entry->score = -1;
458 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
459 int sysno, int score, int relation)
461 struct zset_sort_entry *new_entry = NULL;
464 i = sort_info->num_entries;
469 rel = score - sort_info->entries[i]->score;
476 else if (relation == 'A')
483 j = sort_info->max_entries;
487 if (sort_info->num_entries == j)
490 j = (sort_info->num_entries)++;
492 new_entry = sort_info->entries[j];
495 sort_info->entries[j] = sort_info->entries[j-1];
498 sort_info->entries[i] = new_entry;
500 new_entry->sysno = sysno;
501 new_entry->score = score;
504 void resultSetSort (ZebraHandle zh, NMEM nmem,
505 int num_input_setnames, const char **input_setnames,
506 const char *output_setname,
507 Z_SortKeySpecList *sort_sequence, int *sort_status)
512 if (num_input_setnames == 0)
517 if (num_input_setnames > 1)
522 logf (LOG_DEBUG, "result set sort input=%s output=%s",
523 *input_setnames, output_setname);
524 sset = resultSetGet (zh, input_setnames[0]);
528 zh->errString = nmem_strdup (nmem, input_setnames[0]);
531 if (!(rset = sset->rset))
534 zh->errString = nmem_strdup (nmem, input_setnames[0]);
537 if (strcmp (output_setname, input_setnames[0]))
539 rset = rset_dup (rset);
540 sset = resultSetAdd (zh, output_setname, 1);
543 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
546 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
547 ZebraSet sset, RSET rset,
548 Z_SortKeySpecList *sort_sequence, int *sort_status)
552 struct sortKeyInfo sort_criteria[3];
557 logf (LOG_LOG, "resultSetSortSingle start");
558 sset->sort_info->num_entries = 0;
561 num_criteria = sort_sequence->num_specs;
562 if (num_criteria > 3)
564 for (i = 0; i < num_criteria; i++)
566 Z_SortKeySpec *sks = sort_sequence->specs[i];
569 if (*sks->sortRelation == Z_SortRelation_ascending)
570 sort_criteria[i].relation = 'A';
571 else if (*sks->sortRelation == Z_SortRelation_descending)
572 sort_criteria[i].relation = 'D';
578 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
583 else if (sks->sortElement->which != Z_SortElement_generic)
588 sk = sks->sortElement->u.generic;
591 case Z_SortKey_sortField:
592 logf (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
595 case Z_SortKey_elementSpec:
596 logf (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
599 case Z_SortKey_sortAttributes:
600 logf (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
601 sort_criteria[i].attrUse =
602 zebra_maps_sort (zh->reg->zebra_maps,
603 sk->u.sortAttributes,
604 &sort_criteria[i].numerical);
605 logf (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
606 if (sort_criteria[i].attrUse == -1)
611 if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
619 rfd = rset_open (rset, RSETF_READ);
620 while (rset_read (rset, rfd, &key, &term_index))
622 if (key.sysno != psysno)
626 resultSetInsertSort (zh, sset,
627 sort_criteria, num_criteria, psysno);
630 rset_close (rset, rfd);
632 for (i = 0; i < rset->no_rset_terms; i++)
633 yaz_log (LOG_LOG, "term=\"%s\" nn=%d type=%s count=%d",
634 rset->rset_terms[i]->name,
635 rset->rset_terms[i]->nn,
636 rset->rset_terms[i]->flags,
637 rset->rset_terms[i]->count);
639 *sort_status = Z_SortStatus_success;
640 logf (LOG_LOG, "resultSetSortSingle end");
643 RSET resultSetRef (ZebraHandle zh, Z_ResultSetId *resultSetId)
647 if ((s = resultSetGet (zh, resultSetId)))
652 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
658 ZebraRankClass rank_class;
659 struct rank_control *rc;
660 struct zset_sort_info *sort_info;
662 sort_info = zebraSet->sort_info;
663 sort_info->num_entries = 0;
665 rfd = rset_open (rset, RSETF_READ);
667 yaz_log (LOG_LOG, "resultSetRank");
669 rank_class = zebraRankLookup (zh, "rank-1");
670 rc = rank_class->control;
672 if (rset_read (rset, rfd, &key, &term_index))
674 int psysno = key.sysno;
677 (*rc->begin) (zh->reg, rank_class->class_handle, rset);
682 if (key.sysno != psysno)
684 score = (*rc->calc) (handle, psysno);
686 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
690 (*rc->add) (handle, key.seqno, term_index);
692 while (rset_read (rset, rfd, &key, &term_index));
693 score = (*rc->calc) (handle, psysno);
694 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
695 (*rc->end) (zh->reg, handle);
697 rset_close (rset, rfd);
699 for (i = 0; i < rset->no_rset_terms; i++)
700 yaz_log (LOG_LOG, "term=\"%s\" nn=%d type=%s count=%d",
701 rset->rset_terms[i]->name,
702 rset->rset_terms[i]->nn,
703 rset->rset_terms[i]->flags,
704 rset->rset_terms[i]->count);
706 yaz_log (LOG_LOG, "%d keys, %d distinct sysnos", kno, zebraSet->hits);
709 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
711 ZebraRankClass p = zh->reg->rank_classes;
712 while (p && strcmp (p->control->name, name))
714 if (p && !p->init_flag)
716 if (p->control->create)
717 p->class_handle = (*p->control->create)(zh->reg);
723 void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
725 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
726 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
727 memcpy (p->control, ctrl, sizeof(*p->control));
728 p->control->name = xstrdup (ctrl->name);
730 p->next = reg->rank_classes;
731 reg->rank_classes = p;
734 void zebraRankDestroy (struct zebra_register *reg)
736 ZebraRankClass p = reg->rank_classes;
739 ZebraRankClass p_next = p->next;
740 if (p->init_flag && p->control->destroy)
741 (*p->control->destroy)(reg, p->class_handle);
742 xfree (p->control->name);
747 reg->rank_classes = NULL;