+ else
+ {
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRW_UNSUPP_QUERY_TYPE, 0);
+ }
+ if (rr.query->u.type_1)
+ {
+ rr.stream = assoc->encode;
+ rr.decode = assoc->decode;
+ rr.print = assoc->print;
+ if (srw_req->sort.sortKeys)
+ rr.srw_sortKeys = odr_strdup(assoc->encode,
+ srw_req->sort.sortKeys);
+ rr.association = assoc;
+ rr.hits = 0;
+ rr.errcode = 0;
+ rr.errstring = 0;
+ rr.search_info = 0;
+ rr.search_input = 0;
+
+ if (srw_req->facetList)
+ yaz_oi_set_facetlist(&rr.search_input, assoc->encode,
+ srw_req->facetList);
+
+ yaz_log_zquery_level(log_requestdetail,rr.query);
+
+ (assoc->init->bend_search)(assoc->backend, &rr);
+ if (rr.errcode)
+ {
+ if (rr.errcode == YAZ_BIB1_DATABASE_UNAVAILABLE)
+ {
+ *http_code = 404;
+ }
+ else
+ {
+ srw_error = yaz_diag_bib1_to_srw(rr.errcode);
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, rr.errstring);
+ }
+ }
+ else
+ {
+ int number = srw_req->maximumRecords ?
+ odr_int_to_int(*srw_req->maximumRecords) : 0;
+ int start = srw_req->startRecord ?
+ odr_int_to_int(*srw_req->startRecord) : 1;
+
+ yaz_log(log_requestdetail, "Request to pack %d+%d out of "
+ ODR_INT_PRINTF,
+ start, number, rr.hits);
+
+ srw_res->numberOfRecords = odr_intdup(assoc->encode, rr.hits);
+ if (rr.srw_setname)
+ {
+ srw_res->resultSetId =
+ odr_strdup(assoc->encode, rr.srw_setname );
+ srw_res->resultSetIdleTime =
+ odr_intdup(assoc->encode, *rr.srw_setnameIdleTime );
+ }
+
+ srw_res->facetList = yaz_oi_get_facetlist(&rr.search_info);
+ if (start > rr.hits || start < 1)
+ {
+ /* if hits<=0 and start=1 we don't return a diagnostic */
+ if (start != 1)
+ yaz_add_srw_diagnostic(
+ assoc->encode,
+ &srw_res->diagnostics, &srw_res->num_diagnostics,
+ YAZ_SRW_FIRST_RECORD_POSITION_OUT_OF_RANGE, 0);
+ }
+ else if (number > 0)
+ {
+ int i;
+ int ok = 1;
+ if (start + number > rr.hits)
+ number = odr_int_to_int(rr.hits) - start + 1;
+
+ /* Call bend_present if defined */
+ if (assoc->init->bend_present)
+ {
+ bend_present_rr *bprr = (bend_present_rr*)
+ odr_malloc(assoc->decode, sizeof(*bprr));
+ bprr->setname = "default";
+ bprr->start = start;
+ bprr->number = number;
+ if (srw_req->recordSchema)
+ {
+ bprr->comp = (Z_RecordComposition *) odr_malloc(assoc->decode,
+ sizeof(*bprr->comp));
+ bprr->comp->which = Z_RecordComp_simple;
+ bprr->comp->u.simple = (Z_ElementSetNames *)
+ odr_malloc(assoc->decode, sizeof(Z_ElementSetNames));
+ bprr->comp->u.simple->which = Z_ElementSetNames_generic;
+ bprr->comp->u.simple->u.generic = srw_req->recordSchema;
+ }
+ else
+ {
+ bprr->comp = 0;
+ }
+ bprr->stream = assoc->encode;
+ bprr->referenceId = 0;
+ bprr->print = assoc->print;
+ bprr->association = assoc;
+ bprr->errcode = 0;
+ bprr->errstring = NULL;
+ (*assoc->init->bend_present)(assoc->backend, bprr);
+
+ if (bprr->errcode)
+ {
+ srw_error = yaz_diag_bib1_to_srw(bprr->errcode);
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, bprr->errstring);
+ ok = 0;
+ }
+ }
+
+ if (ok)
+ {
+ int j = 0;
+ int packing = Z_SRW_recordPacking_string;
+ if (srw_req->recordPacking)
+ {
+ packing =
+ yaz_srw_str_to_pack(srw_req->recordPacking);
+ if (packing == -1)
+ packing = Z_SRW_recordPacking_string;
+ }
+ srw_res->records = (Z_SRW_record *)
+ odr_malloc(assoc->encode,
+ number * sizeof(*srw_res->records));
+
+ srw_res->extra_records = (Z_SRW_extra_record **)
+ odr_malloc(assoc->encode,
+ number*sizeof(*srw_res->extra_records));
+
+ for (i = 0; i<number; i++)
+ {
+ int errcode;
+ int last_in_set = 0;
+ const char *addinfo = 0;
+
+ srw_res->records[j].recordPacking = packing;
+ srw_res->records[j].recordData_buf = 0;
+ srw_res->extra_records[j] = 0;
+ yaz_log(YLOG_DEBUG, "srw_bend_fetch %d", i+start);
+ errcode = srw_bend_fetch(assoc, i+start, srw_req,
+ srw_res->records + j,
+ &addinfo, &last_in_set);
+ if (errcode)
+ {
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ yaz_diag_bib1_to_srw(errcode),
+ addinfo);
+
+ break;
+ }
+ if (srw_res->records[j].recordData_buf)
+ j++;
+ if (last_in_set)
+ break;
+ }
+ srw_res->num_records = j;
+ if (!j)
+ srw_res->records = 0;
+ }
+ }
+ if (rr.extra_response_data)
+ {
+ res->extraResponseData_buf = rr.extra_response_data;
+ res->extraResponseData_len = strlen(rr.extra_response_data);
+ }
+ if (strcmp(res->srw_version, "2.") > 0)
+ {
+ if (rr.estimated_hit_count)
+ srw_res->resultCountPrecision =
+ odr_strdup(assoc->encode, "estimate");
+ else if (rr.partial_resultset)
+ srw_res->resultCountPrecision =
+ odr_strdup(assoc->encode, "minimum");
+ else
+ srw_res->resultCountPrecision =
+ odr_strdup(assoc->encode, "exact");
+ }
+ else if (rr.estimated_hit_count || rr.partial_resultset)
+ {
+ yaz_add_srw_diagnostic(
+ assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRW_RESULT_SET_CREATED_WITH_VALID_PARTIAL_RESULTS_AVAILABLE,
+ 0);
+ }
+ }
+ }
+ }
+ if (log_request)
+ {
+ WRBUF wr = wrbuf_alloc();
+
+ wrbuf_printf(wr, "SRWSearch %s ", srw_req->database);
+ if (srw_res->num_diagnostics)
+ wrbuf_printf(wr, "ERROR %s", srw_res->diagnostics[0].uri);
+ else if (*http_code != 200)
+ wrbuf_printf(wr, "ERROR info:http/%d", *http_code);
+ else if (srw_res->numberOfRecords)
+ {
+ wrbuf_printf(wr, "OK " ODR_INT_PRINTF,
+ (srw_res->numberOfRecords ?
+ *srw_res->numberOfRecords : 0));
+ }
+ wrbuf_printf(wr, " %s " ODR_INT_PRINTF "+%d",
+ (srw_res->resultSetId ?
+ srw_res->resultSetId : "-"),
+ (srw_req->startRecord ? *srw_req->startRecord : 1),
+ srw_res->num_records);
+ yaz_log(log_request, "%s %s: %s", wrbuf_cstr(wr), srw_req->queryType,
+ srw_req->query);
+ wrbuf_destroy(wr);