2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1994-09-26 17:06:35 quinn
10 * Revision 1.1 1994/09/26 16:07:56 quinn
11 * Most of the functionality in place.
16 * This module accesses and rearranges the records of the tables.
26 int is_mbuf_size[3] = { 0, 1024, 4096 };
29 * TODO: make internal memory-management scheme for these units.
32 is_mblock *xmalloc_mblock()
36 tmp = xmalloc(sizeof(is_mblock));
41 is_mbuf *xmalloc_mbuf(int type)
45 tmp = xmalloc(sizeof(is_mbuf) + is_mbuf_size[type]);
47 tmp->refcount = type ? 1 : 0;
48 tmp->offset = tmp->num = tmp->cur_record = 0;
49 tmp->data = (char*) tmp + sizeof(is_mbuf);
55 void xfree_mblock(is_mblock *p)
62 void xfree_mbuf(is_mblock *p)
68 void is_m_establish_tab(ISAM is, is_mtable *tab, ISAM_P pos)
70 tab->data = xmalloc_mblock();
73 tab->pos_type = is_type(pos);
74 tab->num_records = -1;
75 tab->data->num_records = -1;
76 tab->data->diskpos = is_block(pos);
77 tab->data->state = IS_MBSTATE_UNREAD;
79 tab->cur_mblock = tab->data;
80 tab->cur_mblock->cur_mbuf = 0;
86 tab->data->num_records = 0;
87 tab->data->diskpos = -1;
88 tab->data->state = IS_MBSTATE_CLEAN;
89 tab->data->data = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
90 tab->cur_mblock = tab->data;
91 tab->cur_mblock->cur_mbuf = tab->data->data;
92 tab->cur_mblock->cur_mbuf->cur_record = 0;
97 void is_m_rewind(is_mtable *tab)
99 tab->cur_mblock = tab->data;
102 tab->data->cur_mbuf = tab->data->data;
104 tab->data->data->cur_record = 0;
108 static int read_current_full(is_mtable *tab, is_mblock *mblock)
110 if (is_p_read_full(tab, mblock) < 0)
112 if (mblock->nextpos && !mblock->next)
114 mblock->next = xmalloc_mblock();
115 mblock->next->diskpos = mblock->nextpos;
116 mblock->next->state = IS_MBSTATE_UNREAD;
117 mblock->next->data = 0;
119 mblock->cur_mbuf = mblock->data;
120 mblock->data->cur_record = 0;
124 int is_m_read_full(is_mtable *tab, is_mblock *mblock)
126 return read_current_full(tab, mblock);
130 * replace the record right behind the pointer.
132 void is_m_replace_record(is_mtable *tab, const void *rec)
134 is_mbuf *mbuf = tab->cur_mblock->cur_mbuf;
136 /* we assume that block is already in memory and that we are in the
137 * right mbuf, and that it has space for us. */
138 memcpy(mbuf->data + mbuf->offset + (mbuf->cur_record - 1) *
139 is_keysize(tab->is), rec, is_keysize(tab->is));
140 tab->cur_mblock->state = IS_MBSTATE_DIRTY;
144 * Delete the record right behind the pointer.
146 void is_m_delete_record(is_mtable *tab)
150 mbuf = tab->cur_mblock->cur_mbuf;
151 if (mbuf->cur_record >= mbuf->num) /* top of mbuf */
156 else /* middle of a block */
158 new = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
159 new->next = mbuf->next;
161 new->data = mbuf->data;
163 new->offset = mbuf->offset + mbuf->cur_record * is_keysize(tab->is);
164 new->num = mbuf->num - mbuf->cur_record;
165 mbuf->num = mbuf->cur_record -1;
167 mbuf->cur_record = 0;
170 tab->cur_mblock->num_records--;
171 tab->cur_mblock->state = tab->data->state = IS_MBSTATE_DIRTY;
174 int is_m_write_record(is_mtable *tab, const void *rec)
176 is_mbuf *mbuf, *oldnext, *dmbuf;
178 /* make sure block is all in memory */
179 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
180 if (read_current_full(tab, tab->cur_mblock) < 0)
182 mbuf = tab->cur_mblock->cur_mbuf;
183 if (mbuf->cur_record >= mbuf->num) /* top of mbuf */
185 /* mbuf is reference or full */
186 if (mbuf->refcount != 1 || mbuf->offset + (mbuf->num + 1) *
187 is_keysize(tab->is) > is_mbuf_size[mbuf->type])
189 oldnext = mbuf->next;
190 mbuf->next = xmalloc_mbuf(IS_MBUF_TYPE_LARGE);
191 mbuf->next->next = oldnext;
193 tab->cur_mblock->cur_mbuf = mbuf;
194 mbuf->cur_record = 0;
199 oldnext = mbuf->next;
200 mbuf->next = xmalloc_mbuf(IS_MBUF_TYPE_MEDIUM);
201 mbuf->next->next = dmbuf = xmalloc_mbuf(IS_MBUF_TYPE_SMALL);
202 dmbuf->data = mbuf->data;
203 dmbuf->next = oldnext;
204 dmbuf->offset = mbuf->offset + mbuf->cur_record * is_keysize(tab->is);
205 dmbuf->num = mbuf->num - mbuf->cur_record;
206 mbuf->num -= dmbuf->num;
208 mbuf = tab->cur_mblock->cur_mbuf = mbuf->next;
209 mbuf->cur_record = 0;
211 log(LOG_DEBUG, "is_m_write_rec(rec == %d)", mbuf->cur_record);
212 memcpy(mbuf->data + mbuf->offset + mbuf->cur_record * is_keysize(tab->is),
213 rec, is_keysize(tab->is));
217 tab->cur_mblock->num_records++;
218 tab->cur_mblock->state = tab->data->state = IS_MBSTATE_DIRTY;
222 void is_m_unread_record(is_mtable *tab)
224 assert(tab->cur_mblock->cur_mbuf->cur_record);
225 tab->cur_mblock->cur_mbuf->cur_record--;
229 * non-destructive read.
231 int is_m_peek_record(is_mtable *tab, void *rec)
235 /* make sure block is all in memory */
236 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
237 if (read_current_full(tab, tab->cur_mblock) < 0)
239 mbuf = tab->cur_mblock->cur_mbuf;
240 if (mbuf->cur_record >= mbuf->num) /* are we at end of mbuf? */
242 if (!mbuf->next) /* end of mblock */
244 if (tab->cur_mblock->next)
246 tab->cur_mblock = tab->cur_mblock->next;
247 if (tab->cur_mblock->next->state <= IS_MBSTATE_PARTIAL)
248 if (read_current_full(tab, tab->cur_mblock->next) < 0)
250 mbuf = tab->cur_mblock->next->data;
253 return 0; /* EOTable */
257 mbuf->cur_record = 0;
259 memcpy(rec, mbuf->data + mbuf->offset + mbuf->cur_record *
260 is_keysize(tab->is), is_keysize(tab->is));
264 int is_m_read_record(is_mtable *tab, void *buf)
268 /* make sure block is all in memory */
269 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
270 if (read_current_full(tab, tab->cur_mblock) < 0)
272 mbuf = tab->cur_mblock->cur_mbuf;
273 if (mbuf->cur_record >= mbuf->num) /* are we at end of mbuf? */
275 if (!mbuf->next) /* end of mblock */
277 if (tab->cur_mblock->next)
279 tab->cur_mblock = tab->cur_mblock->next;
280 if (tab->cur_mblock->state <= IS_MBSTATE_PARTIAL)
281 if (read_current_full(tab, tab->cur_mblock) < 0)
283 tab->cur_mblock->cur_mbuf = mbuf = tab->cur_mblock->data;
286 return 0; /* EOTable */
289 tab->cur_mblock->cur_mbuf = mbuf = mbuf->next;
290 mbuf->cur_record = 0;
292 memcpy(buf, mbuf->data + mbuf->offset + mbuf->cur_record *
293 is_keysize(tab->is), is_keysize(tab->is));
299 * TODO: optimize this function by introducing a higher-level search.
301 int is_m_seek_record(is_mtable *tab, const void *rec)
303 char peek[IS_MAX_RECORD];
308 if (is_m_read_record(tab, &peek) <= 0)
310 if ((rs = (*tab->is->cmp)(peek, rec)) > 0)
312 is_m_unread_record(tab);