Bump year. Change Aps->ApS
[idzebra-moved-to-github.git] / index / zebraapi.c
index 0bd24c2..72b4b69 100644 (file)
@@ -1,6 +1,6 @@
-/* $Id: zebraapi.c,v 1.141 2004-11-29 21:45:11 adam Exp $
-   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
-   Index Data Aps
+/* $Id: zebraapi.c,v 1.146 2005-01-15 19:38:28 adam Exp $
+   Copyright (C) 1995-2005
+   Index Data ApS
 
 This file is part of the Zebra server.
 
@@ -49,7 +49,6 @@ static int log_level_initialized=0;
 static Res zebra_open_res (ZebraHandle zh);
 static void zebra_close_res (ZebraHandle zh);
 
-
 static void zebra_chdir (ZebraService zs)
 {
     const char *dir ;
@@ -125,7 +124,6 @@ ZebraHandle zebra_open (ZebraService zs)
     zh->shadow_enable = 1;
 
     default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1");
-    zh->record_encoding = xstrdup (default_encoding);
 
     zh->iconv_to_utf8 =
         yaz_iconv_open ("UTF-8", default_encoding);
@@ -138,6 +136,8 @@ ZebraHandle zebra_open (ZebraService zs)
         yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported",
            default_encoding);
 
+    zh->record_encoding = 0;
+
     zebra_mutex_cond_lock (&zs->session_lock);
 
     zh->next = zs->sessions;
@@ -315,7 +315,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
 
     if (!(reg->records = rec_open (reg->bfs, rw, record_compression)))
     {
-       yaz_log (YLOG_WARN, "rec_open");
+       yaz_log (YLOG_WARN, "rec_open failed");
        return 0;
     }
     if (rw)
@@ -324,12 +324,12 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
     }
     if (!(reg->dict = dict_open_res (reg->bfs, FNAME_DICT, 40, rw, 0, res)))
     {
-       yaz_log (YLOG_WARN, "dict_open");
+       yaz_log (YLOG_WARN, "dict_open failed");
        return 0;
     }
     if (!(reg->sortIdx = sortIdx_open (reg->bfs, rw)))
     {
-       yaz_log (YLOG_WARN, "sortIdx_open");
+       yaz_log (YLOG_WARN, "sortIdx_open failed");
        return 0;
     }
     if (res_get_match (res, "isam", "s", ISAM_DEFAULT))
@@ -338,7 +338,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
        if (!(reg->isams = isams_open (reg->bfs, FNAME_ISAMS, rw,
                                      key_isams_m(res, &isams_m))))
        {
-           yaz_log (YLOG_WARN, "isams_open");
+           yaz_log (YLOG_WARN, "isams_open failed");
            return 0;
        }
     }
@@ -348,7 +348,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
        if (!(reg->isamc = isc_open (reg->bfs, FNAME_ISAMC,
                                    rw, key_isamc_m(res, &isamc_m))))
        {
-           yaz_log (YLOG_WARN, "isc_open");
+           yaz_log (YLOG_WARN, "isc_open failed");
            return 0;
        }
     }
@@ -359,7 +359,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
        if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
                                        rw, key_isamc_m(res, &isamc_m), 0)))
        {
-           yaz_log (YLOG_WARN, "isamb_open");
+           yaz_log (YLOG_WARN, "isamb_open failed");
            return 0;
        }
     }
@@ -370,7 +370,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
        if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
                                        rw, key_isamc_m(res, &isamc_m), 1)))
        {
-           yaz_log (YLOG_WARN, "isamb_open");
+           yaz_log (YLOG_WARN, "isamb_open failed");
            return 0;
        }
     }
@@ -381,7 +381,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name,
        if (!(reg->isamb = isamb_open (reg->bfs, "isamb",
                                        rw, key_isamc_m(res, &isamc_m), -1)))
        {
-           yaz_log (YLOG_WARN, "isamb_open");
+           yaz_log (YLOG_WARN, "isamb_open failed");
            return 0;
        }
     }
@@ -847,12 +847,13 @@ int zebra_search_RPN (ZebraHandle zh, ODR o,
 }
 
 int zebra_records_retrieve (ZebraHandle zh, ODR stream,
-                            const char *setname, Z_RecordComposition *comp,
-                            oid_value input_format, int num_recs,
-                            ZebraRetrievalRecord *recs)
+                           const char *setname, Z_RecordComposition *comp,
+                           oid_value input_format, int num_recs,
+                           ZebraRetrievalRecord *recs)
 {
-    ZebraPosSet poset;
-    int i, *pos_array, ret = 0;
+    ZebraMetaRecord *poset;
+    int i, ret = 0;
+    zint *pos_array;
     ASSERTZH;
     assert(stream);
     assert(setname);
@@ -865,7 +866,7 @@ int zebra_records_retrieve (ZebraHandle zh, ODR stream,
     if (!zh->res)
     {
         zh->errCode = 30;
-        zh->errString = odr_strdup (stream, setname);
+        zh->errString = odr_strdup(stream, setname);
         return -1;
     }
     
@@ -874,10 +875,10 @@ int zebra_records_retrieve (ZebraHandle zh, ODR stream,
     if (zebra_begin_read (zh))
        return -1;
 
-    pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
+    pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
     for (i = 0; i<num_recs; i++)
        pos_array[i] = recs[i].position;
-    poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
+    poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
     if (!poset)
     {
         yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
@@ -899,13 +900,21 @@ int zebra_records_retrieve (ZebraHandle zh, ODR stream,
            }
            else if (poset[i].sysno)
            {
+               char *buf;
+               int len;
                recs[i].errCode =
                    zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
                                        stream, input_format, comp,
-                                       &recs[i].format, &recs[i].buf,
-                                       &recs[i].len,
-                                       &recs[i].base,
-                                       &recs[i].errString);
+                                       &recs[i].format, &buf, &len,
+                                       &recs[i].base, &recs[i].errString);
+               recs[i].len = len;
+               if (len > 0)
+               {
+                   recs[i].buf = (char*) odr_malloc(stream, len);
+                   memcpy(recs[i].buf, buf, len);
+               }
+               else
+                   recs[i].buf = buf;
                 recs[i].score=poset[i].score;
                 recs[i].sysno=poset[i].sysno;
            }
@@ -913,14 +922,14 @@ int zebra_records_retrieve (ZebraHandle zh, ODR stream,
            {
                char num_str[20];
 
-               sprintf (num_str, "%d", pos_array[i]);  
+               sprintf (num_str, ZINT_FORMAT, pos_array[i]);   
                zh->errCode = 13;
                 zh->errString = odr_strdup (stream, num_str);
                ret = -1;
                 break;
            }
        }
-       zebraPosSetDestroy (zh, poset, num_recs);
+       zebra_meta_records_destroy(zh, poset, num_recs);
     }
     zebra_end_read (zh);
     xfree (pos_array);
@@ -1684,7 +1693,7 @@ int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
     }
 #if HAVE_SYS_TIMES_H
     times (&zh->tms2);
-    yaz_log (YLOG_LOG, "user/system: %ld/%ld",
+    yaz_log (log_level, "user/system: %ld/%ld",
           (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
           (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
     
@@ -1741,8 +1750,7 @@ static int zebra_commit_ex (ZebraHandle zh, int clean_only)
     rval = res_get (zh->res, "shadow");    
     if (!rval)
     {
-        yaz_log (YLOG_WARN, "Cannot perform commit");
-        yaz_log (YLOG_WARN, "No shadow area defined");
+        yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
         return 0;
     }
 
@@ -1776,7 +1784,7 @@ static int zebra_commit_ex (ZebraHandle zh, int clean_only)
     }
     else
     {
-        yaz_log (YLOG_LOG, "nothing to commit");
+        yaz_log (log_level, "nothing to commit");
     }
     bfs_destroy (bfs);
 
@@ -1862,28 +1870,18 @@ int zebra_shadow_enable (ZebraHandle zh, int value)
     return 0;
 }
 
-int zebra_record_encoding (ZebraHandle zh, const char *encoding)
+int zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
 {
     ASSERTZH;
     assert(encoding);
-    yaz_log(log_level,"zebra_record_encoding");
-    zh->errCode=0;
-    xfree (zh->record_encoding);
-
-    /*
-     * Fixme!
-     * Something about charset aliases. Oleg???
-     */
+    yaz_log(log_level,"zebra_octet_term_encoding");
+    zh->errCode = 0;
 
     if (zh->iconv_to_utf8 != 0)
         yaz_iconv_close(zh->iconv_to_utf8);
     if (zh->iconv_from_utf8 != 0)
         yaz_iconv_close(zh->iconv_from_utf8);
     
