2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1994-09-28 16:58:32 quinn
10 * Revision 1.9 1994/09/28 12:56:15 quinn
11 * Added access functions (ISPT)
13 * Revision 1.8 1994/09/28 12:32:17 quinn
16 * Revision 1.7 1994/09/28 11:56:25 quinn
17 * Added sort of input to is_merge
19 * Revision 1.6 1994/09/28 11:29:33 quinn
20 * Added cmp parameter.
22 * Revision 1.5 1994/09/27 20:03:50 quinn
23 * Seems relatively bug-free.
25 * Revision 1.4 1994/09/26 17:11:29 quinn
28 * Revision 1.3 1994/09/26 17:06:35 quinn
31 * Revision 1.1 1994/09/12 08:02:13 quinn
48 static int (*extcmp)(const void *p1, const void *p2);
49 static ispt_struct *ispt_freelist = 0;
51 static ISPT ispt_alloc()
58 ispt_freelist = ispt_freelist->next;
61 p = xmalloc(sizeof(ispt_struct));
65 static void ispt_free(ISPT pt)
67 pt->next = ispt_freelist;
71 static int splitargs(const char *s, char *bf[], int max)
76 while (*s && isspace(*s))
84 log(LOG_WARN, "Ignoring extra args to is resource");
88 while (*s && !isspace(*s))
97 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
101 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
102 int num, size, rs, tmp, i;
105 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
106 new = xmalloc(sizeof(*new));
107 new->writeflag = writeflag;
108 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
109 new->types[i].index = 0; /* dummy */
111 /* determine number and size of blocktypes */
112 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
113 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
115 log(LOG_FATAL, "Failed to locate resource %s", nm);
118 new->num_types = num;
119 for (i = 0; i < num; i++)
121 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
123 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
131 new->types[i].blocksize = size; break;
133 new->types[i].blocksize = size * 1024; break;
135 new->types[i].blocksize = size * 1048576; break;
137 log(LOG_FATAL, "Illegal size suffix: %c", *m);
140 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
143 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
144 new->types[i].blocksize, writeflag)))
146 log(LOG_FATAL, "bf_open failed");
149 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
151 if (th.blocksize != new->types[i].blocksize)
153 log(LOG_FATAL, "File blocksize mismatch in %s", name);
156 new->types[i].freelist = th.freelist;
157 new->types[i].top = th.top;
159 else if (writeflag) /* write dummy superblock to determine top */
161 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
163 log(LOG_FATAL, "Failed to write initial superblock.");
166 new->types[i].freelist = -1;
167 new->types[i].top = rs;
169 /* ELSE: this is an empty file opened in read-only mode. */
171 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
174 log(LOG_FATAL, "Failed to locate resource %s", nm);
177 if ((new->keysize = atoi(r)) <= 0)
179 log(LOG_FATAL, "Must specify positive keysize.");
183 /* determine repack percent */
184 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
185 0), IS_DEF_REPACK_PERCENT)))
187 log(LOG_FATAL, "Failed to locate resource %s", nm);
190 new->repack = atoi(r);
192 /* determine max keys/blocksize */
193 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
194 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
196 log(LOG_FATAL, "Failed to locate resource %s", nm);
199 if (num < new->num_types -1)
201 log(LOG_FATAL, "Not enough elements in %s", nm);
204 for (i = 0; i < num; i++)
206 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
208 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
211 new->types[i].max_keys = tmp;
214 /* determine max keys/block */
215 for (i = 0; i < new->num_types; i++)
217 if (!new->types[i].index)
219 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
220 sizeof(int)) / new->keysize;
221 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
222 sizeof(int)) / new->keysize;
225 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
227 if (new->types[i].max_keys_block0 < 1)
229 log(LOG_FATAL, "Blocksize too small in %s", name);
234 /* determine nice fill rates */
235 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
236 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
238 log(LOG_FATAL, "Failed to locate resource %s", nm);
241 if (num < new->num_types)
243 log(LOG_FATAL, "Not enough elements in %s", nm);
246 for (i = 0; i < num; i++)
248 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
250 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
253 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
255 if (new->types[i].nice_keys_block < 1)
256 new->types[i].nice_keys_block = 1;
259 new->cmp = cmp ? cmp : is_default_cmp;
266 int is_close(ISAM is)
271 log(LOG_DEBUG, "is_close()");
272 for (i = 0; i < is->num_types; i++)
278 th.blocksize = is->types[i].blocksize;
279 th.keysize = is->keysize;
280 th.freelist = is->types[i].freelist;
281 th.top = is->types[i].top;
282 if (is_rb_write(&is->types[i], &th) < 0)
284 log(LOG_FATAL, "Failed to write headerblock");
288 bf_close(is->types[i].bf);
295 static ISAM_P is_address(int type, int pos)
304 int sort_input(const void *p1, const void *p2)
308 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
310 return *((char *)p1) - *((char*)p2);
313 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
317 char keybuf[IS_MAX_RECORD];
318 int oldnum, oldtype, i;
319 char operation, *record;
322 qsort(data, num, is_keysize(is) + 1, sort_input);
323 is_m_establish_tab(is, &tab, pos);
325 if (is_m_read_full(&tab, tab.data) < 0)
327 log(LOG_FATAL, "read_full failed");
330 oldnum = tab.num_records;
331 oldtype = tab.pos_type;
334 operation = *(data)++;
335 record = (char*) data;
336 data += is_keysize(is);
338 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
340 data += 1 + is_keysize(is);
343 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
345 if (operation == KEYOP_INSERT)
347 log(LOG_DEBUG, "XXInserting new record.");
348 is_m_write_record(&tab, record);
351 log(LOG_DEBUG, "XXDeletion failed to find match.");
353 else /* match found */
355 if (operation == KEYOP_INSERT)
357 log(LOG_DEBUG, "XXSkipping insertion - match found.");
360 else if (operation == KEYOP_DELETE)
362 /* try to avoid needlessly moving data */
363 if (num && *(data) == KEYOP_INSERT)
365 /* next key is identical insert? - NOOP - skip it */
366 if (!memcmp(record, data + 1, is_keysize(is)))
368 log(LOG_DEBUG, "XXNoop delete. skipping.");
369 data += 1 + is_keysize(is);
373 /* else check if next key can fit in this position */
374 is_m_peek_record(&tab, keybuf);
375 res = (*is->cmp)(data + 1, keybuf);
378 log(LOG_DEBUG, "XXReplacing record.");
379 is_m_replace_record(&tab, data + 1);
380 data += 1 + is_keysize(is);
385 log(LOG_DEBUG, "Deleting record.");
386 is_m_delete_record(&tab);
391 while (i < tab.is->num_types - 1 && tab.num_records >
392 tab.is->types[i].max_keys)
394 if (i != tab.pos_type)
399 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
400 100) / oldnum > tab.is->repack)
407 pos = is_address(tab.pos_type, tab.data->diskpos);
411 is_m_release_tab(&tab);
416 * Locate a table of keys in an isam file. The ISPT is an individual
417 * position marker for that table.
419 ISPT is_position(ISAM is, ISAM_P pos)
424 is_m_establish_tab(is, &p->tab, pos);
431 void is_pt_free(ISPT ip)
433 is_m_release_tab(&ip->tab);
438 * Read a key from a table.
440 int is_readkey(ISPT ip, void *buf)
442 return is_m_read_record(&ip->tab, buf);
445 int is_numkeys(ISPT ip)
447 return is_m_num_records(&ip->tab);
450 void is_rewind(ISPT ip)
452 is_m_rewind(&ip->tab);