Updated.
[yaz-moved-to-github.git] / server / seshigh.c
index 94c4d45..96058d5 100644 (file)
@@ -1,10 +1,27 @@
 /*
- * Copyright (c) 1995, Index Data
+ * Copyright (c) 1995-1998, Index Data
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: seshigh.c,v $
- * Revision 1.65  1997-09-01 08:53:01  adam
+ * Revision 1.70  1998-01-29 13:15:35  adam
+ * Implemented sort for the backend interface.
+ *
+ * Revision 1.69  1997/09/30 11:48:12  adam
+ * Fixed bug introduced by previous commit.
+ *
+ * Revision 1.68  1997/09/29 13:18:59  adam
+ * Added function, oid_ent_to_oid, to replace the function
+ * oid_getoidbyent, which is not thread safe.
+ *
+ * Revision 1.67  1997/09/17 12:10:40  adam
+ * YAZ version 1.4.
+ *
+ * Revision 1.66  1997/09/05 15:26:44  adam
+ * Added ODR encode in search and scen bend request structures.
+ * Fixed a few enums that caused trouble with C++.
+ *
+ * Revision 1.65  1997/09/01 08:53:01  adam
  * New windows NT/95 port using MSV5.0. The test server 'ztest' was
  * moved a separate directory. MSV5.0 project server.dsp created.
  * As an option, the server can now operate as an NT service.
@@ -263,6 +280,7 @@ static Z_APDU *response_searchRequest(association *assoc, request *reqb,
 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
     int *fd);
 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
+static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
 static void process_close(association *assoc, request *reqb);
 
 static FILE *apduf = 0; /* for use in static mode */
@@ -532,6 +550,15 @@ static int process_request(association *assoc)
            res = process_presentRequest(assoc, req, &fd); break;
        case Z_APDU_scanRequest:
            res = process_scanRequest(assoc, req, &fd); break;
+       case Z_APDU_sortRequest:
+           if (assoc->bend_sort)
+               res = process_sortRequest(assoc, req, &fd);
+           else
+           {
+               logf(LOG_WARN, "Cannot handle SORT APDU");
+               return -1;
+           }
+           break;
        case Z_APDU_close:
            process_close(assoc, req); return 0;
        default:
@@ -666,8 +693,10 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
     if (req->implementationVersion)
        logf(LOG_LOG, "Version:   %s", req->implementationVersion);
 
+    binitreq.stream = assoc->encode;
     binitreq.configname = "default-config";
     binitreq.auth = req->idAuthentication;
+    binitreq.bend_sort = NULL;
     if (!(binitres = bend_init(&binitreq)))
     {
        logf(LOG_WARN, "Bad response from backend.");
@@ -675,6 +704,8 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
     }
 
     assoc->backend = binitres->handle;
+    if ((assoc->bend_sort = binitreq.bend_sort))
+       logf (LOG_DEBUG, "Sort handler installed");
     resp->referenceId = req->referenceId;
     *options = '\0';
     /* let's tell the client what we can do */
@@ -710,7 +741,11 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
        ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
        strcat(options, " concurop");
     }
