2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1995-11-24 17:26:19 quinn
8 * Mostly about making some ISAM stuff in the config file optional.
10 * Revision 1.13 1995/10/17 18:03:15 adam
11 * Commented out qsort in is_merge.
13 * Revision 1.12 1995/09/06 16:11:41 adam
14 * Keysize parameter to is_open (if non-zero).
16 * Revision 1.11 1995/09/04 12:33:46 adam
17 * Various cleanup. YAZ util used instead.
19 * Revision 1.10 1994/09/28 16:58:32 quinn
22 * Revision 1.9 1994/09/28 12:56:15 quinn
23 * Added access functions (ISPT)
25 * Revision 1.8 1994/09/28 12:32:17 quinn
28 * Revision 1.7 1994/09/28 11:56:25 quinn
29 * Added sort of input to is_merge
31 * Revision 1.6 1994/09/28 11:29:33 quinn
32 * Added cmp parameter.
34 * Revision 1.5 1994/09/27 20:03:50 quinn
35 * Seems relatively bug-free.
37 * Revision 1.4 1994/09/26 17:11:29 quinn
40 * Revision 1.3 1994/09/26 17:06:35 quinn
43 * Revision 1.1 1994/09/12 08:02:13 quinn
61 static int (*extcmp)(const void *p1, const void *p2);
62 static ispt_struct *ispt_freelist = 0;
64 static ISPT ispt_alloc()
71 ispt_freelist = ispt_freelist->next;
74 p = xmalloc(sizeof(ispt_struct));
78 static void ispt_free(ISPT pt)
80 pt->next = ispt_freelist;
84 static int splitargs(const char *s, char *bf[], int max)
89 while (*s && isspace(*s))
97 logf (LOG_WARN, "Ignoring extra args to is resource");
101 while (*s && !isspace(*s))
110 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
111 int writeflag, int keysize)
114 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
115 int num, size, rs, tmp, i;
118 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
119 new = xmalloc(sizeof(*new));
120 new->writeflag = writeflag;
121 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
122 new->types[i].index = 0; /* dummy */
124 /* determine number and size of blocktypes */
125 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
126 "blocktypes", 0), "64 1K")) ||
127 !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
129 logf (LOG_FATAL, "Failed to locate resource %s", nm);
132 new->num_types = num;
133 for (i = 0; i < num; i++)
135 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
137 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
145 new->types[i].blocksize = size; break;
147 new->types[i].blocksize = size * 1024; break;
149 new->types[i].blocksize = size * 1048576; break;
151 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
154 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
157 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
158 new->types[i].blocksize, writeflag)))
160 logf (LOG_FATAL, "bf_open failed");
163 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
165 if (th.blocksize != new->types[i].blocksize)
167 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
170 new->types[i].freelist = th.freelist;
171 new->types[i].top = th.top;
173 else if (writeflag) /* write dummy superblock to determine top */
175 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
177 logf (LOG_FATAL, "Failed to write initial superblock.");
180 new->types[i].freelist = -1;
181 new->types[i].top = rs;
183 /* ELSE: this is an empty file opened in read-only mode. */
186 new->keysize = keysize;
189 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
193 logf (LOG_FATAL, "Failed to locate resource %s", nm);
196 if ((new->keysize = atoi(r)) <= 0)
198 logf (LOG_FATAL, "Must specify positive keysize.");
203 /* determine repack percent */
204 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
205 0), IS_DEF_REPACK_PERCENT)))
207 logf (LOG_FATAL, "Failed to locate resource %s", nm);
210 new->repack = atoi(r);
212 /* determine max keys/blocksize */
213 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
214 "maxkeys", 0), "50")) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
216 logf (LOG_FATAL, "Failed to locate resource %s", nm);
219 if (num < new->num_types -1)
221 logf (LOG_FATAL, "Not enough elements in %s", nm);
224 for (i = 0; i < num; i++)
226 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
228 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
231 new->types[i].max_keys = tmp;
234 /* determine max keys/block */
235 for (i = 0; i < new->num_types; i++)
237 if (!new->types[i].index)
239 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
240 sizeof(int)) / new->keysize;
241 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
242 sizeof(int)) / new->keysize;
245 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
247 if (new->types[i].max_keys_block0 < 1)
249 logf (LOG_FATAL, "Blocksize too small in %s", name);
254 /* determine nice fill rates */
255 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
256 "nicefill", 0), "90 90")) || !(num = splitargs(r, pp,
259 logf (LOG_FATAL, "Failed to locate resource %s", nm);
262 if (num < new->num_types)
264 logf (LOG_FATAL, "Not enough elements in %s", nm);
267 for (i = 0; i < num; i++)
269 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
271 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
274 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
276 if (new->types[i].nice_keys_block < 1)
277 new->types[i].nice_keys_block = 1;
280 new->cmp = cmp ? cmp : is_default_cmp;
287 int is_close(ISAM is)
292 logf (LOG_DEBUG, "is_close()");
293 for (i = 0; i < is->num_types; i++)
299 th.blocksize = is->types[i].blocksize;
300 th.keysize = is->keysize;
301 th.freelist = is->types[i].freelist;
302 th.top = is->types[i].top;
303 if (is_rb_write(&is->types[i], &th) < 0)
305 logf (LOG_FATAL, "Failed to write headerblock");
309 bf_close(is->types[i].bf);
316 static ISAM_P is_address(int type, int pos)
325 int sort_input(const void *p1, const void *p2)
329 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
331 return *((char *)p1) - *((char*)p2);
334 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
338 char keybuf[IS_MAX_RECORD];
339 int oldnum, oldtype, i;
340 char operation, *record;
344 qsort(data, num, is_keysize(is) + 1, sort_input);
346 is_m_establish_tab(is, &tab, pos);
348 if (is_m_read_full(&tab, tab.data) < 0)
350 logf (LOG_FATAL, "read_full failed");
353 oldnum = tab.num_records;
354 oldtype = tab.pos_type;
357 operation = *(data)++;
358 record = (char*) data;
359 data += is_keysize(is);
361 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
363 data += 1 + is_keysize(is);
366 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
368 if (operation == KEYOP_INSERT)
370 logf (LOG_DEBUG, "XXInserting new record.");
371 is_m_write_record(&tab, record);
374 logf (LOG_DEBUG, "XXDeletion failed to find match.");
376 else /* match found */
378 if (operation == KEYOP_INSERT)
380 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
383 else if (operation == KEYOP_DELETE)
385 /* try to avoid needlessly moving data */
386 if (num && *(data) == KEYOP_INSERT)
388 /* next key is identical insert? - NOOP - skip it */
389 if (!memcmp(record, data + 1, is_keysize(is)))
391 logf (LOG_DEBUG, "XXNoop delete. skipping.");
392 data += 1 + is_keysize(is);
396 /* else check if next key can fit in this position */
397 is_m_peek_record(&tab, keybuf);
398 res = (*is->cmp)(data + 1, keybuf);
401 logf (LOG_DEBUG, "XXReplacing record.");
402 is_m_replace_record(&tab, data + 1);
403 data += 1 + is_keysize(is);
408 logf (LOG_DEBUG, "Deleting record.");
409 is_m_delete_record(&tab);
414 while (i < tab.is->num_types - 1 && tab.num_records >
415 tab.is->types[i].max_keys)
417 if (i != tab.pos_type)
422 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
423 100) / oldnum > tab.is->repack)
430 pos = is_address(tab.pos_type, tab.data->diskpos);
434 is_m_release_tab(&tab);
439 * Locate a table of keys in an isam file. The ISPT is an individual
440 * position marker for that table.
442 ISPT is_position(ISAM is, ISAM_P pos)
447 is_m_establish_tab(is, &p->tab, pos);
454 void is_pt_free(ISPT ip)
456 is_m_release_tab(&ip->tab);
461 * Read a key from a table.
463 int is_readkey(ISPT ip, void *buf)
465 return is_m_read_record(&ip->tab, buf);
468 int is_numkeys(ISPT ip)
470 return is_m_num_records(&ip->tab);
473 void is_rewind(ISPT ip)
475 is_m_rewind(&ip->tab);