2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1994-09-26 17:11:29 quinn
10 * Revision 1.3 1994/09/26 17:06:35 quinn
13 * Revision 1.1 1994/09/12 08:02:13 quinn
32 static int splitargs(const char *s, char *bf[], int max)
37 while (*s && isspace(*s))
45 log(LOG_WARN, "Ignoring extra args to is resource");
49 while (*s && !isspace(*s))
58 ISAM is_open(const char *name, int writeflag)
61 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
62 int num, size, rs, tmp, i;
65 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
66 new = xmalloc(sizeof(*new));
67 new->writeflag = writeflag;
68 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
69 new->types[i].index = 0; /* dummy */
71 /* determine number and size of blocktypes */
72 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
73 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
75 log(LOG_FATAL, "Failed to locate resource %s", nm);
79 for (i = 0; i < num; i++)
81 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
83 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
91 new->types[i].blocksize = size; break;
93 new->types[i].blocksize = size * 1024; break;
95 new->types[i].blocksize = size * 1048576; break;
97 log(LOG_FATAL, "Illegal size suffix: %c", *m);
100 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
103 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
104 new->types[i].blocksize, writeflag)))
106 log(LOG_FATAL, "bf_open failed");
109 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
111 if (th.blocksize != new->types[i].blocksize)
113 log(LOG_FATAL, "File blocksize mismatch in %s", name);
116 new->types[i].freelist = th.freelist;
117 new->types[i].top = th.top;
119 else if (writeflag) /* write dummy superblock to determine top */
121 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
123 log(LOG_FATAL, "Failed to write initial superblock.");
126 new->types[i].freelist = -1;
127 new->types[i].top = rs;
129 /* ELSE: this is an empty file opened in read-only mode. */
131 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
134 log(LOG_FATAL, "Failed to locate resource %s", nm);
137 if ((new->keysize = atoi(r)) <= 0)
139 log(LOG_FATAL, "Must specify positive keysize.");
143 /* determine repack percent */
144 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
145 0), IS_DEF_REPACK_PERCENT)))
147 log(LOG_FATAL, "Failed to locate resource %s", nm);
150 new->repack = atoi(r);
152 /* determine max keys/blocksize */
153 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
154 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
156 log(LOG_FATAL, "Failed to locate resource %s", nm);
159 if (num < new->num_types -1)
161 log(LOG_FATAL, "Not enough elements in %s", nm);
164 for (i = 0; i < num; i++)
166 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
168 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
171 new->types[i].max_keys = tmp;
174 /* determine max keys/block */
175 for (i = 0; i < new->num_types; i++)
177 if (!new->types[i].index)
179 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
180 sizeof(int)) / new->keysize;
181 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
182 sizeof(int)) / new->keysize;
185 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
187 if (new->types[i].max_keys_block0 < 1)
189 log(LOG_FATAL, "Blocksize too small in %s", name);
194 /* determine nice fill rates */
195 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
196 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
198 log(LOG_FATAL, "Failed to locate resource %s", nm);
201 if (num < new->num_types)
203 log(LOG_FATAL, "Not enough elements in %s", nm);
206 for (i = 0; i < num; i++)
208 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
210 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
213 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
215 if (new->types[i].nice_keys_block < 1)
216 new->types[i].nice_keys_block = 1;
219 new->cmp = is_default_cmp;
226 int is_close(ISAM is)
231 log(LOG_DEBUG, "is_close()");
232 for (i = 0; i < is->num_types; i++)
238 th.blocksize = is->types[i].blocksize;
239 th.keysize = is->keysize;
240 th.freelist = is->types[i].freelist;
241 th.top = is->types[i].top;
242 if (is_rb_write(&is->types[i], &th) < 0)
244 log(LOG_FATAL, "Failed to write headerblock");
248 bf_close(is->types[i].bf);
255 static ISAM_P is_address(int type, int pos)
264 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, const char *data)
268 char keybuf[IS_MAX_RECORD];
270 char operation, *record;
272 is_m_establish_tab(is, &tab, pos);
273 /* TODO: do something to aquire oldnum at this point */
275 if (is_m_read_full(&tab, tab.data) < 0)
277 log(LOG_FATAL, "read_full failed");
280 oldnum = tab.num_records;
281 oldtype = tab.pos_type;
284 operation = *(data)++;
285 record = (char*)data;
286 data += is_keysize(is);
288 while (num && !memcmp(record, data, is_keysize(tab.is) + 1))
290 data += 1 + is_keysize(is);
293 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
295 if (operation == KEYOP_INSERT)
297 log(LOG_DEBUG, "XXInserting new record.");
298 is_m_write_record(&tab, record);
301 log(LOG_DEBUG, "XXDeletion failed to find match.");
303 else /* match found */
305 if (operation == KEYOP_INSERT)
307 log(LOG_DEBUG, "XXSkipping insertion - match found.");
310 else if (operation == KEYOP_DELETE)
312 /* try to avoid needlessly moving data */
313 if (num && *(data) == KEYOP_INSERT)
315 /* next key is identical insert? - NOOP - skip it */
316 if (!memcmp(record, data + 1, is_keysize(is)))
318 log(LOG_DEBUG, "XXNoop delete. skipping.");
319 data += 1 + is_keysize(is);
323 /* else check if next key can fit in this position */
324 is_m_peek_record(&tab, keybuf);
325 res = (*is->cmp)(data + 1, keybuf);
328 log(LOG_DEBUG, "XXReplacing record.");
329 is_m_replace_record(&tab, data + 1);
330 data += 1 + is_keysize(is);
335 log(LOG_DEBUG, "Deleting record.");
336 is_m_delete_record(&tab);
340 while (tab.pos_type < tab.is->num_types - 1 && tab.num_records >
341 tab.is->types[tab.pos_type].max_keys)
343 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
344 100) / oldnum > tab.is->repack)
349 return is_address(tab.pos_type, tab.data->diskpos);
353 * Locate a table of keys in an isam file. The ISPT is an individual
354 * position marker for that table.
356 ISPT is_position(ISAM is, ISAM_P pos);
361 void is_pt_free(ISPT ip);