Bug fix: storeData didn't work with files with multiple records.
[idzebra-moved-to-github.git] / index / recindex.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: recindex.c,v $
7  * Revision 1.18  1997-07-15 16:28:42  adam
8  * Bug fix: storeData didn't work with files with multiple records.
9  * Bug fix: fixed memory management with records; not really well
10  *  thought through.
11  *
12  * Revision 1.17  1997/02/12 20:39:46  adam
13  * Implemented options -f <n> that limits the log to the first <n>
14  * records.
15  * Changed some log messages also.
16  *
17  * Revision 1.16  1996/06/04 10:19:00  adam
18  * Minor changes - removed include of ctype.h.
19  *
20  * Revision 1.15  1996/05/13  14:23:06  adam
21  * Work on compaction of set/use bytes in dictionary.
22  *
23  * Revision 1.14  1996/02/01  20:48:15  adam
24  * The total size of records are always checked in rec_cache_insert to
25  * reduce memory usage.
26  *
27  * Revision 1.13  1995/12/11  09:12:49  adam
28  * The rec_get function returns NULL if record doesn't exist - will
29  * happen in the server if the result set records have been deleted since
30  * the creation of the set (i.e. the search).
31  * The server saves a result temporarily if it is 'volatile', i.e. the
32  * set is register dependent.
33  *
34  * Revision 1.12  1995/12/07  17:38:47  adam
35  * Work locking mechanisms for concurrent updates/commit.
36  *
37  * Revision 1.11  1995/12/06  13:58:26  adam
38  * Improved flushing of records - all flushes except the last one
39  * don't write the last accessed. Also flush takes place if record
40  * info occupy more than about 256k.
41  *
42  * Revision 1.10  1995/12/06  12:41:24  adam
43  * New command 'stat' for the index program.
44  * Filenames can be read from stdin by specifying '-'.
45  * Bug fix/enhancement of the transformation from terms to regular
46  * expressons in the search engine.
47  *
48  * Revision 1.9  1995/11/30  08:34:33  adam
49  * Started work on commit facility.
50  * Changed a few malloc/free to xmalloc/xfree.
51  *
52  * Revision 1.8  1995/11/28  14:26:21  adam
53  * Bug fix: recordId with constant wasn't right.
54  * Bug fix: recordId dictionary entry wasn't deleted when needed.
55  *
56  * Revision 1.7  1995/11/28  09:09:43  adam
57  * Zebra config renamed.
58  * Use setting 'recordId' to identify record now.
59  * Bug fix in recindex.c: rec_release_blocks was invokeded even
60  * though the blocks were already released.
61  * File traversal properly deletes records when needed.
62  *
63  * Revision 1.6  1995/11/25  10:24:06  adam
64  * More record fields - they are enumerated now.
65  * New options: flagStoreData flagStoreKey.
66  *
67  * Revision 1.5  1995/11/22  17:19:18  adam
68  * Record management uses the bfile system.
69  *
70  * Revision 1.4  1995/11/20  16:59:46  adam
71  * New update method: the 'old' keys are saved for each records.
72  *
73  * Revision 1.3  1995/11/16  15:34:55  adam
74  * Uses new record management system in both indexer and server.
75  *
76  * Revision 1.2  1995/11/15  19:13:08  adam
77  * Work on record management.
78  *
79  * Revision 1.1  1995/11/15  14:46:20  adam
80  * Started work on better record management system.
81  *
82  */
83 #include <stdio.h>
84 #include <assert.h>
85 #include <string.h>
86
87 #include "recindxp.h"
88
89 static void rec_write_head (Records p)
90 {
91     int r;
92
93     assert (p);
94     assert (p->index_BFile);
95
96     r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);    
97     if (r)
98     {
99         logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
100         exit (1);
101     }
102 }
103
104 static void rec_tmp_expand (Records p, int size, int dst_type)
105 {
106     if (p->tmp_size < size + 2048 ||
107         p->tmp_size < p->head.block_size[dst_type]*2)
108     {
109         xfree (p->tmp_buf);
110         p->tmp_size = size + p->head.block_size[dst_type]*2 + 2048;
111         p->tmp_buf = xmalloc (p->tmp_size);
112     }
113 }
114
115 static int read_indx (Records p, int sysno, void *buf, int itemsize, 
116                       int ignoreError)
117 {
118     int r;
119     int pos = (sysno-1)*itemsize;
120
121     r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
122     if (r != 1 && !ignoreError)
123     {
124         logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
125               p->index_fname, (long) pos);
126         abort ();
127         exit (1);
128     }
129     return r;
130 }
131
132 static void write_indx (Records p, int sysno, void *buf, int itemsize)
133 {
134     int pos = (sysno-1)*itemsize;
135
136     bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
137 }
138
139 static void rec_release_blocks (Records p, int sysno)
140 {
141     struct record_index_entry entry;
142     int freeblock, freenext;
143     int dst_type;
144
145     if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
146         return ;
147     p->head.total_bytes -= entry.size;
148     freeblock = entry.next;
149     assert (freeblock > 0);
150     dst_type = freeblock & 7;
151     assert (dst_type < REC_BLOCK_TYPES);
152     freeblock = freeblock / 8;
153     while (freeblock)
154     {
155         if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
156                      &freenext) != 1)
157         {
158             logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
159             exit (1);
160         }
161         if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
162                       &p->head.block_free[dst_type]))
163         {
164             logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
165             exit (1);
166         }
167         p->head.block_free[dst_type] = freeblock;
168         freeblock = freenext;
169         p->head.block_used[dst_type]--;
170     }
171 }
172
173 static void rec_delete_single (Records p, Record rec)
174 {
175     struct record_index_entry entry;
176
177     rec_release_blocks (p, rec->sysno);
178
179     entry.next = p->head.index_free;
180     entry.size = 0;
181     p->head.index_free = rec->sysno;
182     write_indx (p, rec->sysno, &entry, sizeof(entry));
183 }
184
185
186 static void rec_write_single (Records p, Record rec)
187 {
188     int i, size = 0;
189     char *cptr;
190     int dst_type = 0;
191     int no_written = 0;
192     int block_prev = -1, block_free;
193     struct record_index_entry entry;
194
195     for (i = 0; i < REC_NO_INFO; i++)
196         if (!rec->info[i])
197             size += sizeof(*rec->size);
198         else
199             size += sizeof(*rec->size) + rec->size[i];
200
201     for (i = 1; i<REC_BLOCK_TYPES; i++)
202         if (size >= p->head.block_move[i])
203             dst_type = i;
204
205     rec_tmp_expand (p, size, dst_type);
206
207     cptr = p->tmp_buf + sizeof(int);           /* a hack! */
208     for (i = 0; i < REC_NO_INFO; i++)
209     {
210         memcpy (cptr, &rec->size[i], sizeof(*rec->size));
211         cptr += sizeof(*rec->size);
212         if (rec->info[i])
213         {
214             memcpy (cptr, rec->info[i], rec->size[i]);
215             cptr += rec->size[i];
216         }
217     }
218     cptr = p->tmp_buf;
219     while (no_written < size)
220     {
221         block_free = p->head.block_free[dst_type];
222         if (block_free)
223         {
224             if (bf_read (p->data_BFile[dst_type],
225                          block_free, 0, sizeof(*p->head.block_free),
226                          &p->head.block_free[dst_type]) != 1)
227             {
228                 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
229                       p->data_fname[dst_type], block_free);
230             }
231         }
232         else
233             block_free = p->head.block_last[dst_type]++;
234         if (block_prev == -1)
235         {
236             entry.next = block_free*8 + dst_type;
237             entry.size = size;
238             p->head.total_bytes += size;
239             write_indx (p, rec->sysno, &entry, sizeof(entry));
240         }
241         else
242         {
243             memcpy (cptr, &block_free, sizeof(int));
244             bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
245             cptr = p->tmp_buf + no_written;
246         }
247         block_prev = block_free;
248         no_written += p->head.block_size[dst_type] - sizeof(int);
249         p->head.block_used[dst_type]++;
250     }
251     assert (block_prev != -1);
252     block_free = 0;
253     memcpy (cptr, &block_free, sizeof(int));
254     bf_write (p->data_BFile[dst_type], block_prev, 0,
255               sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
256 }
257
258 static void rec_update_single (Records p, Record rec)
259 {
260     rec_release_blocks (p, rec->sysno);
261     rec_write_single (p, rec);
262 }
263
264 Records rec_open (int rw)
265 {
266     Records p;
267     int i, r;
268
269     p = xmalloc (sizeof(*p));
270     p->rw = rw;
271     p->tmp_size = 1024;
272     p->tmp_buf = xmalloc (p->tmp_size);
273     p->index_fname = "recindex";
274     p->index_BFile = bf_open (p->index_fname, 128, rw);
275     if (p->index_BFile == NULL)
276     {
277         logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
278         exit (1);
279     }
280     r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
281     switch (r)
282     {
283     case 0:
284         memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
285         p->head.index_free = 0;
286         p->head.index_last = 1;
287         p->head.no_records = 0;
288         p->head.total_bytes = 0;
289         for (i = 0; i<REC_BLOCK_TYPES; i++)
290         {
291             p->head.block_free[i] = 0;
292             p->head.block_last[i] = 1;
293             p->head.block_used[i] = 0;
294         }
295         p->head.block_size[0] = 128;
296         p->head.block_move[0] = 0;
297         for (i = 1; i<REC_BLOCK_TYPES; i++)
298         {
299             p->head.block_size[i] = p->head.block_size[i-1] * 4;
300             p->head.block_move[i] = p->head.block_size[i] * 3;
301         }
302         if (rw)
303             rec_write_head (p);
304         break;
305     case 1:
306         memcpy (&p->head, p->tmp_buf, sizeof(p->head));
307         if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
308         {
309             logf (LOG_FATAL, "read %s. bad header", p->index_fname);
310             exit (1);
311         }
312         break;
313     }
314     for (i = 0; i<REC_BLOCK_TYPES; i++)
315     {
316         char str[80];
317         sprintf (str, "recdata%c", i + 'A');
318         p->data_fname[i] = xmalloc (strlen(str)+1);
319         strcpy (p->data_fname[i], str);
320         p->data_BFile[i] = NULL;
321     }
322     for (i = 0; i<REC_BLOCK_TYPES; i++)
323     {
324         if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
325                                           p->head.block_size[i],
326                                           rw)))
327         {
328             logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
329             exit (1);
330         }
331     }
332     p->cache_max = 10;
333     p->cache_cur = 0;
334     p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
335     return p;
336 }
337
338 static void rec_cache_flush (Records p, int saveCount)
339 {
340     int i, j;
341
342     if (saveCount >= p->cache_cur)
343         saveCount = 0;
344     for (i = 0; i<p->cache_cur - saveCount; i++)
345     {
346         struct record_cache_entry *e = p->record_cache + i;
347         switch (e->flag)
348         {
349         case recordFlagNop:
350             break;
351         case recordFlagNew:
352             rec_write_single (p, e->rec);
353             break;
354         case recordFlagWrite:
355             rec_update_single (p, e->rec);
356             break;
357         case recordFlagDelete:
358             rec_delete_single (p, e->rec);
359             break;
360         }
361         rec_rm (&e->rec);
362     }
363     for (j = 0; j<saveCount; j++, i++)
364         memcpy (p->record_cache+j, p->record_cache+i,
365                 sizeof(*p->record_cache));
366     p->cache_cur = saveCount;
367 }
368
369 static Record *rec_cache_lookup (Records p, int sysno,
370                                  enum recordCacheFlag flag)
371 {
372     int i;
373     for (i = 0; i<p->cache_cur; i++)
374     {
375         struct record_cache_entry *e = p->record_cache + i;
376         if (e->rec->sysno == sysno)
377         {
378             if (flag != recordFlagNop && e->flag == recordFlagNop)
379                 e->flag = flag;
380             return &e->rec;
381         }
382     }
383     return NULL;
384 }
385
386 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
387 {
388     struct record_cache_entry *e;
389
390     if (p->cache_cur == p->cache_max)
391         rec_cache_flush (p, 1);
392     else if (p->cache_cur > 0)
393     {
394         int i, j;
395         int used = 0;
396         for (i = 0; i<p->cache_cur; i++)
397         {
398             Record r = (p->record_cache + i)->rec;
399             for (j = 0; j<REC_NO_INFO; j++)
400                 used += r->size[j];
401         }
402         if (used > 256000)
403             rec_cache_flush (p, 1);
404     }
405     assert (p->cache_cur < p->cache_max);
406
407     e = p->record_cache + (p->cache_cur)++;
408     e->flag = flag;
409     e->rec = rec_cp (rec);
410 }
411
412 void rec_close (Records *pp)
413 {
414     Records p = *pp;
415     int i;
416
417     assert (p);
418
419     rec_cache_flush (p, 0);
420     xfree (p->record_cache);
421
422     if (p->rw)
423         rec_write_head (p);
424
425     if (p->index_BFile)
426         bf_close (p->index_BFile);
427
428     for (i = 0; i<REC_BLOCK_TYPES; i++)
429     {
430         if (p->data_BFile[i])
431             bf_close (p->data_BFile[i]);
432         xfree (p->data_fname[i]);
433     }
434     xfree (p->tmp_buf);
435     xfree (p);
436     *pp = NULL;
437 }
438
439
440 Record rec_get (Records p, int sysno)
441 {
442     int i;
443     Record rec, *recp;
444     struct record_index_entry entry;
445     int freeblock, dst_type;
446     char *nptr, *cptr;
447
448     assert (sysno > 0);
449     assert (p);
450
451     if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
452         return rec_cp (*recp);
453
454     if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
455         return NULL;       /* record is not there! */
456
457     if (!entry.size)
458         return NULL;       /* record is deleted */
459
460     dst_type = entry.next & 7;
461     assert (dst_type < REC_BLOCK_TYPES);
462     freeblock = entry.next / 8;
463
464     assert (freeblock > 0);
465     
466     rec = xmalloc (sizeof(*rec));
467     rec_tmp_expand (p, entry.size, dst_type);
468
469     cptr = p->tmp_buf;
470     bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
471     memcpy (&freeblock, cptr, sizeof(freeblock));
472
473     while (freeblock)
474     {
475         int tmp;
476
477         cptr += p->head.block_size[dst_type] - sizeof(freeblock);
478         
479         memcpy (&tmp, cptr, sizeof(tmp));
480         bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
481         memcpy (&freeblock, cptr, sizeof(freeblock));
482         memcpy (cptr, &tmp, sizeof(tmp));
483     }
484
485     rec->sysno = sysno;
486     nptr = p->tmp_buf + sizeof(freeblock);
487     for (i = 0; i < REC_NO_INFO; i++)
488     {
489         memcpy (&rec->size[i], nptr, sizeof(*rec->size));
490         nptr += sizeof(*rec->size);
491         if (rec->size[i])
492         {
493             rec->info[i] = xmalloc (rec->size[i]);
494             memcpy (rec->info[i], nptr, rec->size[i]);
495             nptr += rec->size[i];
496         }
497         else
498             rec->info[i] = NULL;
499     }
500     rec_cache_insert (p, rec, recordFlagNop);
501     return rec;
502 }
503
504 Record rec_new (Records p)
505 {
506     int sysno, i;
507     Record rec;
508
509     assert (p);
510     rec = xmalloc (sizeof(*rec));
511     if (1 || p->head.index_free == 0)
512         sysno = (p->head.index_last)++;
513     else
514     {
515         struct record_index_entry entry;
516
517         read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
518         sysno = p->head.index_free;
519         p->head.index_free = entry.next;
520     }
521     (p->head.no_records)++;
522     rec->sysno = sysno;
523     for (i = 0; i < REC_NO_INFO; i++)
524     {
525         rec->info[i] = NULL;
526         rec->size[i] = 0;
527     }
528     rec_cache_insert (p, rec, recordFlagNew);
529     return rec;
530 }
531
532 void rec_del (Records p, Record *recpp)
533 {
534     Record *recp;
535
536     (p->head.no_records)--;
537     if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
538     {
539         rec_rm (recp);
540         *recp = *recpp;
541     }
542     else
543     {
544         rec_cache_insert (p, *recpp, recordFlagDelete);
545         rec_rm (recpp);
546     }
547     *recpp = NULL;
548 }
549
550 void rec_put (Records p, Record *recpp)
551 {
552     Record *recp;
553
554     if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
555     {
556         rec_rm (recp);
557         *recp = *recpp;
558     }
559     else
560     {
561         rec_cache_insert (p, *recpp, recordFlagWrite);
562         rec_rm (recpp);
563     }
564     *recpp = NULL;
565 }
566
567 void rec_rm (Record *recpp)
568 {
569     int i;
570
571     if (!*recpp)
572         return ;
573     for (i = 0; i < REC_NO_INFO; i++)
574         xfree ((*recpp)->info[i]);
575     xfree (*recpp);
576     *recpp = NULL;
577 }
578
579 Record rec_cp (Record rec)
580 {
581     Record n;
582     int i;
583
584     n = xmalloc (sizeof(*n));
585     n->sysno = rec->sysno;
586     for (i = 0; i < REC_NO_INFO; i++)
587         if (!rec->info[i])
588         {
589             n->info[i] = NULL;
590             n->size[i] = 0;
591         }
592         else
593         {
594             n->size[i] = rec->size[i];
595             n->info[i] = xmalloc (rec->size[i]);
596             memcpy (n->info[i], rec->info[i], rec->size[i]);
597         }
598     return n;
599 }
600
601
602 char *rec_strdup (const char *s, size_t *len)
603 {
604     char *p;
605
606     if (!s)
607     {
608         *len = 0;
609         return NULL;
610     }
611     *len = strlen(s)+1;
612     p = xmalloc (*len);
613     strcpy (p, s);
614     return p;
615 }
616