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