2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1997-09-17 12:19:20 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.22 1996/10/29 13:56:53 adam
12 * Include of zebrautl.h instead of alexutil.h.
14 * Revision 1.21 1996/03/29 14:11:47 quinn
17 * Revision 1.20 1996/03/19 13:14:57 quinn
20 * Revision 1.19 1996/02/10 12:20:56 quinn
21 * *** empty log message ***
23 * Revision 1.18 1996/02/06 10:19:56 quinn
24 * Attempt at fixing bug. Not all blocks were read before they were unlinked
25 * prior to a remap operation.
27 * Revision 1.17 1995/12/06 15:48:44 quinn
28 * Fixed update-problem.
30 * Revision 1.16 1995/12/06 14:48:26 quinn
31 * Fixed some strange bugs.
33 * Revision 1.15 1995/12/06 09:59:45 quinn
34 * Fixed memory-consumption bug in memory.c
35 * Added more blocksizes to the default ISAM configuration.
37 * Revision 1.14 1995/11/24 17:26:19 quinn
38 * Mostly about making some ISAM stuff in the config file optional.
40 * Revision 1.13 1995/10/17 18:03:15 adam
41 * Commented out qsort in is_merge.
43 * Revision 1.12 1995/09/06 16:11:41 adam
44 * Keysize parameter to is_open (if non-zero).
46 * Revision 1.11 1995/09/04 12:33:46 adam
47 * Various cleanup. YAZ util used instead.
49 * Revision 1.10 1994/09/28 16:58:32 quinn
52 * Revision 1.9 1994/09/28 12:56:15 quinn
53 * Added access functions (ISPT)
55 * Revision 1.8 1994/09/28 12:32:17 quinn
58 * Revision 1.7 1994/09/28 11:56:25 quinn
59 * Added sort of input to is_merge
61 * Revision 1.6 1994/09/28 11:29:33 quinn
62 * Added cmp parameter.
64 * Revision 1.5 1994/09/27 20:03:50 quinn
65 * Seems relatively bug-free.
67 * Revision 1.4 1994/09/26 17:11:29 quinn
70 * Revision 1.3 1994/09/26 17:06:35 quinn
73 * Revision 1.1 1994/09/12 08:02:13 quinn
90 static ispt_struct *ispt_freelist = 0;
94 int total_merge_operations;
96 int dub_items_removed;
100 int delete_insert_noop;
109 static ISPT ispt_alloc()
116 ispt_freelist = ispt_freelist->next;
119 p = xmalloc(sizeof(ispt_struct));
123 static void ispt_free(ISPT pt)
125 pt->next = ispt_freelist;
129 static int splitargs(const char *s, char *bf[], int max)
134 while (*s && isspace(*s))
142 logf (LOG_WARN, "Ignoring extra args to is resource");
146 while (*s && !isspace(*s))
155 ISAM is_open(BFiles bfs, const char *name,
156 int (*cmp)(const void *p1, const void *p2),
157 int writeflag, int keysize, Res res)
160 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
161 int num, size, rs, tmp, i;
164 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
167 statistics.total_merge_operations = 0;
168 statistics.total_items = 0;
169 statistics.dub_items_removed = 0;
170 statistics.new_items = 0;
171 statistics.failed_deletes = 0;
172 statistics.skipped_inserts = 0;
173 statistics.delete_insert_noop = 0;
174 statistics.delete_replace = 0;
175 statistics.delete = 0;
176 statistics.remaps = 0;
177 statistics.new_tables = 0;
178 statistics.block_jumps = 0;
179 statistics.tab_deletes = 0;
182 new = xmalloc(sizeof(*new));
183 new->writeflag = writeflag;
184 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
185 new->types[i].index = 0; /* dummy */
187 /* determine number and size of blocktypes */
188 if (!(r = res_get_def(res,
189 nm = strconcat(name, ".",
190 "blocktypes", 0), "64 512 4K 32K")) ||
191 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
193 logf (LOG_FATAL, "Failed to locate resource %s", nm);
196 new->num_types = num;
197 for (i = 0; i < num; i++)
199 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
201 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
209 new->types[i].blocksize = size; break;
211 new->types[i].blocksize = size * 1024; break;
213 new->types[i].blocksize = size * 1048576; break;
215 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
218 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
221 if (!(new->types[i].bf = bf_open(bfs, strconcat(name, m, 0),
222 new->types[i].blocksize, writeflag)))
224 logf (LOG_FATAL, "bf_open failed");
227 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
229 if (th.blocksize != new->types[i].blocksize)
231 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
234 new->types[i].freelist = th.freelist;
235 new->types[i].top = th.top;
237 else if (writeflag) /* write dummy superblock to determine top */
239 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
241 logf (LOG_FATAL, "Failed to write initial superblock.");
244 new->types[i].freelist = -1;
245 new->types[i].top = rs;
247 /* ELSE: this is an empty file opened in read-only mode. */
250 new->keysize = keysize;
253 if (!(r = res_get_def(res, nm = strconcat(name, ".",
254 "keysize", 0), "4")))
256 logf (LOG_FATAL, "Failed to locate resource %s", nm);
259 if ((new->keysize = atoi(r)) <= 0)
261 logf (LOG_FATAL, "Must specify positive keysize.");
266 /* determine repack percent */
267 if (!(r = res_get_def(res, nm = strconcat(name, ".", "repack",
268 0), IS_DEF_REPACK_PERCENT)))
270 logf (LOG_FATAL, "Failed to locate resource %s", nm);
273 new->repack = atoi(r);
275 /* determine max keys/blocksize */
276 if (!(r = res_get_def(res,
277 nm = strconcat(name, ".",
278 "maxkeys", 0), "50 640 10000")) ||
279 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
281 logf (LOG_FATAL, "Failed to locate resource %s", nm);
284 if (num < new->num_types -1)
286 logf (LOG_FATAL, "Not enough elements in %s", nm);
289 for (i = 0; i < num; i++)
291 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
293 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
296 new->types[i].max_keys = tmp;
299 /* determine max keys/block */
300 for (i = 0; i < new->num_types; i++)
302 if (!new->types[i].index)
304 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
305 sizeof(int)) / new->keysize;
306 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
307 sizeof(int)) / new->keysize;
310 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
312 if (new->types[i].max_keys_block0 < 1)
314 logf (LOG_FATAL, "Blocksize too small in %s", name);
319 /* determine nice fill rates */
320 if (!(r = res_get_def(res,
321 nm = strconcat(name, ".",
322 "nicefill", 0), "90 90 90 95")) ||
323 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
325 logf (LOG_FATAL, "Failed to locate resource %s", nm);
328 if (num < new->num_types)
330 logf (LOG_FATAL, "Not enough elements in %s", nm);
333 for (i = 0; i < num; i++)
335 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
337 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
340 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
342 if (new->types[i].nice_keys_block < 1)
343 new->types[i].nice_keys_block = 1;
346 new->cmp = cmp ? cmp : is_default_cmp;
353 int is_close(ISAM is)
358 logf (LOG_DEBUG, "is_close()");
359 for (i = 0; i < is->num_types; i++)
365 th.blocksize = is->types[i].blocksize;
366 th.keysize = is->keysize;
367 th.freelist = is->types[i].freelist;
368 th.top = is->types[i].top;
369 if (is_rb_write(&is->types[i], &th) < 0)
371 logf (LOG_FATAL, "Failed to write headerblock");
375 bf_close(is->types[i].bf);
380 logf(LOG_LOG, "ISAM statistics:");
381 logf(LOG_LOG, "total_merge_operations %d",
382 statistics.total_merge_operations);
383 logf(LOG_LOG, "total_items %d", statistics.total_items);
384 logf(LOG_LOG, "dub_items_removed %d",
385 statistics.dub_items_removed);
386 logf(LOG_LOG, "new_items %d", statistics.new_items);
387 logf(LOG_LOG, "failed_deletes %d",
388 statistics.failed_deletes);
389 logf(LOG_LOG, "skipped_inserts %d",
390 statistics.skipped_inserts);
391 logf(LOG_LOG, "delete_insert_noop %d",
392 statistics.delete_insert_noop);
393 logf(LOG_LOG, "delete_replace %d",
394 statistics.delete_replace);
395 logf(LOG_LOG, "delete %d", statistics.delete);
396 logf(LOG_LOG, "remaps %d", statistics.remaps);
397 logf(LOG_LOG, "block_jumps %d", statistics.block_jumps);
398 logf(LOG_LOG, "tab_deletes %d", statistics.tab_deletes);
404 static ISAM_P is_address(int type, int pos)
413 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
417 char keybuf[IS_MAX_RECORD];
418 int oldnum, oldtype, i;
419 char operation, *record;
421 statistics.total_merge_operations++;
422 statistics.total_items += num;
424 statistics.new_tables++;
426 is_m_establish_tab(is, &tab, pos);
428 if (is_m_read_full(&tab, tab.data) < 0)
430 logf (LOG_FATAL, "read_full failed");
433 oldnum = tab.num_records;
434 oldtype = tab.pos_type;
437 operation = *(data)++;
438 record = (char*) data;
439 data += is_keysize(is);
441 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
443 data += 1 + is_keysize(is);
445 statistics.dub_items_removed++;
447 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
449 if (operation == KEYOP_INSERT)
451 logf (LOG_DEBUG, "XXInserting new record.");
452 is_m_write_record(&tab, record);
453 statistics.new_items++;
457 logf (LOG_DEBUG, "XXDeletion failed to find match.");
458 statistics.failed_deletes++;
461 else /* match found */
463 if (operation == KEYOP_INSERT)
465 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
466 statistics.skipped_inserts++;
469 else if (operation == KEYOP_DELETE)
471 /* try to avoid needlessly moving data */
472 if (num && *(data) == KEYOP_INSERT)
474 /* next key is identical insert? - NOOP - skip it */
475 if (!memcmp(record, data + 1, is_keysize(is)))
477 logf (LOG_DEBUG, "XXNoop delete. skipping.");
478 data += 1 + is_keysize(is);
480 while (num && !memcmp(data, data + is_keysize(tab.is) +
481 1, is_keysize(tab.is) + 1))
483 data += 1 + is_keysize(is);
485 statistics.dub_items_removed++;
487 statistics.delete_insert_noop++;
490 /* else check if next key can fit in this position */
491 if (is_m_peek_record(&tab, keybuf) &&
492 (*is->cmp)(data + 1, keybuf) < 0)
494 logf (LOG_DEBUG, "XXReplacing record.");
495 is_m_replace_record(&tab, data + 1);
496 data += 1 + is_keysize(is);
498 while (num && !memcmp(data, data + is_keysize(tab.is) +
499 1, is_keysize(tab.is) + 1))
501 data += 1 + is_keysize(is);
503 statistics.dub_items_removed++;
505 statistics.delete_replace++;
509 logf (LOG_DEBUG, "Deleting record.");
510 is_m_delete_record(&tab);
516 while (i < tab.is->num_types - 1 && tab.num_records >
517 tab.is->types[i].max_keys)
519 if (i != tab.pos_type)
521 /* read remaining blocks */
522 for (; tab.cur_mblock; tab.cur_mblock = tab.cur_mblock->next)
523 if (tab.cur_mblock->state < IS_MBSTATE_CLEAN)
524 is_m_read_full(&tab, tab.cur_mblock);
528 statistics.block_jumps++;
530 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
531 100) / oldnum > tab.is->repack)
541 pos = is_address(tab.pos_type, tab.data->diskpos);
546 statistics.tab_deletes++;
548 is_m_release_tab(&tab);
553 * Locate a table of keys in an isam file. The ISPT is an individual
554 * position marker for that table.
556 ISPT is_position(ISAM is, ISAM_P pos)
561 is_m_establish_tab(is, &p->tab, pos);
568 void is_pt_free(ISPT ip)
570 is_m_release_tab(&ip->tab);
575 * Read a key from a table.
577 int is_readkey(ISPT ip, void *buf)
579 return is_m_read_record(&ip->tab, buf, 0);
582 int is_numkeys(ISPT ip)
584 return is_m_num_records(&ip->tab);
587 void is_rewind(ISPT ip)
589 is_m_rewind(&ip->tab);