2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1996-03-19 13:14:57 quinn
10 * Revision 1.19 1996/02/10 12:20:56 quinn
11 * *** empty log message ***
13 * Revision 1.18 1996/02/06 10:19:56 quinn
14 * Attempt at fixing bug. Not all blocks were read before they were unlinked
15 * prior to a remap operation.
17 * Revision 1.17 1995/12/06 15:48:44 quinn
18 * Fixed update-problem.
20 * Revision 1.16 1995/12/06 14:48:26 quinn
21 * Fixed some strange bugs.
23 * Revision 1.15 1995/12/06 09:59:45 quinn
24 * Fixed memory-consumption bug in memory.c
25 * Added more blocksizes to the default ISAM configuration.
27 * Revision 1.14 1995/11/24 17:26:19 quinn
28 * Mostly about making some ISAM stuff in the config file optional.
30 * Revision 1.13 1995/10/17 18:03:15 adam
31 * Commented out qsort in is_merge.
33 * Revision 1.12 1995/09/06 16:11:41 adam
34 * Keysize parameter to is_open (if non-zero).
36 * Revision 1.11 1995/09/04 12:33:46 adam
37 * Various cleanup. YAZ util used instead.
39 * Revision 1.10 1994/09/28 16:58:32 quinn
42 * Revision 1.9 1994/09/28 12:56:15 quinn
43 * Added access functions (ISPT)
45 * Revision 1.8 1994/09/28 12:32:17 quinn
48 * Revision 1.7 1994/09/28 11:56:25 quinn
49 * Added sort of input to is_merge
51 * Revision 1.6 1994/09/28 11:29:33 quinn
52 * Added cmp parameter.
54 * Revision 1.5 1994/09/27 20:03:50 quinn
55 * Seems relatively bug-free.
57 * Revision 1.4 1994/09/26 17:11:29 quinn
60 * Revision 1.3 1994/09/26 17:06:35 quinn
63 * Revision 1.1 1994/09/12 08:02:13 quinn
81 static ispt_struct *ispt_freelist = 0;
85 int total_merge_operations;
87 int dub_items_removed;
91 int delete_insert_noop;
100 static ISPT ispt_alloc()
107 ispt_freelist = ispt_freelist->next;
110 p = xmalloc(sizeof(ispt_struct));
114 static void ispt_free(ISPT pt)
116 pt->next = ispt_freelist;
120 static int splitargs(const char *s, char *bf[], int max)
125 while (*s && isspace(*s))
133 logf (LOG_WARN, "Ignoring extra args to is resource");
137 while (*s && !isspace(*s))
146 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
147 int writeflag, int keysize)
150 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
151 int num, size, rs, tmp, i;
154 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
157 statistics.total_merge_operations = 0;
158 statistics.total_items = 0;
159 statistics.dub_items_removed = 0;
160 statistics.new_items = 0;
161 statistics.failed_deletes = 0;
162 statistics.skipped_inserts = 0;
163 statistics.delete_insert_noop = 0;
164 statistics.delete_replace = 0;
165 statistics.delete = 0;
166 statistics.remaps = 0;
167 statistics.new_tables = 0;
168 statistics.block_jumps = 0;
169 statistics.tab_deletes = 0;
172 new = xmalloc(sizeof(*new));
173 new->writeflag = writeflag;
174 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
175 new->types[i].index = 0; /* dummy */
177 /* determine number and size of blocktypes */
178 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
179 "blocktypes", 0), "64 512 4K 32K")) ||
180 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
182 logf (LOG_FATAL, "Failed to locate resource %s", nm);
185 new->num_types = num;
186 for (i = 0; i < num; i++)
188 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
190 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
198 new->types[i].blocksize = size; break;
200 new->types[i].blocksize = size * 1024; break;
202 new->types[i].blocksize = size * 1048576; break;
204 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
207 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
210 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
211 new->types[i].blocksize, writeflag)))
213 logf (LOG_FATAL, "bf_open failed");
216 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
218 if (th.blocksize != new->types[i].blocksize)
220 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
223 new->types[i].freelist = th.freelist;
224 new->types[i].top = th.top;
226 else if (writeflag) /* write dummy superblock to determine top */
228 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
230 logf (LOG_FATAL, "Failed to write initial superblock.");
233 new->types[i].freelist = -1;
234 new->types[i].top = rs;
236 /* ELSE: this is an empty file opened in read-only mode. */
239 new->keysize = keysize;
242 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
246 logf (LOG_FATAL, "Failed to locate resource %s", nm);
249 if ((new->keysize = atoi(r)) <= 0)
251 logf (LOG_FATAL, "Must specify positive keysize.");
256 /* determine repack percent */
257 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
258 0), IS_DEF_REPACK_PERCENT)))
260 logf (LOG_FATAL, "Failed to locate resource %s", nm);
263 new->repack = atoi(r);
265 /* determine max keys/blocksize */
266 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
267 "maxkeys", 0), "50 640 10000")) || !(num = splitargs(r, pp,
270 logf (LOG_FATAL, "Failed to locate resource %s", nm);
273 if (num < new->num_types -1)
275 logf (LOG_FATAL, "Not enough elements in %s", nm);
278 for (i = 0; i < num; i++)
280 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
282 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
285 new->types[i].max_keys = tmp;
288 /* determine max keys/block */
289 for (i = 0; i < new->num_types; i++)
291 if (!new->types[i].index)
293 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
294 sizeof(int)) / new->keysize;
295 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
296 sizeof(int)) / new->keysize;
299 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
301 if (new->types[i].max_keys_block0 < 1)
303 logf (LOG_FATAL, "Blocksize too small in %s", name);
308 /* determine nice fill rates */
309 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
310 "nicefill", 0), "90 90 90 95")) || !(num = splitargs(r, pp,
313 logf (LOG_FATAL, "Failed to locate resource %s", nm);
316 if (num < new->num_types)
318 logf (LOG_FATAL, "Not enough elements in %s", nm);
321 for (i = 0; i < num; i++)
323 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
325 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
328 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
330 if (new->types[i].nice_keys_block < 1)
331 new->types[i].nice_keys_block = 1;
334 new->cmp = cmp ? cmp : is_default_cmp;
341 int is_close(ISAM is)
346 logf (LOG_DEBUG, "is_close()");
347 for (i = 0; i < is->num_types; i++)
353 th.blocksize = is->types[i].blocksize;
354 th.keysize = is->keysize;
355 th.freelist = is->types[i].freelist;
356 th.top = is->types[i].top;
357 if (is_rb_write(&is->types[i], &th) < 0)
359 logf (LOG_FATAL, "Failed to write headerblock");
363 bf_close(is->types[i].bf);
368 logf(LOG_LOG, "ISAM statistics:");
369 logf(LOG_LOG, "total_merge_operations %d",
370 statistics.total_merge_operations);
371 logf(LOG_LOG, "total_items %d", statistics.total_items);
372 logf(LOG_LOG, "dub_items_removed %d",
373 statistics.dub_items_removed);
374 logf(LOG_LOG, "new_items %d", statistics.new_items);
375 logf(LOG_LOG, "failed_deletes %d",
376 statistics.failed_deletes);
377 logf(LOG_LOG, "skipped_inserts %d",
378 statistics.skipped_inserts);
379 logf(LOG_LOG, "delete_insert_noop %d",
380 statistics.delete_insert_noop);
381 logf(LOG_LOG, "delete_replace %d",
382 statistics.delete_replace);
383 logf(LOG_LOG, "delete %d", statistics.delete);
384 logf(LOG_LOG, "remaps %d", statistics.remaps);
385 logf(LOG_LOG, "block_jumps %d", statistics.block_jumps);
386 logf(LOG_LOG, "tab_deletes %d", statistics.tab_deletes);
392 static ISAM_P is_address(int type, int pos)
401 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
405 char keybuf[IS_MAX_RECORD];
406 int oldnum, oldtype, i;
407 char operation, *record;
409 statistics.total_merge_operations++;
410 statistics.total_items += num;
412 statistics.new_tables++;
414 is_m_establish_tab(is, &tab, pos);
416 if (is_m_read_full(&tab, tab.data) < 0)
418 logf (LOG_FATAL, "read_full failed");
421 oldnum = tab.num_records;
422 oldtype = tab.pos_type;
425 operation = *(data)++;
426 record = (char*) data;
427 data += is_keysize(is);
429 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
431 data += 1 + is_keysize(is);
433 statistics.dub_items_removed++;
435 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
437 if (operation == KEYOP_INSERT)
439 logf (LOG_DEBUG, "XXInserting new record.");
440 is_m_write_record(&tab, record);
441 statistics.new_items++;
445 logf (LOG_DEBUG, "XXDeletion failed to find match.");
446 statistics.failed_deletes++;
449 else /* match found */
451 if (operation == KEYOP_INSERT)
453 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
454 statistics.skipped_inserts++;
457 else if (operation == KEYOP_DELETE)
459 /* try to avoid needlessly moving data */
460 if (num && *(data) == KEYOP_INSERT)
462 /* next key is identical insert? - NOOP - skip it */
463 if (!memcmp(record, data + 1, is_keysize(is)))
465 logf (LOG_DEBUG, "XXNoop delete. skipping.");
466 data += 1 + is_keysize(is);
468 while (num && !memcmp(data, data + is_keysize(tab.is) +
469 1, is_keysize(tab.is) + 1))
471 data += 1 + is_keysize(is);
473 statistics.dub_items_removed++;
475 statistics.delete_insert_noop++;
478 /* else check if next key can fit in this position */
479 is_m_peek_record(&tab, keybuf);
480 res = (*is->cmp)(data + 1, keybuf);
483 logf (LOG_DEBUG, "XXReplacing record.");
484 is_m_replace_record(&tab, data + 1);
485 data += 1 + is_keysize(is);
487 while (num && !memcmp(data, data + is_keysize(tab.is) +
488 1, is_keysize(tab.is) + 1))
490 data += 1 + is_keysize(is);
492 statistics.dub_items_removed++;
494 statistics.delete_replace++;
498 logf (LOG_DEBUG, "Deleting record.");
499 is_m_delete_record(&tab);
505 while (i < tab.is->num_types - 1 && tab.num_records >
506 tab.is->types[i].max_keys)
508 if (i != tab.pos_type)
510 /* read remaining blocks */
511 for (; tab.cur_mblock; tab.cur_mblock = tab.cur_mblock->next)
512 if (tab.cur_mblock->state < IS_MBSTATE_CLEAN)
513 is_m_read_full(&tab, tab.cur_mblock);
517 statistics.block_jumps++;
519 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
520 100) / oldnum > tab.is->repack)
530 pos = is_address(tab.pos_type, tab.data->diskpos);
535 statistics.tab_deletes++;
537 is_m_release_tab(&tab);
542 * Locate a table of keys in an isam file. The ISPT is an individual
543 * position marker for that table.
545 ISPT is_position(ISAM is, ISAM_P pos)
550 is_m_establish_tab(is, &p->tab, pos);
557 void is_pt_free(ISPT ip)
559 is_m_release_tab(&ip->tab);
564 * Read a key from a table.
566 int is_readkey(ISPT ip, void *buf)
568 return is_m_read_record(&ip->tab, buf, 0);
571 int is_numkeys(ISPT ip)
573 return is_m_num_records(&ip->tab);
576 void is_rewind(ISPT ip)
578 is_m_rewind(&ip->tab);