2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1994-09-28 11:29:33 quinn
10 * Revision 1.5 1994/09/27 20:03:50 quinn
11 * Seems relatively bug-free.
13 * Revision 1.4 1994/09/26 17:11:29 quinn
16 * Revision 1.3 1994/09/26 17:06:35 quinn
19 * Revision 1.1 1994/09/12 08:02:13 quinn
36 static int splitargs(const char *s, char *bf[], int max)
41 while (*s && isspace(*s))
49 log(LOG_WARN, "Ignoring extra args to is resource");
53 while (*s && !isspace(*s))
62 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
66 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
67 int num, size, rs, tmp, i;
70 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
71 new = xmalloc(sizeof(*new));
72 new->writeflag = writeflag;
73 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
74 new->types[i].index = 0; /* dummy */
76 /* determine number and size of blocktypes */
77 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
78 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
80 log(LOG_FATAL, "Failed to locate resource %s", nm);
84 for (i = 0; i < num; i++)
86 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
88 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
96 new->types[i].blocksize = size; break;
98 new->types[i].blocksize = size * 1024; break;
100 new->types[i].blocksize = size * 1048576; break;
102 log(LOG_FATAL, "Illegal size suffix: %c", *m);
105 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
108 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
109 new->types[i].blocksize, writeflag)))
111 log(LOG_FATAL, "bf_open failed");
114 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
116 if (th.blocksize != new->types[i].blocksize)
118 log(LOG_FATAL, "File blocksize mismatch in %s", name);
121 new->types[i].freelist = th.freelist;
122 new->types[i].top = th.top;
124 else if (writeflag) /* write dummy superblock to determine top */
126 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
128 log(LOG_FATAL, "Failed to write initial superblock.");
131 new->types[i].freelist = -1;
132 new->types[i].top = rs;
134 /* ELSE: this is an empty file opened in read-only mode. */
136 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
139 log(LOG_FATAL, "Failed to locate resource %s", nm);
142 if ((new->keysize = atoi(r)) <= 0)
144 log(LOG_FATAL, "Must specify positive keysize.");
148 /* determine repack percent */
149 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
150 0), IS_DEF_REPACK_PERCENT)))
152 log(LOG_FATAL, "Failed to locate resource %s", nm);
155 new->repack = atoi(r);
157 /* determine max keys/blocksize */
158 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
159 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
161 log(LOG_FATAL, "Failed to locate resource %s", nm);
164 if (num < new->num_types -1)
166 log(LOG_FATAL, "Not enough elements in %s", nm);
169 for (i = 0; i < num; i++)
171 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
173 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
176 new->types[i].max_keys = tmp;
179 /* determine max keys/block */
180 for (i = 0; i < new->num_types; i++)
182 if (!new->types[i].index)
184 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
185 sizeof(int)) / new->keysize;
186 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
187 sizeof(int)) / new->keysize;
190 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
192 if (new->types[i].max_keys_block0 < 1)
194 log(LOG_FATAL, "Blocksize too small in %s", name);
199 /* determine nice fill rates */
200 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
201 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
203 log(LOG_FATAL, "Failed to locate resource %s", nm);
206 if (num < new->num_types)
208 log(LOG_FATAL, "Not enough elements in %s", nm);
211 for (i = 0; i < num; i++)
213 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
215 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
218 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
220 if (new->types[i].nice_keys_block < 1)
221 new->types[i].nice_keys_block = 1;
224 new->cmp = cmp ? cmp : is_default_cmp;
231 int is_close(ISAM is)
236 log(LOG_DEBUG, "is_close()");
237 for (i = 0; i < is->num_types; i++)
243 th.blocksize = is->types[i].blocksize;
244 th.keysize = is->keysize;
245 th.freelist = is->types[i].freelist;
246 th.top = is->types[i].top;
247 if (is_rb_write(&is->types[i], &th) < 0)
249 log(LOG_FATAL, "Failed to write headerblock");
253 bf_close(is->types[i].bf);
260 static ISAM_P is_address(int type, int pos)
269 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, const char *data)
273 char keybuf[IS_MAX_RECORD];
274 int oldnum, oldtype, i;
275 char operation, *record;
277 is_m_establish_tab(is, &tab, pos);
278 /* TODO: do something to aquire oldnum at this point */
280 if (is_m_read_full(&tab, tab.data) < 0)
282 log(LOG_FATAL, "read_full failed");
285 oldnum = tab.num_records;
286 oldtype = tab.pos_type;
289 operation = *(data)++;
290 record = (char*) data;
291 data += is_keysize(is);
293 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
295 data += 1 + is_keysize(is);
298 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
300 if (operation == KEYOP_INSERT)
302 log(LOG_DEBUG, "XXInserting new record.");
303 is_m_write_record(&tab, record);
306 log(LOG_DEBUG, "XXDeletion failed to find match.");
308 else /* match found */
310 if (operation == KEYOP_INSERT)
312 log(LOG_DEBUG, "XXSkipping insertion - match found.");
315 else if (operation == KEYOP_DELETE)
317 /* try to avoid needlessly moving data */
318 if (num && *(data) == KEYOP_INSERT)
320 /* next key is identical insert? - NOOP - skip it */
321 if (!memcmp(record, data + 1, is_keysize(is)))
323 log(LOG_DEBUG, "XXNoop delete. skipping.");
324 data += 1 + is_keysize(is);
328 /* else check if next key can fit in this position */
329 is_m_peek_record(&tab, keybuf);
330 res = (*is->cmp)(data + 1, keybuf);
333 log(LOG_DEBUG, "XXReplacing record.");
334 is_m_replace_record(&tab, data + 1);
335 data += 1 + is_keysize(is);
340 log(LOG_DEBUG, "Deleting record.");
341 is_m_delete_record(&tab);
346 while (i < tab.is->num_types - 1 && tab.num_records >
347 tab.is->types[i].max_keys)
349 if (i != tab.pos_type)
354 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
355 100) / oldnum > tab.is->repack)
362 pos = is_address(tab.pos_type, tab.data->diskpos);
366 is_m_release_tab(&tab);
371 * Locate a table of keys in an isam file. The ISPT is an individual
372 * position marker for that table.
374 ISPT is_position(ISAM is, ISAM_P pos);
379 void is_pt_free(ISPT ip);