2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1995-09-04 12:33:46 adam
8 * Various cleanup. YAZ util used instead.
10 * Revision 1.10 1994/09/28 16:58:32 quinn
13 * Revision 1.9 1994/09/28 12:56:15 quinn
14 * Added access functions (ISPT)
16 * Revision 1.8 1994/09/28 12:32:17 quinn
19 * Revision 1.7 1994/09/28 11:56:25 quinn
20 * Added sort of input to is_merge
22 * Revision 1.6 1994/09/28 11:29:33 quinn
23 * Added cmp parameter.
25 * Revision 1.5 1994/09/27 20:03:50 quinn
26 * Seems relatively bug-free.
28 * Revision 1.4 1994/09/26 17:11:29 quinn
31 * Revision 1.3 1994/09/26 17:06:35 quinn
34 * Revision 1.1 1994/09/12 08:02:13 quinn
52 static int (*extcmp)(const void *p1, const void *p2);
53 static ispt_struct *ispt_freelist = 0;
55 static ISPT ispt_alloc()
62 ispt_freelist = ispt_freelist->next;
65 p = xmalloc(sizeof(ispt_struct));
69 static void ispt_free(ISPT pt)
71 pt->next = ispt_freelist;
75 static int splitargs(const char *s, char *bf[], int max)
80 while (*s && isspace(*s))
88 logf (LOG_WARN, "Ignoring extra args to is resource");
92 while (*s && !isspace(*s))
101 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
105 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
106 int num, size, rs, tmp, i;
109 logf (LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
110 new = xmalloc(sizeof(*new));
111 new->writeflag = writeflag;
112 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
113 new->types[i].index = 0; /* dummy */
115 /* determine number and size of blocktypes */
116 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
117 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
119 logf (LOG_FATAL, "Failed to locate resource %s", nm);
122 new->num_types = num;
123 for (i = 0; i < num; i++)
125 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
127 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
135 new->types[i].blocksize = size; break;
137 new->types[i].blocksize = size * 1024; break;
139 new->types[i].blocksize = size * 1048576; break;
141 logf (LOG_FATAL, "Illegal size suffix: %c", *m);
144 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
147 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
148 new->types[i].blocksize, writeflag)))
150 logf (LOG_FATAL, "bf_open failed");
153 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
155 if (th.blocksize != new->types[i].blocksize)
157 logf (LOG_FATAL, "File blocksize mismatch in %s", name);
160 new->types[i].freelist = th.freelist;
161 new->types[i].top = th.top;
163 else if (writeflag) /* write dummy superblock to determine top */
165 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
167 logf (LOG_FATAL, "Failed to write initial superblock.");
170 new->types[i].freelist = -1;
171 new->types[i].top = rs;
173 /* ELSE: this is an empty file opened in read-only mode. */
175 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
178 logf (LOG_FATAL, "Failed to locate resource %s", nm);
181 if ((new->keysize = atoi(r)) <= 0)
183 logf (LOG_FATAL, "Must specify positive keysize.");
187 /* determine repack percent */
188 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
189 0), IS_DEF_REPACK_PERCENT)))
191 logf (LOG_FATAL, "Failed to locate resource %s", nm);
194 new->repack = atoi(r);
196 /* determine max keys/blocksize */
197 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
198 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
200 logf (LOG_FATAL, "Failed to locate resource %s", nm);
203 if (num < new->num_types -1)
205 logf (LOG_FATAL, "Not enough elements in %s", nm);
208 for (i = 0; i < num; i++)
210 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
212 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
215 new->types[i].max_keys = tmp;
218 /* determine max keys/block */
219 for (i = 0; i < new->num_types; i++)
221 if (!new->types[i].index)
223 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
224 sizeof(int)) / new->keysize;
225 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
226 sizeof(int)) / new->keysize;
229 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
231 if (new->types[i].max_keys_block0 < 1)
233 logf (LOG_FATAL, "Blocksize too small in %s", name);
238 /* determine nice fill rates */
239 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
240 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
242 logf (LOG_FATAL, "Failed to locate resource %s", nm);
245 if (num < new->num_types)
247 logf (LOG_FATAL, "Not enough elements in %s", nm);
250 for (i = 0; i < num; i++)
252 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
254 logf (LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
257 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
259 if (new->types[i].nice_keys_block < 1)
260 new->types[i].nice_keys_block = 1;
263 new->cmp = cmp ? cmp : is_default_cmp;
270 int is_close(ISAM is)
275 logf (LOG_DEBUG, "is_close()");
276 for (i = 0; i < is->num_types; i++)
282 th.blocksize = is->types[i].blocksize;
283 th.keysize = is->keysize;
284 th.freelist = is->types[i].freelist;
285 th.top = is->types[i].top;
286 if (is_rb_write(&is->types[i], &th) < 0)
288 logf (LOG_FATAL, "Failed to write headerblock");
292 bf_close(is->types[i].bf);
299 static ISAM_P is_address(int type, int pos)
308 int sort_input(const void *p1, const void *p2)
312 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
314 return *((char *)p1) - *((char*)p2);
317 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
321 char keybuf[IS_MAX_RECORD];
322 int oldnum, oldtype, i;
323 char operation, *record;
326 qsort(data, num, is_keysize(is) + 1, sort_input);
327 is_m_establish_tab(is, &tab, pos);
329 if (is_m_read_full(&tab, tab.data) < 0)
331 logf (LOG_FATAL, "read_full failed");
334 oldnum = tab.num_records;
335 oldtype = tab.pos_type;
338 operation = *(data)++;
339 record = (char*) data;
340 data += is_keysize(is);
342 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
344 data += 1 + is_keysize(is);
347 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
349 if (operation == KEYOP_INSERT)
351 logf (LOG_DEBUG, "XXInserting new record.");
352 is_m_write_record(&tab, record);
355 logf (LOG_DEBUG, "XXDeletion failed to find match.");
357 else /* match found */
359 if (operation == KEYOP_INSERT)
361 logf (LOG_DEBUG, "XXSkipping insertion - match found.");
364 else if (operation == KEYOP_DELETE)
366 /* try to avoid needlessly moving data */
367 if (num && *(data) == KEYOP_INSERT)
369 /* next key is identical insert? - NOOP - skip it */
370 if (!memcmp(record, data + 1, is_keysize(is)))
372 logf (LOG_DEBUG, "XXNoop delete. skipping.");
373 data += 1 + is_keysize(is);
377 /* else check if next key can fit in this position */
378 is_m_peek_record(&tab, keybuf);
379 res = (*is->cmp)(data + 1, keybuf);
382 logf (LOG_DEBUG, "XXReplacing record.");
383 is_m_replace_record(&tab, data + 1);
384 data += 1 + is_keysize(is);
389 logf (LOG_DEBUG, "Deleting record.");
390 is_m_delete_record(&tab);
395 while (i < tab.is->num_types - 1 && tab.num_records >
396 tab.is->types[i].max_keys)
398 if (i != tab.pos_type)
403 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
404 100) / oldnum > tab.is->repack)
411 pos = is_address(tab.pos_type, tab.data->diskpos);
415 is_m_release_tab(&tab);
420 * Locate a table of keys in an isam file. The ISPT is an individual
421 * position marker for that table.
423 ISPT is_position(ISAM is, ISAM_P pos)
428 is_m_establish_tab(is, &p->tab, pos);
435 void is_pt_free(ISPT ip)
437 is_m_release_tab(&ip->tab);
442 * Read a key from a table.
444 int is_readkey(ISPT ip, void *buf)
446 return is_m_read_record(&ip->tab, buf);
449 int is_numkeys(ISPT ip)
451 return is_m_num_records(&ip->tab);
454 void is_rewind(ISPT ip)
456 is_m_rewind(&ip->tab);