-    zh->record_encoding = xstrdup (encoding);
-    
-    yaz_log(YLOG_DEBUG, "Reset record encoding: %s", encoding);
-    
     zh->iconv_to_utf8 =
         yaz_iconv_open ("UTF-8", encoding);
     if (zh->iconv_to_utf8 == 0)
@@ -1896,6 +1894,18 @@ int zebra_record_encoding (ZebraHandle zh, const char *encoding)
     return 0;
 }
 
+int zebra_record_encoding (ZebraHandle zh, const char *encoding)
+{
+    ASSERTZH;
+    yaz_log(log_level,"zebra_record_encoding");
+    zh->errCode = 0;
+    xfree (zh->record_encoding);
+    zh->record_encoding = 0;
+    if (encoding)
+       zh->record_encoding = xstrdup (encoding);
+    return 0;
+}
+
 int zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
 {
     ASSERTZH;
@@ -1945,94 +1955,17 @@ int zebra_set_shadow_enable (ZebraHandle zh, int value)
     return 0;
 }
 
-/* almost the same as zebra_records_retrieve ... but how did it work? 
-   I mean for multiple records ??? CHECK ??? */
+/* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
+   so that it's identicical to the original api_records_retrieve */
 void api_records_retrieve (ZebraHandle zh, ODR stream,
                           const char *setname, Z_RecordComposition *comp,
                           oid_value input_format, int num_recs,
                           ZebraRetrievalRecord *recs)
 {
-    ZebraPosSet poset;
-    int i, *pos_array;
-    ASSERTZH;
-    assert(stream);
-    assert(setname);
-    assert(comp);
-    assert(recs);
-    assert(num_recs>0);
-    yaz_log(log_level,"api_records_retrieve s=%s n=%d",setname,num_recs);
-
-    if (!zh->res)
-    {
-        zh->errCode = 30;
-        zh->errString = odr_strdup (stream, setname);
-        return;
-    }
-    
-    zh->errCode = 0; 
-
-    if (zebra_begin_read (zh))
-       return;
-
-    pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
-    for (i = 0; i<num_recs; i++)
-       pos_array[i] = recs[i].position;
-    poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
-    if (!poset)
-    {
-        yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
-        zh->errCode = 30;
-        zh->errString = nmem_strdup (stream->mem, setname);
-    }
-    else
-    {
-       for (i = 0; i<num_recs; i++)
-       {
-           if (poset[i].term)
-           {
-               recs[i].errCode = 0;
-               recs[i].format = VAL_SUTRS;
-               recs[i].len = strlen(poset[i].term);
-               recs[i].buf = poset[i].term;
-               recs[i].base = poset[i].db;
-               recs[i].sysno = 0;
-           
-           }
-           else if (poset[i].sysno)
-           {
-             /* changed here ??? CHECK ??? */
-             char *b;
-               recs[i].errCode =
-                   zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
-                                       stream, input_format, comp,
-                                       &recs[i].format, 
-                                       &b,
-                                       &recs[i].len,
-                                       &recs[i].base,
-                                       &recs[i].errString);
-               recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
-               memcpy(recs[i].buf, b, recs[i].len);
-               recs[i].sysno = poset[i].sysno;
-               recs[i].score = poset[i].score;
-           }
-           else
-           {
-               char num_str[20];
-
-               sprintf (num_str, "%d", pos_array[i]);  
-               zh->errCode = 13;
-                zh->errString = odr_strdup (stream, num_str);
-                break;
-           }
-
-       }
-       zebraPosSetDestroy (zh, poset, num_recs);
-    }
-    zebra_end_read (zh);
-    xfree (pos_array);
+    zebra_records_retrieve(zh, stream, setname, comp, input_format,
+                          num_recs, recs);
 }
 
-
 /* ---------------------------------------------------------------------------
   Record insert(=update), delete