2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1994-09-27 20:03:50 quinn
8 * Seems relatively bug-free.
10 * Revision 1.4 1994/09/26 17:11:29 quinn
13 * Revision 1.3 1994/09/26 17:06:35 quinn
16 * Revision 1.1 1994/09/12 08:02:13 quinn
33 static int splitargs(const char *s, char *bf[], int max)
38 while (*s && isspace(*s))
46 log(LOG_WARN, "Ignoring extra args to is resource");
50 while (*s && !isspace(*s))
59 ISAM is_open(const char *name, int writeflag)
62 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
63 int num, size, rs, tmp, i;
66 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
67 new = xmalloc(sizeof(*new));
68 new->writeflag = writeflag;
69 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
70 new->types[i].index = 0; /* dummy */
72 /* determine number and size of blocktypes */
73 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
74 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
76 log(LOG_FATAL, "Failed to locate resource %s", nm);
80 for (i = 0; i < num; i++)
82 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
84 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
92 new->types[i].blocksize = size; break;
94 new->types[i].blocksize = size * 1024; break;
96 new->types[i].blocksize = size * 1048576; break;
98 log(LOG_FATAL, "Illegal size suffix: %c", *m);
101 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
104 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
105 new->types[i].blocksize, writeflag)))
107 log(LOG_FATAL, "bf_open failed");
110 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
112 if (th.blocksize != new->types[i].blocksize)
114 log(LOG_FATAL, "File blocksize mismatch in %s", name);
117 new->types[i].freelist = th.freelist;
118 new->types[i].top = th.top;
120 else if (writeflag) /* write dummy superblock to determine top */
122 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
124 log(LOG_FATAL, "Failed to write initial superblock.");
127 new->types[i].freelist = -1;
128 new->types[i].top = rs;
130 /* ELSE: this is an empty file opened in read-only mode. */
132 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
135 log(LOG_FATAL, "Failed to locate resource %s", nm);
138 if ((new->keysize = atoi(r)) <= 0)
140 log(LOG_FATAL, "Must specify positive keysize.");
144 /* determine repack percent */
145 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
146 0), IS_DEF_REPACK_PERCENT)))
148 log(LOG_FATAL, "Failed to locate resource %s", nm);
151 new->repack = atoi(r);
153 /* determine max keys/blocksize */
154 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
155 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
157 log(LOG_FATAL, "Failed to locate resource %s", nm);
160 if (num < new->num_types -1)
162 log(LOG_FATAL, "Not enough elements in %s", nm);
165 for (i = 0; i < num; i++)
167 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
169 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
172 new->types[i].max_keys = tmp;
175 /* determine max keys/block */
176 for (i = 0; i < new->num_types; i++)
178 if (!new->types[i].index)
180 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
181 sizeof(int)) / new->keysize;
182 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
183 sizeof(int)) / new->keysize;
186 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
188 if (new->types[i].max_keys_block0 < 1)
190 log(LOG_FATAL, "Blocksize too small in %s", name);
195 /* determine nice fill rates */
196 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
197 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
199 log(LOG_FATAL, "Failed to locate resource %s", nm);
202 if (num < new->num_types)
204 log(LOG_FATAL, "Not enough elements in %s", nm);
207 for (i = 0; i < num; i++)
209 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
211 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
214 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
216 if (new->types[i].nice_keys_block < 1)
217 new->types[i].nice_keys_block = 1;
220 new->cmp = is_default_cmp;
227 int is_close(ISAM is)
232 log(LOG_DEBUG, "is_close()");
233 for (i = 0; i < is->num_types; i++)
239 th.blocksize = is->types[i].blocksize;
240 th.keysize = is->keysize;
241 th.freelist = is->types[i].freelist;
242 th.top = is->types[i].top;
243 if (is_rb_write(&is->types[i], &th) < 0)
245 log(LOG_FATAL, "Failed to write headerblock");
249 bf_close(is->types[i].bf);
256 static ISAM_P is_address(int type, int pos)
265 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, const char *data)
269 char keybuf[IS_MAX_RECORD];
270 int oldnum, oldtype, i;
271 char operation, *record;
273 is_m_establish_tab(is, &tab, pos);
274 /* TODO: do something to aquire oldnum at this point */
276 if (is_m_read_full(&tab, tab.data) < 0)
278 log(LOG_FATAL, "read_full failed");
281 oldnum = tab.num_records;
282 oldtype = tab.pos_type;
285 operation = *(data)++;
286 record = (char*) data;
287 data += is_keysize(is);
289 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
291 data += 1 + is_keysize(is);
294 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
296 if (operation == KEYOP_INSERT)
298 log(LOG_DEBUG, "XXInserting new record.");
299 is_m_write_record(&tab, record);
302 log(LOG_DEBUG, "XXDeletion failed to find match.");
304 else /* match found */
306 if (operation == KEYOP_INSERT)
308 log(LOG_DEBUG, "XXSkipping insertion - match found.");
311 else if (operation == KEYOP_DELETE)
313 /* try to avoid needlessly moving data */
314 if (num && *(data) == KEYOP_INSERT)
316 /* next key is identical insert? - NOOP - skip it */
317 if (!memcmp(record, data + 1, is_keysize(is)))
319 log(LOG_DEBUG, "XXNoop delete. skipping.");
320 data += 1 + is_keysize(is);
324 /* else check if next key can fit in this position */
325 is_m_peek_record(&tab, keybuf);
326 res = (*is->cmp)(data + 1, keybuf);
329 log(LOG_DEBUG, "XXReplacing record.");
330 is_m_replace_record(&tab, data + 1);
331 data += 1 + is_keysize(is);
336 log(LOG_DEBUG, "Deleting record.");
337 is_m_delete_record(&tab);
342 while (i < tab.is->num_types - 1 && tab.num_records >
343 tab.is->types[i].max_keys)
345 if (i != tab.pos_type)
350 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
351 100) / oldnum > tab.is->repack)
358 pos = is_address(tab.pos_type, tab.data->diskpos);
362 is_m_release_tab(&tab);
367 * Locate a table of keys in an isam file. The ISPT is an individual
368 * position marker for that table.
370 ISPT is_position(ISAM is, ISAM_P pos);
375 void is_pt_free(ISPT ip);