2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-09-29 14:01:43 adam
10 * Revision 1.4 1995/09/28 14:22:57 adam
11 * Sort uses smaller temporary files.
13 * Revision 1.3 1995/09/06 16:11:17 adam
14 * Option: only one word key per file.
16 * Revision 1.2 1995/09/04 12:33:42 adam
17 * Various cleanup. YAZ util used instead.
19 * Revision 1.1 1995/09/04 09:10:37 adam
20 * More work on index add/del/update.
21 * Merge sort implemented.
22 * Initial work on z39 server.
34 #define KEY_SIZE (1+sizeof(struct it_key))
35 #define INP_NAME_MAX 8192
36 #define INP_BUF_START 60000
37 #define INP_BUF_ADD 400000
39 static int no_diffs = 0;
40 static int no_updates = 0;
41 static int no_insertions = 0;
42 static int no_iterations = 0;
44 static int read_one (FILE *inf, char *name, char *key)
50 if ((c=getc(inf)) == EOF)
54 for (i = 0; i<KEY_SIZE; i++)
55 ((char *)key)[i] = getc (inf);
60 static int inp (Dict dict, ISAM isam, const char *name)
64 char next_name[INP_NAME_MAX+1];
65 char cur_name[INP_NAME_MAX+1];
66 int key_buf_size = INP_BUF_START;
72 next_key = xmalloc (KEY_SIZE);
73 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
74 if (!(inf = fopen (name, "r")))
76 logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
79 more = read_one (inf, cur_name, key_buf);
80 while (more) /* EOF ? */
83 key_buf_ptr = KEY_SIZE;
86 if (!(more = read_one (inf, next_name, next_key)))
88 if (*next_name && strcmp (next_name, cur_name))
90 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
91 key_buf_ptr += KEY_SIZE;
92 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
95 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
96 memcpy (new_key_buf, key_buf, key_buf_size);
97 key_buf_size += INP_BUF_ADD;
99 key_buf = new_key_buf;
103 nmemb = key_buf_ptr / KEY_SIZE;
104 assert (nmemb*KEY_SIZE == key_buf_ptr);
105 if ((info = dict_lookup (dict, cur_name)))
107 ISAM_P isam_p, isam_p2;
108 logf (LOG_DEBUG, "updating %s", cur_name);
110 memcpy (&isam_p, info+1, sizeof(ISAM_P));
111 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
112 if (isam_p2 != isam_p)
113 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
118 logf (LOG_DEBUG, "inserting %s", cur_name);
120 isam_p = is_merge (isam, 0, nmemb, key_buf);
121 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
123 memcpy (key_buf, next_key, KEY_SIZE);
124 strcpy (cur_name, next_name);
130 void key_input (const char *dict_fname, const char *isam_fname,
131 const char *key_fname, int cache)
136 dict = dict_open (dict_fname, cache, 1);
139 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
142 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
145 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
148 inp (dict, isam, key_fname);
151 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
152 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
153 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
154 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);