2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1997-09-09 13:38:11 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.14 1996/10/29 13:56:56 adam
11 * Include of zebrautl.h instead of alexutil.h.
13 * Revision 1.13 1996/03/20 13:29:16 quinn
16 * Revision 1.12 1996/03/11 14:52:23 quinn
17 * Fixed update bug. Repeated insertion in the same area sometimes caused
20 * Revision 1.11 1996/02/10 12:20:58 quinn
21 * *** empty log message ***
23 * Revision 1.10 1995/12/12 14:12:47 quinn
24 * *** empty log message ***
26 * Revision 1.9 1995/12/06 15:48:46 quinn
27 * Fixed update-problem.
29 * Revision 1.8 1995/12/06 14:48:27 quinn
30 * Fixed some strange bugs.
32 * Revision 1.7 1995/12/06 09:59:46 quinn
33 * Fixed memory-consumption bug in memory.c
34 * Added more blocksizes to the default ISAM configuration.
36 * Revision 1.6 1995/09/04 12:33:47 adam
37 * Various cleanup. YAZ util used instead.
39 * Revision 1.5 1994/09/28 16:58:33 quinn
42 * Revision 1.4 1994/09/27 20:03:52 quinn
43 * Seems relatively bug-free.
45 * Revision 1.3 1994/09/26 17:11:30 quinn
48 * Revision 1.2 1994/09/26 17:06:35 quinn
51 * Revision 1.1 1994/09/26 16:07:56 quinn
52 * Most of the functionality in place.
57 * This module accesses and rearranges the records of the tables.
68 int is_mbuf_size[3] = { 0, 1024, 4096 };
70 static is_mblock *mblock_tmplist = 0, *mblock_freelist = 0;
71 static is_mbuf *mbuf_freelist[3] = {0, 0, 0};
73 #define MALLOC_CHUNK 20
75 is_mblock *xmalloc_mblock()
82 mblock_freelist = xmalloc(sizeof(is_mblock) * MALLOC_CHUNK);
83 for (i = 0; i < MALLOC_CHUNK - 1; i++)
84 mblock_freelist[i].next = &mblock_freelist[i+1];
85 mblock_freelist[i].next = 0;
87 tmp = mblock_freelist;
88 mblock_freelist = mblock_freelist->next;
90 tmp->state = IS_MBSTATE_UNREAD;
95 is_mbuf *xmalloc_mbuf(int type)
99 if (mbuf_freelist[type])
101 tmp = mbuf_freelist[type];
102 mbuf_freelist[type] = tmp->next;
106 tmp = xmalloc(sizeof(is_mbuf) + is_mbuf_size[type]);
109 tmp->refcount = type ? 1 : 0;
110 tmp->offset = tmp->num = tmp->cur_record = 0;
111 tmp->data = (char*) tmp + sizeof(is_mbuf);
116 void xfree_mbuf(is_mbuf *p)
118 p->next = mbuf_freelist[p->type];
119 mbuf_freelist[p->type] = p;
122 void xfree_mbufs(is_mbuf *l)
134 void xfree_mblock(is_mblock *p)
136 xfree_mbufs(p->data);
137 p->next = mblock_freelist;
141 void xrelease_mblock(is_mblock *p)
143 p->next = mblock_tmplist;
147 void xfree_mblocks(is_mblock *l)
159 void is_m_establish_tab(ISAM is, is_mtable *tab, ISAM_P pos)
161 tab->data = xmalloc_mblock();
164 tab->pos_type = is_type(pos);
165 tab->num_records = -1;
166 tab->data->num_records = -1;
167 tab->data->diskpos = is_block(pos);
168 tab->data->state = IS_MBSTATE_UNREAD;
170 tab->cur_mblock = tab->data;
171 tab->cur_mblock->cur_mbuf = 0;
177 tab->num_records = 0;
178 tab->data->num_records = 0;
179 tab->data->diskpos = -1;
180 tab->data->state = IS_MBSTATE_CLEAN;
181 tab->data->data = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
182 tab->cur_mblock = tab->data;
183 tab->cur_mblock->cur_mbuf = tab->data->data;
184 tab->cur_mblock->cur_mbuf->cur_record = 0;
190 void is_m_release_tab(is_mtable *tab)
192 xfree_mblocks(tab->data);
193 xfree_mblocks(mblock_tmplist);
197 void is_m_rewind(is_mtable *tab)
199 tab->cur_mblock = tab->data;
202 tab->data->cur_mbuf = tab->data->data;
204 tab->data->data->cur_record = 0;
208 static int read_current_full(is_mtable *tab, is_mblock *mblock)
210 if (is_p_read_full(tab, mblock) < 0)
212 if (mblock->nextpos && !mblock->next)
214 mblock->next = xmalloc_mblock();
215 mblock->next->diskpos = mblock->nextpos;
216 mblock->next->state = IS_MBSTATE_UNREAD;
217 mblock->next->data = 0;
219 mblock->cur_mbuf = mblock->data;
220 mblock->data->cur_record = 0;
224 int is_m_read_full(is_mtable *tab, is_mblock *mblock)
226 return read_current_full(tab, mblock);
230 * replace the record right behind the pointer.
232 void is_m_replace_record(is_mtable *tab, const void *rec)
234 is_mbuf *mbuf = tab->cur_mblock->cur_mbuf;
236 /* we assume that block is already in memory and that we are in the
237 * right mbuf, and that it has space for us. */
238 memcpy(mbuf->data + mbuf->offset + (mbuf->cur_record - 1) *
239 is_keysize(tab->is), rec, is_keysize(tab->is));
240 tab->cur_mblock->state = IS_MBSTATE_DIRTY;
244 * Delete the record right behind the pointer.
246 void is_m_delete_record(is_mtable *tab)
250 mbuf = tab->cur_mblock->cur_mbuf;
251 if (mbuf->cur_record >= mbuf->num) /* top of mbuf */
256 else if (mbuf->cur_record == 1) /* beginning of mbuf */
259 mbuf->offset +=is_keysize(tab->is);
260 mbuf->cur_record = 0;
262 else /* middle of mbuf */
264 /* insert block after current one */
265 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
266 new->next = mbuf->next;
269 /* virtually transfer everything after current record to new one. */
270 new->data = mbuf->data;
272 new->offset = mbuf->offset + mbuf->cur_record * is_keysize(tab->is);
273 new->num = mbuf->num - mbuf->cur_record;
275 /* old buf now only contains stuff before current record */
276 mbuf->num = mbuf->cur_record -1;
277 tab->cur_mblock->cur_mbuf = new;
280 tab->cur_mblock->num_records--;
281 tab->cur_mblock->state = tab->data->state = IS_MBSTATE_DIRTY;
284 int is_m_write_record(is_mtable *tab, const void *rec)
286 is_mbuf *mbuf, *oldnext, *dmbuf;
288 /* make sure block is all in memory */
289 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
290 if (read_current_full(tab, tab->cur_mblock) < 0)
292 mbuf = tab->cur_mblock->cur_mbuf;
293 if (mbuf->cur_record >= mbuf->num) /* top of mbuf */
295 /* mbuf is reference or full */
296 if (mbuf->refcount != 1 || mbuf->offset + (mbuf->num + 1) *
297 is_keysize(tab->is) > is_mbuf_size[mbuf->type])
299 oldnext = mbuf->next;
300 mbuf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
301 mbuf->next->next = oldnext;
303 tab->cur_mblock->cur_mbuf = mbuf;
304 mbuf->cur_record = 0;
309 oldnext = mbuf->next;
310 mbuf->next = xmalloc_mbuf(IS_MBUF_TYPE_MEDIUM);
311 mbuf->next->next = dmbuf = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
312 dmbuf->data = mbuf->data;
313 dmbuf->next = oldnext;
314 dmbuf->offset = mbuf->offset + mbuf->cur_record * is_keysize(tab->is);
315 dmbuf->num = mbuf->num - mbuf->cur_record;
316 mbuf->num -= dmbuf->num;
318 mbuf = tab->cur_mblock->cur_mbuf = mbuf->next;
319 mbuf->cur_record = 0;
322 logf (LOG_DEBUG, "is_m_write_rec(rec == %d)", mbuf->cur_record);
324 memcpy(mbuf->data + mbuf->offset + mbuf->cur_record * is_keysize(tab->is),
325 rec, is_keysize(tab->is));
329 tab->cur_mblock->num_records++;
330 tab->cur_mblock->state = tab->data->state = IS_MBSTATE_DIRTY;
334 void is_m_unread_record(is_mtable *tab)
336 assert(tab->cur_mblock->cur_mbuf->cur_record);
338 tab->cur_mblock->cur_mbuf = tab->last_mbuf;
340 tab->cur_mblock->cur_mbuf->cur_record--;
344 * non-destructive read.
346 int is_m_peek_record(is_mtable *tab, void *rec)
351 /* make sure block is all in memory */
352 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
353 if (read_current_full(tab, tab->cur_mblock) < 0)
355 mblock = tab->cur_mblock;
356 mbuf = mblock->cur_mbuf;
357 if (mbuf->cur_record >= mbuf->num) /* are we at end of mbuf? */
359 if (!mbuf->next) /* end of mblock */
363 mblock = mblock->next;
364 if (mblock->state <= IS_MBSTATE_PARTIAL)
365 if (read_current_full(tab, mblock) < 0)
370 return 0; /* EOTable */
374 mbuf->cur_record = 0;
376 memcpy(rec, mbuf->data + mbuf->offset + mbuf->cur_record *
377 is_keysize(tab->is), is_keysize(tab->is));
381 int is_m_read_record(is_mtable *tab, void *buf, int keep)
385 /* make sure block is all in memory */
386 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
387 if (read_current_full(tab, tab->cur_mblock) < 0)
389 mbuf = tab->cur_mblock->cur_mbuf;
390 if (mbuf->cur_record >= mbuf->num) /* are we at end of mbuf? */
392 if (!mbuf->next) /* end of mblock */
394 if (!keep && tab->cur_mblock->state == IS_MBSTATE_CLEAN &&
395 tab->cur_mblock->diskpos > 0)
397 xfree_mbufs(tab->cur_mblock->data);
398 tab->cur_mblock->data = 0;
399 tab->cur_mblock->state = IS_MBSTATE_UNREAD;
401 if (tab->cur_mblock->next)
403 tab->cur_mblock = tab->cur_mblock->next;
404 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
405 if (read_current_full(tab, tab->cur_mblock) < 0)
407 tab->cur_mblock->cur_mbuf = mbuf = tab->cur_mblock->data;
411 return 0; /* EOTable */
415 tab->last_mbuf = mbuf;
416 tab->cur_mblock->cur_mbuf = mbuf = mbuf->next;
418 mbuf->cur_record = 0;
422 memcpy(buf, mbuf->data + mbuf->offset + mbuf->cur_record *
423 is_keysize(tab->is), is_keysize(tab->is));
429 * TODO: optimize this function by introducing a higher-level search.
431 int is_m_seek_record(is_mtable *tab, const void *rec)
433 char peek[IS_MAX_RECORD];
438 if (is_m_read_record(tab, &peek, 1) <= 0)
440 if ((rs = (*tab->is->cmp)(peek, rec)) > 0)
442 is_m_unread_record(tab);
450 int is_m_num_records(is_mtable *tab)
452 if (tab->data->state < IS_MBSTATE_PARTIAL)
453 if (read_current_full(tab, tab->data) < 0)
455 logf (LOG_FATAL, "read full failed");
458 return tab->num_records;