2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1997-09-17 12:19:16 adam
8 * Zebra version corresponds to YAZ version 1.4.
9 * Changed Zebra server so that it doesn't depend on global common_resource.
11 * Revision 1.18 1997/07/15 16:28:42 adam
12 * Bug fix: storeData didn't work with files with multiple records.
13 * Bug fix: fixed memory management with records; not really well
16 * Revision 1.17 1997/02/12 20:39:46 adam
17 * Implemented options -f <n> that limits the log to the first <n>
19 * Changed some log messages also.
21 * Revision 1.16 1996/06/04 10:19:00 adam
22 * Minor changes - removed include of ctype.h.
24 * Revision 1.15 1996/05/13 14:23:06 adam
25 * Work on compaction of set/use bytes in dictionary.
27 * Revision 1.14 1996/02/01 20:48:15 adam
28 * The total size of records are always checked in rec_cache_insert to
29 * reduce memory usage.
31 * Revision 1.13 1995/12/11 09:12:49 adam
32 * The rec_get function returns NULL if record doesn't exist - will
33 * happen in the server if the result set records have been deleted since
34 * the creation of the set (i.e. the search).
35 * The server saves a result temporarily if it is 'volatile', i.e. the
36 * set is register dependent.
38 * Revision 1.12 1995/12/07 17:38:47 adam
39 * Work locking mechanisms for concurrent updates/commit.
41 * Revision 1.11 1995/12/06 13:58:26 adam
42 * Improved flushing of records - all flushes except the last one
43 * don't write the last accessed. Also flush takes place if record
44 * info occupy more than about 256k.
46 * Revision 1.10 1995/12/06 12:41:24 adam
47 * New command 'stat' for the index program.
48 * Filenames can be read from stdin by specifying '-'.
49 * Bug fix/enhancement of the transformation from terms to regular
50 * expressons in the search engine.
52 * Revision 1.9 1995/11/30 08:34:33 adam
53 * Started work on commit facility.
54 * Changed a few malloc/free to xmalloc/xfree.
56 * Revision 1.8 1995/11/28 14:26:21 adam
57 * Bug fix: recordId with constant wasn't right.
58 * Bug fix: recordId dictionary entry wasn't deleted when needed.
60 * Revision 1.7 1995/11/28 09:09:43 adam
61 * Zebra config renamed.
62 * Use setting 'recordId' to identify record now.
63 * Bug fix in recindex.c: rec_release_blocks was invokeded even
64 * though the blocks were already released.
65 * File traversal properly deletes records when needed.
67 * Revision 1.6 1995/11/25 10:24:06 adam
68 * More record fields - they are enumerated now.
69 * New options: flagStoreData flagStoreKey.
71 * Revision 1.5 1995/11/22 17:19:18 adam
72 * Record management uses the bfile system.
74 * Revision 1.4 1995/11/20 16:59:46 adam
75 * New update method: the 'old' keys are saved for each records.
77 * Revision 1.3 1995/11/16 15:34:55 adam
78 * Uses new record management system in both indexer and server.
80 * Revision 1.2 1995/11/15 19:13:08 adam
81 * Work on record management.
83 * Revision 1.1 1995/11/15 14:46:20 adam
84 * Started work on better record management system.
93 static void rec_write_head (Records p)
98 assert (p->index_BFile);
100 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
103 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
108 static void rec_tmp_expand (Records p, int size, int dst_type)
110 if (p->tmp_size < size + 2048 ||
111 p->tmp_size < p->head.block_size[dst_type]*2)
114 p->tmp_size = size + p->head.block_size[dst_type]*2 + 2048;
115 p->tmp_buf = xmalloc (p->tmp_size);
119 static int read_indx (Records p, int sysno, void *buf, int itemsize,
123 int pos = (sysno-1)*itemsize;
125 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
126 if (r != 1 && !ignoreError)
128 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
129 p->index_fname, (long) pos);
136 static void write_indx (Records p, int sysno, void *buf, int itemsize)
138 int pos = (sysno-1)*itemsize;
140 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
143 static void rec_release_blocks (Records p, int sysno)
145 struct record_index_entry entry;
146 int freeblock, freenext;
149 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
151 p->head.total_bytes -= entry.size;
152 freeblock = entry.next;
153 assert (freeblock > 0);
154 dst_type = freeblock & 7;
155 assert (dst_type < REC_BLOCK_TYPES);
156 freeblock = freeblock / 8;
159 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
162 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
165 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
166 &p->head.block_free[dst_type]))
168 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
171 p->head.block_free[dst_type] = freeblock;
172 freeblock = freenext;
173 p->head.block_used[dst_type]--;
177 static void rec_delete_single (Records p, Record rec)
179 struct record_index_entry entry;
181 rec_release_blocks (p, rec->sysno);
183 entry.next = p->head.index_free;
185 p->head.index_free = rec->sysno;
186 write_indx (p, rec->sysno, &entry, sizeof(entry));
190 static void rec_write_single (Records p, Record rec)
196 int block_prev = -1, block_free;
197 struct record_index_entry entry;
199 for (i = 0; i < REC_NO_INFO; i++)
201 size += sizeof(*rec->size);
203 size += sizeof(*rec->size) + rec->size[i];
205 for (i = 1; i<REC_BLOCK_TYPES; i++)
206 if (size >= p->head.block_move[i])
209 rec_tmp_expand (p, size, dst_type);
211 cptr = p->tmp_buf + sizeof(int); /* a hack! */
212 for (i = 0; i < REC_NO_INFO; i++)
214 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
215 cptr += sizeof(*rec->size);
218 memcpy (cptr, rec->info[i], rec->size[i]);
219 cptr += rec->size[i];
223 while (no_written < size)
225 block_free = p->head.block_free[dst_type];
228 if (bf_read (p->data_BFile[dst_type],
229 block_free, 0, sizeof(*p->head.block_free),
230 &p->head.block_free[dst_type]) != 1)
232 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
233 p->data_fname[dst_type], block_free);
237 block_free = p->head.block_last[dst_type]++;
238 if (block_prev == -1)
240 entry.next = block_free*8 + dst_type;
242 p->head.total_bytes += size;
243 write_indx (p, rec->sysno, &entry, sizeof(entry));
247 memcpy (cptr, &block_free, sizeof(int));
248 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
249 cptr = p->tmp_buf + no_written;
251 block_prev = block_free;
252 no_written += p->head.block_size[dst_type] - sizeof(int);
253 p->head.block_used[dst_type]++;
255 assert (block_prev != -1);
257 memcpy (cptr, &block_free, sizeof(int));
258 bf_write (p->data_BFile[dst_type], block_prev, 0,
259 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
262 static void rec_update_single (Records p, Record rec)
264 rec_release_blocks (p, rec->sysno);
265 rec_write_single (p, rec);
268 Records rec_open (BFiles bfs, int rw)
273 p = xmalloc (sizeof(*p));
276 p->tmp_buf = xmalloc (p->tmp_size);
277 p->index_fname = "recindex";
278 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
279 if (p->index_BFile == NULL)
281 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
284 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
288 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
289 p->head.index_free = 0;
290 p->head.index_last = 1;
291 p->head.no_records = 0;
292 p->head.total_bytes = 0;
293 for (i = 0; i<REC_BLOCK_TYPES; i++)
295 p->head.block_free[i] = 0;
296 p->head.block_last[i] = 1;
297 p->head.block_used[i] = 0;
299 p->head.block_size[0] = 128;
300 p->head.block_move[0] = 0;
301 for (i = 1; i<REC_BLOCK_TYPES; i++)
303 p->head.block_size[i] = p->head.block_size[i-1] * 4;
304 p->head.block_move[i] = p->head.block_size[i] * 3;
310 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
311 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
313 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
318 for (i = 0; i<REC_BLOCK_TYPES; i++)
321 sprintf (str, "recdata%c", i + 'A');
322 p->data_fname[i] = xmalloc (strlen(str)+1);
323 strcpy (p->data_fname[i], str);
324 p->data_BFile[i] = NULL;
326 for (i = 0; i<REC_BLOCK_TYPES; i++)
328 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
329 p->head.block_size[i],
332 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
338 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
342 static void rec_cache_flush (Records p, int saveCount)
346 if (saveCount >= p->cache_cur)
348 for (i = 0; i<p->cache_cur - saveCount; i++)
350 struct record_cache_entry *e = p->record_cache + i;
356 rec_write_single (p, e->rec);
358 case recordFlagWrite:
359 rec_update_single (p, e->rec);
361 case recordFlagDelete:
362 rec_delete_single (p, e->rec);
367 for (j = 0; j<saveCount; j++, i++)
368 memcpy (p->record_cache+j, p->record_cache+i,
369 sizeof(*p->record_cache));
370 p->cache_cur = saveCount;
373 static Record *rec_cache_lookup (Records p, int sysno,
374 enum recordCacheFlag flag)
377 for (i = 0; i<p->cache_cur; i++)
379 struct record_cache_entry *e = p->record_cache + i;
380 if (e->rec->sysno == sysno)
382 if (flag != recordFlagNop && e->flag == recordFlagNop)
390 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
392 struct record_cache_entry *e;
394 if (p->cache_cur == p->cache_max)
395 rec_cache_flush (p, 1);
396 else if (p->cache_cur > 0)
400 for (i = 0; i<p->cache_cur; i++)
402 Record r = (p->record_cache + i)->rec;
403 for (j = 0; j<REC_NO_INFO; j++)
407 rec_cache_flush (p, 1);
409 assert (p->cache_cur < p->cache_max);
411 e = p->record_cache + (p->cache_cur)++;
413 e->rec = rec_cp (rec);
416 void rec_close (Records *pp)
423 rec_cache_flush (p, 0);
424 xfree (p->record_cache);
430 bf_close (p->index_BFile);
432 for (i = 0; i<REC_BLOCK_TYPES; i++)
434 if (p->data_BFile[i])
435 bf_close (p->data_BFile[i]);
436 xfree (p->data_fname[i]);
444 Record rec_get (Records p, int sysno)
448 struct record_index_entry entry;
449 int freeblock, dst_type;
455 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
456 return rec_cp (*recp);
458 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
459 return NULL; /* record is not there! */
462 return NULL; /* record is deleted */
464 dst_type = entry.next & 7;
465 assert (dst_type < REC_BLOCK_TYPES);
466 freeblock = entry.next / 8;
468 assert (freeblock > 0);
470 rec = xmalloc (sizeof(*rec));
471 rec_tmp_expand (p, entry.size, dst_type);
474 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
475 memcpy (&freeblock, cptr, sizeof(freeblock));
481 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
483 memcpy (&tmp, cptr, sizeof(tmp));
484 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
485 memcpy (&freeblock, cptr, sizeof(freeblock));
486 memcpy (cptr, &tmp, sizeof(tmp));
490 nptr = p->tmp_buf + sizeof(freeblock);
491 for (i = 0; i < REC_NO_INFO; i++)
493 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
494 nptr += sizeof(*rec->size);
497 rec->info[i] = xmalloc (rec->size[i]);
498 memcpy (rec->info[i], nptr, rec->size[i]);
499 nptr += rec->size[i];
504 rec_cache_insert (p, rec, recordFlagNop);
508 Record rec_new (Records p)
514 rec = xmalloc (sizeof(*rec));
515 if (1 || p->head.index_free == 0)
516 sysno = (p->head.index_last)++;
519 struct record_index_entry entry;
521 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
522 sysno = p->head.index_free;
523 p->head.index_free = entry.next;
525 (p->head.no_records)++;
527 for (i = 0; i < REC_NO_INFO; i++)
532 rec_cache_insert (p, rec, recordFlagNew);
536 void rec_del (Records p, Record *recpp)
540 (p->head.no_records)--;
541 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
548 rec_cache_insert (p, *recpp, recordFlagDelete);
554 void rec_put (Records p, Record *recpp)
558 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
565 rec_cache_insert (p, *recpp, recordFlagWrite);
571 void rec_rm (Record *recpp)
577 for (i = 0; i < REC_NO_INFO; i++)
578 xfree ((*recpp)->info[i]);
583 Record rec_cp (Record rec)
588 n = xmalloc (sizeof(*n));
589 n->sysno = rec->sysno;
590 for (i = 0; i < REC_NO_INFO; i++)
598 n->size[i] = rec->size[i];
599 n->info[i] = xmalloc (rec->size[i]);
600 memcpy (n->info[i], rec->info[i], rec->size[i]);
606 char *rec_strdup (const char *s, size_t *len)