2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1996-02-10 12:20:56 quinn
8 * *** empty log message ***
10 * Revision 1.18 1996/02/06 10:19:56 quinn
11 * Attempt at fixing bug. Not all blocks were read before they were unlinked
12 * prior to a remap operation.
14 * Revision 1.17 1995/12/06 15:48:44 quinn
15 * Fixed update-problem.
17 * Revision 1.16 1995/12/06 14:48:26 quinn
18 * Fixed some strange bugs.
20 * Revision 1.15 1995/12/06 09:59:45 quinn
21 * Fixed memory-consumption bug in memory.c
22 * Added more blocksizes to the default ISAM configuration.
24 * Revision 1.14 1995/11/24 17:26:19 quinn
25 * Mostly about making some ISAM stuff in the config file optional.
27 * Revision 1.13 1995/10/17 18:03:15 adam
28 * Commented out qsort in is_merge.
30 * Revision 1.12 1995/09/06 16:11:41 adam
31 * Keysize parameter to is_open (if non-zero).
33 * Revision 1.11 1995/09/04 12:33:46 adam
34 * Various cleanup. YAZ util used instead.
36 * Revision 1.10 1994/09/28 16:58:32 quinn
39 * Revision 1.9 1994/09/28 12:56:15 quinn
40 * Added access functions (ISPT)
42 * Revision 1.8 1994/09/28 12:32:17 quinn
45 * Revision 1.7 1994/09/28 11:56:25 quinn
46 * Added sort of input to is_merge
48 * Revision 1.6 1994/09/28 11:29:33 quinn
49 * Added cmp parameter.
51 * Revision 1.5 1994/09/27 20:03:50 quinn
52 * Seems relatively bug-free.
54 * Revision 1.4 1994/09/26 17:11:29 quinn
57 * Revision 1.3 1994/09/26 17:06:35 quinn
60 * Revision 1.1 1994/09/12 08:02:13 quinn
78 static ispt_struct *ispt_freelist = 0;
82 int total_merge_operations;
84 int dub_items_removed;
88 int delete_insert_noop;
97 static ISPT ispt_alloc()
104 ispt_freelist = ispt_freelist->next;
107 p = xmalloc(sizeof(ispt_struct));
111 static void ispt_free(ISPT pt)
113 pt->next = ispt_freelist;
117 static int splitargs(const char *s, char *bf[], int max)
122 while (*s && isspace(*s))
130 logf (LOG_WARN, "Ignoring extra args to is resource");
134 while (*s && !isspace(*s))
143 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
144 int writeflag, int keysize)
147 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
148 int num, size, rs, tmp, i;
151 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
154 statistics.total_merge_operations = 0;
155 statistics.total_items = 0;
156 statistics.dub_items_removed = 0;
157 statistics.new_items = 0;
158 statistics.failed_deletes = 0;
159 statistics.skipped_inserts = 0;
160 statistics.delete_insert_noop = 0;
161 statistics.delete_replace = 0;
162 statistics.delete = 0;
163 statistics.remaps = 0;
164 statistics.new_tables = 0;
165 statistics.block_jumps = 0;
166 statistics.tab_deletes = 0;
169 new = xmalloc(sizeof(*new));
170 new->writeflag = writeflag;
171 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
172 new->types[i].index = 0; /* dummy */
174 /* determine number and size of blocktypes */
175 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
176 "blocktypes", 0), "64 512 4K 32K")) ||
177 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
179 logf (LOG_FATAL, "Failed to locate resource %s", nm);
182 new->num_types = num;
183 for (i = 0; i < num; i++)
185 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
187 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
195 new->types[i].blocksize = size; break;
197 new->types[i].blocksize = size * 1024; break;
199 new->types[i].blocksize = size * 1048576; break;
201 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
204 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
207 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
208 new->types[i].blocksize, writeflag)))
210 logf (LOG_FATAL, "bf_open failed");
213 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
215 if (th.blocksize != new->types[i].blocksize)
217 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
220 new->types[i].freelist = th.freelist;
221 new->types[i].top = th.top;
223 else if (writeflag) /* write dummy superblock to determine top */
225 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
227 logf (LOG_FATAL, "Failed to write initial superblock.");
230 new->types[i].freelist = -1;
231 new->types[i].top = rs;
233 /* ELSE: this is an empty file opened in read-only mode. */
236 new->keysize = keysize;
239 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
243 logf (LOG_FATAL, "Failed to locate resource %s", nm);
246 if ((new->keysize = atoi(r)) <= 0)
248 logf (LOG_FATAL, "Must specify positive keysize.");
253 /* determine repack percent */
254 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
255 0), IS_DEF_REPACK_PERCENT)))
257 logf (LOG_FATAL, "Failed to locate resource %s", nm);
260 new->repack = atoi(r);
262 /* determine max keys/blocksize */
263 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
264 "maxkeys", 0), "50 640 10000")) || !(num = splitargs(r, pp,
267 logf (LOG_FATAL, "Failed to locate resource %s", nm);
270 if (num < new->num_types -1)
272 logf (LOG_FATAL, "Not enough elements in %s", nm);
275 for (i = 0; i < num; i++)
277 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
279 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
282 new->types[i].max_keys = tmp;
285 /* determine max keys/block */
286 for (i = 0; i < new->num_types; i++)
288 if (!new->types[i].index)
290 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
291 sizeof(int)) / new->keysize;
292 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
293 sizeof(int)) / new->keysize;
296 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
298 if (new->types[i].max_keys_block0 < 1)
300 logf (LOG_FATAL, "Blocksize too small in %s", name);
305 /* determine nice fill rates */
306 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
307 "nicefill", 0), "90 90 90 95")) || !(num = splitargs(r, pp,
310 logf (LOG_FATAL, "Failed to locate resource %s", nm);
313 if (num < new->num_types)
315 logf (LOG_FATAL, "Not enough elements in %s", nm);
318 for (i = 0; i < num; i++)
320 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
322 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
325 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
327 if (new->types[i].nice_keys_block < 1)
328 new->types[i].nice_keys_block = 1;
331 new->cmp = cmp ? cmp : is_default_cmp;
338 int is_close(ISAM is)
343 logf (LOG_DEBUG, "is_close()");
344 for (i = 0; i < is->num_types; i++)
350 th.blocksize = is->types[i].blocksize;
351 th.keysize = is->keysize;
352 th.freelist = is->types[i].freelist;
353 th.top = is->types[i].top;
354 if (is_rb_write(&is->types[i], &th) < 0)
356 logf (LOG_FATAL, "Failed to write headerblock");
360 bf_close(is->types[i].bf);
366 logf(LOG_LOG, "ISAM statistics:");
367 logf(LOG_LOG, "total_merge_operations %d",
368 statistics.total_merge_operations);
369 logf(LOG_LOG, "total_items %d", statistics.total_items);
370 logf(LOG_LOG, "dub_items_removed %d",
371 statistics.dub_items_removed);
372 logf(LOG_LOG, "new_items %d", statistics.new_items);
373 logf(LOG_LOG, "failed_deletes %d",
374 statistics.failed_deletes);
375 logf(LOG_LOG, "skipped_inserts %d",
376 statistics.skipped_inserts);
377 logf(LOG_LOG, "delete_insert_noop %d",
378 statistics.delete_insert_noop);
379 logf(LOG_LOG, "delete_replace %d",
380 statistics.delete_replace);
381 logf(LOG_LOG, "delete %d", statistics.delete);
382 logf(LOG_LOG, "remaps %d", statistics.remaps);
383 logf(LOG_LOG, "block_jumps %d", statistics.block_jumps);
384 logf(LOG_LOG, "tab_deletes %d", statistics.tab_deletes);
389 static ISAM_P is_address(int type, int pos)
398 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
402 char keybuf[IS_MAX_RECORD];
403 int oldnum, oldtype, i;
404 char operation, *record;
406 statistics.total_merge_operations++;
407 statistics.total_items += num;
409 statistics.new_tables++;
411 is_m_establish_tab(is, &tab, pos);
413 if (is_m_read_full(&tab, tab.data) < 0)
415 logf (LOG_FATAL, "read_full failed");
418 oldnum = tab.num_records;
419 oldtype = tab.pos_type;
422 operation = *(data)++;
423 record = (char*) data;
424 data += is_keysize(is);
426 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
428 data += 1 + is_keysize(is);
430 statistics.dub_items_removed++;
432 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
434 if (operation == KEYOP_INSERT)
436 logf (LOG_DEBUG, "XXInserting new record.");
437 is_m_write_record(&tab, record);
438 statistics.new_items++;
442 logf (LOG_DEBUG, "XXDeletion failed to find match.");
443 statistics.failed_deletes++;
446 else /* match found */
448 if (operation == KEYOP_INSERT)
450 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
451 statistics.skipped_inserts++;
454 else if (operation == KEYOP_DELETE)
456 /* try to avoid needlessly moving data */
457 if (num && *(data) == KEYOP_INSERT)
459 /* next key is identical insert? - NOOP - skip it */
460 if (!memcmp(record, data + 1, is_keysize(is)))
462 logf (LOG_DEBUG, "XXNoop delete. skipping.");
463 data += 1 + is_keysize(is);
465 while (num && !memcmp(data, data + is_keysize(tab.is) +
466 1, is_keysize(tab.is) + 1))
468 data += 1 + is_keysize(is);
470 statistics.dub_items_removed++;
472 statistics.delete_insert_noop++;
475 /* else check if next key can fit in this position */
476 is_m_peek_record(&tab, keybuf);
477 res = (*is->cmp)(data + 1, keybuf);
480 logf (LOG_DEBUG, "XXReplacing record.");
481 is_m_replace_record(&tab, data + 1);
482 data += 1 + is_keysize(is);
484 while (num && !memcmp(data, data + is_keysize(tab.is) +
485 1, is_keysize(tab.is) + 1))
487 data += 1 + is_keysize(is);
489 statistics.dub_items_removed++;
491 statistics.delete_replace++;
495 logf (LOG_DEBUG, "Deleting record.");
496 is_m_delete_record(&tab);
502 while (i < tab.is->num_types - 1 && tab.num_records >
503 tab.is->types[i].max_keys)
505 if (i != tab.pos_type)
507 /* read remaining blocks */
508 for (; tab.cur_mblock; tab.cur_mblock = tab.cur_mblock->next)
509 if (tab.cur_mblock->state < IS_MBSTATE_CLEAN)
510 is_m_read_full(&tab, tab.cur_mblock);
514 statistics.block_jumps++;
516 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
517 100) / oldnum > tab.is->repack)
527 pos = is_address(tab.pos_type, tab.data->diskpos);
532 statistics.tab_deletes++;
534 is_m_release_tab(&tab);
539 * Locate a table of keys in an isam file. The ISPT is an individual
540 * position marker for that table.
542 ISPT is_position(ISAM is, ISAM_P pos)
547 is_m_establish_tab(is, &p->tab, pos);
554 void is_pt_free(ISPT ip)
556 is_m_release_tab(&ip->tab);
561 * Read a key from a table.
563 int is_readkey(ISPT ip, void *buf)
565 return is_m_read_record(&ip->tab, buf, 0);
568 int is_numkeys(ISPT ip)
570 return is_m_num_records(&ip->tab);
573 void is_rewind(ISPT ip)
575 is_m_rewind(&ip->tab);