- // now make a best guess for the database in which we think the record is located
- // if its not in our best guess, try the other databases one by one, till we had them all
- scan_db=*cached_success_db;
- scan_count=0;
-
- while(1){
- scan_count++;
- if(scan_count>numbases){
- // well...we scanned all databases and still nothing...give up
- yaz_log(log_level_sort, "zebra_sort_read failed (record not found in indices)");
- break;
- }
-
- // the criteria[i].ord is the file id of the sort index
- yaz_log(log_level_sort, "pre zebra_sort_type ord is %d", criteria[i].ord[scan_db]);
- zebra_sort_type(zh->reg->sort_index, criteria[i].ord[scan_db]);
- if(zebra_sort_read(zh->reg->sort_index, this_entry_buf)){
- // allright, found it
- // cache this db so we start trying from this db for next record
- *cached_success_db=scan_db;
- break;
- }else{
- yaz_log(log_level_sort, "record not found in database, trying next one");
- scan_db++;
- if(scan_db>=numbases){
- scan_db=0;
+ yaz_log(log_level_sort, "pre zebra_sort_type ord is %d",
+ criteria[i].ord[database_no]);
+ zebra_sort_type(zh->reg->sort_index, criteria[i].ord[database_no]);
+ wrbuf_rewind(w);
+ if (zebra_sort_read(zh->reg->sort_index, 0, w))
+ {
+ /* consider each sort entry and take lowest/highest one
+ of the one as sorting key depending on whether sort is
+ ascending/descending */
+ int off = 0;
+ while (off != wrbuf_len(w))
+ {
+ size_t l = strlen(wrbuf_buf(w)+off);
+ assert(off < wrbuf_len(w));
+
+ if (l >= SORT_IDX_ENTRYSIZE)
+ l = SORT_IDX_ENTRYSIZE-1;
+ if ( (off == 0)
+ || (criteria[i].relation == 'A'
+ && strcmp(wrbuf_buf(w)+off, this_entry_buf) < 0)
+ || (criteria[i].relation == 'D'
+ && strcmp(wrbuf_buf(w)+off, this_entry_buf) > 0)
+ )
+ {
+ memcpy(this_entry_buf, wrbuf_buf(w)+off, l);
+ this_entry_buf[l] = '\0';