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