Updated MSVC project files.
[idzebra-moved-to-github.git] / index / recindex.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: recindex.c,v $
7  * Revision 1.24  1999-06-25 13:48:02  adam
8  * Updated MSVC project files.
9  * Added BZIP2 record compression (not very well tested).
10  *
11  * Revision 1.23  1999/05/26 07:49:13  adam
12  * C++ compilation.
13  *
14  * Revision 1.22  1999/02/18 12:49:34  adam
15  * Changed file naming scheme for register files as well as record
16  * store/index files.
17  *
18  * Revision 1.21  1999/02/02 14:51:03  adam
19  * Updated WIN32 code specific sections. Changed header.
20  *
21  * Revision 1.20  1998/01/12 15:04:08  adam
22  * The test option (-s) only uses read-lock (and not write lock).
23  *
24  * Revision 1.19  1997/09/17 12:19:16  adam
25  * Zebra version corresponds to YAZ version 1.4.
26  * Changed Zebra server so that it doesn't depend on global common_resource.
27  *
28  * Revision 1.18  1997/07/15 16:28:42  adam
29  * Bug fix: storeData didn't work with files with multiple records.
30  * Bug fix: fixed memory management with records; not really well
31  *  thought through.
32  *
33  * Revision 1.17  1997/02/12 20:39:46  adam
34  * Implemented options -f <n> that limits the log to the first <n>
35  * records.
36  * Changed some log messages also.
37  *
38  * Revision 1.16  1996/06/04 10:19:00  adam
39  * Minor changes - removed include of ctype.h.
40  *
41  * Revision 1.15  1996/05/13  14:23:06  adam
42  * Work on compaction of set/use bytes in dictionary.
43  *
44  * Revision 1.14  1996/02/01  20:48:15  adam
45  * The total size of records are always checked in rec_cache_insert to
46  * reduce memory usage.
47  *
48  * Revision 1.13  1995/12/11  09:12:49  adam
49  * The rec_get function returns NULL if record doesn't exist - will
50  * happen in the server if the result set records have been deleted since
51  * the creation of the set (i.e. the search).
52  * The server saves a result temporarily if it is 'volatile', i.e. the
53  * set is register dependent.
54  *
55  * Revision 1.12  1995/12/07  17:38:47  adam
56  * Work locking mechanisms for concurrent updates/commit.
57  *
58  * Revision 1.11  1995/12/06  13:58:26  adam
59  * Improved flushing of records - all flushes except the last one
60  * don't write the last accessed. Also flush takes place if record
61  * info occupy more than about 256k.
62  *
63  * Revision 1.10  1995/12/06  12:41:24  adam
64  * New command 'stat' for the index program.
65  * Filenames can be read from stdin by specifying '-'.
66  * Bug fix/enhancement of the transformation from terms to regular
67  * expressons in the search engine.
68  *
69  * Revision 1.9  1995/11/30  08:34:33  adam
70  * Started work on commit facility.
71  * Changed a few malloc/free to xmalloc/xfree.
72  *
73  * Revision 1.8  1995/11/28  14:26:21  adam
74  * Bug fix: recordId with constant wasn't right.
75  * Bug fix: recordId dictionary entry wasn't deleted when needed.
76  *
77  * Revision 1.7  1995/11/28  09:09:43  adam
78  * Zebra config renamed.
79  * Use setting 'recordId' to identify record now.
80  * Bug fix in recindex.c: rec_release_blocks was invokeded even
81  * though the blocks were already released.
82  * File traversal properly deletes records when needed.
83  *
84  * Revision 1.6  1995/11/25  10:24:06  adam
85  * More record fields - they are enumerated now.
86  * New options: flagStoreData flagStoreKey.
87  *
88  * Revision 1.5  1995/11/22  17:19:18  adam
89  * Record management uses the bfile system.
90  *
91  * Revision 1.4  1995/11/20  16:59:46  adam
92  * New update method: the 'old' keys are saved for each records.
93  *
94  * Revision 1.3  1995/11/16  15:34:55  adam
95  * Uses new record management system in both indexer and server.
96  *
97  * Revision 1.2  1995/11/15  19:13:08  adam
98  * Work on record management.
99  *
100  * Revision 1.1  1995/11/15  14:46:20  adam
101  * Started work on better record management system.
102  *
103  */
104
105
106 /*
107  *  Format of first block
108  *      next       (4 bytes)
109  *      ref_count  (4 bytes)
110  *      block      (504 bytes)
111  *
112  *  Format of subsequent blocks 
113  *      next  (4 bytes)
114  *      block (508 bytes)
115  *
116  *  Format of each record
117  *      sysno
118  *      (length, data) - pairs
119  *      length = 0 if same as previous
120  */
121 #include <stdio.h>
122 #include <assert.h>
123 #include <string.h>
124
125 #include "recindxp.h"
126
127 #if HAVE_BZLIB_H
128 #include <bzlib.h>
129 #endif
130 static void rec_write_head (Records p)
131 {
132     int r;
133
134     assert (p);
135     assert (p->index_BFile);
136
137     r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);    
138     if (r)
139     {
140         logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
141         exit (1);
142     }
143 }
144
145 static void rec_tmp_expand (Records p, int size)
146 {
147     if (p->tmp_size < size + 2048 ||
148         p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
149     {
150         xfree (p->tmp_buf);
151         p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
152         p->tmp_buf = (char *) xmalloc (p->tmp_size);
153     }
154 }
155
156 static int read_indx (Records p, int sysno, void *buf, int itemsize, 
157                       int ignoreError)
158 {
159     int r;
160     int pos = (sysno-1)*itemsize;
161
162     r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
163     if (r != 1 && !ignoreError)
164     {
165         logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
166               p->index_fname, (long) pos);
167         exit (1);
168     }
169     return r;
170 }
171
172 static void write_indx (Records p, int sysno, void *buf, int itemsize)
173 {
174     int pos = (sysno-1)*itemsize;
175
176     bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
177 }
178
179 static void rec_release_blocks (Records p, int sysno)
180 {
181     struct record_index_entry entry;
182     int freeblock;
183     int block_and_ref[2];
184     int dst_type;
185     int first = 1;
186
187     logf (LOG_LOG, "release_blocks for sysno=%d", sysno);
188     if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
189         return ;
190
191     freeblock = entry.next;
192     assert (freeblock > 0);
193     dst_type = freeblock & 7;
194     assert (dst_type < REC_BLOCK_TYPES);
195     freeblock = freeblock / 8;
196     while (freeblock)
197     {
198         if (bf_read (p->data_BFile[dst_type], freeblock, 0,
199                      sizeof(block_and_ref), block_and_ref) != 1)
200         {
201             logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
202             exit (1);
203         }
204         if (first)
205         {
206             block_and_ref[1]--;
207             if (block_and_ref[1])
208             {
209                 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
210                               sizeof(block_and_ref), block_and_ref))
211                 {
212                     logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
213                     exit (1);
214                 }
215                 return;
216             }
217             first = 0;
218         }
219         
220         if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
221                       &p->head.block_free[dst_type]))
222         {
223             logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
224             exit (1);
225         }
226         p->head.block_free[dst_type] = freeblock;
227         freeblock = block_and_ref[0];
228
229         p->head.block_used[dst_type]--;
230     }
231     p->head.total_bytes -= entry.size;
232 }
233
234 static void rec_delete_single (Records p, Record rec)
235 {
236     struct record_index_entry entry;
237
238     rec_release_blocks (p, rec->sysno);
239
240     entry.next = p->head.index_free;
241     entry.size = 0;
242     p->head.index_free = rec->sysno;
243     write_indx (p, rec->sysno, &entry, sizeof(entry));
244 }
245
246 static void rec_write_tmp_buf (Records p, int size, int *sysnos);
247
248 static void rec_write_single (Records p, Record rec)
249 {
250
251     int sysnos[2];
252     int i, size = 0;
253     char *cptr;
254
255     logf (LOG_LOG, " rec_write_single !!!!!!!!!!!!!!!!!!!!!!!!!!!!");
256     for (i = 0; i < REC_NO_INFO; i++)
257         if (!rec->info[i])
258             size += sizeof(*rec->size);
259         else
260             size += sizeof(*rec->size) + rec->size[i];
261
262     rec_tmp_expand (p, size);
263
264     cptr = p->tmp_buf + sizeof(int);           /* a hack! */
265     for (i = 0; i < REC_NO_INFO; i++)
266     {
267         memcpy (cptr, &rec->size[i], sizeof(*rec->size));
268         cptr += sizeof(*rec->size);
269         if (rec->info[i])
270         {
271             memcpy (cptr, rec->info[i], rec->size[i]);
272             cptr += rec->size[i];
273         }
274     }
275     sysnos[0] = rec->sysno;
276     sysnos[1] = -1;
277     rec_write_tmp_buf (p, size, sysnos);
278 }
279
280 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
281 {
282     struct record_index_entry entry;
283     int no_written = 0;
284     char *cptr = p->tmp_buf;
285     int block_prev = -1, block_free;
286     int dst_type = 0;
287     int i;
288
289     for (i = 1; i<REC_BLOCK_TYPES; i++)
290         if (size >= p->head.block_move[i])
291             dst_type = i;
292     while (no_written < size)
293     {
294         block_free = p->head.block_free[dst_type];
295         if (block_free)
296         {
297             if (bf_read (p->data_BFile[dst_type],
298                          block_free, 0, sizeof(*p->head.block_free),
299                          &p->head.block_free[dst_type]) != 1)
300             {
301                 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
302                       p->data_fname[dst_type], block_free);
303                 exit (1);
304             }
305         }
306         else
307             block_free = p->head.block_last[dst_type]++;
308         if (block_prev == -1)
309         {
310             entry.next = block_free*8 + dst_type;
311             entry.size = size;
312             p->head.total_bytes += size;
313             while (*sysnos > 0)
314             {
315                 write_indx (p, *sysnos, &entry, sizeof(entry));
316                 sysnos++;
317             }
318         }
319         else
320         {
321             memcpy (cptr, &block_free, sizeof(int));
322             bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
323             cptr = p->tmp_buf + no_written;
324         }
325         block_prev = block_free;
326         no_written += p->head.block_size[dst_type] - sizeof(int);
327         p->head.block_used[dst_type]++;
328     }
329     assert (block_prev != -1);
330     block_free = 0;
331     memcpy (cptr, &block_free, sizeof(int));
332     bf_write (p->data_BFile[dst_type], block_prev, 0,
333               sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
334 }
335
336 static void rec_update_single (Records p, Record rec)
337 {
338     rec_release_blocks (p, rec->sysno);
339     rec_write_single (p, rec);
340 }
341
342 Records rec_open (BFiles bfs, int rw)
343 {
344     Records p;
345     int i, r;
346
347     p = (Records) xmalloc (sizeof(*p));
348     p->rw = rw;
349     p->tmp_size = 1024;
350     p->tmp_buf = (char *) xmalloc (p->tmp_size);
351     p->index_fname = "reci";
352     p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
353     if (p->index_BFile == NULL)
354     {
355         logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
356         exit (1);
357     }
358     r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
359     switch (r)
360     {
361     case 0:
362         memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
363         p->head.index_free = 0;
364         p->head.index_last = 1;
365         p->head.no_records = 0;
366         p->head.total_bytes = 0;
367         for (i = 0; i<REC_BLOCK_TYPES; i++)
368         {
369             p->head.block_free[i] = 0;
370             p->head.block_last[i] = 1;
371             p->head.block_used[i] = 0;
372         }
373         p->head.block_size[0] = 128;
374         p->head.block_move[0] = 0;
375         for (i = 1; i<REC_BLOCK_TYPES; i++)
376         {
377             p->head.block_size[i] = p->head.block_size[i-1] * 4;
378             p->head.block_move[i] = p->head.block_size[i] * 3;
379         }
380         if (rw)
381             rec_write_head (p);
382         break;
383     case 1:
384         memcpy (&p->head, p->tmp_buf, sizeof(p->head));
385         if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
386         {
387             logf (LOG_FATAL, "read %s. bad header", p->index_fname);
388             exit (1);
389         }
390         break;
391     }
392     for (i = 0; i<REC_BLOCK_TYPES; i++)
393     {
394         char str[80];
395         sprintf (str, "recd%c", i + 'A');
396         p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
397         strcpy (p->data_fname[i], str);
398         p->data_BFile[i] = NULL;
399     }
400     for (i = 0; i<REC_BLOCK_TYPES; i++)
401     {
402         if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
403                                           p->head.block_size[i],
404                                           rw)))
405         {
406             logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
407             exit (1);
408         }
409     }
410     p->cache_max = 400;
411     p->cache_cur = 0;
412     p->record_cache = (struct record_cache_entry *)
413         xmalloc (sizeof(*p->record_cache)*p->cache_max);
414     return p;
415 }
416
417 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
418 {
419     (*len) = 0;
420     while (n > 127)
421     {
422         buf[*len] = 128 + (n & 127);
423         n = n >> 7;
424         (*len)++;
425     }
426     buf[*len] = n;
427     (*len)++;
428 }
429
430 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
431 {
432     unsigned n = 0;
433     unsigned w = 1;
434     (*len) = 0;
435
436     while (buf[*len] > 127)
437     {
438         n += w*(buf[*len] & 127);
439         w = w << 7;
440         (*len)++;
441     }
442     n += w * buf[*len];
443     (*len)++;
444     *np = n;
445 }
446 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
447                                     char **out_buf, int *out_size,
448                                     int *out_offset)
449 {
450     int i;
451     int len;
452
453     for (i = 0; i<REC_NO_INFO; i++)
454     {
455         if (*out_offset + (int) rec->size[i] + 20 > *out_size)
456         {
457             int new_size = *out_offset + rec->size[i] + 65536;
458             char *np = (char *) xmalloc (new_size);
459             if (*out_offset)
460                 memcpy (np, *out_buf, *out_offset);
461             xfree (*out_buf);
462             *out_size = new_size;
463             *out_buf = np;
464         }
465         if (i == 0)
466         {
467             rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
468             (*out_offset) += len;
469         }
470         if (rec->size[i] == 0)
471         {
472             rec_encode_unsigned (1, *out_buf + *out_offset, &len);
473             (*out_offset) += len;
474         }
475         else if (last_rec && rec->size[i] == last_rec->size[i] &&
476                  !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
477         {
478             rec_encode_unsigned (0, *out_buf + *out_offset, &len);
479             (*out_offset) += len;
480         }
481         else
482         {
483             rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
484             (*out_offset) += len;
485             memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
486             (*out_offset) += rec->size[i];
487         }
488     }
489 }
490
491 static void rec_write_multiple (Records p, int saveCount)
492 {
493     int i;
494     int ref_count = 0;
495     Record last_rec = 0;
496     int out_size = 1000;
497     int out_offset = 0;
498     char *out_buf = (char *) xmalloc (out_size);
499     int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
500     int *sysnop = sysnos;
501
502     for (i = 0; i<p->cache_cur - saveCount; i++)
503     {
504         struct record_cache_entry *e = p->record_cache + i;
505         switch (e->flag)
506         {
507         case recordFlagNew:
508             rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
509                                     &out_size, &out_offset);
510             *sysnop++ = e->rec->sysno;
511             ref_count++;
512             e->flag = recordFlagNop;
513             last_rec = e->rec;
514             break;
515         case recordFlagWrite:
516             rec_release_blocks (p, e->rec->sysno);
517             rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
518                                     &out_size, &out_offset);
519             *sysnop++ = e->rec->sysno;
520             ref_count++;
521             e->flag = recordFlagNop;
522             last_rec = e->rec;
523             break;
524         default:
525             break;
526         }
527     }
528     *sysnop = -1;
529     if (ref_count)
530     {
531         int csize = out_offset + (out_offset >> 6) + 620;
532
533         rec_tmp_expand (p, csize);
534 #if HAVE_BZLIB_H        
535         i = bzBuffToBuffCompress (p->tmp_buf+2*sizeof(int), &csize,
536                                   out_buf, out_offset, 9, 0, 30);
537         if (i != BZ_OK)
538         {
539             logf (LOG_FATAL, "bzBuffToCompress error code=%d", i);
540             exit (1);
541         }
542 #else
543         memcpy (p->tmp_buf + 2*sizeof(int), out_buf, out_offset);
544         csize = out_offset;
545 #endif
546         memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
547         /* -------- compression */
548         rec_write_tmp_buf (p, csize + sizeof(int), sysnos);
549     }
550     xfree (out_buf);
551     xfree (sysnos);
552 }
553
554 static void rec_cache_flush (Records p, int saveCount)
555 {
556     int i, j;
557
558     if (saveCount >= p->cache_cur)
559         saveCount = 0;
560
561     rec_write_multiple (p, saveCount);
562     for (i = 0; i<p->cache_cur - saveCount; i++)
563     {
564         struct record_cache_entry *e = p->record_cache + i;
565         switch (e->flag)
566         {
567         case recordFlagNop:
568             break;
569         case recordFlagNew:
570             rec_write_single (p, e->rec);
571             break;
572         case recordFlagWrite:
573             rec_update_single (p, e->rec);
574             break;
575         case recordFlagDelete:
576             rec_delete_single (p, e->rec);
577             break;
578         }
579         rec_rm (&e->rec);
580     }
581     for (j = 0; j<saveCount; j++, i++)
582         memcpy (p->record_cache+j, p->record_cache+i,
583                 sizeof(*p->record_cache));
584     p->cache_cur = saveCount;
585 }
586
587 static Record *rec_cache_lookup (Records p, int sysno,
588                                  enum recordCacheFlag flag)
589 {
590     int i;
591     for (i = 0; i<p->cache_cur; i++)
592     {
593         struct record_cache_entry *e = p->record_cache + i;
594         if (e->rec->sysno == sysno)
595         {
596             if (flag != recordFlagNop && e->flag == recordFlagNop)
597                 e->flag = flag;
598             return &e->rec;
599         }
600     }
601     return NULL;
602 }
603
604 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
605 {
606     struct record_cache_entry *e;
607
608     if (p->cache_cur == p->cache_max)
609         rec_cache_flush (p, 1);
610     else if (p->cache_cur > 0)
611     {
612         int i, j;
613         int used = 0;
614         for (i = 0; i<p->cache_cur; i++)
615         {
616             Record r = (p->record_cache + i)->rec;
617             for (j = 0; j<REC_NO_INFO; j++)
618                 used += r->size[j];
619         }
620         if (used > 256000)
621             rec_cache_flush (p, 1);
622     }
623     assert (p->cache_cur < p->cache_max);
624
625     e = p->record_cache + (p->cache_cur)++;
626     e->flag = flag;
627     e->rec = rec_cp (rec);
628 }
629
630 void rec_close (Records *pp)
631 {
632     Records p = *pp;
633     int i;
634
635     assert (p);
636
637     rec_cache_flush (p, 0);
638     xfree (p->record_cache);
639
640     if (p->rw)
641         rec_write_head (p);
642
643     if (p->index_BFile)
644         bf_close (p->index_BFile);
645
646     for (i = 0; i<REC_BLOCK_TYPES; i++)
647     {
648         if (p->data_BFile[i])
649             bf_close (p->data_BFile[i]);
650         xfree (p->data_fname[i]);
651     }
652     xfree (p->tmp_buf);
653     xfree (p);
654     *pp = NULL;
655 }
656
657
658 Record rec_get (Records p, int sysno)
659 {
660     int i, in_size;
661     Record rec, *recp;
662     struct record_index_entry entry;
663     int freeblock, dst_type;
664     char *nptr, *cptr;
665     char *in_buf = 0;
666
667     assert (sysno > 0);
668     assert (p);
669
670     if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
671         return rec_cp (*recp);
672
673     if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
674         return NULL;       /* record is not there! */
675
676     if (!entry.size)
677         return NULL;       /* record is deleted */
678
679     dst_type = entry.next & 7;
680     assert (dst_type < REC_BLOCK_TYPES);
681     freeblock = entry.next / 8;
682
683     assert (freeblock > 0);
684     
685     rec = (Record) xmalloc (sizeof(*rec));
686     rec_tmp_expand (p, entry.size);
687
688     cptr = p->tmp_buf;
689     bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
690     memcpy (&freeblock, cptr, sizeof(freeblock));
691
692     while (freeblock)
693     {
694         int tmp;
695
696         cptr += p->head.block_size[dst_type] - sizeof(freeblock);
697         
698         memcpy (&tmp, cptr, sizeof(tmp));
699         bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
700         memcpy (&freeblock, cptr, sizeof(freeblock));
701         memcpy (cptr, &tmp, sizeof(tmp));
702     }
703
704     rec->sysno = sysno;
705 #if HAVE_BZLIB_H
706     in_size = entry.size * 30+100;
707     in_buf = (char *) xmalloc (in_size);
708     i = bzBuffToBuffDecompress (in_buf, &in_size, p->tmp_buf+2*sizeof(int),
709                                 entry.size-sizeof(int), 0, 4);
710     if (i != BZ_OK)
711     {
712         logf (LOG_FATAL, "bzBuffToDecompress error code=%d", i);
713         exit (1);
714     }
715 #else
716     in_buf = p->tmp_buf + 2*sizeof(int);
717     in_size = entry.size - sizeof(int);
718 #endif
719     nptr = in_buf;                /* skip ref count */
720     while (nptr < in_buf + in_size)
721     {
722         int this_sysno;
723         int len;
724         rec_decode_unsigned (&this_sysno, nptr, &len);
725         nptr += len;
726
727         for (i = 0; i < REC_NO_INFO; i++)
728         {
729             int this_size;
730             rec_decode_unsigned (&this_size, nptr, &len);
731             nptr += len;
732
733             if (this_size == 0)
734                 continue;
735             rec->size[i] = this_size-1;
736
737             if (rec->size[i])
738             {
739                 rec->info[i] = (char *) xmalloc (rec->size[i]);
740                 memcpy (rec->info[i], nptr, rec->size[i]);
741                 nptr += rec->size[i];
742             }
743             else
744                 rec->info[i] = NULL;
745         }
746         if (this_sysno == sysno)
747             break;
748     }
749     xfree (in_buf);
750     rec_cache_insert (p, rec, recordFlagNop);
751     return rec;
752 }
753
754 Record rec_new (Records p)
755 {
756     int sysno, i;
757     Record rec;
758
759     assert (p);
760     rec = (Record) xmalloc (sizeof(*rec));
761     if (1 || p->head.index_free == 0)
762         sysno = (p->head.index_last)++;
763     else
764     {
765         struct record_index_entry entry;
766
767         read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
768         sysno = p->head.index_free;
769         p->head.index_free = entry.next;
770     }
771     (p->head.no_records)++;
772     rec->sysno = sysno;
773     for (i = 0; i < REC_NO_INFO; i++)
774     {
775         rec->info[i] = NULL;
776         rec->size[i] = 0;
777     }
778     rec_cache_insert (p, rec, recordFlagNew);
779     return rec;
780 }
781
782 void rec_del (Records p, Record *recpp)
783 {
784     Record *recp;
785
786     (p->head.no_records)--;
787     if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
788     {
789         rec_rm (recp);
790         *recp = *recpp;
791     }
792     else
793     {
794         rec_cache_insert (p, *recpp, recordFlagDelete);
795         rec_rm (recpp);
796     }
797     *recpp = NULL;
798 }
799
800 void rec_put (Records p, Record *recpp)
801 {
802     Record *recp;
803
804     if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
805     {
806         rec_rm (recp);
807         *recp = *recpp;
808     }
809     else
810     {
811         rec_cache_insert (p, *recpp, recordFlagWrite);
812         rec_rm (recpp);
813     }
814     *recpp = NULL;
815 }
816
817 void rec_rm (Record *recpp)
818 {
819     int i;
820
821     if (!*recpp)
822         return ;
823     for (i = 0; i < REC_NO_INFO; i++)
824         xfree ((*recpp)->info[i]);
825     xfree (*recpp);
826     *recpp = NULL;
827 }
828
829 Record rec_cp (Record rec)
830 {
831     Record n;
832     int i;
833
834     n = (Record) xmalloc (sizeof(*n));
835     n->sysno = rec->sysno;
836     for (i = 0; i < REC_NO_INFO; i++)
837         if (!rec->info[i])
838         {
839             n->info[i] = NULL;
840             n->size[i] = 0;
841         }
842         else
843         {
844             n->size[i] = rec->size[i];
845             n->info[i] = (char *) xmalloc (rec->size[i]);
846             memcpy (n->info[i], rec->info[i], rec->size[i]);
847         }
848     return n;
849 }
850
851
852 char *rec_strdup (const char *s, size_t *len)
853 {
854     char *p;
855
856     if (!s)
857     {
858         *len = 0;
859         return NULL;
860     }
861     *len = strlen(s)+1;
862     p = (char *) xmalloc (*len);
863     strcpy (p, s);
864     return p;
865 }
866