2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1994-09-28 12:32:17 quinn
10 * Revision 1.7 1994/09/28 11:56:25 quinn
11 * Added sort of input to is_merge
13 * Revision 1.6 1994/09/28 11:29:33 quinn
14 * Added cmp parameter.
16 * Revision 1.5 1994/09/27 20:03:50 quinn
17 * Seems relatively bug-free.
19 * Revision 1.4 1994/09/26 17:11:29 quinn
22 * Revision 1.3 1994/09/26 17:06:35 quinn
25 * Revision 1.1 1994/09/12 08:02:13 quinn
42 static int (*extcmp)(const void *p1, const void *p2);
44 static int splitargs(const char *s, char *bf[], int max)
49 while (*s && isspace(*s))
57 log(LOG_WARN, "Ignoring extra args to is resource");
61 while (*s && !isspace(*s))
70 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
74 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
75 int num, size, rs, tmp, i;
78 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
79 new = xmalloc(sizeof(*new));
80 new->writeflag = writeflag;
81 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
82 new->types[i].index = 0; /* dummy */
84 /* determine number and size of blocktypes */
85 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
86 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
88 log(LOG_FATAL, "Failed to locate resource %s", nm);
92 for (i = 0; i < num; i++)
94 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
96 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
104 new->types[i].blocksize = size; break;
106 new->types[i].blocksize = size * 1024; break;
108 new->types[i].blocksize = size * 1048576; break;
110 log(LOG_FATAL, "Illegal size suffix: %c", *m);
113 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
116 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
117 new->types[i].blocksize, writeflag)))
119 log(LOG_FATAL, "bf_open failed");
122 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
124 if (th.blocksize != new->types[i].blocksize)
126 log(LOG_FATAL, "File blocksize mismatch in %s", name);
129 new->types[i].freelist = th.freelist;
130 new->types[i].top = th.top;
132 else if (writeflag) /* write dummy superblock to determine top */
134 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
136 log(LOG_FATAL, "Failed to write initial superblock.");
139 new->types[i].freelist = -1;
140 new->types[i].top = rs;
142 /* ELSE: this is an empty file opened in read-only mode. */
144 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
147 log(LOG_FATAL, "Failed to locate resource %s", nm);
150 if ((new->keysize = atoi(r)) <= 0)
152 log(LOG_FATAL, "Must specify positive keysize.");
156 /* determine repack percent */
157 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
158 0), IS_DEF_REPACK_PERCENT)))
160 log(LOG_FATAL, "Failed to locate resource %s", nm);
163 new->repack = atoi(r);
165 /* determine max keys/blocksize */
166 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
167 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
169 log(LOG_FATAL, "Failed to locate resource %s", nm);
172 if (num < new->num_types -1)
174 log(LOG_FATAL, "Not enough elements in %s", nm);
177 for (i = 0; i < num; i++)
179 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
181 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
184 new->types[i].max_keys = tmp;
187 /* determine max keys/block */
188 for (i = 0; i < new->num_types; i++)
190 if (!new->types[i].index)
192 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
193 sizeof(int)) / new->keysize;
194 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
195 sizeof(int)) / new->keysize;
198 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
200 if (new->types[i].max_keys_block0 < 1)
202 log(LOG_FATAL, "Blocksize too small in %s", name);
207 /* determine nice fill rates */
208 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
209 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
211 log(LOG_FATAL, "Failed to locate resource %s", nm);
214 if (num < new->num_types)
216 log(LOG_FATAL, "Not enough elements in %s", nm);
219 for (i = 0; i < num; i++)
221 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
223 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
226 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
228 if (new->types[i].nice_keys_block < 1)
229 new->types[i].nice_keys_block = 1;
232 new->cmp = cmp ? cmp : is_default_cmp;
239 int is_close(ISAM is)
244 log(LOG_DEBUG, "is_close()");
245 for (i = 0; i < is->num_types; i++)
251 th.blocksize = is->types[i].blocksize;
252 th.keysize = is->keysize;
253 th.freelist = is->types[i].freelist;
254 th.top = is->types[i].top;
255 if (is_rb_write(&is->types[i], &th) < 0)
257 log(LOG_FATAL, "Failed to write headerblock");
261 bf_close(is->types[i].bf);
268 static ISAM_P is_address(int type, int pos)
277 int sort_input(const void *p1, const void *p2)
281 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
283 return *((char *)p1) - *((char*)p2);
286 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
290 char keybuf[IS_MAX_RECORD];
291 int oldnum, oldtype, i;
292 char operation, *record;
295 qsort(data, num, is_keysize(is) + 1, sort_input);
296 is_m_establish_tab(is, &tab, pos);
298 if (is_m_read_full(&tab, tab.data) < 0)
300 log(LOG_FATAL, "read_full failed");
303 oldnum = tab.num_records;
304 oldtype = tab.pos_type;
307 operation = *(data)++;
308 record = (char*) data;
309 data += is_keysize(is);
311 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
313 data += 1 + is_keysize(is);
316 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
318 if (operation == KEYOP_INSERT)
320 log(LOG_DEBUG, "XXInserting new record.");
321 is_m_write_record(&tab, record);
324 log(LOG_DEBUG, "XXDeletion failed to find match.");
326 else /* match found */
328 if (operation == KEYOP_INSERT)
330 log(LOG_DEBUG, "XXSkipping insertion - match found.");
333 else if (operation == KEYOP_DELETE)
335 /* try to avoid needlessly moving data */
336 if (num && *(data) == KEYOP_INSERT)
338 /* next key is identical insert? - NOOP - skip it */
339 if (!memcmp(record, data + 1, is_keysize(is)))
341 log(LOG_DEBUG, "XXNoop delete. skipping.");
342 data += 1 + is_keysize(is);
346 /* else check if next key can fit in this position */
347 is_m_peek_record(&tab, keybuf);
348 res = (*is->cmp)(data + 1, keybuf);
351 log(LOG_DEBUG, "XXReplacing record.");
352 is_m_replace_record(&tab, data + 1);
353 data += 1 + is_keysize(is);
358 log(LOG_DEBUG, "Deleting record.");
359 is_m_delete_record(&tab);
364 while (i < tab.is->num_types - 1 && tab.num_records >
365 tab.is->types[i].max_keys)
367 if (i != tab.pos_type)
372 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
373 100) / oldnum > tab.is->repack)
380 pos = is_address(tab.pos_type, tab.data->diskpos);
384 is_m_release_tab(&tab);
389 * Locate a table of keys in an isam file. The ISPT is an individual
390 * position marker for that table.
392 ISPT is_position(ISAM is, ISAM_P pos);
397 void is_pt_free(ISPT ip);