2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1994-09-26 16:07:53 quinn
8 * Most of the functionality in place.
10 * Revision 1.1 1994/09/12 08:02:13 quinn
29 static int splitargs(const char *s, char *bf[], int max)
34 while (*s && isspace(*s))
42 log(LOG_WARN, "Ignoring extra args to is resource");
46 while (*s && !isspace(*s))
55 ISAM is_open(const char *name, int writeflag)
58 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
59 int num, size, rs, tmp, i;
62 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
63 new = xmalloc(sizeof(*new));
64 new->writeflag = writeflag;
65 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
66 new->types[i].index = 0; /* dummy */
68 /* determine number and size of blocktypes */
69 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
70 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
72 log(LOG_FATAL, "Failed to locate resource %s", nm);
76 for (i = 0; i < num; i++)
78 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
80 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
88 new->types[i].blocksize = size; break;
90 new->types[i].blocksize = size * 1024; break;
92 new->types[i].blocksize = size * 1048576; break;
94 log(LOG_FATAL, "Illegal size suffix: %c", *m);
97 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
100 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
101 new->types[i].blocksize, writeflag)))
103 log(LOG_FATAL, "bf_open failed");
106 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
108 if (th.blocksize != new->types[i].blocksize)
110 log(LOG_FATAL, "File blocksize mismatch in %s", name);
113 new->types[i].freelist = th.freelist;
114 new->types[i].top = th.top;
116 else if (writeflag) /* write dummy superblock to determine top */
118 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
120 log(LOG_FATAL, "Failed to write initial superblock.");
123 new->types[i].freelist = -1;
124 new->types[i].top = rs;
126 /* ELSE: this is an empty file opened in read-only mode. */
128 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
131 log(LOG_FATAL, "Failed to locate resource %s", nm);
134 if ((new->keysize = atoi(r)) <= 0)
136 log(LOG_FATAL, "Must specify positive keysize.");
140 /* determine repack percent */
141 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
142 0), IS_DEF_REPACK_PERCENT)))
144 log(LOG_FATAL, "Failed to locate resource %s", nm);
147 new->repack = atoi(r);
149 /* determine max keys/blocksize */
150 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
151 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
153 log(LOG_FATAL, "Failed to locate resource %s", nm);
156 if (num < new->num_types -1)
158 log(LOG_FATAL, "Not enough elements in %s", nm);
161 for (i = 0; i < num; i++)
163 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
165 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
168 new->types[i].max_keys = tmp;
171 /* determine max keys/block */
172 for (i = 0; i < new->num_types; i++)
174 if (!new->types[i].index)
176 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
177 sizeof(int)) / new->keysize;
178 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
179 sizeof(int)) / new->keysize;
182 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
184 if (new->types[i].max_keys_block0 < 1)
186 log(LOG_FATAL, "Blocksize too small in %s", name);
191 /* determine nice fill rates */
192 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
193 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
195 log(LOG_FATAL, "Failed to locate resource %s", nm);
198 if (num < new->num_types)
200 log(LOG_FATAL, "Not enough elements in %s", nm);
203 for (i = 0; i < num; i++)
205 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
207 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
210 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
212 if (new->types[i].nice_keys_block < 1)
213 new->types[i].nice_keys_block = 1;
216 new->cmp = is_default_cmp;
223 int is_close(ISAM is)
228 log(LOG_DEBUG, "is_close()");
229 for (i = 0; i < is->num_types; i++)
235 th.blocksize = is->types[i].blocksize;
236 th.keysize = is->keysize;
237 th.freelist = is->types[i].freelist;
238 th.top = is->types[i].top;
239 if (is_rb_write(&is->types[i], &th) < 0)
241 log(LOG_FATAL, "Failed to write headerblock");
245 bf_close(is->types[i].bf);
252 static ISAM_P is_address(int type, int pos)
261 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, const char *data)
265 char keybuf[IS_MAX_RECORD];
267 char operation, *record;
269 is_m_establish_tab(is, &tab, pos);
270 /* TODO: do something to aquire oldnum at this point */
272 if (is_m_read_full(&tab, tab.data) < 0)
274 log(LOG_FATAL, "read_full failed");
277 oldnum = tab.num_records;
278 oldtype = tab.pos_type;
281 operation = *(data)++;
282 record = (char*)data;
283 data += is_keysize(is);
285 while (num && !memcmp(record, data, is_keysize(tab.is) + 1))
287 data += 1 + is_keysize(is);
290 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
292 if (operation == KEYOP_INSERT)
294 log(LOG_DEBUG, "XXInserting new record.");
295 is_m_write_record(&tab, record);
298 log(LOG_DEBUG, "XXDeletion failed to find match.");
300 else /* match found */
302 if (operation == KEYOP_INSERT)
304 log(LOG_DEBUG, "XXSkipping insertion - match found.");
307 else if (operation == KEYOP_DELETE)
309 /* try to avoid needlessly moving data */
310 if (num && *(data) == KEYOP_INSERT)
312 /* next key is identical insert? - NOOP - skip it */
313 if (!memcmp(record, data + 1, is_keysize(is)))
315 log(LOG_DEBUG, "XXNoop delete. skipping.");
316 data += 1 + is_keysize(is);
320 /* else check if next key can fit in this position */
321 is_m_peek_record(&tab, keybuf);
322 res = (*is->cmp)(data + 1, keybuf);
325 log(LOG_DEBUG, "XXReplacing record.");
326 is_m_replace_record(&tab, data + 1);
327 data += 1 + is_keysize(is);
332 log(LOG_DEBUG, "Deleting record.");
333 is_m_delete_record(&tab);
337 while (tab.pos_type < tab.is->num_types - 1 && tab.num_records >
338 tab.is->types[tab.pos_type].max_keys)
340 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
341 100) / oldnum > tab.is->repack)
346 return is_address(tab.pos_type, tab.data->diskpos);