2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1996-10-29 13:56:56 adam
8 * Include of zebrautl.h instead of alexutil.h.
10 * Revision 1.13 1996/03/20 13:29:16 quinn
13 * Revision 1.12 1996/03/11 14:52:23 quinn
14 * Fixed update bug. Repeated insertion in the same area sometimes caused
17 * Revision 1.11 1996/02/10 12:20:58 quinn
18 * *** empty log message ***
20 * Revision 1.10 1995/12/12 14:12:47 quinn
21 * *** empty log message ***
23 * Revision 1.9 1995/12/06 15:48:46 quinn
24 * Fixed update-problem.
26 * Revision 1.8 1995/12/06 14:48:27 quinn
27 * Fixed some strange bugs.
29 * Revision 1.7 1995/12/06 09:59:46 quinn
30 * Fixed memory-consumption bug in memory.c
31 * Added more blocksizes to the default ISAM configuration.
33 * Revision 1.6 1995/09/04 12:33:47 adam
34 * Various cleanup. YAZ util used instead.
36 * Revision 1.5 1994/09/28 16:58:33 quinn
39 * Revision 1.4 1994/09/27 20:03:52 quinn
40 * Seems relatively bug-free.
42 * Revision 1.3 1994/09/26 17:11:30 quinn
45 * Revision 1.2 1994/09/26 17:06:35 quinn
48 * Revision 1.1 1994/09/26 16:07:56 quinn
49 * Most of the functionality in place.
54 * This module accesses and rearranges the records of the tables.
63 int is_mbuf_size[3] = { 0, 1024, 4096 };
65 static is_mblock *mblock_tmplist = 0, *mblock_freelist = 0;
66 static is_mbuf *mbuf_freelist[3] = {0, 0, 0};
68 #define MALLOC_CHUNK 20
70 is_mblock *xmalloc_mblock()
77 mblock_freelist = xmalloc(sizeof(is_mblock) * MALLOC_CHUNK);
78 for (i = 0; i < MALLOC_CHUNK - 1; i++)
79 mblock_freelist[i].next = &mblock_freelist[i+1];
80 mblock_freelist[i].next = 0;
82 tmp = mblock_freelist;
83 mblock_freelist = mblock_freelist->next;
85 tmp->state = IS_MBSTATE_UNREAD;
90 is_mbuf *xmalloc_mbuf(int type)
94 if (mbuf_freelist[type])
96 tmp = mbuf_freelist[type];
97 mbuf_freelist[type] = tmp->next;
101 tmp = xmalloc(sizeof(is_mbuf) + is_mbuf_size[type]);
104 tmp->refcount = type ? 1 : 0;
105 tmp->offset = tmp->num = tmp->cur_record = 0;
106 tmp->data = (char*) tmp + sizeof(is_mbuf);
111 void xfree_mbuf(is_mbuf *p)
113 p->next = mbuf_freelist[p->type];
114 mbuf_freelist[p->type] = p;
117 void xfree_mbufs(is_mbuf *l)
129 void xfree_mblock(is_mblock *p)
131 xfree_mbufs(p->data);
132 p->next = mblock_freelist;
136 void xrelease_mblock(is_mblock *p)
138 p->next = mblock_tmplist;
142 void xfree_mblocks(is_mblock *l)
154 void is_m_establish_tab(ISAM is, is_mtable *tab, ISAM_P pos)
156 tab->data = xmalloc_mblock();
159 tab->pos_type = is_type(pos);
160 tab->num_records = -1;
161 tab->data->num_records = -1;
162 tab->data->diskpos = is_block(pos);
163 tab->data->state = IS_MBSTATE_UNREAD;
165 tab->cur_mblock = tab->data;
166 tab->cur_mblock->cur_mbuf = 0;
172 tab->num_records = 0;
173 tab->data->num_records = 0;
174 tab->data->diskpos = -1;
175 tab->data->state = IS_MBSTATE_CLEAN;
176 tab->data->data = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
177 tab->cur_mblock = tab->data;
178 tab->cur_mblock->cur_mbuf = tab->data->data;
179 tab->cur_mblock->cur_mbuf->cur_record = 0;
185 void is_m_release_tab(is_mtable *tab)
187 xfree_mblocks(tab->data);
188 xfree_mblocks(mblock_tmplist);
192 void is_m_rewind(is_mtable *tab)
194 tab->cur_mblock = tab->data;
197 tab->data->cur_mbuf = tab->data->data;
199 tab->data->data->cur_record = 0;
203 static int read_current_full(is_mtable *tab, is_mblock *mblock)
205 if (is_p_read_full(tab, mblock) < 0)
207 if (mblock->nextpos && !mblock->next)
209 mblock->next = xmalloc_mblock();
210 mblock->next->diskpos = mblock->nextpos;
211 mblock->next->state = IS_MBSTATE_UNREAD;
212 mblock->next->data = 0;
214 mblock->cur_mbuf = mblock->data;
215 mblock->data->cur_record = 0;
219 int is_m_read_full(is_mtable *tab, is_mblock *mblock)
221 return read_current_full(tab, mblock);
225 * replace the record right behind the pointer.
227 void is_m_replace_record(is_mtable *tab, const void *rec)
229 is_mbuf *mbuf = tab->cur_mblock->cur_mbuf;
231 /* we assume that block is already in memory and that we are in the
232 * right mbuf, and that it has space for us. */
233 memcpy(mbuf->data + mbuf->offset + (mbuf->cur_record - 1) *
234 is_keysize(tab->is), rec, is_keysize(tab->is));
235 tab->cur_mblock->state = IS_MBSTATE_DIRTY;
239 * Delete the record right behind the pointer.
241 void is_m_delete_record(is_mtable *tab)
245 mbuf = tab->cur_mblock->cur_mbuf;
246 if (mbuf->cur_record >= mbuf->num) /* top of mbuf */
251 else if (mbuf->cur_record == 1) /* beginning of mbuf */
254 mbuf->offset +=is_keysize(tab->is);
255 mbuf->cur_record = 0;
257 else /* middle of mbuf */
259 /* insert block after current one */
260 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
261 new->next = mbuf->next;
264 /* virtually transfer everything after current record to new one. */
265 new->data = mbuf->data;
267 new->offset = mbuf->offset + mbuf->cur_record * is_keysize(tab->is);
268 new->num = mbuf->num - mbuf->cur_record;
270 /* old buf now only contains stuff before current record */
271 mbuf->num = mbuf->cur_record -1;
272 tab->cur_mblock->cur_mbuf = new;
275 tab->cur_mblock->num_records--;
276 tab->cur_mblock->state = tab->data->state = IS_MBSTATE_DIRTY;
279 int is_m_write_record(is_mtable *tab, const void *rec)
281 is_mbuf *mbuf, *oldnext, *dmbuf;
283 /* make sure block is all in memory */
284 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
285 if (read_current_full(tab, tab->cur_mblock) < 0)
287 mbuf = tab->cur_mblock->cur_mbuf;
288 if (mbuf->cur_record >= mbuf->num) /* top of mbuf */
290 /* mbuf is reference or full */
291 if (mbuf->refcount != 1 || mbuf->offset + (mbuf->num + 1) *
292 is_keysize(tab->is) > is_mbuf_size[mbuf->type])
294 oldnext = mbuf->next;
295 mbuf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
296 mbuf->next->next = oldnext;
298 tab->cur_mblock->cur_mbuf = mbuf;
299 mbuf->cur_record = 0;
304 oldnext = mbuf->next;
305 mbuf->next = xmalloc_mbuf(IS_MBUF_TYPE_MEDIUM);
306 mbuf->next->next = dmbuf = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
307 dmbuf->data = mbuf->data;
308 dmbuf->next = oldnext;
309 dmbuf->offset = mbuf->offset + mbuf->cur_record * is_keysize(tab->is);
310 dmbuf->num = mbuf->num - mbuf->cur_record;
311 mbuf->num -= dmbuf->num;
313 mbuf = tab->cur_mblock->cur_mbuf = mbuf->next;
314 mbuf->cur_record = 0;
317 logf (LOG_DEBUG, "is_m_write_rec(rec == %d)", mbuf->cur_record);
319 memcpy(mbuf->data + mbuf->offset + mbuf->cur_record * is_keysize(tab->is),
320 rec, is_keysize(tab->is));
324 tab->cur_mblock->num_records++;
325 tab->cur_mblock->state = tab->data->state = IS_MBSTATE_DIRTY;
329 void is_m_unread_record(is_mtable *tab)
331 assert(tab->cur_mblock->cur_mbuf->cur_record);
333 tab->cur_mblock->cur_mbuf = tab->last_mbuf;
335 tab->cur_mblock->cur_mbuf->cur_record--;
339 * non-destructive read.
341 int is_m_peek_record(is_mtable *tab, void *rec)
346 /* make sure block is all in memory */
347 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
348 if (read_current_full(tab, tab->cur_mblock) < 0)
350 mblock = tab->cur_mblock;
351 mbuf = mblock->cur_mbuf;
352 if (mbuf->cur_record >= mbuf->num) /* are we at end of mbuf? */
354 if (!mbuf->next) /* end of mblock */
358 mblock = mblock->next;
359 if (mblock->state <= IS_MBSTATE_PARTIAL)
360 if (read_current_full(tab, mblock) < 0)
365 return 0; /* EOTable */
369 mbuf->cur_record = 0;
371 memcpy(rec, mbuf->data + mbuf->offset + mbuf->cur_record *
372 is_keysize(tab->is), is_keysize(tab->is));
376 int is_m_read_record(is_mtable *tab, void *buf, int keep)
380 /* make sure block is all in memory */
381 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
382 if (read_current_full(tab, tab->cur_mblock) < 0)
384 mbuf = tab->cur_mblock->cur_mbuf;
385 if (mbuf->cur_record >= mbuf->num) /* are we at end of mbuf? */
387 if (!mbuf->next) /* end of mblock */
389 if (!keep && tab->cur_mblock->state == IS_MBSTATE_CLEAN &&
390 tab->cur_mblock->diskpos > 0)
392 xfree_mbufs(tab->cur_mblock->data);
393 tab->cur_mblock->data = 0;
394 tab->cur_mblock->state = IS_MBSTATE_UNREAD;
396 if (tab->cur_mblock->next)
398 tab->cur_mblock = tab->cur_mblock->next;
399 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
400 if (read_current_full(tab, tab->cur_mblock) < 0)
402 tab->cur_mblock->cur_mbuf = mbuf = tab->cur_mblock->data;
406 return 0; /* EOTable */
410 tab->last_mbuf = mbuf;
411 tab->cur_mblock->cur_mbuf = mbuf = mbuf->next;
413 mbuf->cur_record = 0;
417 memcpy(buf, mbuf->data + mbuf->offset + mbuf->cur_record *
418 is_keysize(tab->is), is_keysize(tab->is));
424 * TODO: optimize this function by introducing a higher-level search.
426 int is_m_seek_record(is_mtable *tab, const void *rec)
428 char peek[IS_MAX_RECORD];
433 if (is_m_read_record(tab, &peek, 1) <= 0)
435 if ((rs = (*tab->is->cmp)(peek, rec)) > 0)
437 is_m_unread_record(tab);
445 int is_m_num_records(is_mtable *tab)
447 if (tab->data->state < IS_MBSTATE_PARTIAL)
448 if (read_current_full(tab, tab->data) < 0)
450 logf (LOG_FATAL, "read full failed");
453 return tab->num_records;