2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1995-12-06 16:06:43 adam
8 * Better diagnostics. Work on 'real' dictionary deletion.
10 * Revision 1.10 1995/12/06 12:41:22 adam
11 * New command 'stat' for the index program.
12 * Filenames can be read from stdin by specifying '-'.
13 * Bug fix/enhancement of the transformation from terms to regular
14 * expressons in the search engine.
16 * Revision 1.9 1995/10/10 12:24:39 adam
17 * Temporary sort files are compressed.
19 * Revision 1.8 1995/10/04 16:57:19 adam
20 * Key input and merge sort in one pass.
22 * Revision 1.7 1995/10/02 15:18:52 adam
23 * New member in recRetrieveCtrl: diagnostic.
25 * Revision 1.6 1995/09/29 15:51:56 adam
26 * First work on multi-way read.
28 * Revision 1.5 1995/09/29 14:01:43 adam
31 * Revision 1.4 1995/09/28 14:22:57 adam
32 * Sort uses smaller temporary files.
34 * Revision 1.3 1995/09/06 16:11:17 adam
35 * Option: only one word key per file.
37 * Revision 1.2 1995/09/04 12:33:42 adam
38 * Various cleanup. YAZ util used instead.
40 * Revision 1.1 1995/09/04 09:10:37 adam
41 * More work on index add/del/update.
42 * Merge sort implemented.
43 * Initial work on z39 server.
57 #define KEY_SIZE (1+sizeof(struct it_key))
58 #define INP_NAME_MAX 8192
59 #define INP_BUF_START 60000
60 #define INP_BUF_ADD 400000
62 static int no_diffs = 0;
63 static int no_updates = 0;
64 static int no_deletions = 0;
65 static int no_insertions = 0;
66 static int no_iterations = 0;
70 off_t offset; /* file offset */
71 unsigned char *buf; /* buffer block */
72 size_t buf_size; /* number of read bytes in block */
73 size_t chunk; /* number of bytes allocated */
74 size_t buf_ptr; /* current position in buffer */
75 char *prev_name; /* last word read */
76 int sysno; /* last sysno */
77 int seqno; /* last seqno */
80 void key_file_chunk_read (struct key_file *f)
84 sprintf (fname, TEMP_FNAME, f->no);
85 fd = open (fname, O_RDONLY);
88 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
91 logf (LOG_LOG, "reading chunk from %s", fname);
92 if (lseek (fd, f->offset, SEEK_SET) == -1)
94 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
97 while (f->chunk - nr > 0)
99 r = read (fd, f->buf + nr, f->chunk - nr);
106 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
114 struct key_file *key_file_init (int no, int chunk)
118 f = xmalloc (sizeof(*f));
124 f->buf = xmalloc (f->chunk);
125 f->prev_name = xmalloc (256);
126 *f->prev_name = '\0';
127 key_file_chunk_read (f);
131 int key_file_getc (struct key_file *f)
133 if (f->buf_ptr < f->buf_size)
134 return f->buf[(f->buf_ptr)++];
135 if (f->buf_size < f->chunk)
137 f->offset += f->buf_size;
138 key_file_chunk_read (f);
139 if (f->buf_ptr < f->buf_size)
140 return f->buf[(f->buf_ptr)++];
145 int key_file_decode (struct key_file *f)
149 c = key_file_getc (f);
156 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
159 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
160 d = (d << 8) + (key_file_getc (f) & 0xff);
163 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
164 d = (d << 8) + (key_file_getc (f) & 0xff);
165 d = (d << 8) + (key_file_getc (f) & 0xff);
171 int key_file_read (struct key_file *f, char *key)
176 c = key_file_getc (f);
179 strcpy (key, f->prev_name);
188 while ((key[i++] = key_file_getc (f)))
190 strcpy (f->prev_name, key);
193 d = key_file_decode (f);
196 itkey.sysno = d + f->sysno;
199 f->sysno = itkey.sysno;
202 d = key_file_decode (f);
203 itkey.seqno = d + f->seqno;
204 f->seqno = itkey.seqno;
205 memcpy (key + i, &itkey, sizeof(struct it_key));
206 return i + sizeof (struct it_key);
211 struct key_file **file;
216 int (*cmp)(const void *p1, const void *p2);
219 struct heap_info *key_heap_init (int nkeys,
220 int (*cmp)(const void *p1, const void *p2))
222 struct heap_info *hi;
225 hi = xmalloc (sizeof(*hi));
226 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
227 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
229 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
231 for (i = 0; i<= nkeys; i++)
234 hi->info.buf[i] = xmalloc (512);
239 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
244 hi->ptr[i1] = hi->ptr[i2];
249 static void key_heap_delete (struct heap_info *hi)
251 int cur = 1, child = 2;
253 assert (hi->heapnum > 0);
255 key_heap_swap (hi, 1, hi->heapnum);
257 while (child <= hi->heapnum) {
258 if (child < hi->heapnum &&
259 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
260 &hi->info.buf[hi->ptr[child+1]]) > 0)
262 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
263 &hi->info.buf[hi->ptr[child]]) > 0)
265 key_heap_swap (hi, cur, child);
274 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
279 cur = ++(hi->heapnum);
280 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
281 hi->info.file[hi->ptr[cur]] = kf;
284 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
285 &hi->info.buf[hi->ptr[cur]]) > 0)
287 key_heap_swap (hi, cur, parent);
293 static int heap_read_one (struct heap_info *hi, char *name, char *key)
302 strcpy (name, hi->info.buf[n]);
303 kf = hi->info.file[n];
305 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
306 key_heap_delete (hi);
307 if ((r = key_file_read (kf, rbuf)))
308 key_heap_insert (hi, rbuf, r, kf);
313 int heap_inp (Dict dict, ISAM isam, struct heap_info *hi)
316 char next_name[INP_NAME_MAX+1];
317 char cur_name[INP_NAME_MAX+1];
318 int key_buf_size = INP_BUF_START;
324 next_key = xmalloc (KEY_SIZE);
325 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
326 more = heap_read_one (hi, cur_name, key_buf);
327 while (more) /* EOF ? */
330 key_buf_ptr = KEY_SIZE;
333 if (!(more = heap_read_one (hi, next_name, next_key)))
335 if (*next_name && strcmp (next_name, cur_name))
337 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
338 key_buf_ptr += KEY_SIZE;
339 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
342 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
343 memcpy (new_key_buf, key_buf, key_buf_size);
344 key_buf_size += INP_BUF_ADD;
346 key_buf = new_key_buf;
350 nmemb = key_buf_ptr / KEY_SIZE;
351 assert (nmemb*KEY_SIZE == key_buf_ptr);
352 if ((info = dict_lookup (dict, cur_name)))
354 ISAM_P isam_p, isam_p2;
355 logf (LOG_DEBUG, "updating %s", cur_name);
356 memcpy (&isam_p, info+1, sizeof(ISAM_P));
357 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
362 dict_delete (dict, cur_name);
368 if (isam_p2 != isam_p)
369 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
375 logf (LOG_DEBUG, "inserting %s", cur_name);
377 isam_p = is_merge (isam, 0, nmemb, key_buf);
378 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
380 memcpy (key_buf, next_key, KEY_SIZE);
381 strcpy (cur_name, next_name);
386 void key_input (const char *dict_fname, const char *isam_fname,
387 int nkeys, int cache)
392 struct key_file **kf;
395 struct heap_info *hi;
397 dict = dict_open (dict_fname, cache, 1);
400 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
403 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
406 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
410 kf = xmalloc ((1+nkeys) * sizeof(*kf));
411 for (i = 1; i<=nkeys; i++)
412 kf[i] = key_file_init (i, 32768);
414 hi = key_heap_init (nkeys, key_qsort_compare);
415 for (i = 1; i<=nkeys; i++)
416 if ((r = key_file_read (kf[i], rbuf)))
417 key_heap_insert (hi, rbuf, r, kf[i]);
418 heap_inp (dict, isam, hi);
423 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
424 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
425 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
426 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
427 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);