mi_delete_all.c 2.46 KB
Newer Older
1
/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6

unknown's avatar
unknown committed
7 8 9 10
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
11

unknown's avatar
unknown committed
12 13
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
unknown's avatar
unknown committed
15 16

/* Remove all rows from a MyISAM table */
17
/* This clears the status information and truncates files */
unknown's avatar
unknown committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include "myisamdef.h"

int mi_delete_all_rows(MI_INFO *info)
{
  uint i;
  MYISAM_SHARE *share=info->s;
  MI_STATE_INFO *state=&share->state;
  DBUG_ENTER("mi_delete_all_rows");

  if (share->options & HA_OPTION_READ_ONLY_DATA)
  {
    DBUG_RETURN(my_errno=EACCES);
  }
  if (_mi_readinfo(info,F_WRLCK,1))
    DBUG_RETURN(my_errno);
  if (_mi_mark_file_changed(info))
    goto err;

  info->state->records=info->state->del=state->split=0;
  state->dellink = HA_OFFSET_ERROR;
  state->sortkey=  (ushort) ~0;
  info->state->key_file_length=share->base.keystart;
  info->state->data_file_length=0;
  info->state->empty=info->state->key_empty=0;
43
  info->state->checksum=0;
unknown's avatar
unknown committed
44

unknown's avatar
unknown committed
45
  for (i=share->base.max_key_block_length/MI_MIN_KEY_BLOCK_LENGTH ; i-- ; )
unknown's avatar
unknown committed
46 47 48 49
    state->key_del[i]= HA_OFFSET_ERROR;
  for (i=0 ; i < share->base.keys ; i++)
    state->key_root[i]= HA_OFFSET_ERROR;

50
  myisam_log_command(MI_LOG_DELETE_ALL,info,(uchar*) 0,0,0);
unknown's avatar
unknown committed
51 52 53 54
  /*
    If we are using delayed keys or if the user has done changes to the tables
    since it was locked then there may be key blocks in the key cache
  */
unknown's avatar
unknown committed
55
  flush_key_blocks(share->key_cache, share->kfile, FLUSH_IGNORE_CHANGED);
56 57
#ifdef HAVE_MMAP
  if (share->file_map)
58
    mi_munmap_file(info);
59
#endif
Marc Alff's avatar
Marc Alff committed
60 61
  if (mysql_file_chsize(info->dfile, 0, 0, MYF(MY_WME)) ||
      mysql_file_chsize(share->kfile, share->base.keystart, 0, MYF(MY_WME)))
62
    goto err;
Konstantin Osipov's avatar
Konstantin Osipov committed
63
  (void) _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
unknown's avatar
unknown committed
64 65 66 67 68
  DBUG_RETURN(0);

err:
  {
    int save_errno=my_errno;
Konstantin Osipov's avatar
Konstantin Osipov committed
69
    (void) _mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
unknown's avatar
unknown committed
70 71 72 73
    info->update|=HA_STATE_WRITTEN;	/* Buffer changed */
    DBUG_RETURN(my_errno=save_errno);
  }
} /* mi_delete */