+ ++i;
+ j = sort_info->max_entries;
+ if (i == j)
+ return;
+
+ if (sort_info->num_entries == j)
+ --j;
+ else
+ j = (sort_info->num_entries)++;
+
+ new_entry = sort_info->entries[j];
+ while (j != i)
+ {
+ sort_info->entries[j] = sort_info->entries[j-1];
+ --j;
+ }
+ sort_info->entries[i] = new_entry;
+ assert (new_entry);
+ new_entry->sysno = sysno;
+ new_entry->score = score;
+}
+
+void resultSetSort (ZebraHandle zh, NMEM nmem,
+ int num_input_setnames, const char **input_setnames,
+ const char *output_setname,
+ Z_SortKeySpecList *sort_sequence, int *sort_status)
+{
+ ZebraSet sset;
+ RSET rset;
+
+ if (num_input_setnames == 0)
+ {
+ zh->errCode = 208;
+ return ;
+ }
+ if (num_input_setnames > 1)
+ {
+ zh->errCode = 230;
+ return;
+ }
+ yaz_log (LOG_DEBUG, "result set sort input=%s output=%s",
+ *input_setnames, output_setname);
+ sset = resultSetGet (zh, input_setnames[0]);
+ if (!sset)
+ {
+ zh->errCode = 30;
+ zh->errString = nmem_strdup (nmem, input_setnames[0]);
+ return;
+ }
+ if (!(rset = sset->rset))
+ {
+ zh->errCode = 30;
+ zh->errString = nmem_strdup (nmem, input_setnames[0]);
+ return;
+ }
+ if (strcmp (output_setname, input_setnames[0]))
+ {
+ rset = rset_dup (rset);
+ sset = resultSetAdd (zh, output_setname, 1);
+ sset->rset = rset;
+ }
+ resultSetSortSingle (zh, nmem, sset, rset, sort_sequence, sort_status);
+}
+
+void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
+ ZebraSet sset, RSET rset,
+ Z_SortKeySpecList *sort_sequence, int *sort_status)
+{
+ int i;
+ zint psysno = 0;
+ struct it_key key;
+ struct sortKeyInfo sort_criteria[3];
+ int num_criteria;
+ RSFD rfd;
+
+ yaz_log (LOG_LOG, "resultSetSortSingle start");
+ assert(nmem); /* compiler shut up about unused param */
+ sset->sort_info->num_entries = 0;
+
+ 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))
+ {
+#if IT_KEY_NEW
+ zint this_sys = key.mem[0];
+#else
+ zint this_sys = key.sysno;
+#endif
+ 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;