-
+    if (ODR_MASK_GET(req->options, Z_Options_sort && binitreq.bend_sort))
+    {
+       ODR_MASK_SET(resp->options, Z_Options_sort);
+       strcat(options, " sort");
+    }
     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
     {
        ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
@@ -756,6 +791,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
  */
 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
 {
+    int oid[OID_SIZE];
     Z_Records *rec = odr_malloc (assoc->encode, sizeof(*rec));
     oident bib1;
     int *err = odr_malloc (assoc->encode, sizeof(*err));
@@ -777,7 +813,8 @@ static Z_Records *diagrec(association *assoc, int error, char *addinfo)
 #else
     rec->u.nonSurrogateDiagnostic = dr;
 #endif
-    dr->diagnosticSetId = oid_getoidbyent(&bib1);
+    dr->diagnosticSetId = odr_oiddup (assoc->encode,
+                                      oid_ent_to_oid(&bib1, oid));
     dr->condition = err;
     dr->which = Z_DiagForm_v2AddInfo;
     dr->addinfo = addinfo ? addinfo : "";
@@ -790,6 +827,7 @@ static Z_Records *diagrec(association *assoc, int error, char *addinfo)
 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
                                            int error, char *addinfo)
 {
+    int oid[OID_SIZE];
     Z_NamePlusRecord *rec = odr_malloc (assoc->encode, sizeof(*rec));
     int *err = odr_malloc (assoc->encode, sizeof(*err));
     oident bib1;
@@ -807,7 +845,8 @@ static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
     rec->u.surrogateDiagnostic = drec;
     drec->which = Z_DiagRec_defaultFormat;
     drec->u.defaultFormat = dr;
-    dr->diagnosticSetId = oid_getoidbyent(&bib1);
+    dr->diagnosticSetId = odr_oiddup (assoc->encode,
+                                      oid_ent_to_oid(&bib1, oid));
     dr->condition = err;
     dr->which = Z_DiagForm_v2AddInfo;
     dr->addinfo = addinfo ? addinfo : "";
@@ -819,6 +858,7 @@ static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
  */
 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
 {
+    int oid[OID_SIZE];
     Z_DiagRecs *recs = odr_malloc (assoc->encode, sizeof(*recs));
     int *err = odr_malloc (assoc->encode, sizeof(*err));
     oident bib1;
@@ -838,7 +878,8 @@ static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
     drec->which = Z_DiagRec_defaultFormat;
     drec->u.defaultFormat = rec;
 
-    rec->diagnosticSetId = oid_getoidbyent(&bib1);
+    rec->diagnosticSetId = odr_oiddup (assoc->encode,
+                                      oid_ent_to_oid(&bib1, oid));
     rec->condition = err;
     rec->which = Z_DiagForm_v2AddInfo;
     rec->addinfo = addinfo ? addinfo : "";
@@ -849,6 +890,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
                                int *num, Z_RecordComposition *comp,
                                int *next, int *pres, oid_value format)
 {
+    int oid[OID_SIZE];
     int recno, total_length = 0, toget = *num, dumped_records = 0;
     Z_Records *records = odr_malloc (a->encode, sizeof(*records));
     Z_NamePlusRecordList *reclist = odr_malloc (a->encode, sizeof(*reclist));
@@ -952,7 +994,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
        recform.oclass = CLASS_RECSYN;
        recform.value = fres->format;
        thisext->direct_reference = odr_oiddup(a->encode,
-           oid_getoidbyent(&recform));
+                                              oid_ent_to_oid(&recform, oid));
        thisext->indirect_reference = 0;
        thisext->descriptor = 0;
        if (fres->len < 0) /* Structured data */
@@ -1024,6 +1066,7 @@ static Z_APDU *process_searchRequest(association *assoc, request *reqb,
     bsrq.num_bases = req->num_databaseNames;
     bsrq.basenames = req->databaseNames;
     bsrq.query = req->query;
+    bsrq.stream = assoc->encode;
 
     if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
        return 0;
@@ -1213,7 +1256,7 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
     bend_scanresult *srs;
     oident *attset;
 
-    logf(LOG_LOG, "Got scanrequest");
+    logf(LOG_LOG, "Got ScanRequest");
     *scanStatus = Z_Scan_failure;
     *numberOfEntriesReturned = 0;
 
@@ -1244,6 +1287,7 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
        srq.basenames = req->databaseNames;
        srq.num_entries = *req->numberOfTermsRequested;
        srq.term = req->termListAndStartPoint;
+       srq.stream = assoc->encode;
        if (!(attset = oid_getentbyoid(req->attributeSet)) ||
            attset->oclass != CLASS_RECSYN)
            srq.attributeset = VAL_NONE;
@@ -1304,6 +1348,43 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
     return apdu;
 }
 
+static Z_APDU *process_sortRequest(association *assoc, request *reqb,
+    int *fd)
+{
+    Z_SortRequest *req = reqb->request->u.sortRequest;
+    Z_SortResponse *res = odr_malloc (assoc->encode, sizeof(*res));
+    bend_sortrequest bsrq;
+    bend_sortresult *bsrt;
+    Z_APDU *apdu = odr_malloc (assoc->encode, sizeof(*apdu));
+
+    logf(LOG_LOG, "Got SortRequest.");
+
+    bsrq.num_input_setnames = req->inputResultSetNames->num_strings;
+    bsrq.input_setnames = req->inputResultSetNames->strings;
+    bsrq.output_setname = req->sortedResultSetName;
+    bsrq.sort_sequence = req->sortSequence;
+    bsrq.stream = assoc->encode;
+
+    bsrt = odr_malloc (assoc->encode, sizeof(*bsrt));
+    bsrt->sort_status = Z_SortStatus_failure;
+    bsrt->errcode = 0;
+    bsrt->errstring = 0;
+
+    (*assoc->bend_sort)(assoc->backend, &bsrq, bsrt);
+
+    res->referenceId = req->referenceId;
+    res->sortStatus = odr_malloc (assoc->encode, sizeof(*res->sortStatus));
+    *res->sortStatus = bsrt->sort_status;
+    res->resultSetStatus = 0;
+    if (bsrt->errcode)
+       res->diagnostics = diagrecs(assoc, bsrt->errcode, bsrt->errstring);
+    res->otherInfo = 0;
+
+    apdu->which = Z_APDU_sortResponse;
+    apdu->u.sortResponse = res;
+    return apdu;
+}
+
 static void process_close(association *assoc, request *reqb)
 {
     Z_Close *req = reqb->request->u.close;
@@ -1321,7 +1402,7 @@ static void process_close(association *assoc, request *reqb)
        "unspecified"
     };
 
-    logf(LOG_LOG, "Got close, reason %s, message %s",
+    logf(LOG_LOG, "Got Close, reason %s, message %s",
        reasons[*req->closeReason], req->diagnosticInformation ?
        req->diagnosticInformation : "NULL");
     if (assoc->version < 3) /* to make do_force respond with close */