23 #include "myisam_priv.h"
24 #include <drizzled/charset.h>
25 #include <drizzled/util/test.h>
28 using namespace drizzled;
32 int mi_lock_database(
MI_INFO *info,
int lock_type)
37 #if defined(FULL_LOG) || defined(_lint)
42 info->s->in_use=
new list<Session *>;
44 if (lock_type == F_EXTRA_LCK)
48 info->lock_type= lock_type;
49 info->s->in_use->push_front(info->in_use);
52 #if defined(FULL_LOG) || defined(_lint)
57 if (share->kfile >= 0)
61 if (info->lock_type == F_RDLCK)
62 count= --share->r_locks;
64 count= --share->w_locks;
66 if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
71 mi_print_error(info->s, HA_ERR_CRASHED);
72 mi_mark_crashed(info);
77 if (share->changed && !share->w_locks)
79 if ((info->s->mmaped_length != info->s->state.state.data_file_length) &&
80 (info->s->nonmmaped_inserts > MAX_NONMAPPED_INSERTS))
82 mi_remap_file(info, info->s->state.state.data_file_length);
83 info->s->nonmmaped_inserts= 0;
85 share->state.process= share->last_process=share->this_process;
86 share->state.unique= info->last_unique= info->this_unique;
87 share->state.update_count= info->last_loop= ++info->this_loop;
88 if (mi_state_info_write(share->kfile, &share->state, 1))
94 mi_print_error(info->s, HA_ERR_CRASHED);
95 mi_mark_crashed(info);
98 #if defined(FULL_LOG) || defined(_lint)
99 if (info->lock_type != F_EXTRA_LCK)
105 else if (!share->w_locks)
112 info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
113 info->lock_type= F_UNLCK;
114 info->s->in_use->remove(info->in_use);
117 if (info->lock_type == F_WRLCK)
125 #if defined(FULL_LOG) || defined(_lint)
126 if (share->w_locks == 1)
133 info->lock_type=lock_type;
136 if (!share->r_locks && !share->w_locks)
138 #if defined(FULL_LOG) || defined(_lint)
141 if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
146 if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
153 _mi_test_if_changed(info);
156 info->lock_type=lock_type;
157 info->s->in_use->push_front(info->in_use);
160 if (info->lock_type == F_RDLCK)
162 if (share->r_locks == 1)
164 #if defined(FULL_LOG) || defined(_lint)
169 info->lock_type=lock_type;
173 if (!(share->options & HA_OPTION_READ_ONLY_DATA))
177 #if defined(FULL_LOG) || defined(_lint)
182 if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
191 _mi_test_if_changed(info);
193 info->lock_type=lock_type;
196 info->s->in_use->push_front(info->in_use);
211 if( info->owned_by_merge && (info->s)->kfile < 0 )
213 error = HA_ERR_NO_SUCH_TABLE;
217 #if defined(FULL_LOG) || defined(_lint)
218 lock_type|=(int) (flag << 8);
219 myisam_log_command(MI_LOG_LOCK,info,(
unsigned char*) &lock_type,
sizeof(lock_type),
230 int _mi_readinfo(
register MI_INFO *info,
int lock_type,
int check_keybuffer)
232 if (info->lock_type == F_UNLCK)
235 if (!share->tot_locks)
237 if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
239 int error=errno ? errno : -1;
245 _mi_test_if_changed(info);
247 else if (lock_type == F_WRLCK && info->lock_type == F_RDLCK)
261 int _mi_writeinfo(
register MI_INFO *info, uint32_t operation)
267 if (share->tot_locks == 0)
272 share->state.process= share->last_process= share->this_process;
273 share->state.unique= info->last_unique= info->this_unique;
274 share->state.update_count= info->last_loop= ++info->this_loop;
275 if ((error=mi_state_info_write(share->kfile, &share->state, 1)))
289 int _mi_test_if_changed(
register MI_INFO *info)
292 if (share->state.process != share->last_process ||
293 share->state.unique != info->last_unique ||
294 share->state.update_count != info->last_loop)
296 share->last_process=share->state.process;
297 info->last_unique= share->state.unique;
298 info->last_loop= share->state.update_count;
299 info->update|= HA_STATE_WRITTEN;
300 info->data_changed= 1;
303 return (!(info->update & HA_STATE_AKTIV) ||
304 (info->update & (HA_STATE_WRITTEN | HA_STATE_DELETED |
305 HA_STATE_KEY_CHANGED)));
326 int _mi_mark_file_changed(
MI_INFO *info)
328 unsigned char buff[3];
331 if (!(share->state.changed & STATE_CHANGED) || ! share->global_changed)
333 share->state.changed|=(STATE_CHANGED | STATE_NOT_ANALYZED |
334 STATE_NOT_OPTIMIZED_KEYS);
335 if (!share->global_changed)
337 share->global_changed=1;
338 share->state.open_count++;
340 if (!share->temporary)
342 mi_int2store(buff,share->state.open_count);
344 return(my_pwrite(share->kfile,buff,
sizeof(buff),
345 sizeof(share->state.header),
358 int _mi_decrement_open_count(
MI_INFO *info)
360 unsigned char buff[2];
362 int lock_error=0,write_error=0;
363 if (share->global_changed)
365 uint32_t old_lock=info->lock_type;
366 share->global_changed=0;
367 lock_error=mi_lock_database(info,F_WRLCK);
369 if (share->state.open_count > 0)
371 share->state.open_count--;
372 mi_int2store(buff,share->state.open_count);
373 write_error=my_pwrite(share->kfile,buff,
sizeof(buff),
374 sizeof(share->state.header),
378 lock_error=mi_lock_database(info,old_lock);
380 return test(lock_error || write_error);