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