~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2005 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include "myisamdef.h"
17
#ifdef HAVE_SYS_MMAN_H
18
#include <sys/mman.h>
19
#endif
492.1.7 by Monty Taylor
Moved test() to its own file.
20
#include <drizzled/util/test.h>
1 by brian
clean slate
21
22
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function);
23
24
25
/*
26
  Set options and buffers to optimize table handling
27
28
  SYNOPSIS
29
    mi_extra()
30
    info	open table
31
    function	operation
304 by Brian Aker
ulong cleanup, remove log code from myisam.
32
    extra_arg	Pointer to extra argument (normally pointer to uint32_t)
1 by brian
clean slate
33
    		Used when function is one of:
34
		HA_EXTRA_WRITE_CACHE
35
		HA_EXTRA_CACHE
36
  RETURN VALUES
37
    0  ok
38
    #  error
39
*/
40
41
int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg)
42
{
43
  int error=0;
304 by Brian Aker
ulong cleanup, remove log code from myisam.
44
  uint32_t cache_size;
1 by brian
clean slate
45
  MYISAM_SHARE *share=info->s;
46
47
  switch (function) {
48
  case HA_EXTRA_RESET_STATE:		/* Reset state (don't free buffers) */
49
    info->lastinx= 0;			/* Use first index as def */
50
    info->last_search_keypage=info->lastpos= HA_OFFSET_ERROR;
51
    info->page_changed=1;
52
					/* Next/prev gives first/last */
53
    if (info->opt_flag & READ_CACHE_USED)
54
    {
55
      reinit_io_cache(&info->rec_cache,READ_CACHE,0,
154 by Brian Aker
Removed oddball types in my_global.h
56
		      (bool) (info->lock_type != F_UNLCK),
57
		      (bool) test(info->update & HA_STATE_ROW_CHANGED)
1 by brian
clean slate
58
		      );
59
    }
60
    info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
61
		   HA_STATE_PREV_FOUND);
62
    break;
63
  case HA_EXTRA_CACHE:
64
    if (info->lock_type == F_UNLCK &&
65
	(share->options & HA_OPTION_PACK_RECORD))
66
    {
67
      error=1;			/* Not possibly if not locked */
68
      my_errno=EACCES;
69
      break;
70
    }
71
    if (info->s->file_map) /* Don't use cache if mmap */
72
      break;
73
    if (info->opt_flag & WRITE_CACHE_USED)
74
    {
75
      info->opt_flag&= ~WRITE_CACHE_USED;
76
      if ((error=end_io_cache(&info->rec_cache)))
77
	break;
78
    }
79
    if (!(info->opt_flag &
80
	  (READ_CACHE_USED | WRITE_CACHE_USED | MEMMAP_USED)))
81
    {
304 by Brian Aker
ulong cleanup, remove log code from myisam.
82
      cache_size= (extra_arg ? *(uint32_t*) extra_arg :
1 by brian
clean slate
83
		   my_default_record_cache_size);
84
      if (!(init_io_cache(&info->rec_cache,info->dfile,
398.1.4 by Monty Taylor
Renamed max/min.
85
			 (uint) cmin(info->state->data_file_length+1,
1 by brian
clean slate
86
				    cache_size),
154 by Brian Aker
Removed oddball types in my_global.h
87
			  READ_CACHE,0L,(bool) (info->lock_type != F_UNLCK),
1 by brian
clean slate
88
			  MYF(share->write_flag & MY_WAIT_IF_FULL))))
89
      {
90
	info->opt_flag|=READ_CACHE_USED;
91
	info->update&= ~HA_STATE_ROW_CHANGED;
92
      }
93
      if (share->concurrent_insert)
94
	info->rec_cache.end_of_file=info->state->data_file_length;
95
    }
96
    break;
97
  case HA_EXTRA_REINIT_CACHE:
98
    if (info->opt_flag & READ_CACHE_USED)
99
    {
100
      reinit_io_cache(&info->rec_cache,READ_CACHE,info->nextpos,
154 by Brian Aker
Removed oddball types in my_global.h
101
		      (bool) (info->lock_type != F_UNLCK),
102
		      (bool) test(info->update & HA_STATE_ROW_CHANGED));
1 by brian
clean slate
103
      info->update&= ~HA_STATE_ROW_CHANGED;
104
      if (share->concurrent_insert)
105
	info->rec_cache.end_of_file=info->state->data_file_length;
106
    }
107
    break;
108
  case HA_EXTRA_WRITE_CACHE:
109
    if (info->lock_type == F_UNLCK)
110
    {
111
      error=1;			/* Not possibly if not locked */
112
      break;
113
    }
114
304 by Brian Aker
ulong cleanup, remove log code from myisam.
115
    cache_size= (extra_arg ? *(uint32_t*) extra_arg :
1 by brian
clean slate
116
		 my_default_record_cache_size);
117
    if (!(info->opt_flag &
118
	  (READ_CACHE_USED | WRITE_CACHE_USED | OPT_NO_ROWS)) &&
119
	!share->state.header.uniques)
120
      if (!(init_io_cache(&info->rec_cache,info->dfile, cache_size,
121
			 WRITE_CACHE,info->state->data_file_length,
154 by Brian Aker
Removed oddball types in my_global.h
122
			  (bool) (info->lock_type != F_UNLCK),
1 by brian
clean slate
123
			  MYF(share->write_flag & MY_WAIT_IF_FULL))))
124
      {
125
	info->opt_flag|=WRITE_CACHE_USED;
126
	info->update&= ~(HA_STATE_ROW_CHANGED |
127
			 HA_STATE_WRITE_AT_END |
128
			 HA_STATE_EXTEND_BLOCK);
129
      }
130
    break;
131
  case HA_EXTRA_PREPARE_FOR_UPDATE:
132
    if (info->s->data_file_type != DYNAMIC_RECORD)
133
      break;
134
    /* Remove read/write cache if dynamic rows */
135
  case HA_EXTRA_NO_CACHE:
136
    if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
137
    {
138
      info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
139
      error=end_io_cache(&info->rec_cache);
140
      /* Sergei will insert full text index caching here */
141
    }
142
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
143
    if (info->opt_flag & MEMMAP_USED)
144
      madvise((char*) share->file_map, share->state.state.data_file_length,
145
              MADV_RANDOM);
146
#endif
147
    break;
148
  case HA_EXTRA_FLUSH_CACHE:
149
    if (info->opt_flag & WRITE_CACHE_USED)
150
    {
151
      if ((error=flush_io_cache(&info->rec_cache)))
152
      {
153
        mi_print_error(info->s, HA_ERR_CRASHED);
154
	mi_mark_crashed(info);			/* Fatal error found */
155
      }
156
    }
157
    break;
158
  case HA_EXTRA_NO_READCHECK:
159
    info->opt_flag&= ~READ_CHECK_USED;		/* No readcheck */
160
    break;
161
  case HA_EXTRA_READCHECK:
162
    info->opt_flag|= READ_CHECK_USED;
163
    break;
164
  case HA_EXTRA_KEYREAD:			/* Read only keys to record */
165
  case HA_EXTRA_REMEMBER_POS:
166
    info->opt_flag |= REMEMBER_OLD_POS;
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
167
    memcpy(info->lastkey+share->base.max_key_length*2,
168
           info->lastkey,info->lastkey_length);
1 by brian
clean slate
169
    info->save_update=	info->update;
170
    info->save_lastinx= info->lastinx;
171
    info->save_lastpos= info->lastpos;
172
    info->save_lastkey_length=info->lastkey_length;
173
    if (function == HA_EXTRA_REMEMBER_POS)
174
      break;
175
    /* fall through */
176
  case HA_EXTRA_KEYREAD_CHANGE_POS:
177
    info->opt_flag |= KEY_READ_USED;
178
    info->read_record=_mi_read_key_record;
179
    break;
180
  case HA_EXTRA_NO_KEYREAD:
181
  case HA_EXTRA_RESTORE_POS:
182
    if (info->opt_flag & REMEMBER_OLD_POS)
183
    {
212.6.12 by Mats Kindahl
Removing redundant use of casts in MyISAM storage for memcmp(), memcpy(), memset(), and memmove().
184
      memcpy(info->lastkey,
185
             info->lastkey+share->base.max_key_length*2,
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
186
             info->save_lastkey_length);
1 by brian
clean slate
187
      info->update=	info->save_update | HA_STATE_WRITTEN;
188
      info->lastinx=	info->save_lastinx;
189
      info->lastpos=	info->save_lastpos;
190
      info->lastkey_length=info->save_lastkey_length;
191
    }
192
    info->read_record=	share->read_record;
193
    info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
194
    break;
195
  case HA_EXTRA_NO_USER_CHANGE: /* Database is somehow locked agains changes */
196
    info->lock_type= F_EXTRA_LCK; /* Simulate as locked */
197
    break;
198
  case HA_EXTRA_WAIT_LOCK:
199
    info->lock_wait=0;
200
    break;
201
  case HA_EXTRA_NO_WAIT_LOCK:
202
    info->lock_wait=MY_DONT_WAIT;
203
    break;
204
  case HA_EXTRA_NO_KEYS:
205
    if (info->lock_type == F_UNLCK)
206
    {
207
      error=1;					/* Not possibly if not lock */
208
      break;
209
    }
210
    if (mi_is_any_key_active(share->state.key_map))
211
    {
212
      MI_KEYDEF *key=share->keyinfo;
482 by Brian Aker
Remove uint.
213
      uint32_t i;
1 by brian
clean slate
214
      for (i=0 ; i < share->base.keys ; i++,key++)
215
      {
216
        if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
217
        {
218
          mi_clear_key_active(share->state.key_map, i);
219
          info->update|= HA_STATE_CHANGED;
220
        }
221
      }
222
223
      if (!share->changed)
224
      {
225
	share->state.changed|= STATE_CHANGED | STATE_NOT_ANALYZED;
226
	share->changed=1;			/* Update on close */
227
	if (!share->global_changed)
228
	{
229
	  share->global_changed=1;
230
	  share->state.open_count++;
231
	}
232
      }
233
      share->state.state= *info->state;
234
      error=mi_state_info_write(share->kfile,&share->state,1 | 2);
235
    }
236
    break;
237
  case HA_EXTRA_FORCE_REOPEN:
238
    pthread_mutex_lock(&THR_LOCK_myisam);
239
    share->last_version= 0L;			/* Impossible version */
240
    pthread_mutex_unlock(&THR_LOCK_myisam);
241
    break;
242
  case HA_EXTRA_PREPARE_FOR_DROP:
243
    pthread_mutex_lock(&THR_LOCK_myisam);
244
    share->last_version= 0L;			/* Impossible version */
245
#ifdef __WIN__REMOVE_OBSOLETE_WORKAROUND
246
    /* Close the isam and data files as Win32 can't drop an open table */
247
    pthread_mutex_lock(&share->intern_lock);
248
    if (flush_key_blocks(share->key_cache, share->kfile,
249
			 (function == HA_EXTRA_FORCE_REOPEN ?
250
			  FLUSH_RELEASE : FLUSH_IGNORE_CHANGED)))
251
    {
252
      error=my_errno;
253
      share->changed=1;
254
      mi_print_error(info->s, HA_ERR_CRASHED);
255
      mi_mark_crashed(info);			/* Fatal error found */
256
    }
257
    if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
258
    {
259
      info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
260
      error=end_io_cache(&info->rec_cache);
261
    }
262
    if (info->lock_type != F_UNLCK && ! info->was_locked)
263
    {
264
      info->was_locked=info->lock_type;
265
      if (mi_lock_database(info,F_UNLCK))
266
	error=my_errno;
267
      info->lock_type = F_UNLCK;
268
    }
269
    if (share->kfile >= 0)
270
      _mi_decrement_open_count(info);
271
    if (share->kfile >= 0 && my_close(share->kfile,MYF(0)))
272
      error=my_errno;
273
    {
274
      LIST *list_element ;
275
      for (list_element=myisam_open_list ;
276
	   list_element ;
277
	   list_element=list_element->next)
278
      {
279
	MI_INFO *tmpinfo=(MI_INFO*) list_element->data;
280
	if (tmpinfo->s == info->s)
281
	{
282
	  if (tmpinfo->dfile >= 0 && my_close(tmpinfo->dfile,MYF(0)))
283
	    error = my_errno;
284
	  tmpinfo->dfile= -1;
285
	}
286
      }
287
    }
288
    share->kfile= -1;				/* Files aren't open anymore */
289
    pthread_mutex_unlock(&share->intern_lock);
290
#endif
291
    pthread_mutex_unlock(&THR_LOCK_myisam);
292
    break;
293
  case HA_EXTRA_FLUSH:
294
    if (!share->temporary)
295
      flush_key_blocks(share->key_cache, share->kfile, FLUSH_KEEP);
296
#ifdef HAVE_PWRITE
297
    _mi_decrement_open_count(info);
298
#endif
299
    if (share->not_flushed)
300
    {
301
      share->not_flushed=0;
302
      if (my_sync(share->kfile, MYF(0)))
303
	error= my_errno;
304
      if (my_sync(info->dfile, MYF(0)))
305
	error= my_errno;
306
      if (error)
307
      {
308
	share->changed=1;
309
        mi_print_error(info->s, HA_ERR_CRASHED);
310
	mi_mark_crashed(info);			/* Fatal error found */
311
      }
312
    }
313
    if (share->base.blobs)
314
      mi_alloc_rec_buff(info, -1, &info->rec_buff);
315
    break;
316
  case HA_EXTRA_NORMAL:				/* Theese isn't in use */
317
    info->quick_mode=0;
318
    break;
319
  case HA_EXTRA_QUICK:
320
    info->quick_mode=1;
321
    break;
322
  case HA_EXTRA_NO_ROWS:
323
    if (!share->state.header.uniques)
324
      info->opt_flag|= OPT_NO_ROWS;
325
    break;
326
  case HA_EXTRA_PRELOAD_BUFFER_SIZE:
304 by Brian Aker
ulong cleanup, remove log code from myisam.
327
    info->preload_buff_size= *((uint32_t *) extra_arg); 
1 by brian
clean slate
328
    break;
329
  case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
330
  case HA_EXTRA_CHANGE_KEY_TO_DUP:
331
    mi_extra_keyflag(info, function);
332
    break;
333
  case HA_EXTRA_MMAP:
334
#ifdef HAVE_MMAP
335
    pthread_mutex_lock(&share->intern_lock);
336
    /*
337
      Memory map the data file if it is not already mapped. It is safe
338
      to memory map a file while other threads are using file I/O on it.
339
      Assigning a new address to a function pointer is an atomic
340
      operation. intern_lock prevents that two or more mappings are done
341
      at the same time.
342
    */
343
    if (!share->file_map)
344
    {
345
      if (mi_dynmap_file(info, share->state.state.data_file_length))
346
      {
347
        error= my_errno= errno;
348
      }
349
      else
350
      {
351
        share->file_read= mi_mmap_pread;
352
        share->file_write= mi_mmap_pwrite;
353
      }
354
    }
355
    pthread_mutex_unlock(&share->intern_lock);
356
#endif
357
    break;
358
  case HA_EXTRA_KEY_CACHE:
359
  case HA_EXTRA_NO_KEY_CACHE:
360
  default:
361
    break;
362
  }
