2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1999-02-02 14:51:15 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.21 1998/11/16 16:03:46 adam
11 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
13 * Revision 1.20 1998/11/16 10:10:53 adam
14 * Fixed problem with zebraPosSetCreate that occurred when positions were
17 * Revision 1.19 1998/09/22 10:48:22 adam
18 * Minor changes in search API.
20 * Revision 1.18 1998/09/22 10:03:45 adam
21 * Changed result sets to be persistent in the sense that they can
22 * be re-searched if needed.
23 * Fixed memory leak in rsm_or.
25 * Revision 1.17 1998/06/23 15:33:36 adam
26 * Added feature to specify sort criteria in query (type 7 specifies
29 * Revision 1.16 1998/05/20 10:12:24 adam
30 * Implemented automatic EXPLAIN database maintenance.
31 * Modified Zebra to work with ASN.1 compiled version of YAZ.
33 * Revision 1.15 1998/03/05 08:45:14 adam
34 * New result set model and modular ranking system. Moved towards
35 * descent server API. System information stored as "SGML" records.
37 * Revision 1.14 1998/02/10 16:39:15 adam
40 * Revision 1.13 1998/02/10 12:03:06 adam
43 * Revision 1.12 1997/09/25 14:57:36 adam
46 * Revision 1.11 1996/12/23 15:30:46 adam
48 * Bug fix: result sets weren't deleted after server shut down.
50 * Revision 1.10 1995/10/30 15:08:08 adam
53 * Revision 1.9 1995/10/17 18:02:14 adam
54 * New feature: databases. Implemented as prefix to words in dictionary.
56 * Revision 1.8 1995/10/10 13:59:25 adam
57 * Function rset_open changed its wflag parameter to general flags.
59 * Revision 1.7 1995/10/06 14:38:01 adam
60 * New result set method: r_score.
61 * Local no (sysno) and score is transferred to retrieveCtrl.
63 * Revision 1.6 1995/09/28 09:19:49 adam
64 * xfree/xmalloc used everywhere.
65 * Extract/retrieve method seems to work for text records.
67 * Revision 1.5 1995/09/27 16:17:32 adam
68 * More work on retrieve.
70 * Revision 1.4 1995/09/07 13:58:36 adam
71 * New parameter: result-set file descriptor (RSFD) to support multiple
72 * positions within the same result-set.
73 * Boolean operators: and, or, not implemented.
74 * Result-set references.
76 * Revision 1.3 1995/09/06 16:11:19 adam
77 * Option: only one word key per file.
79 * Revision 1.2 1995/09/06 10:33:04 adam
80 * More work on present. Some log messages removed.
82 * Revision 1.1 1995/09/05 15:28:40 adam
83 * More work on search engine.
97 #define SORT_IDX_ENTRYSIZE 64
98 #define ZSET_SORT_MAX_LEVEL 3
108 struct zset_sort_info *sort_info;
109 struct zebra_set *next;
112 struct zset_sort_entry {
115 char buf[ZSET_SORT_MAX_LEVEL][SORT_IDX_ENTRYSIZE];
118 struct zset_sort_info {
121 struct zset_sort_entry *all_entries;
122 struct zset_sort_entry **entries;
125 ZebraSet resultSetAddRPN (ZebraHandle zh, ODR input, ODR output,
126 Z_RPNQuery *rpn, int num_bases, char **basenames,
132 zh->errString = NULL;
135 zebraSet = resultSetAdd (zh, setname, 1);
139 zebraSet->num_bases = num_bases;
140 zebraSet->basenames = basenames;
141 zebraSet->nmem = odr_extract_mem (input);
143 zebraSet->rset = rpn_search (zh, output->mem, rpn,
145 zebraSet->basenames, zebraSet->name,
147 zh->hits = zebraSet->hits;
153 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov)
158 for (s = zh->sets; s; s = s->next)
159 if (!strcmp (s->name, name))
163 logf (LOG_DEBUG, "updating result set %s", name);
167 rset_delete (s->rset);
169 nmem_destroy (s->nmem);
173 logf (LOG_DEBUG, "adding result set %s", name);
174 s = xmalloc (sizeof(*s));
177 s->name = xmalloc (strlen(name)+1);
178 strcpy (s->name, name);
180 s->sort_info = xmalloc (sizeof(*s->sort_info));
181 s->sort_info->max_entries = 1000;
182 s->sort_info->entries =
183 xmalloc (sizeof(*s->sort_info->entries) *
184 s->sort_info->max_entries);
185 s->sort_info->all_entries =
186 xmalloc (sizeof(*s->sort_info->all_entries) *
187 s->sort_info->max_entries);
188 for (i = 0; i < s->sort_info->max_entries; i++)
189 s->sort_info->entries[i] = s->sort_info->all_entries + i;
196 ZebraSet resultSetGet (ZebraHandle zh, const char *name)
200 for (s = zh->sets; s; s = s->next)
201 if (!strcmp (s->name, name))
203 if (!s->rset && s->rpn)
205 NMEM nmem = nmem_create ();
207 rpn_search (zh, nmem, s->rpn, s->num_bases,
208 s->basenames, s->name, s);
218 void resultSetDestroy (ZebraHandle zh)
222 for (s = zh->sets; s; s = s1)
226 xfree (s->sort_info->all_entries);
227 xfree (s->sort_info->entries);
228 xfree (s->sort_info);
231 nmem_destroy (s->nmem);
232 rset_delete (s->rset);
239 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
240 int num, int *positions)
246 struct zset_sort_info *sort_info;
248 if (!(sset = resultSetGet (zh, name)))
250 if (!(rset = sset->rset))
252 sr = xmalloc (sizeof(*sr) * num);
253 for (i = 0; i<num; i++)
258 sort_info = sset->sort_info;
263 for (i = 0; i<num; i++)
265 position = positions[i];
266 if (position > 0 && position <= sort_info->num_entries)
268 logf (LOG_DEBUG, "got pos=%d (sorted)", position);
269 sr[i].sysno = sort_info->entries[position-1]->sysno;
270 sr[i].score = sort_info->entries[position-1]->score;
274 /* did we really get all entries using sort ? */
275 for (i = 0; i<num; i++)
280 if (i < num) /* nope, get the rest, unsorted - sorry */
290 position = sort_info->num_entries;
291 while (num_i < num && positions[num_i] < position)
293 rfd = rset_open (rset, RSETF_READ);
294 while (num_i < num && rset_read (rset, rfd, &key, &term_index))
296 if (key.sysno != psysno)
301 /* determine we alreay have this in our set */
302 for (i = sort_info->num_entries; --i >= 0; )
303 if (psysno == sort_info->entries[i]->sysno)
309 assert (num_i < num);
310 if (position == positions[num_i])
312 sr[num_i].sysno = psysno;
313 logf (LOG_DEBUG, "got pos=%d (unsorted)", position);
314 sr[num_i].score = -1;
319 rset_close (rset, rfd);
324 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num)
334 void resultSetInsertSort (ZebraHandle zh, ZebraSet sset,
335 struct sortKey *criteria, int num_criteria,
338 struct zset_sort_entry this_entry;
339 struct zset_sort_entry *new_entry = NULL;
340 struct zset_sort_info *sort_info = sset->sort_info;
343 sortIdx_sysno (zh->sortIdx, sysno);
344 for (i = 0; i<num_criteria; i++)
346 sortIdx_type (zh->sortIdx, criteria[i].attrUse);
347 sortIdx_read (zh->sortIdx, this_entry.buf[i]);
349 i = sort_info->num_entries;
353 for (j = 0; j<num_criteria; j++)
355 rel = memcmp (this_entry.buf[j], sort_info->entries[i]->buf[j],
362 if (criteria[j].relation == 'A')
367 else if (criteria[j].relation == 'D')
373 j = sort_info->max_entries-1;
377 new_entry = sort_info->entries[j];
380 sort_info->entries[j] = sort_info->entries[j-1];
383 sort_info->entries[j] = new_entry;
385 if (sort_info->num_entries != sort_info->max_entries)
386 (sort_info->num_entries)++;
387 for (i = 0; i<num_criteria; i++)
388 memcpy (new_entry->buf[i], this_entry.buf[i], SORT_IDX_ENTRYSIZE);
389 new_entry->sysno = sysno;
390 new_entry->score = -1;
393 void resultSetInsertRank (ZebraHandle zh, struct zset_sort_info *sort_info,
394 int sysno, int score, int relation)
396 struct zset_sort_entry *new_entry = NULL;
399 i = sort_info->num_entries;
404 rel = score - sort_info->entries[i]->score;
411 else if (relation == 'A')
417 j = sort_info->max_entries-1;
421 new_entry = sort_info->entries[j];
424 sort_info->entries[j] = sort_info->entries[j-1];
427 sort_info->entries[j] = new_entry;
429 if (sort_info->num_entries != sort_info->max_entries)
430 (sort_info->num_entries)++;
431 new_entry->sysno = sysno;
432 new_entry->score = score;
435 void resultSetSort (ZebraHandle zh, NMEM nmem,
436 int num_input_setnames, const char **input_setnames,
437 const char *output_setname,
438 Z_SortKeySpecList *sort_sequence, int *sort_status)
443 if (num_input_setnames == 0)
448 if (num_input_setnames > 1)
453 logf (LOG_DEBUG, "result set sort input=%s output=%s",
454 *input_setnames, output_setname);
455 sset = resultSetGet (zh, input_setnames[0]);
459 zh->errString = nmem_strdup (nmem, input_setnames[0]);
462 if (!(rset = sset->rset))
465 zh->errString = nmem_strdup (nmem, input_setnames[0]);
468 if (strcmp (output_setname, input_setnames[0]))
470 rset = rset_dup (rset);
471 sset = resultSetAdd (zh, output_setname, 1);
474 resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
477 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
478 ZebraSet sset, RSET rset,
479 Z_SortKeySpecList *sort_sequence, int *sort_status)
483 struct sortKey sort_criteria[3];
488 logf (LOG_DEBUG, "resultSetSortSingle start");
489 sset->sort_info->num_entries = 0;
492 num_criteria = sort_sequence->num_specs;
493 if (num_criteria > 3)
495 for (i = 0; i < num_criteria; i++)
497 Z_SortKeySpec *sks = sort_sequence->specs[i];
500 if (*sks->sortRelation == Z_SortRelation_ascending)
501 sort_criteria[i].relation = 'A';
502 else if (*sks->sortRelation == Z_SortRelation_descending)
503 sort_criteria[i].relation = 'D';
509 if (sks->sortElement->which == Z_SortElement_databaseSpecific)
514 else if (sks->sortElement->which != Z_SortElement_generic)
519 sk = sks->sortElement->u.generic;
522 case Z_SortKey_sortField:
523 logf (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
526 case Z_SortKey_elementSpec:
527 logf (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
530 case Z_SortKey_sortAttributes:
531 logf (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
532 sort_criteria[i].attrUse =
533 zebra_maps_sort (zh->zebra_maps, sk->u.sortAttributes);
534 logf (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
535 if (sort_criteria[i].attrUse == -1)
540 if (sortIdx_type (zh->sortIdx, sort_criteria[i].attrUse))
548 rfd = rset_open (rset, RSETF_READ);
549 while (rset_read (rset, rfd, &key, &term_index))
551 if (key.sysno != psysno)
555 resultSetInsertSort (zh, sset,
556 sort_criteria, num_criteria, psysno);
559 rset_close (rset, rfd);
562 *sort_status = Z_SortStatus_success;
563 logf (LOG_DEBUG, "resultSetSortSingle end");
566 RSET resultSetRef (ZebraHandle zh, Z_ResultSetId *resultSetId)
570 if ((s = resultSetGet (zh, resultSetId)))
575 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
581 ZebraRankClass rank_class;
582 struct rank_control *rc;
583 struct zset_sort_info *sort_info;
585 sort_info = zebraSet->sort_info;
586 sort_info->num_entries = 0;
588 rfd = rset_open (rset, RSETF_READ);
590 logf (LOG_DEBUG, "resultSetRank");
591 for (i = 0; i < rset->no_rset_terms; i++)
592 logf (LOG_DEBUG, "term=\"%s\" cnt=%d type=%s",
593 rset->rset_terms[i]->name,
594 rset->rset_terms[i]->nn,
595 rset->rset_terms[i]->flags);
597 rank_class = zebraRankLookup (zh, "rank-1");
598 rc = rank_class->control;
600 if (rset_read (rset, rfd, &key, &term_index))
602 int psysno = key.sysno;
605 (*rc->begin) (zh, rank_class->class_handle, rset);
610 if (key.sysno != psysno)
612 score = (*rc->calc) (handle, psysno);
614 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
618 (*rc->add) (handle, key.seqno, term_index);
620 while (rset_read (rset, rfd, &key, &term_index));
621 score = (*rc->calc) (handle, psysno);
622 resultSetInsertRank (zh, sort_info, psysno, score, 'A');
623 (*rc->end) (zh, handle);
625 rset_close (rset, rfd);
626 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, zebraSet->hits);
629 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
631 ZebraRankClass p = zh->rank_classes;
632 while (p && strcmp (p->control->name, name))
634 if (p && !p->init_flag)
636 if (p->control->create)
637 p->class_handle = (*p->control->create)(zh);
643 void zebraRankInstall (ZebraHandle zh, struct rank_control *ctrl)
645 ZebraRankClass p = xmalloc (sizeof(*p));
646 p->control = xmalloc (sizeof(*p->control));
647 memcpy (p->control, ctrl, sizeof(*p->control));
648 p->control->name = xstrdup (ctrl->name);
650 p->next = zh->rank_classes;
651 zh->rank_classes = p;
654 void zebraRankDestroy (ZebraHandle zh)
656 ZebraRankClass p = zh->rank_classes;
659 ZebraRankClass p_next = p->next;
660 if (p->init_flag && p->control->destroy)
661 (*p->control->destroy)(zh, p->class_handle);
662 xfree (p->control->name);
667 zh->rank_classes = NULL;