+ }
+ }
+}
+
+static void srw_bend_scan(association *assoc, request *req,
+ Z_SRW_scanRequest *srw_req,
+ Z_SRW_scanResponse *srw_res,
+ int *http_code)
+{
+ yaz_log(log_requestdetail, "Got SRW ScanRequest");
+
+ *http_code = 200;
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
+ if (srw_res->num_diagnostics == 0 && assoc->init)
+ {
+ struct scan_entry *save_entries;
+
+ bend_scan_rr *bsrr = (bend_scan_rr *)
+ odr_malloc (assoc->encode, sizeof(*bsrr));
+ bsrr->num_bases = 1;
+ bsrr->basenames = &srw_req->database;
+
+ bsrr->num_entries = srw_req->maximumTerms ?
+ *srw_req->maximumTerms : 10;
+ bsrr->term_position = srw_req->responsePosition ?
+ *srw_req->responsePosition : 1;
+
+ bsrr->errcode = 0;
+ bsrr->errstring = 0;
+ bsrr->referenceId = 0;
+ bsrr->stream = assoc->encode;
+ bsrr->print = assoc->print;
+ bsrr->step_size = odr_intdup(assoc->decode, 0);
+ bsrr->entries = 0;
+
+ if (bsrr->num_entries > 0)
+ {
+ int i;
+ bsrr->entries = odr_malloc(assoc->decode, sizeof(*bsrr->entries) *
+ bsrr->num_entries);
+ for (i = 0; i<bsrr->num_entries; i++)
+ {
+ bsrr->entries[i].term = 0;
+ bsrr->entries[i].occurrences = 0;
+ bsrr->entries[i].errcode = 0;
+ bsrr->entries[i].errstring = 0;
+ bsrr->entries[i].display_term = 0;
+ }
+ }
+ save_entries = bsrr->entries; /* save it so we can compare later */
+
+ if (srw_req->query_type == Z_SRW_query_type_pqf &&
+ assoc->init->bend_scan)
+ {
+ Odr_oid *scan_attributeSet = 0;
+ oident *attset;
+ YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
+
+ bsrr->term = yaz_pqf_scan(pqf_parser, assoc->decode,
+ &scan_attributeSet,
+ srw_req->scanClause.pqf);
+ if (scan_attributeSet &&
+ (attset = oid_getentbyoid(scan_attributeSet)) &&
+ (attset->oclass == CLASS_ATTSET ||
+ attset->oclass == CLASS_GENERAL))
+ bsrr->attributeset = attset->value;
+ else
+ bsrr->attributeset = VAL_NONE;
+ yaz_pqf_destroy(pqf_parser);
+ bsrr->scanClause = 0;
+ ((int (*)(void *, bend_scan_rr *))
+ (*assoc->init->bend_scan))(assoc->backend, bsrr);
+ }
+ else if (srw_req->query_type == Z_SRW_query_type_cql
+ && assoc->init->bend_srw_scan)
+ {
+ if (assoc->cql_transform)
+ {
+ int srw_error;
+ bsrr->scanClause = 0;
+ bsrr->attributeset = VAL_NONE;
+ bsrr->term = odr_malloc(assoc->decode, sizeof(*bsrr->term));
+ srw_error = cql2pqf_scan(assoc->encode,
+ srw_req->scanClause.cql,
+ assoc->cql_transform,
+ bsrr->term);
+ if (srw_error)
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, 0);
+ else
+ {
+ ((int (*)(void *, bend_scan_rr *))
+ (*assoc->init->bend_scan))(assoc->backend, bsrr);
+ }
+ }
+ else
+ {
+ bsrr->term = 0;
+ bsrr->attributeset = VAL_NONE;
+ bsrr->scanClause = srw_req->scanClause.cql;
+ ((int (*)(void *, bend_scan_rr *))
+ (*assoc->init->bend_srw_scan))(assoc->backend, bsrr);
+ }
+ }
+ else
+ {
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics, 4, "scan");
+ }
+ if (bsrr->errcode)
+ {
+ int srw_error;
+ if (bsrr->errcode == 109) /* database unavailable */
+ {
+ *http_code = 404;
+ return;
+ }
+ srw_error = yaz_diag_bib1_to_srw (bsrr->errcode);
+
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, bsrr->errstring);
+ }
+ else if (srw_res->num_diagnostics == 0 && bsrr->num_entries)
+ {
+ int i;
+ srw_res->terms = (Z_SRW_scanTerm*)
+ odr_malloc(assoc->encode, sizeof(*srw_res->terms) *
+ bsrr->num_entries);
+
+ srw_res->num_terms = bsrr->num_entries;
+ for (i = 0; i<bsrr->num_entries; i++)
+ {
+ Z_SRW_scanTerm *t = srw_res->terms + i;
+ t->value = odr_strdup(assoc->encode, bsrr->entries[i].term);
+ t->numberOfRecords =
+ odr_intdup(assoc->encode, bsrr->entries[i].occurrences);
+ t->displayTerm = 0;
+ if (save_entries == bsrr->entries &&
+ bsrr->entries[i].display_term)
+ {
+ /* the entries was _not_ set by the handler. So it's
+ safe to test for new member display_term. It is
+ NULL'ed by us.
+ */
+ t->displayTerm = odr_strdup(assoc->encode,
+ bsrr->entries[i].display_term);
+ }
+ t->whereInList = 0;
+ }
+ }
+ }
+ if (log_request)
+ {
+ WRBUF wr = wrbuf_alloc();
+ const char *querytype = 0;
+ const char *querystr = 0;
+
+ switch(srw_req->query_type)
+ {
+ case Z_SRW_query_type_pqf:
+ querytype = "PQF";
+ querystr = srw_req->scanClause.pqf;
+ break;
+ case Z_SRW_query_type_cql:
+ querytype = "CQL";
+ querystr = srw_req->scanClause.cql;
+ break;
+ default:
+ querytype = "Unknown";
+ querystr = "";
+ }
+ wrbuf_printf(wr, "SRWScan %d+%d",
+ (srw_req->responsePosition ?
+ *srw_req->responsePosition : 1),
+ (srw_req->maximumTerms ?
+ *srw_req->maximumTerms : 1));
+ if (srw_res->num_diagnostics)
+ wrbuf_printf(wr, " ERROR %s", srw_res->diagnostics[0].uri);
+ else
+ wrbuf_printf(wr, " OK -");
+ wrbuf_printf(wr, " %s: %s", querytype, querystr);
+ yaz_log(log_request, "%s", wrbuf_buf(wr) );
+ wrbuf_free(wr, 1);