304 by Brian Aker
ulong cleanup, remove log code from myisam.
363
51.1.99 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
364
  return(error);
1 by brian
clean slate
365
} /* mi_extra */
366
367
368
void mi_set_index_cond_func(MI_INFO *info, index_cond_func_t func,
369
                            void *func_arg)
370
{
371
  info->index_cond_func= func;
372
  info->index_cond_func_arg= func_arg;
373
}
374
375
/*
376
    Start/Stop Inserting Duplicates Into a Table, WL#1648.
377
 */
378
static void mi_extra_keyflag(MI_INFO *info, enum ha_extra_function function)
379
{
482 by Brian Aker
Remove uint.
380
  uint32_t  idx;
1 by brian
clean slate
381
382
  for (idx= 0; idx< info->s->base.keys; idx++)
383
  {
384
    switch (function) {
385
    case HA_EXTRA_CHANGE_KEY_TO_UNIQUE:
386
      info->s->keyinfo[idx].flag|= HA_NOSAME;
387
      break;
388
    case HA_EXTRA_CHANGE_KEY_TO_DUP:
389
      info->s->keyinfo[idx].flag&= ~(HA_NOSAME);
390
      break;
391
    default:
392
      break;
393
    }
394
  }
395
}
396
397
398
int mi_reset(MI_INFO *info)
399
{
400
  int error= 0;
401
  MYISAM_SHARE *share=info->s;
402
  /*
403
    Free buffers and reset the following flags:
404
    EXTRA_CACHE, EXTRA_WRITE_CACHE, EXTRA_KEYREAD, EXTRA_QUICK
405
406
    If the row buffer cache is large (for dynamic tables), reduce it
407
    to save memory.
408
  */
409
  if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
410
  {
411
    info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
412
    error= end_io_cache(&info->rec_cache);
413
  }
414
  if (share->base.blobs)
415
    mi_alloc_rec_buff(info, -1, &info->rec_buff);
416
#if defined(HAVE_MMAP) && defined(HAVE_MADVISE)
417
  if (info->opt_flag & MEMMAP_USED)
418
    madvise((char*) share->file_map, share->state.state.data_file_length,
419
            MADV_RANDOM);
420
#endif
421
  info->opt_flag&= ~(KEY_READ_USED | REMEMBER_OLD_POS);
422
  info->quick_mode=0;
423
  info->lastinx= 0;			/* Use first index as def */
424
  info->last_search_keypage= info->lastpos= HA_OFFSET_ERROR;
425
  info->page_changed= 1;
426
  info->update= ((info->update & HA_STATE_CHANGED) | HA_STATE_NEXT_FOUND |
427
                 HA_STATE_PREV_FOUND);
51.1.99 by Jay Pipes
Removed/replaced DBUG symbols and TRUE/FALSE
428
  return(error);
1 by brian
clean slate
429
}