Various cleanup. YAZ util used instead.
[idzebra-moved-to-github.git] / index / kinput.c
1 /*
2  * Copyright (C) 1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: kinput.c,v $
7  * Revision 1.2  1995-09-04 12:33:42  adam
8  * Various cleanup. YAZ util used instead.
9  *
10  * Revision 1.1  1995/09/04  09:10:37  adam
11  * More work on index add/del/update.
12  * Merge sort implemented.
13  * Initial work on z39 server.
14  *
15  */
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <assert.h>
22
23 #include "index.h"
24
25 #define KEY_SIZE (1+sizeof(struct it_key))
26 #define INP_NAME_MAX 8192
27 #define INP_BUF_START 60000
28 #define INP_BUF_ADD  400000
29
30 static int no_diffs   = 0;
31 static int no_updates = 0;
32 static int no_insertions = 0;
33 static int no_iterations = 0;
34
35 static int read_one (FILE *inf, char *name, char *key)
36 {
37     int c;
38     int i = 0;
39     name[0] = 0;
40     do
41     {
42         if ((c=getc(inf)) == EOF)
43             return 0;
44         name[i++] = c;
45     } while (c);
46     for (i = 0; i<KEY_SIZE; i++)
47         ((char *)key)[i] = getc (inf);
48     ++no_iterations;
49     return 1;
50 }
51
52 static int inp (Dict dict, ISAM isam, const char *name)
53 {
54     FILE *inf;
55     char *info;
56     char next_name[INP_NAME_MAX+1];
57     char cur_name[INP_NAME_MAX+1];
58     int key_buf_size = INP_BUF_START;
59     int key_buf_ptr;
60     char *next_key;
61     char *key_buf;
62     
63     next_key = xmalloc (KEY_SIZE);
64     key_buf = xmalloc (key_buf_size * (KEY_SIZE));
65     if (!(inf = fopen (name, "r")))
66     {
67         logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
68         exit (1);
69     }
70     read_one (inf, cur_name, key_buf);
71     while (cur_name[0])                   /* EOF ? */
72     {
73         int nmemb;
74         key_buf_ptr = KEY_SIZE;
75         while (1)
76         {
77             if (!read_one (inf, next_name, next_key))
78                 break;
79             if (strcmp (next_name, cur_name))
80                 break;
81             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
82             key_buf_ptr += KEY_SIZE;
83             if (key_buf_ptr+KEY_SIZE >= key_buf_size)
84             {
85                 char *new_key_buf;
86                 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
87                 memcpy (new_key_buf, key_buf, key_buf_size);
88                 key_buf_size += INP_BUF_ADD;
89                 xfree (key_buf);
90                 key_buf = new_key_buf;
91             }
92         }
93         no_diffs++;
94         nmemb = key_buf_ptr / KEY_SIZE;
95         assert (nmemb*KEY_SIZE == key_buf_ptr);
96         if ((info = dict_lookup (dict, cur_name)))
97         {
98             ISAM_P isam_p, isam_p2;
99             logf (LOG_DEBUG, "updating %s", cur_name);
100             no_updates++;
101             memcpy (&isam_p, info+1, sizeof(ISAM_P));
102             isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
103             if (isam_p2 != isam_p)
104                 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
105         }
106         else
107         {
108             ISAM_P isam_p;
109             logf (LOG_DEBUG, "inserting %s", cur_name);
110             no_insertions++;
111             isam_p = is_merge (isam, 0, nmemb, key_buf);
112             dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
113         }
114         memcpy (key_buf, next_key, KEY_SIZE);
115         strcpy (cur_name, next_name);
116     }
117     fclose (inf);
118     return 0;
119 }
120
121 void key_input (const char *dict_fname, const char *isam_fname,
122                 const char *key_fname, int cache)
123 {
124     Dict dict;
125     ISAM isam;
126
127     dict = dict_open (dict_fname, cache, 1);
128     if (!dict)
129     {
130         logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
131         exit (1);
132     }
133     isam = is_open (isam_fname, key_compare, 1);
134     if (!isam)
135     {
136         logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
137         exit (1);
138     }
139     inp (dict, isam, key_fname);
140     dict_close (dict);
141     is_close (isam);
142     logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
143     logf (LOG_LOG, "Distinct words .%7d", no_diffs);
144     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
145     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
146 }