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