2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 2002-02-20 17:30:01 adam
8 * Work on new API. Locking system re-implemented
10 * Revision 1.31 2001/11/19 23:05:22 adam
11 * Added a few prototypes.
13 * Revision 1.30 2001/10/15 19:53:43 adam
14 * POSIX thread updates. First work on term sets.
16 * Revision 1.29 2001/01/22 10:42:56 adam
17 * Added numerical sort.
19 * Revision 1.28 2000/07/07 12:49:20 adam
20 * Optimized resultSetInsert{Rank,Sort}.
22 * Revision 1.27 2000/04/05 09:49:36 adam
23 * On Unix, zebra/z'mbol uses automake.
25 * Revision 1.26 2000/03/20 19:08:36 adam
26 * Added remote record import using Z39.50 extended services and Segment
29 * Revision 1.25 2000/03/15 15:00:31 adam
30 * First work on threaded version.
32 * Revision 1.24 1999/11/04 15:00:45 adam
33 * Implemented delete result set(s).
35 * Revision 1.23 1999/05/26 07:49:13 adam
38 * Revision 1.22 1999/02/02 14:51:15 adam
39 * Updated WIN32 code specific sections. Changed header.
41 * Revision 1.21 1998/11/16 16:03:46 adam
42 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
44 * Revision 1.20 1998/11/16 10:10:53 adam
45 * Fixed problem with zebraPosSetCreate that occurred when positions were
48 * Revision 1.19 1998/09/22 10:48:22 adam
49 * Minor changes in search API.
51 * Revision 1.18 1998/09/22 10:03:45 adam
52 * Changed result sets to be persistent in the sense that they can
53 * be re-searched if needed.
54 * Fixed memory leak in rsm_or.
56 * Revision 1.17 1998/06/23 15:33:36 adam
57 * Added feature to specify sort criteria in query (type 7 specifies
60 * Revision 1.16 1998/05/20 10:12:24 adam
61 * Implemented automatic EXPLAIN database maintenance.
62 * Modified Zebra to work with ASN.1 compiled version of YAZ.
64 * Revision 1.15 1998/03/05 08:45:14 adam
65 * New result set model and modular ranking system. Moved towards
66 * descent server API. System information stored as "SGML" records.
68 * Revision 1.14 1998/02/10 16:39:15 adam
71 * Revision 1.13 1998/02/10 12:03:06 adam
74 * Revision 1.12 1997/09/25 14:57:36 adam
77 * Revision 1.11 1996/12/23 15:30:46 adam
79 * Bug fix: result sets weren't deleted after server shut down.
81 * Revision 1.10 1995/10/30 15:08:08 adam
84 * Revision 1.9 1995/10/17 18:02:14 adam
85 * New feature: databases. Implemented as prefix to words in dictionary.
87 * Revision 1.8 1995/10/10 13:59:25 adam
88 * Function rset_open changed its wflag parameter to general flags.
90 * Revision 1.7 1995/10/06 14:38:01 adam
91 * New result set method: r_score.
92 * Local no (sysno) and score is transferred to retrieveCtrl.
94 * Revision 1.6 1995/09/28 09:19:49 adam
95 * xfree/xmalloc used everywhere.
96 * Extract/retrieve method seems to work for text records.
98 * Revision 1.5 1995/09/27 16:17:32 adam
99 * More work on retrieve.
101 * Revision 1.4 1995/09/07 13:58:36 adam
102 * New parameter: result-set file descriptor (RSFD) to support multiple
103 * positions within the same result-set.
104 * Boolean operators: and, or, not implemented.
105 * Result-set references.
107 * Revision 1.3 1995/09/06 16:11:19 adam
108 * Option: only one word key per file.
110 * Revision 1.2 1995/09/06 10:33:04 adam
111 * More work on present. Some log messages removed.
113 * Revision 1.1 1995/09/05 15:28:40 adam
114 * More work on search engine.
128 #define SORT_IDX_ENTRYSIZE 64
129 #define ZSET_SORT_MAX_LEVEL 3
131 struct zebra_set_term_entry {
146 struct zset_sort_info *sort_info;
147 struct zebra_set_term_entry *term_entries;
148 int term_entries_max;
149 struct zebra_set *next;
153 struct zset_sort_entry {
156 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
159 struct zset_sort_info {
162 struct zset_sort_entry *all_entries;
163 struct zset_sort_entry **entries;
166 ZebraSet resultSetAddRPN (ZebraHandle zh, ODR input, ODR output,
167 Z_RPNQuery *rpn, int num_bases, char **basenames,
173 zh->errString = NULL;
176 zebraSet = resultSetAdd (zh, setname, 1);
179 zebraSet->locked = 1;
181 zebraSet->num_bases = num_bases;
182 zebraSet->basenames = basenames;
183 zebraSet->nmem = odr_extract_mem (input);
185 zebraSet->rset = rpn_search (zh, output->mem, rpn,
187 zebraSet->basenames, zebraSet->name,
189 zh->hits = zebraSet->hits;
192 zebraSet->locked = 0;
196 void resultSetAddTerm (ZebraHandle zh, ZebraSet s, int reg_type,
197 const char *db, int set,
198 int use, const char *term)
201 s->nmem = nmem_create ();
202 if (!s->term_entries)
205 s->term_entries_max = 1000;
207 nmem_malloc (s->nmem, s->term_entries_max *
208 sizeof(*s->term_entries));
209 for (i = 0; i < s->term_entries_max; i++)
210 s->term_entries[i].term = 0;
212 if (s->hits < s->term_entries_max)
214 s->term_entries[s->hits].reg_type = reg_type;
215 s->term_entries[s->hits].db = nmem_strdup (s->nmem, db);
216 s->term_entries[s->hits].set = set;
217 s->term_entries[s->hits].use = use;
218 s->term_entries[s->hits].term = nmem_strdup (s->nmem, term);
223 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
228 for (s = zh->sets; s; s = s->next)
229 if (!strcmp (s->name, name))
233 logf (LOG_DEBUG, "updating result set %s", name);
234 if (!ov || s->locked)
237 rset_delete (s->rset);
239 nmem_destroy (s->nmem);
243 logf (LOG_DEBUG, "adding result set %s", name);
244 s = (ZebraSet) xmalloc (sizeof(*s));
247 s->name = (char *) xmalloc (strlen(name)+1);
248 strcpy (s->name, name);
250 s->sort_info = (struct zset_sort_info *)
251 xmalloc (sizeof(*s->sort_info));
252 s->sort_info->max_entries = 1000;
253 s->sort_info->entries = (struct zset_sort_entry **)
254 xmalloc (sizeof(*s->sort_info->entries) *
255 s->sort_info->max_entries);
256 s->sort_info->all_entries = (struct zset_sort_entry *)
257 xmalloc (sizeof(*s->sort_info->all_entries) *
258 s->sort_info->max_entries);
259 for (i = 0; i < s->sort_info->max_entries; i++)
260 s->sort_info->entries[i] = s->sort_info->all_entries + i;
271 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
275 for (s = zh->sets; s; s = s->next)
276 if (!strcmp (s->name, name))
278 if (!s->term_entries && !s->rset && s->rpn)
280 NMEM nmem = nmem_create ();
281 yaz_log (LOG_LOG, "research %s", name);
283 rpn_search (zh, nmem, s->rpn, s->num_bases,
284 s->basenames, s->name, s);
292 void resultSetInvalidate (ZebraHandle zh)
294 ZebraSet s = zh->sets;
296 for (; s; s = s->next)
299 rset_delete (s->rset);
304 void resultSetDestroy (ZebraHandle zh, int num, char **names,int *statuses)
306 ZebraSet * ss = &zh->sets;
310 for (i = 0; i<num; i++)
311 statuses[i] = Z_DeleteStatus_resultSetDidNotExist;
313 zh->errString = NULL;
320 for (i = 0; i<num; i++)
321 if (!strcmp (s->name, names[i]))
324 statuses[i] = Z_DeleteStatus_success;
333 xfree (s->sort_info->all_entries);
334 xfree (s->sort_info->entries);
335 xfree (s->sort_info);
338 nmem_destroy (s->nmem);
340 rset_delete (s->rset);
349 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
350 int num, int *positions)
356 struct zset_sort_info *sort_info;
358 if (!(sset = resultSetGet (zh, name)))
360 if (!(rset = sset->rset))
362 if (!sset->term_entries)
364 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
365 for (i = 0; i<num; i++)
367 struct zebra_set_term_entry *entry = sset->term_entries;
374 if (positions[i] <= sset->term_entries_max)
376 sr[i].term = sset->term_entries[positions[i]-1].term;
377 sr[i].db = sset->term_entries[positions[i]-1].db;
383 sr = (ZebraPosSet) xmalloc (sizeof(*sr) * num);
384 for (i = 0; i<num; i++)
391 sort_info = sset->sort_info;
396 for (i = 0; i<num; i++)
398 position = positions[i];
399 if (position > 0 && position <= sort_info->num_entries)
401 logf (LOG_DEBUG, "got pos=%d (sorted)", position);
402 sr[i].sysno = sort_info->entries[position-1]->sysno;
403 sr[i].score = sort_info->entries[position-1]->score;
407 /* did we really get all entries using sort ? */
408 for (i = 0; i<num; i++)
413 if (i < num) /* nope, get the rest, unsorted - sorry */
423 position = sort_info->num_entries;
424 while (num_i < num && positions[num_i] < position)
426 rfd = rset_open (rset, RSETF_READ);
427 while (num_i < num && rset_read (rset, rfd, &key, &term_index))
429 if (key.sysno != psysno)
434 /* determine we alreay have this in our set */
435 for (i = sort_info->num_entries; --i >= 0; )
436 if (psysno == sort_info->entries[i]->sysno)
442 assert (num_i < num);
443 if (position == positions[num_i])
445 sr[num_i].sysno = psysno;
446 logf (LOG_DEBUG, "got pos=%d (unsorted)", position);
447 sr[num_i].score = -1;
452 rset_close (rset, rfd);
458 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
469 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
470 struct sortKeyInfo *criteria, int num_criteria,
473 struct zset_sort_entry this_entry;
474 struct zset_sort_entry *new_entry = NULL;
475 struct zset_sort_info *sort_info = sset->sort_info;
478 sortIdx_sysno (zh->service->sortIdx, sysno);
479 for (i = 0; i<num_criteria; i++)
481 sortIdx_type (zh->service->sortIdx, criteria[i].attrUse);
482 sortIdx_read (zh->service->sortIdx, this_entry.buf[i]);
484 i = sort_info->num_entries;
488 for (j = 0; j<num_criteria; j++)
490 if (criteria[j].numerical)
492 double diff = atof(this_entry.buf[j]) -
493 atof(sort_info->entries[i]->buf[j]);
502 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
510 if (criteria[j].relation == 'A')
515 else if (criteria[j].relation == 'D')
522 j = sort_info->max_entries;
526 if (sort_info->num_entries == j)
529 j = (sort_info->num_entries)++;
530 new_entry = sort_info->entries[j];
533 sort_info->entries[j] = sort_info->entries[j-1];
536 sort_info->entries[i] = new_entry;
538 for (i = 0; i<num_criteria; i++)
539 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
540 new_entry->sysno = sysno;
541 new_entry->score = -1;
544 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
545 int sysno, int score, int relation)
547 struct zset_sort_entry *new_entry = NULL;
550 i = sort_info->num_entries;
555 rel = score - sort_info->entries[i]->score;
562 else if (relation == 'A')
569 j = sort_info->max_entries;
573 if (sort_info->num_entries == j)
576 j = (sort_info->num_entries)++;
578 new_entry = sort_info->entries[j];
581 sort_info->entries[j] = sort_info->entries[j-1];
584 sort_info->entries[i] = new_entry;
586 new_entry->sysno = sysno;
587 new_entry->score = score;
590 void resultSetSort (ZebraHandle zh, NMEM nmem,
591 int num_input_setnames, const char **input_setnames,
592 const char *output_setname,
593 Z_SortKeySpecList *sort_sequence, int *sort_status)
598 if (num_input_setnames == 0)
603 if (num_input_setnames > 1)
608 logf (LOG_DEBUG, "result set sort input=%s output=%s",
609 *input_setnames, output_setname);
610 sset = resultSetGet (zh, input_setnames[0]);
614 zh->errString = nmem_strdup (nmem, input_setnames[0]);
617 if (!(rset = sset->rset))
620 zh->errString = nmem_strdup (nmem, input_setnames[0]);
623 if (strcmp (output_setname, input_setnames[0]))
625 rset = rset_dup (rset);
626 sset = resultSetAdd (zh, output_setname, 1);
629 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
632 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
633 ZebraSet sset, RSET rset,
634 Z_SortKeySpecList *sort_sequence, int *sort_status)
638 struct sortKeyInfo sort_criteria[3];
643 logf (LOG_DEBUG, "resultSetSortSingle start");
644 sset->sort_info->num_entries = 0;
647 num_criteria = sort_sequence->num_specs;
648 if (num_criteria > 3)
650 for (i = 0; i < num_criteria; i++)
652 Z_SortKeySpec *sks = sort_sequence->specs[i];
655 if (*sks->sortRelation == Z_SortRelation_ascending)
656 sort_criteria[i].relation = 'A';
657 else if (*sks->sortRelation == Z_SortRelation_descending)
658 sort_criteria[i].relation = 'D';
664 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
669 else if (sks->sortElement->which != Z_SortElement_generic)
674 sk = sks->sortElement->u.generic;
677 case Z_SortKey_sortField:
678 logf (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
681 case Z_SortKey_elementSpec:
682 logf (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
685 case Z_SortKey_sortAttributes:
686 logf (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
687 sort_criteria[i].attrUse =
688 zebra_maps_sort (zh->service->zebra_maps,
689 sk->u.sortAttributes,
690 &sort_criteria[i].numerical);
691 logf (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
692 if (sort_criteria[i].attrUse == -1)
697 if (sortIdx_type (zh->service->sortIdx, sort_criteria[i].attrUse))
705 rfd = rset_open (rset, RSETF_READ);
706 while (rset_read (rset, rfd, &key, &term_index))
708 if (key.sysno != psysno)
712 resultSetInsertSort (zh, sset,
713 sort_criteria, num_criteria, psysno);
716 rset_close (rset, rfd);
718 *sort_status = Z_SortStatus_success;
719 logf (LOG_DEBUG, "resultSetSortSingle end");
722 RSET resultSetRef (ZebraHandle zh, Z_ResultSetId *resultSetId)
726 if ((s = resultSetGet (zh, resultSetId)))
731 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
737 ZebraRankClass rank_class;
738 struct rank_control *rc;
739 struct zset_sort_info *sort_info;
741 sort_info = zebraSet->sort_info;
742 sort_info->num_entries = 0;
744 rfd = rset_open (rset, RSETF_READ);
746 logf (LOG_DEBUG, "resultSetRank");
747 for (i = 0; i < rset->no_rset_terms; i++)
748 logf (LOG_DEBUG, "term=\"%s\" cnt=%d type=%s",
749 rset->rset_terms[i]->name,
750 rset->rset_terms[i]->nn,
751 rset->rset_terms[i]->flags);
753 rank_class = zebraRankLookup (zh, "rank-1");
754 rc = rank_class->control;
756 if (rset_read (rset, rfd, &key, &term_index))
758 int psysno = key.sysno;
761 (*rc->begin) (zh, rank_class->class_handle, rset);
766 if (key.sysno != psysno)
768 score = (*rc->calc) (handle, psysno);
770 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
774 (*rc->add) (handle, key.seqno, term_index);
776 while (rset_read (rset, rfd, &key, &term_index));
777 score = (*rc->calc) (handle, psysno);
778 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
779 (*rc->end) (zh, handle);
781 rset_close (rset, rfd);
782 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, zebraSet->hits);
785 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
787 ZebraRankClass p = zh->service->rank_classes;
788 while (p && strcmp (p->control->name, name))
790 if (p && !p->init_flag)
792 if (p->control->create)
793 p->class_handle = (*p->control->create)(zh->service);
799 void zebraRankInstall (ZebraService zh, struct rank_control *ctrl)
801 ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
802 p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
803 memcpy (p->control, ctrl, sizeof(*p->control));
804 p->control->name = xstrdup (ctrl->name);
806 p->next = zh->rank_classes;
807 zh->rank_classes = p;
810 void zebraRankDestroy (ZebraService zh)
812 ZebraRankClass p = zh->rank_classes;
815 ZebraRankClass p_next = p->next;
816 if (p->init_flag && p->control->destroy)
817 (*p->control->destroy)(zh, p->class_handle);
818 xfree (p->control->name);
823 zh->rank_classes = NULL;