Update for YAZ 3s libyaz_server.la
[simpleserver-moved-to-github.git] / SimpleServer.xs
index d8675e7..7146877 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: SimpleServer.xs,v 1.54 2006-12-01 10:44:58 sondberg Exp $ 
+ * $Id: SimpleServer.xs,v 1.60 2007-04-17 20:26:58 adam Exp $ 
  * ----------------------------------------------------------------------
  * 
  * Copyright (c) 2000-2004, Index Data.
@@ -37,6 +37,7 @@
 #include <yaz/wrbuf.h>
 #include <yaz/querytowrbuf.h>
 #include <stdio.h>
+#include <yaz/mutex.h>
 #ifdef WIN32
 #else
 #include <unistd.h>
@@ -51,7 +52,7 @@
 #define sv_undef PL_sv_undef
 #endif
 
-NMEM_MUTEX simpleserver_mutex;
+YAZ_MUTEX simpleserver_mutex;
 
 typedef struct {
        SV *handle;
@@ -85,7 +86,7 @@ SV *explain_ref = NULL;
 PerlInterpreter *root_perl_context;
 int MAX_OID = 15;
 
-#define GRS_BUF_SIZE 512
+#define GRS_BUF_SIZE 8192
 
 CV * simpleserver_sv2cv(SV *handler) {
     STRLEN len;
@@ -129,7 +130,7 @@ void tst_clones(void)
 
 int simpleserver_clone(void) {
 #ifdef USE_ITHREADS
-     nmem_mutex_enter(simpleserver_mutex);
+     yaz_mutex_enter(simpleserver_mutex);
      if (1)
      {
          PerlInterpreter *current = PERL_GET_CONTEXT;
@@ -145,14 +146,14 @@ int simpleserver_clone(void) {
              PERL_SET_CONTEXT( perl_interp );
          }
      }
-     nmem_mutex_leave(simpleserver_mutex);
+     yaz_mutex_leave(simpleserver_mutex);
 #endif
      return 0;
 }
 
 
 void simpleserver_free(void) {
-    nmem_mutex_enter(simpleserver_mutex);
+    yaz_mutex_enter(simpleserver_mutex);
     if (1)
     {
         PerlInterpreter *current_interp = PERL_GET_CONTEXT;
@@ -167,7 +168,7 @@ void simpleserver_free(void) {
             perl_free(current_interp);
        }
     }
-    nmem_mutex_leave(simpleserver_mutex);
+    yaz_mutex_leave(simpleserver_mutex);
 }
 
 
@@ -195,7 +196,7 @@ Z_GenericRecord *read_grs1(char *str, ODR o)
                }
                len = ptr - str;
                if (len > GRS_BUF_SIZE) {
-                   yaz_log(LOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
+                   yaz_log(YLOG_WARN, "GRS string too long - truncating (%d > %d)", len, GRS_BUF_SIZE);
                    len = GRS_BUF_SIZE;
                }
                strncpy(line, str, len);
@@ -210,7 +211,7 @@ Z_GenericRecord *read_grs1(char *str, ODR o)
                }
                if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
                {
-                       yaz_log(LOG_WARN, "Bad data in '%s'", buf);
+                       yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
                        return r;
                }
                if (!type && *value == '0')
@@ -222,7 +223,7 @@ Z_GenericRecord *read_grs1(char *str, ODR o)
                        buf++;
                if (r->num_elements >= GRS_MAX_FIELDS)
                {
-                       yaz_log(LOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
+                       yaz_log(YLOG_WARN, "Max number of GRS-1 elements exceeded [GRS_MAX_FIELDS=%d]", GRS_MAX_FIELDS);
                        exit(0);
                }
                r->elements[r->num_elements] = t = (Z_TaggedElement *) odr_malloc(o, sizeof(Z_TaggedElement));
@@ -262,7 +263,6 @@ Z_GenericRecord *read_grs1(char *str, ODR o)
 
 
 
-
 static void oid2str(Odr_oid *o, WRBUF buf)
 {
     for (; *o >= 0; o++) {
@@ -274,6 +274,13 @@ static void oid2str(Odr_oid *o, WRBUF buf)
     }
 }
 
+WRBUF oid2dotted(int *oid)
+{
+    WRBUF buf = wrbuf_alloc();
+    oid2str(oid, buf);
+    return buf;
+}
+               
 
 WRBUF zquery2pquery(Z_Query *q)
 {
@@ -461,7 +468,7 @@ int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr)
     oid2str(sattr->id, attrset_wr);
     hv_store(sort_spec, "ATTRSET", 7,
              newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
-    wrbuf_free(attrset_wr, 1);
+    wrbuf_destroy(attrset_wr);
 
     hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
 
@@ -518,7 +525,7 @@ int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
                 oid2str(zspec->schema.oid, elementSpec);
                 hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
                          newSVpv(elementSpec->buf, elementSpec->pos), 0);
-                wrbuf_free(elementSpec, 1);
+                wrbuf_destroy(elementSpec);
             }
             else if (zspec->which == Z_Schema_uri)
             {
@@ -759,7 +766,7 @@ int bend_search(void *handle, bend_search_rr *rr)
        sv_free( (SV*) aref);
        sv_free( (SV*) href);
        if (query)
-           wrbuf_free(query, 1);
+           wrbuf_destroy(query);
        PUTBACK;
        FREETMPS;
        LEAVE;
@@ -767,71 +774,6 @@ int bend_search(void *handle, bend_search_rr *rr)
 }
 
 
-/* ### this is worryingly similar to oid2str() */
-WRBUF oid2dotted(int *oid)
-{
-
-       WRBUF buf = wrbuf_alloc();
-       int dot = 0;
-
-       for (; *oid != -1 ; oid++)
-       {
-               char ibuf[16];
-               if (dot)
-               {
-                       wrbuf_putc(buf, '.');
-               }
-               else
-               {
-                       dot = 1;
-               }
-               sprintf(ibuf, "%d", *oid);
-               wrbuf_puts(buf, ibuf);
-       }
-       return buf;
-}
-               
-
-int dotted2oid(char *dotted, int *buffer)
-{
-        int *oid;
-        char ibuf[16];
-        char *ptr;
-        int n = 0;
-
-        ptr = ibuf;
-        oid = buffer;
-        while (*dotted)
-        {
-                if (*dotted == '.')
-                {
-                        n++;
-                       if (n == MAX_OID)  /* Terminate if more than MAX_OID entries */
-                       {
-                               *oid = -1;
-                               return -1;
-                       }
-                        *ptr = 0;
-                        sscanf(ibuf, "%d", oid++);
-                        ptr = ibuf;
-                        dotted++;
-
-                }
-                else
-                {
-                        *ptr++ = *dotted++;
-                }
-        }
-        if (n < MAX_OID)
-       {
-               *ptr = 0;
-               sscanf(ibuf, "%d", oid++);
-       }
-        *oid = -1;
-       return 0;
-}
-
-
 int bend_fetch(void *handle, bend_fetch_rr *rr)
 {
        HV *href;
@@ -850,7 +792,6 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        char *ODR_basename;
        char *ODR_errstr;
        int *ODR_oid_buf;
-       oident *oid;
        WRBUF oid_dotted;
        Zfront_handle *zhandle = (Zfront_handle *)handle;
        CV* handler_cv = 0;
@@ -870,8 +811,8 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        if (rr->schema)
                hv_store(href, "SCHEMA", 6, newSVpv(rr->schema, 0), 0);
        temp = hv_store(href, "OFFSET", 6, newSViv(rr->number), 0);
-       if (rr->request_format_raw != 0) {
-           oid_dotted = oid2dotted(rr->request_format_raw);
+       if (rr->request_format != 0) {
+           oid_dotted = oid2dotted(rr->request_format);
        } else {
            /* Probably an SRU request: assume XML is required */
            oid_dotted = wrbuf_alloc();
@@ -985,16 +926,17 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        rr->basename = ODR_basename;
 
        ptr = SvPV(rep_form, length);
+
        ODR_oid_buf = (int *)odr_malloc(rr->stream, (MAX_OID + 1) * sizeof(int));
-       if (dotted2oid(ptr, ODR_oid_buf) == -1)         /* Maximum number of OID elements exceeded */
+       if (oid_dotstring_to_oid(ptr, ODR_oid_buf))
        {
                printf("Net::Z3950::SimpleServer: WARNING: OID structure too long, max length is %d\n", MAX_OID);
        }
-       rr->output_format_raw = ODR_oid_buf;    
+       rr->output_format = ODR_oid_buf;        
        
        ptr = SvPV(record, length);
-       oid = oid_getentbyoid(ODR_oid_buf);
-       if (oid->value == VAL_GRS1)             /* Treat GRS-1 records separately */
+        /* Treat GRS-1 records separately */
+       if (!oid_oidcmp(ODR_oid_buf, yaz_oid_recsyn_grs_1))
        {
                rr->record = (char *) read_grs1(ptr, rr->stream);
                rr->len = -1;
@@ -1020,7 +962,7 @@ int bend_fetch(void *handle, bend_fetch_rr *rr)
        }
        rr->surrogate_flag = SvIV(sur_flag);
 
-       wrbuf_free(oid_dotted, 1);
+       wrbuf_destroy(oid_dotted);
        sv_free((SV*) href);
        sv_free(basename);
        sv_free(record);
@@ -1615,7 +1557,7 @@ start_server(...)
                }
                *argv_buf = NULL;
                root_perl_context = PERL_GET_CONTEXT;
-               nmem_mutex_create(&simpleserver_mutex);
+               yaz_mutex_create(&simpleserver_mutex);
 #if 0
                /* only for debugging perl_clone .. */
                tst_clones();