Multiple registers (alpha early)
[idzebra-moved-to-github.git] / index / kinput.c
index b9d7f06..b6eaca2 100644 (file)
@@ -3,7 +3,7 @@
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
  *
- * $Id: kinput.c,v 1.45 2002-02-20 17:30:01 adam Exp $
+ * $Id: kinput.c,v 1.47 2002-04-04 14:14:13 adam Exp $
  *
  * Bugs
  *  - Allocates a lot of memory for the merge process, but never releases it.
@@ -23,7 +23,6 @@
 #include <assert.h>
 
 #include "index.h"
-#include "zserver.h"
 
 #define KEY_SIZE (1+sizeof(struct it_key))
 #define INP_NAME_MAX 768
@@ -65,7 +64,7 @@ void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
 {
     const char *pre;
     
-    pre = res_get_def (zh->service->res, "keyTmpDir", ".");
+    pre = res_get_def (zh->res, "keyTmpDir", ".");
     sprintf (fname, "%s/key%d.tmp", pre, no);
 }
 
@@ -117,6 +116,13 @@ void key_file_chunk_read (struct key_file *f)
     close (fd);
 }
 
+void key_file_destroy (struct key_file *f)
+{
+    xfree (f->buf);
+    xfree (f->prev_name);
+    xfree (f);
+}
+
 struct key_file *key_file_init (int no, int chunk, Res res)
 {
     struct key_file *f;
@@ -253,6 +259,19 @@ struct heap_info *key_heap_init (int nkeys,
     return hi;
 }
 
+void key_heap_destroy (struct heap_info *hi, int nkeys)
+{
+    int i;
+    yaz_log (LOG_LOG, "key_heap_destroy");
+    for (i = 0; i<=nkeys; i++)
+        xfree (hi->info.buf[i]);
+    
+    xfree (hi->info.buf);
+    xfree (hi->ptr);
+    xfree (hi->info.file);
+    xfree (hi);
+}
+
 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
 {
     int swap;
@@ -619,7 +638,7 @@ void zebra_index_merge (ZebraHandle zh)
     int i, r;
     struct heap_info *hi;
     struct progressInfo progressInfo;
-    int nkeys = zh->key_file_no;
+    int nkeys = zh->reg->key_file_no;
     
     if (nkeys < 0)
     {
@@ -642,32 +661,32 @@ void zebra_index_merge (ZebraHandle zh)
     time (&progressInfo.lastTime);
     for (i = 1; i<=nkeys; i++)
     {
-        kf[i] = key_file_init (i, 8192, zh->service->res);
+        kf[i] = key_file_init (i, 8192, zh->res);
         kf[i]->readHandler = progressFunc;
         kf[i]->readInfo = &progressInfo;
         progressInfo.totalBytes += kf[i]->length;
         progressInfo.totalOffset += kf[i]->buf_size;
     }
     hi = key_heap_init (nkeys, key_qsort_compare);
-    hi->dict = zh->service->dict;
-    hi->isams = zh->service->isams;
+    hi->dict = zh->reg->dict;
+    hi->isams = zh->reg->isams;
 #if ZMBOL
-    hi->isam = zh->service->isam;
-    hi->isamc = zh->service->isamc;
-    hi->isamd = zh->service->isamd;
+    hi->isam = zh->reg->isam;
+    hi->isamc = zh->reg->isamc;
+    hi->isamd = zh->reg->isamd;
 #endif
     
     for (i = 1; i<=nkeys; i++)
         if ((r = key_file_read (kf[i], rbuf)))
             key_heap_insert (hi, rbuf, r, kf[i]);
-    if (zh->service->isams)
+    if (zh->reg->isams)
        heap_inps (hi);
 #if ZMBOL
-    else if (zh->service->isamc)
+    else if (zh->reg->isamc)
         heap_inpc (hi);
-    else if (zh->service->isam)
+    else if (zh->reg->isam)
        heap_inp (hi);
-    else if (zh->service->isamd)
+    else if (zh->reg->isamd)
        heap_inpd (hi);
 #endif
        
@@ -681,79 +700,12 @@ void zebra_index_merge (ZebraHandle zh)
     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
     logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
-    zh->key_file_no = 0;
-}
+    zh->reg->key_file_no = 0;
 
-void key_input (ZebraHandle zh, int nkeys, int cache, Res res)
-                
-{
-    struct key_file **kf;
-    char rbuf[1024];
-    int i, r;
-    struct heap_info *hi;
-    struct progressInfo progressInfo;
-
-    if (nkeys < 0)
-    {
-        char fname[1024];
-        nkeys = 0;
-        while (1)
-        {
-            getFnameTmp (res, fname, nkeys+1);
-            if (access (fname, R_OK) == -1)
-                break;
-            nkeys++;
-        }
-        if (!nkeys)
-            return ;
-    }
-    kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
-    progressInfo.totalBytes = 0;
-    progressInfo.totalOffset = 0;
-    time (&progressInfo.startTime);
-    time (&progressInfo.lastTime);
-    for (i = 1; i<=nkeys; i++)
-    {
-        kf[i] = key_file_init (i, 8192, res);
-        kf[i]->readHandler = progressFunc;
-        kf[i]->readInfo = &progressInfo;
-        progressInfo.totalBytes += kf[i]->length;
-        progressInfo.totalOffset += kf[i]->buf_size;
-    }
-    hi = key_heap_init (nkeys, key_qsort_compare);
-    hi->dict = zh->service->dict;
-    hi->isams = zh->service->isams;
-#if ZMBOL
-    hi->isam = zh->service->isam;
-    hi->isamc = zh->service->isamc;
-    hi->isamd = zh->service->isamd;
-#endif
-    
-    for (i = 1; i<=nkeys; i++)
-        if ((r = key_file_read (kf[i], rbuf)))
-            key_heap_insert (hi, rbuf, r, kf[i]);
-    if (hi->isams)
-       heap_inps (hi);
-#if ZMBOL
-    else if (hi->isamc)
-        heap_inpc (hi);
-    else if (hi->isam)
-       heap_inp (hi);
-    else if (hi->isamd)
-       heap_inpd (hi);
-#endif
-       
+    key_heap_destroy (hi, nkeys);
     for (i = 1; i<=nkeys; i++)
-    {
-        getFnameTmp (res, rbuf, i);
-        unlink (rbuf);
-    }
-    logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
-    logf (LOG_LOG, "Distinct words .%7d", no_diffs);
-    logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
-    logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
-    logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
-
-    /* xmalloc_trav("unfreed"); while hunting leaks */     
+        key_file_destroy (kf[i]);
+    xfree (kf);
 }
 
+