2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1994-09-28 11:56:25 quinn
8 * Added sort of input to is_merge
10 * Revision 1.6 1994/09/28 11:29:33 quinn
11 * Added cmp parameter.
13 * Revision 1.5 1994/09/27 20:03:50 quinn
14 * Seems relatively bug-free.
16 * Revision 1.4 1994/09/26 17:11:29 quinn
19 * Revision 1.3 1994/09/26 17:06:35 quinn
22 * Revision 1.1 1994/09/12 08:02:13 quinn
39 static int (*extcmp)(const void *p1, const void *p2);
41 static int splitargs(const char *s, char *bf[], int max)
46 while (*s && isspace(*s))
54 log(LOG_WARN, "Ignoring extra args to is resource");
58 while (*s && !isspace(*s))
67 ISAM is_open(const char *name, int (*cmp)(const void *p1, const void *p2),
71 char *nm, *r, *pp[IS_MAX_BLOCKTYPES+1], m[2];
72 int num, size, rs, tmp, i;
75 log(LOG_DEBUG, "is_open(%s, %s)", name, writeflag ? "RW" : "RDONLY");
76 new = xmalloc(sizeof(*new));
77 new->writeflag = writeflag;
78 for (i = 0; i < IS_MAX_BLOCKTYPES; i++)
79 new->types[i].index = 0; /* dummy */
81 /* determine number and size of blocktypes */
82 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
83 "blocktypes", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
85 log(LOG_FATAL, "Failed to locate resource %s", nm);
89 for (i = 0; i < num; i++)
91 if ((rs = sscanf(pp[i], "%d%1[bBkKmM]", &size, m)) < 1)
93 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
101 new->types[i].blocksize = size; break;
103 new->types[i].blocksize = size * 1024; break;
105 new->types[i].blocksize = size * 1048576; break;
107 log(LOG_FATAL, "Illegal size suffix: %c", *m);
110 new->types[i].dbuf = xmalloc(new->types[i].blocksize);
113 if (!(new->types[i].bf = bf_open(strconcat(name, m, 0),
114 new->types[i].blocksize, writeflag)))
116 log(LOG_FATAL, "bf_open failed");
119 if ((rs = is_rb_read(&new->types[i], &th)) > 0)
121 if (th.blocksize != new->types[i].blocksize)
123 log(LOG_FATAL, "File blocksize mismatch in %s", name);
126 new->types[i].freelist = th.freelist;
127 new->types[i].top = th.top;
129 else if (writeflag) /* write dummy superblock to determine top */
131 if ((rs = is_rb_write(&new->types[i], &th)) <=0) /* dummy */
133 log(LOG_FATAL, "Failed to write initial superblock.");
136 new->types[i].freelist = -1;
137 new->types[i].top = rs;
139 /* ELSE: this is an empty file opened in read-only mode. */
141 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "keysize",
144 log(LOG_FATAL, "Failed to locate resource %s", nm);
147 if ((new->keysize = atoi(r)) <= 0)
149 log(LOG_FATAL, "Must specify positive keysize.");
153 /* determine repack percent */
154 if (!(r = res_get_def(common_resource, nm = strconcat(name, ".", "repack",
155 0), IS_DEF_REPACK_PERCENT)))
157 log(LOG_FATAL, "Failed to locate resource %s", nm);
160 new->repack = atoi(r);
162 /* determine max keys/blocksize */
163 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
164 "maxkeys", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
166 log(LOG_FATAL, "Failed to locate resource %s", nm);
169 if (num < new->num_types -1)
171 log(LOG_FATAL, "Not enough elements in %s", nm);
174 for (i = 0; i < num; i++)
176 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
178 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
181 new->types[i].max_keys = tmp;
184 /* determine max keys/block */
185 for (i = 0; i < new->num_types; i++)
187 if (!new->types[i].index)
189 new->types[i].max_keys_block = (new->types[i].blocksize - 2 *
190 sizeof(int)) / new->keysize;
191 new->types[i].max_keys_block0 = (new->types[i].blocksize - 3 *
192 sizeof(int)) / new->keysize;
195 new->types[i].max_keys_block = new->types[i].max_keys_block0 /
197 if (new->types[i].max_keys_block0 < 1)
199 log(LOG_FATAL, "Blocksize too small in %s", name);
204 /* determine nice fill rates */
205 if (!(r = res_get(common_resource, nm = strconcat(name, ".",
206 "nicefill", 0))) || !(num = splitargs(r, pp, IS_MAX_BLOCKTYPES)))
208 log(LOG_FATAL, "Failed to locate resource %s", nm);
211 if (num < new->num_types)
213 log(LOG_FATAL, "Not enough elements in %s", nm);
216 for (i = 0; i < num; i++)
218 if ((rs = sscanf(pp[i], "%d", &tmp)) < 1)
220 log(LOG_FATAL, "Error in resource %s: %s", r, pp[i]);
223 new->types[i].nice_keys_block = (new->types[i].max_keys_block0 * tmp) /
225 if (new->types[i].nice_keys_block < 1)
226 new->types[i].nice_keys_block = 1;
229 new->cmp = cmp ? cmp : is_default_cmp;
236 int is_close(ISAM is)
241 log(LOG_DEBUG, "is_close()");
242 for (i = 0; i < is->num_types; i++)
248 th.blocksize = is->types[i].blocksize;
249 th.keysize = is->keysize;
250 th.freelist = is->types[i].freelist;
251 th.top = is->types[i].top;
252 if (is_rb_write(&is->types[i], &th) < 0)
254 log(LOG_FATAL, "Failed to write headerblock");
258 bf_close(is->types[i].bf);
265 static ISAM_P is_address(int type, int pos)
274 int sort_input(const void *p1, const void *p2)
278 if ((rs = (*extcmp)(((char *)p1) + 1, ((char *)p2) + 1)))
280 return *((char *)p1) - *((char*)p2);
283 ISAM_P is_merge(ISAM is, ISAM_P pos, int num, char *data)
287 char keybuf[IS_MAX_RECORD];
288 int oldnum, oldtype, i;
289 char operation, *record;
292 qsort(data, num, is_keysize(is) + 1, sort_input);
293 is_m_establish_tab(is, &tab, pos);
294 /* TODO: do something to aquire oldnum at this point */
296 if (is_m_read_full(&tab, tab.data) < 0)
298 log(LOG_FATAL, "read_full failed");
301 oldnum = tab.num_records;
302 oldtype = tab.pos_type;
305 operation = *(data)++;
306 record = (char*) data;
307 data += is_keysize(is);
309 while (num && !memcmp(record - 1, data, is_keysize(tab.is) + 1))
311 data += 1 + is_keysize(is);
314 if ((res = is_m_seek_record(&tab, record)) > 0) /* no match */
316 if (operation == KEYOP_INSERT)
318 log(LOG_DEBUG, "XXInserting new record.");
319 is_m_write_record(&tab, record);
322 log(LOG_DEBUG, "XXDeletion failed to find match.");
324 else /* match found */
326 if (operation == KEYOP_INSERT)
328 log(LOG_DEBUG, "XXSkipping insertion - match found.");
331 else if (operation == KEYOP_DELETE)
333 /* try to avoid needlessly moving data */
334 if (num && *(data) == KEYOP_INSERT)
336 /* next key is identical insert? - NOOP - skip it */
337 if (!memcmp(record, data + 1, is_keysize(is)))
339 log(LOG_DEBUG, "XXNoop delete. skipping.");
340 data += 1 + is_keysize(is);
344 /* else check if next key can fit in this position */
345 is_m_peek_record(&tab, keybuf);
346 res = (*is->cmp)(data + 1, keybuf);
349 log(LOG_DEBUG, "XXReplacing record.");
350 is_m_replace_record(&tab, data + 1);
351 data += 1 + is_keysize(is);
356 log(LOG_DEBUG, "Deleting record.");
357 is_m_delete_record(&tab);
362 while (i < tab.is->num_types - 1 && tab.num_records >
363 tab.is->types[i].max_keys)
365 if (i != tab.pos_type)
370 if (!oldnum || tab.pos_type != oldtype || (abs(oldnum - tab.num_records) *
371 100) / oldnum > tab.is->repack)
378 pos = is_address(tab.pos_type, tab.data->diskpos);
382 is_m_release_tab(&tab);
387 * Locate a table of keys in an isam file. The ISPT is an individual
388 * position marker for that table.
390 ISPT is_position(ISAM is, ISAM_P pos);
395 void is_pt_free(ISPT ip);