2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1996-02-06 10:19:56 quinn
8 * Attempt at fixing bug. Not all blocks were read before they were unlinked
9 * prior to a remap operation.
11 * Revision 1.17 1995/12/06 15:48:44 quinn
12 * Fixed update-problem.
14 * Revision 1.16 1995/12/06 14:48:26 quinn
15 * Fixed some strange bugs.
17 * Revision 1.15 1995/12/06 09:59:45 quinn
18 * Fixed memory-consumption bug in memory.c
19 * Added more blocksizes to the default ISAM configuration.
21 * Revision 1.14 1995/11/24 17:26:19 quinn
22 * Mostly about making some ISAM stuff in the config file optional.
24 * Revision 1.13 1995/10/17 18:03:15 adam
25 * Commented out qsort in is_merge.
27 * Revision 1.12 1995/09/06 16:11:41 adam
28 * Keysize parameter to is_open (if non-zero).
30 * Revision 1.11 1995/09/04 12:33:46 adam
31 * Various cleanup. YAZ util used instead.
33 * Revision 1.10 1994/09/28 16:58:32 quinn
36 * Revision 1.9 1994/09/28 12:56:15 quinn
37 * Added access functions (ISPT)
39 * Revision 1.8 1994/09/28 12:32:17 quinn
42 * Revision 1.7 1994/09/28 11:56:25 quinn
43 * Added sort of input to is_merge
45 * Revision 1.6 1994/09/28 11:29:33 quinn
46 * Added cmp parameter.
48 * Revision 1.5 1994/09/27 20:03:50 quinn
49 * Seems relatively bug-free.
51 * Revision 1.4 1994/09/26 17:11:29 quinn
54 * Revision 1.3 1994/09/26 17:06:35 quinn
57 * Revision 1.1 1994/09/12 08:02:13 quinn
75 static int (*extcmp)(const void *p1, const void *p2);
76 static ispt_struct *ispt_freelist = 0;
78 static ISPT ispt_alloc()
85 ispt_freelist = ispt_freelist->next;
88 p = xmalloc(sizeof(ispt_struct));
92 static void ispt_free(ISPT pt)
94 pt->next = ispt_freelist;
98 static int splitargs(const char *s, char *bf[], int max)
103 while (*s && isspace(*s))
111 logf (LOG_WARN, "Ignoring extra args to is resource");
115 while (*s && !isspace(*s))
124 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
125 int writeflag, int keysize)
128 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
129 int num, size, rs, tmp, i;
132 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
133 new = xmalloc(sizeof(*new));
134 new->writeflag = writeflag;
135 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
136 new->types[i].index = 0; /* dummy */
138 /* determine number and size of blocktypes */
139 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
140 "blocktypes", 0), "64 512 4K 32K")) ||
141 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
143 logf (LOG_FATAL, "Failed to locate resource %s", nm);
146 new->num_types = num;
147 for (i = 0; i < num; i++)
149 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
151 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
159 new->types[i].blocksize = size; break;
161 new->types[i].blocksize = size * 1024; break;
163 new->types[i].blocksize = size * 1048576; break;
165 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
168 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
171 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
172 new->types[i].blocksize, writeflag)))
174 logf (LOG_FATAL, "bf_open failed");
177 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
179 if (th.blocksize != new->types[i].blocksize)
181 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
184 new->types[i].freelist = th.freelist;
185 new->types[i].top = th.top;
187 else if (writeflag) /* write dummy superblock to determine top */
189 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
191 logf (LOG_FATAL, "Failed to write initial superblock.");
194 new->types[i].freelist = -1;
195 new->types[i].top = rs;
197 /* ELSE: this is an empty file opened in read-only mode. */
200 new->keysize = keysize;
203 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
207 logf (LOG_FATAL, "Failed to locate resource %s", nm);
210 if ((new->keysize = atoi(r)) <= 0)
212 logf (LOG_FATAL, "Must specify positive keysize.");
217 /* determine repack percent */
218 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
219 0), IS_DEF_REPACK_PERCENT)))
221 logf (LOG_FATAL, "Failed to locate resource %s", nm);
224 new->repack = atoi(r);
226 /* determine max keys/blocksize */
227 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
228 "maxkeys", 0), "50 640 10000")) || !(num = splitargs(r, pp,
231 logf (LOG_FATAL, "Failed to locate resource %s", nm);
234 if (num < new->num_types -1)
236 logf (LOG_FATAL, "Not enough elements in %s", nm);
239 for (i = 0; i < num; i++)
241 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
243 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
246 new->types[i].max_keys = tmp;
249 /* determine max keys/block */
250 for (i = 0; i < new->num_types; i++)
252 if (!new->types[i].index)
254 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
255 sizeof(int)) / new->keysize;
256 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
257 sizeof(int)) / new->keysize;
260 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
262 if (new->types[i].max_keys_block0 < 1)
264 logf (LOG_FATAL, "Blocksize too small in %s", name);
269 /* determine nice fill rates */
270 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
271 "nicefill", 0), "90 90 90 95")) || !(num = splitargs(r, pp,
274 logf (LOG_FATAL, "Failed to locate resource %s", nm);
277 if (num < new->num_types)
279 logf (LOG_FATAL, "Not enough elements in %s", nm);
282 for (i = 0; i < num; i++)
284 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
286 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
289 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
291 if (new->types[i].nice_keys_block < 1)
292 new->types[i].nice_keys_block = 1;
295 new->cmp = cmp ? cmp : is_default_cmp;
302 int is_close(ISAM is)
307 logf (LOG_DEBUG, "is_close()");
308 for (i = 0; i < is->num_types; i++)
314 th.blocksize = is->types[i].blocksize;
315 th.keysize = is->keysize;
316 th.freelist = is->types[i].freelist;
317 th.top = is->types[i].top;
318 if (is_rb_write(&is->types[i], &th) < 0)
320 logf (LOG_FATAL, "Failed to write headerblock");
324 bf_close(is->types[i].bf);
331 static ISAM_P is_address(int type, int pos)
340 int sort_input(const void *p1, const void *p2)
344 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
346 return *((char *)p1) - *((char*)p2);
349 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
353 char keybuf[IS_MAX_RECORD];
354 int oldnum, oldtype, i;
355 char operation, *record;
359 qsort(data, num, is_keysize(is) + 1, sort_input);
361 is_m_establish_tab(is, &tab, pos);
363 if (is_m_read_full(&tab, tab.data) < 0)
365 logf (LOG_FATAL, "read_full failed");
368 oldnum = tab.num_records;
369 oldtype = tab.pos_type;
372 operation = *(data)++;
373 record = (char*) data;
374 data += is_keysize(is);
376 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
378 data += 1 + is_keysize(is);
381 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
383 if (operation == KEYOP_INSERT)
385 logf (LOG_DEBUG, "XXInserting new record.");
386 is_m_write_record(&tab, record);
389 logf (LOG_DEBUG, "XXDeletion failed to find match.");
391 else /* match found */
393 if (operation == KEYOP_INSERT)
395 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
398 else if (operation == KEYOP_DELETE)
400 /* try to avoid needlessly moving data */
401 if (num && *(data) == KEYOP_INSERT)
403 /* next key is identical insert? - NOOP - skip it */
404 if (!memcmp(record, data + 1, is_keysize(is)))
406 logf (LOG_DEBUG, "XXNoop delete. skipping.");
407 data += 1 + is_keysize(is);
411 /* else check if next key can fit in this position */
412 is_m_peek_record(&tab, keybuf);
413 res = (*is->cmp)(data + 1, keybuf);
416 logf (LOG_DEBUG, "XXReplacing record.");
417 is_m_replace_record(&tab, data + 1);
418 data += 1 + is_keysize(is);
423 logf (LOG_DEBUG, "Deleting record.");
424 is_m_delete_record(&tab);
429 while (i < tab.is->num_types - 1 && tab.num_records >
430 tab.is->types[i].max_keys)
432 if (i != tab.pos_type)
434 /* read remaining blocks */
435 for (; tab.cur_mblock; tab.cur_mblock = tab.cur_mblock->next)
436 if (tab.cur_mblock->state < IS_MBSTATE_CLEAN)
437 is_m_read_full(&tab, tab.cur_mblock);
441 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
442 100) / oldnum > tab.is->repack)
449 pos = is_address(tab.pos_type, tab.data->diskpos);
453 is_m_release_tab(&tab);
458 * Locate a table of keys in an isam file. The ISPT is an individual
459 * position marker for that table.
461 ISPT is_position(ISAM is, ISAM_P pos)
466 is_m_establish_tab(is, &p->tab, pos);
473 void is_pt_free(ISPT ip)
475 is_m_release_tab(&ip->tab);
480 * Read a key from a table.
482 int is_readkey(ISPT ip, void *buf)
484 return is_m_read_record(&ip->tab, buf, 0);
487 int is_numkeys(ISPT ip)
489 return is_m_num_records(&ip->tab);
492 void is_rewind(ISPT ip)
494 is_m_rewind(&ip->tab);