+ sset->hits = 0;
+ num_criteria = sort_sequence->num_specs;
+ if (num_criteria > 3)
+ num_criteria = 3;
+ for (i = 0; i < num_criteria; i++)
+ {
+ Z_SortKeySpec *sks = sort_sequence->specs[i];
+ Z_SortKey *sk;
+
+ if (*sks->sortRelation == Z_SortKeySpec_ascending)
+ sort_criteria[i].relation = 'A';
+ else if (*sks->sortRelation == Z_SortKeySpec_descending)
+ sort_criteria[i].relation = 'D';
+ else
+ {
+ zh->errCode = 214;
+ return;
+ }
+ if (sks->sortElement->which == Z_SortElement_databaseSpecific)
+ {
+ zh->errCode = 210;
+ return;
+ }
+ else if (sks->sortElement->which != Z_SortElement_generic)
+ {
+ zh->errCode = 237;
+ return;
+ }
+ sk = sks->sortElement->u.generic;
+ switch (sk->which)
+ {
+ case Z_SortKey_sortField:
+ yaz_log (LOG_DEBUG, "Sort: key %d is of type sortField", i+1);
+ zh->errCode = 207;
+ return;
+ case Z_SortKey_elementSpec:
+ yaz_log (LOG_DEBUG, "Sort: key %d is of type elementSpec", i+1);
+ zh->errCode = 207;
+ return;
+ case Z_SortKey_sortAttributes:
+ yaz_log (LOG_DEBUG, "Sort: key %d is of type sortAttributes", i+1);
+ sort_criteria[i].attrUse =
+ zebra_maps_sort (zh->reg->zebra_maps,
+ sk->u.sortAttributes,
+ &sort_criteria[i].numerical);
+ yaz_log (LOG_DEBUG, "use value = %d", sort_criteria[i].attrUse);
+ if (sort_criteria[i].attrUse == -1)
+ {
+ zh->errCode = 116;
+ return;
+ }
+ if (sortIdx_type (zh->reg->sortIdx, sort_criteria[i].attrUse))
+ {
+ zh->errCode = 207;
+ return;
+ }
+ break;
+ }
+ }
+ rfd = rset_open (rset, RSETF_READ);
+ while (rset_read (rfd, &key,&termid))
+ /* FIXME - pass a TERMID *, and use it for something below !! */
+ {
+ zint this_sys = key.mem[0];
+ if (this_sys != psysno)
+ {
+ (sset->hits)++;
+ psysno = this_sys;
+ resultSetInsertSort (zh, sset,
+ sort_criteria, num_criteria, psysno);
+ }
+ }
+ rset_close (rfd);
+
+#if 0
+ for (i = 0; i < rset->no_rset_terms; i++)
+ yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT
+ " type=%s count=" ZINT_FORMAT,
+ rset->rset_terms[i]->name,
+ rset->rset_terms[i]->nn,
+ rset->rset_terms[i]->flags,
+ rset->rset_terms[i]->count);
+#endif
+ *sort_status = Z_SortResponse_success;
+ yaz_log (LOG_LOG, "resultSetSortSingle end");
+}
+
+RSET resultSetRef (ZebraHandle zh, const char *resultSetId)
+{
+ ZebraSet s;
+
+ if ((s = resultSetGet (zh, resultSetId)))
+ return s->rset;
+ return NULL;
+}
+
+void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset)
+{
+ zint kno = 0;
+ struct it_key key;
+ RSFD rfd;
+ TERMID termid;
+ int i;
+ ZebraRankClass rank_class;
+ struct rank_control *rc;
+ struct zset_sort_info *sort_info;
+ const char *rank_handler_name = res_get_def(zh->res, "rank", "rank-1");
+ double cur,tot;
+ zint est=-2; /* -2 not done, -1 can't do, >0 actual estimate*/
+ zint esthits;
+ double ratio;
+
+ sort_info = zebraSet->sort_info;
+ sort_info->num_entries = 0;
+ zebraSet->hits = 0;
+ rfd = rset_open (rset, RSETF_READ);
+
+ rank_class = zebraRankLookup (zh, rank_handler_name);
+ if (!rank_class)
+ {
+ yaz_log (LOG_WARN, "No such rank handler: %s", rank_handler_name);
+ return;
+ }
+ rc = rank_class->control;
+
+ if (rset_read (rfd, &key, &termid))
+ {
+ zint psysno = key.mem[0];
+ int score;
+ void *handle =
+ (*rc->begin) (zh->reg, rank_class->class_handle, rset);
+ (zebraSet->hits)++;
+ esthits=atoi(res_get_def(zh->res,"estimatehits","0"));
+ if (!esthits)
+ est=-1; /* can not do */
+ do
+ {
+ zint this_sys = key.mem[0];
+ kno++;
+ if (this_sys != psysno)
+ {
+ score = (*rc->calc) (handle, psysno);
+
+ resultSetInsertRank (zh, sort_info, psysno, score, 'A');
+ (zebraSet->hits)++;
+ psysno = this_sys;
+ }
+ /* FIXME - Ranking is broken, since rsets no longer have */
+ /* term lists! */
+ /* (*rc->add) (handle, this_sys, term_index); */
+
+ if ( (est==-2) && (zebraSet->hits==esthits))
+ { /* time to estimate the hits */
+ rset_pos(rfd,&cur,&tot);
+ if (tot>0) {
+ ratio=cur/tot;
+ est=(zint)(0.5+zebraSet->hits/ratio);
+ logf(LOG_LOG, "Estimating hits (%s) "
+ "%0.1f->"ZINT_FORMAT
+ "; %0.1f->"ZINT_FORMAT,
+ rset->control->desc,
+ cur, zebraSet->hits,
+ tot,est);
+ i=0; /* round to 3 significant digits */
+ while (est>1000) {
+ est/=10;
+ i++;
+ }
+ while (i--) est*=10;
+ zebraSet->hits=est;
+ }
+ }
+ }
+ while (rset_read (rfd, &key,&termid) && (est<0) );
+ score = (*rc->calc) (handle, psysno);
+ resultSetInsertRank (zh, sort_info, psysno, score, 'A');
+ (*rc->end) (zh->reg, handle);
+ }
+ rset_close (rfd);
+/*
+ for (i = 0; i < rset->no_rset_terms; i++)
+ {
+ if (est>0)
+ rset->rset_terms[i]->count =
+ est=(zint)(rset->rset_terms[i]->count/ratio);
+ yaz_log (LOG_LOG, "term=\"%s\" nn=" ZINT_FORMAT
+ " type=%s count=" ZINT_FORMAT,
+ rset->rset_terms[i]->name,
+ rset->rset_terms[i]->nn,
+ rset->rset_terms[i]->flags,
+ rset->rset_terms[i]->count);
+ }
+*/
+ yaz_log (LOG_DEBUG, ZINT_FORMAT " keys, "ZINT_FORMAT" distinct sysnos",
+ kno, zebraSet->hits);
+}
+
+ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name)
+{
+ ZebraRankClass p = zh->reg->rank_classes;
+ while (p && strcmp (p->control->name, name))
+ p = p->next;
+ if (p && !p->init_flag)
+ {
+ if (p->control->create)
+ p->class_handle = (*p->control->create)(zh);
+ p->init_flag = 1;
+ }
+ return p;
+}
+
+void zebraRankInstall (struct zebra_register *reg, struct rank_control *ctrl)
+{
+ ZebraRankClass p = (ZebraRankClass) xmalloc (sizeof(*p));
+ p->control = (struct rank_control *) xmalloc (sizeof(*p->control));
+ memcpy (p->control, ctrl, sizeof(*p->control));
+ p->control->name = xstrdup (ctrl->name);
+ p->init_flag = 0;
+ p->next = reg->rank_classes;
+ reg->rank_classes = p;
+}
+
+void zebraRankDestroy (struct zebra_register *reg)
+{
+ ZebraRankClass p = reg->rank_classes;
+ while (p)
+ {
+ ZebraRankClass p_next = p->next;
+ if (p->init_flag && p->control->destroy)
+ (*p->control->destroy)(reg, p->class_handle);
+ xfree (p->control->name);
+ xfree (p->control);
+ xfree (p);
+ p = p_next;
+ }
+ reg->rank_classes = NULL;