2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-09-06 16:11:41 adam
8 * Keysize parameter to is_open (if non-zero).
10 * Revision 1.11 1995/09/04 12:33:46 adam
11 * Various cleanup. YAZ util used instead.
13 * Revision 1.10 1994/09/28 16:58:32 quinn
16 * Revision 1.9 1994/09/28 12:56:15 quinn
17 * Added access functions (ISPT)
19 * Revision 1.8 1994/09/28 12:32:17 quinn
22 * Revision 1.7 1994/09/28 11:56:25 quinn
23 * Added sort of input to is_merge
25 * Revision 1.6 1994/09/28 11:29:33 quinn
26 * Added cmp parameter.
28 * Revision 1.5 1994/09/27 20:03:50 quinn
29 * Seems relatively bug-free.
31 * Revision 1.4 1994/09/26 17:11:29 quinn
34 * Revision 1.3 1994/09/26 17:06:35 quinn
37 * Revision 1.1 1994/09/12 08:02:13 quinn
55 static int (*extcmp)(const void *p1, const void *p2);
56 static ispt_struct *ispt_freelist = 0;
58 static ISPT ispt_alloc()
65 ispt_freelist = ispt_freelist->next;
68 p = xmalloc(sizeof(ispt_struct));
72 static void ispt_free(ISPT pt)
74 pt->next = ispt_freelist;
78 static int splitargs(const char *s, char *bf[], int max)
83 while (*s && isspace(*s))
91 logf (LOG_WARN, "Ignoring extra args to is resource");
95 while (*s && !isspace(*s))
104 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
105 int writeflag, int keysize)
108 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
109 int num, size, rs, tmp, i;
112 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
113 new = xmalloc(sizeof(*new));
114 new->writeflag = writeflag;
115 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
116 new->types[i].index = 0; /* dummy */
118 /* determine number and size of blocktypes */
119 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
120 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
122 logf (LOG_FATAL, "Failed to locate resource %s", nm);
125 new->num_types = num;
126 for (i = 0; i < num; i++)
128 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
130 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
138 new->types[i].blocksize = size; break;
140 new->types[i].blocksize = size * 1024; break;
142 new->types[i].blocksize = size * 1048576; break;
144 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
147 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
150 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
151 new->types[i].blocksize, writeflag)))
153 logf (LOG_FATAL, "bf_open failed");
156 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
158 if (th.blocksize != new->types[i].blocksize)
160 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
163 new->types[i].freelist = th.freelist;
164 new->types[i].top = th.top;
166 else if (writeflag) /* write dummy superblock to determine top */
168 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
170 logf (LOG_FATAL, "Failed to write initial superblock.");
173 new->types[i].freelist = -1;
174 new->types[i].top = rs;
176 /* ELSE: this is an empty file opened in read-only mode. */
179 new->keysize = keysize;
182 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".",
186 logf (LOG_FATAL, "Failed to locate resource %s", nm);
189 if ((new->keysize = atoi(r)) <= 0)
191 logf (LOG_FATAL, "Must specify positive keysize.");
196 /* determine repack percent */
197 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
198 0), IS_DEF_REPACK_PERCENT)))
200 logf (LOG_FATAL, "Failed to locate resource %s", nm);
203 new->repack = atoi(r);
205 /* determine max keys/blocksize */
206 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
207 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
209 logf (LOG_FATAL, "Failed to locate resource %s", nm);
212 if (num < new->num_types -1)
214 logf (LOG_FATAL, "Not enough elements in %s", nm);
217 for (i = 0; i < num; i++)
219 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
221 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
224 new->types[i].max_keys = tmp;
227 /* determine max keys/block */
228 for (i = 0; i < new->num_types; i++)
230 if (!new->types[i].index)
232 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
233 sizeof(int)) / new->keysize;
234 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
235 sizeof(int)) / new->keysize;
238 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
240 if (new->types[i].max_keys_block0 < 1)
242 logf (LOG_FATAL, "Blocksize too small in %s", name);
247 /* determine nice fill rates */
248 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
249 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
251 logf (LOG_FATAL, "Failed to locate resource %s", nm);
254 if (num < new->num_types)
256 logf (LOG_FATAL, "Not enough elements in %s", nm);
259 for (i = 0; i < num; i++)
261 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
263 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
266 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
268 if (new->types[i].nice_keys_block < 1)
269 new->types[i].nice_keys_block = 1;
272 new->cmp = cmp ? cmp : is_default_cmp;
279 int is_close(ISAM is)
284 logf (LOG_DEBUG, "is_close()");
285 for (i = 0; i < is->num_types; i++)
291 th.blocksize = is->types[i].blocksize;
292 th.keysize = is->keysize;
293 th.freelist = is->types[i].freelist;
294 th.top = is->types[i].top;
295 if (is_rb_write(&is->types[i], &th) < 0)
297 logf (LOG_FATAL, "Failed to write headerblock");
301 bf_close(is->types[i].bf);
308 static ISAM_P is_address(int type, int pos)
317 int sort_input(const void *p1, const void *p2)
321 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
323 return *((char *)p1) - *((char*)p2);
326 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
330 char keybuf[IS_MAX_RECORD];
331 int oldnum, oldtype, i;
332 char operation, *record;
335 qsort(data, num, is_keysize(is) + 1, sort_input);
336 is_m_establish_tab(is, &tab, pos);
338 if (is_m_read_full(&tab, tab.data) < 0)
340 logf (LOG_FATAL, "read_full failed");
343 oldnum = tab.num_records;
344 oldtype = tab.pos_type;
347 operation = *(data)++;
348 record = (char*) data;
349 data += is_keysize(is);
351 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
353 data += 1 + is_keysize(is);
356 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
358 if (operation == KEYOP_INSERT)
360 logf (LOG_DEBUG, "XXInserting new record.");
361 is_m_write_record(&tab, record);
364 logf (LOG_DEBUG, "XXDeletion failed to find match.");
366 else /* match found */
368 if (operation == KEYOP_INSERT)
370 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
373 else if (operation == KEYOP_DELETE)
375 /* try to avoid needlessly moving data */
376 if (num && *(data) == KEYOP_INSERT)
378 /* next key is identical insert? - NOOP - skip it */
379 if (!memcmp(record, data + 1, is_keysize(is)))
381 logf (LOG_DEBUG, "XXNoop delete. skipping.");
382 data += 1 + is_keysize(is);
386 /* else check if next key can fit in this position */
387 is_m_peek_record(&tab, keybuf);
388 res = (*is->cmp)(data + 1, keybuf);
391 logf (LOG_DEBUG, "XXReplacing record.");
392 is_m_replace_record(&tab, data + 1);
393 data += 1 + is_keysize(is);
398 logf (LOG_DEBUG, "Deleting record.");
399 is_m_delete_record(&tab);
404 while (i < tab.is->num_types - 1 && tab.num_records >
405 tab.is->types[i].max_keys)
407 if (i != tab.pos_type)
412 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
413 100) / oldnum > tab.is->repack)
420 pos = is_address(tab.pos_type, tab.data->diskpos);
424 is_m_release_tab(&tab);
429 * Locate a table of keys in an isam file. The ISPT is an individual
430 * position marker for that table.
432 ISPT is_position(ISAM is, ISAM_P pos)
437 is_m_establish_tab(is, &p->tab, pos);
444 void is_pt_free(ISPT ip)
446 is_m_release_tab(&ip->tab);
451 * Read a key from a table.
453 int is_readkey(ISPT ip, void *buf)
455 return is_m_read_record(&ip->tab, buf);
458 int is_numkeys(ISPT ip)
460 return is_m_num_records(&ip->tab);
463 void is_rewind(ISPT ip)
465 is_m_rewind(&ip->tab);