2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.25 1999-02-02 14:51:16 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.24 1997/10/27 14:25:39 adam
13 * Revision 1.23 1997/09/17 12:19:20 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.22 1996/10/29 13:56:53 adam
18 * Include of zebrautl.h instead of alexutil.h.
20 * Revision 1.21 1996/03/29 14:11:47 quinn
23 * Revision 1.20 1996/03/19 13:14:57 quinn
26 * Revision 1.19 1996/02/10 12:20:56 quinn
27 * *** empty log message ***
29 * Revision 1.18 1996/02/06 10:19:56 quinn
30 * Attempt at fixing bug. Not all blocks were read before they were unlinked
31 * prior to a remap operation.
33 * Revision 1.17 1995/12/06 15:48:44 quinn
34 * Fixed update-problem.
36 * Revision 1.16 1995/12/06 14:48:26 quinn
37 * Fixed some strange bugs.
39 * Revision 1.15 1995/12/06 09:59:45 quinn
40 * Fixed memory-consumption bug in memory.c
41 * Added more blocksizes to the default ISAM configuration.
43 * Revision 1.14 1995/11/24 17:26:19 quinn
44 * Mostly about making some ISAM stuff in the config file optional.
46 * Revision 1.13 1995/10/17 18:03:15 adam
47 * Commented out qsort in is_merge.
49 * Revision 1.12 1995/09/06 16:11:41 adam
50 * Keysize parameter to is_open (if non-zero).
52 * Revision 1.11 1995/09/04 12:33:46 adam
53 * Various cleanup. YAZ util used instead.
55 * Revision 1.10 1994/09/28 16:58:32 quinn
58 * Revision 1.9 1994/09/28 12:56:15 quinn
59 * Added access functions (ISPT)
61 * Revision 1.8 1994/09/28 12:32:17 quinn
64 * Revision 1.7 1994/09/28 11:56:25 quinn
65 * Added sort of input to is_merge
67 * Revision 1.6 1994/09/28 11:29:33 quinn
68 * Added cmp parameter.
70 * Revision 1.5 1994/09/27 20:03:50 quinn
71 * Seems relatively bug-free.
73 * Revision 1.4 1994/09/26 17:11:29 quinn
76 * Revision 1.3 1994/09/26 17:06:35 quinn
79 * Revision 1.1 1994/09/12 08:02:13 quinn
96 static ispt_struct *ispt_freelist = 0;
100 int total_merge_operations;
102 int dub_items_removed;
106 int delete_insert_noop;
115 static ISPT ispt_alloc()
122 ispt_freelist = ispt_freelist->next;
125 p = xmalloc(sizeof(ispt_struct));
129 static void ispt_free(ISPT pt)
131 pt->next = ispt_freelist;
135 static int splitargs(const char *s, char *bf[], int max)
140 while (*s && isspace(*s))
148 logf (LOG_WARN, "Ignoring extra args to is resource");
152 while (*s && !isspace(*s))
161 ISAM is_open(BFiles bfs, const char *name,
162 int (*cmp)(const void *p1, const void *p2),
163 int writeflag, int keysize, Res res)
166 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
167 int num, size, rs, tmp, i;
170 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
173 statistics.total_merge_operations = 0;
174 statistics.total_items = 0;
175 statistics.dub_items_removed = 0;
176 statistics.new_items = 0;
177 statistics.failed_deletes = 0;
178 statistics.skipped_inserts = 0;
179 statistics.delete_insert_noop = 0;
180 statistics.delete_replace = 0;
181 statistics.delete = 0;
182 statistics.remaps = 0;
183 statistics.new_tables = 0;
184 statistics.block_jumps = 0;
185 statistics.tab_deletes = 0;
188 new = xmalloc(sizeof(*new));
189 new->writeflag = writeflag;
190 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
191 new->types[i].index = 0; /* dummy */
193 /* determine number and size of blocktypes */
194 if (!(r = res_get_def(res,
195 nm = strconcat(name, ".",
196 "blocktypes", 0), "64 512 4K 32K")) ||
197 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
199 logf (LOG_FATAL, "Failed to locate resource %s", nm);
202 new->num_types = num;
203 for (i = 0; i < num; i++)
205 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
207 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
215 new->types[i].blocksize = size; break;
217 new->types[i].blocksize = size * 1024; break;
219 new->types[i].blocksize = size * 1048576; break;
221 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
224 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
227 if (!(new->types[i].bf = bf_open(bfs, strconcat(name, m, 0),
228 new->types[i].blocksize, writeflag)))
230 logf (LOG_FATAL, "bf_open failed");
233 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
235 if (th.blocksize != new->types[i].blocksize)
237 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
240 new->types[i].freelist = th.freelist;
241 new->types[i].top = th.top;
243 else if (writeflag) /* write dummy superblock to determine top */
245 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
247 logf (LOG_FATAL, "Failed to write initial superblock.");
250 new->types[i].freelist = -1;
251 new->types[i].top = rs;
253 /* ELSE: this is an empty file opened in read-only mode. */
256 new->keysize = keysize;
259 if (!(r = res_get_def(res, nm = strconcat(name, ".",
260 "keysize", 0), "4")))
262 logf (LOG_FATAL, "Failed to locate resource %s", nm);
265 if ((new->keysize = atoi(r)) <= 0)
267 logf (LOG_FATAL, "Must specify positive keysize.");
272 /* determine repack percent */
273 if (!(r = res_get_def(res, nm = strconcat(name, ".", "repack",
274 0), IS_DEF_REPACK_PERCENT)))
276 logf (LOG_FATAL, "Failed to locate resource %s", nm);
279 new->repack = atoi(r);
281 /* determine max keys/blocksize */
282 if (!(r = res_get_def(res,
283 nm = strconcat(name, ".",
284 "maxkeys", 0), "50 640 10000")) ||
285 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
287 logf (LOG_FATAL, "Failed to locate resource %s", nm);
290 if (num < new->num_types -1)
292 logf (LOG_FATAL, "Not enough elements in %s", nm);
295 for (i = 0; i < num; i++)
297 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
299 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
302 new->types[i].max_keys = tmp;
305 /* determine max keys/block */
306 for (i = 0; i < new->num_types; i++)
308 if (!new->types[i].index)
310 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
311 sizeof(int)) / new->keysize;
312 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
313 sizeof(int)) / new->keysize;
316 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
318 if (new->types[i].max_keys_block0 < 1)
320 logf (LOG_FATAL, "Blocksize too small in %s", name);
325 /* determine nice fill rates */
326 if (!(r = res_get_def(res,
327 nm = strconcat(name, ".",
328 "nicefill", 0), "90 90 90 95")) ||
329 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
331 logf (LOG_FATAL, "Failed to locate resource %s", nm);
334 if (num < new->num_types)
336 logf (LOG_FATAL, "Not enough elements in %s", nm);
339 for (i = 0; i < num; i++)
341 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
343 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
346 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
348 if (new->types[i].nice_keys_block < 1)
349 new->types[i].nice_keys_block = 1;
352 new->cmp = cmp ? cmp : is_default_cmp;
359 int is_close(ISAM is)
364 logf (LOG_DEBUG, "is_close()");
365 for (i = 0; i < is->num_types; i++)
371 th.blocksize = is->types[i].blocksize;
372 th.keysize = is->keysize;
373 th.freelist = is->types[i].freelist;
374 th.top = is->types[i].top;
375 if (is_rb_write(&is->types[i], &th) < 0)
377 logf (LOG_FATAL, "Failed to write headerblock");
381 bf_close(is->types[i].bf);
384 for (i = 0; i < is->num_types; i++)
385 xfree (is->types[i].dbuf);
389 logf(LOG_LOG, "ISAM statistics:");
390 logf(LOG_LOG, "total_merge_operations %d",
391 statistics.total_merge_operations);
392 logf(LOG_LOG, "total_items %d", statistics.total_items);
393 logf(LOG_LOG, "dub_items_removed %d",
394 statistics.dub_items_removed);
395 logf(LOG_LOG, "new_items %d", statistics.new_items);
396 logf(LOG_LOG, "failed_deletes %d",
397 statistics.failed_deletes);
398 logf(LOG_LOG, "skipped_inserts %d",
399 statistics.skipped_inserts);
400 logf(LOG_LOG, "delete_insert_noop %d",
401 statistics.delete_insert_noop);
402 logf(LOG_LOG, "delete_replace %d",
403 statistics.delete_replace);
404 logf(LOG_LOG, "delete %d", statistics.delete);
405 logf(LOG_LOG, "remaps %d", statistics.remaps);
406 logf(LOG_LOG, "block_jumps %d", statistics.block_jumps);
407 logf(LOG_LOG, "tab_deletes %d", statistics.tab_deletes);
413 static ISAM_P is_address(int type, int pos)
422 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
426 char keybuf[IS_MAX_RECORD];
427 int oldnum, oldtype, i;
428 char operation, *record;
430 statistics.total_merge_operations++;
431 statistics.total_items += num;
433 statistics.new_tables++;
435 is_m_establish_tab(is, &tab, pos);
437 if (is_m_read_full(&tab, tab.data) < 0)
439 logf (LOG_FATAL, "read_full failed");
442 oldnum = tab.num_records;
443 oldtype = tab.pos_type;
446 operation = *(data)++;
447 record = (char*) data;
448 data += is_keysize(is);
450 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
452 data += 1 + is_keysize(is);
454 statistics.dub_items_removed++;
456 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
458 if (operation == KEYOP_INSERT)
460 logf (LOG_DEBUG, "XXInserting new record.");
461 is_m_write_record(&tab, record);
462 statistics.new_items++;
466 logf (LOG_DEBUG, "XXDeletion failed to find match.");
467 statistics.failed_deletes++;
470 else /* match found */
472 if (operation == KEYOP_INSERT)
474 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
475 statistics.skipped_inserts++;
478 else if (operation == KEYOP_DELETE)
480 /* try to avoid needlessly moving data */
481 if (num && *(data) == KEYOP_INSERT)
483 /* next key is identical insert? - NOOP - skip it */
484 if (!memcmp(record, data + 1, is_keysize(is)))
486 logf (LOG_DEBUG, "XXNoop delete. skipping.");
487 data += 1 + is_keysize(is);
489 while (num && !memcmp(data, data + is_keysize(tab.is) +
490 1, is_keysize(tab.is) + 1))
492 data += 1 + is_keysize(is);
494 statistics.dub_items_removed++;
496 statistics.delete_insert_noop++;
499 /* else check if next key can fit in this position */
500 if (is_m_peek_record(&tab, keybuf) &&
501 (*is->cmp)(data + 1, keybuf) < 0)
503 logf (LOG_DEBUG, "XXReplacing record.");
504 is_m_replace_record(&tab, data + 1);
505 data += 1 + is_keysize(is);
507 while (num && !memcmp(data, data + is_keysize(tab.is) +
508 1, is_keysize(tab.is) + 1))
510 data += 1 + is_keysize(is);
512 statistics.dub_items_removed++;
514 statistics.delete_replace++;
518 logf (LOG_DEBUG, "Deleting record.");
519 is_m_delete_record(&tab);
525 while (i < tab.is->num_types - 1 && tab.num_records >
526 tab.is->types[i].max_keys)
528 if (i != tab.pos_type)
530 /* read remaining blocks */
531 for (; tab.cur_mblock; tab.cur_mblock = tab.cur_mblock->next)
532 if (tab.cur_mblock->state < IS_MBSTATE_CLEAN)
533 is_m_read_full(&tab, tab.cur_mblock);
537 statistics.block_jumps++;
539 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
540 100) / oldnum > tab.is->repack)
550 pos = is_address(tab.pos_type, tab.data->diskpos);
555 statistics.tab_deletes++;
557 is_m_release_tab(&tab);
562 * Locate a table of keys in an isam file. The ISPT is an individual
563 * position marker for that table.
565 ISPT is_position(ISAM is, ISAM_P pos)
570 is_m_establish_tab(is, &p->tab, pos);
577 void is_pt_free(ISPT ip)
579 is_m_release_tab(&ip->tab);
584 * Read a key from a table.
586 int is_readkey(ISPT ip, void *buf)
588 return is_m_read_record(&ip->tab, buf, 0);
591 int is_numkeys(ISPT ip)
593 return is_m_num_records(&ip->tab);
596 void is_rewind(ISPT ip)
598 is_m_rewind(&ip->tab);