2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1994-09-28 12:56:15 quinn
8 * Added access functions (ISPT)
10 * Revision 1.8 1994/09/28 12:32:17 quinn
13 * Revision 1.7 1994/09/28 11:56:25 quinn
14 * Added sort of input to is_merge
16 * Revision 1.6 1994/09/28 11:29:33 quinn
17 * Added cmp parameter.
19 * Revision 1.5 1994/09/27 20:03:50 quinn
20 * Seems relatively bug-free.
22 * Revision 1.4 1994/09/26 17:11:29 quinn
25 * Revision 1.3 1994/09/26 17:06:35 quinn
28 * Revision 1.1 1994/09/12 08:02:13 quinn
45 static int (*extcmp)(const void *p1, const void *p2);
46 static ispt_struct *ispt_freelist = 0;
48 static ISPT ispt_alloc()
55 ispt_freelist = ispt_freelist->next;
58 p = xmalloc(sizeof(ispt_struct));
62 static void ispt_free(ISPT pt)
64 pt->next = ispt_freelist;
68 static int splitargs(const char *s, char *bf[], int max)
73 while (*s && isspace(*s))
81 log(LOG_WARN, "Ignoring extra args to is resource");
85 while (*s && !isspace(*s))
94 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
98 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
99 int num, size, rs, tmp, i;
102 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
103 new = xmalloc(sizeof(*new));
104 new->writeflag = writeflag;
105 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
106 new->types[i].index = 0; /* dummy */
108 /* determine number and size of blocktypes */
109 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
110 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
112 log(LOG_FATAL, "Failed to locate resource %s", nm);
115 new->num_types = num;
116 for (i = 0; i < num; i++)
118 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
120 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
128 new->types[i].blocksize = size; break;
130 new->types[i].blocksize = size * 1024; break;
132 new->types[i].blocksize = size * 1048576; break;
134 log(LOG_FATAL, "Illegal size suffix: %c", *m);
137 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
140 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
141 new->types[i].blocksize, writeflag)))
143 log(LOG_FATAL, "bf_open failed");
146 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
148 if (th.blocksize != new->types[i].blocksize)
150 log(LOG_FATAL, "File blocksize mismatch in %s", name);
153 new->types[i].freelist = th.freelist;
154 new->types[i].top = th.top;
156 else if (writeflag) /* write dummy superblock to determine top */
158 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
160 log(LOG_FATAL, "Failed to write initial superblock.");
163 new->types[i].freelist = -1;
164 new->types[i].top = rs;
166 /* ELSE: this is an empty file opened in read-only mode. */
168 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
171 log(LOG_FATAL, "Failed to locate resource %s", nm);
174 if ((new->keysize = atoi(r)) <= 0)
176 log(LOG_FATAL, "Must specify positive keysize.");
180 /* determine repack percent */
181 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
182 0), IS_DEF_REPACK_PERCENT)))
184 log(LOG_FATAL, "Failed to locate resource %s", nm);
187 new->repack = atoi(r);
189 /* determine max keys/blocksize */
190 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
191 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
193 log(LOG_FATAL, "Failed to locate resource %s", nm);
196 if (num < new->num_types -1)
198 log(LOG_FATAL, "Not enough elements in %s", nm);
201 for (i = 0; i < num; i++)
203 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
205 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
208 new->types[i].max_keys = tmp;
211 /* determine max keys/block */
212 for (i = 0; i < new->num_types; i++)
214 if (!new->types[i].index)
216 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
217 sizeof(int)) / new->keysize;
218 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
219 sizeof(int)) / new->keysize;
222 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
224 if (new->types[i].max_keys_block0 < 1)
226 log(LOG_FATAL, "Blocksize too small in %s", name);
231 /* determine nice fill rates */
232 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
233 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
235 log(LOG_FATAL, "Failed to locate resource %s", nm);
238 if (num < new->num_types)
240 log(LOG_FATAL, "Not enough elements in %s", nm);
243 for (i = 0; i < num; i++)
245 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
247 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
250 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
252 if (new->types[i].nice_keys_block < 1)
253 new->types[i].nice_keys_block = 1;
256 new->cmp = cmp ? cmp : is_default_cmp;
263 int is_close(ISAM is)
268 log(LOG_DEBUG, "is_close()");
269 for (i = 0; i < is->num_types; i++)
275 th.blocksize = is->types[i].blocksize;
276 th.keysize = is->keysize;
277 th.freelist = is->types[i].freelist;
278 th.top = is->types[i].top;
279 if (is_rb_write(&is->types[i], &th) < 0)
281 log(LOG_FATAL, "Failed to write headerblock");
285 bf_close(is->types[i].bf);
292 static ISAM_P is_address(int type, int pos)
301 int sort_input(const void *p1, const void *p2)
305 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
307 return *((char *)p1) - *((char*)p2);
310 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
314 char keybuf[IS_MAX_RECORD];
315 int oldnum, oldtype, i;
316 char operation, *record;
319 qsort(data, num, is_keysize(is) + 1, sort_input);
320 is_m_establish_tab(is, &tab, pos);
322 if (is_m_read_full(&tab, tab.data) < 0)
324 log(LOG_FATAL, "read_full failed");
327 oldnum = tab.num_records;
328 oldtype = tab.pos_type;
331 operation = *(data)++;
332 record = (char*) data;
333 data += is_keysize(is);
335 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
337 data += 1 + is_keysize(is);
340 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
342 if (operation == KEYOP_INSERT)
344 log(LOG_DEBUG, "XXInserting new record.");
345 is_m_write_record(&tab, record);
348 log(LOG_DEBUG, "XXDeletion failed to find match.");
350 else /* match found */
352 if (operation == KEYOP_INSERT)
354 log(LOG_DEBUG, "XXSkipping insertion - match found.");
357 else if (operation == KEYOP_DELETE)
359 /* try to avoid needlessly moving data */
360 if (num && *(data) == KEYOP_INSERT)
362 /* next key is identical insert? - NOOP - skip it */
363 if (!memcmp(record, data + 1, is_keysize(is)))
365 log(LOG_DEBUG, "XXNoop delete. skipping.");
366 data += 1 + is_keysize(is);
370 /* else check if next key can fit in this position */
371 is_m_peek_record(&tab, keybuf);
372 res = (*is->cmp)(data + 1, keybuf);
375 log(LOG_DEBUG, "XXReplacing record.");
376 is_m_replace_record(&tab, data + 1);
377 data += 1 + is_keysize(is);
382 log(LOG_DEBUG, "Deleting record.");
383 is_m_delete_record(&tab);
388 while (i < tab.is->num_types - 1 && tab.num_records >
389 tab.is->types[i].max_keys)
391 if (i != tab.pos_type)
396 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
397 100) / oldnum > tab.is->repack)
404 pos = is_address(tab.pos_type, tab.data->diskpos);
408 is_m_release_tab(&tab);
413 * Locate a table of keys in an isam file. The ISPT is an individual
414 * position marker for that table.
416 ISPT is_position(ISAM is, ISAM_P pos)
421 is_m_establish_tab(is, &p->tab, pos);
428 void is_pt_free(ISPT ip)
430 is_m_release_tab(&ip->tab);
435 * Read a key from a table.
437 int is_readkey(ISPT ip, void *buf)
439 return is_m_read_record(&ip->tab, buf);