1 /* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
15 
16 /* Remove all rows from a MARIA table */
17 /* This clears the status information and truncates files */
18 
19 #include "maria_def.h"
20 #include "trnman.h"
21 
22 /**
23    @brief deletes all rows from a table
24 
25    @param  info             Maria handler
26 
27    @note It is important that this function does not rely on the state
28    information, as it may be called by ma_apply_undo_bulk_insert() on an
29    inconsistent table left by a crash.
30 
31    @return Operation status
32      @retval 0      ok
33      @retval 1      error
34 */
35 
maria_delete_all_rows(MARIA_HA * info)36 int maria_delete_all_rows(MARIA_HA *info)
37 {
38   MARIA_SHARE *share= info->s;
39   my_bool log_record;
40   LSN lsn;
41 #ifdef HAVE_MMAP
42   my_bool mmap_file= share->file_map != 0;
43 #endif
44   DBUG_ENTER("maria_delete_all_rows");
45 
46   if (share->options & HA_OPTION_READ_ONLY_DATA)
47   {
48     DBUG_RETURN(my_errno=EACCES);
49   }
50   /**
51      @todo LOCK take X-lock on table here.
52      When we have versioning, if some other thread is looking at this table,
53      we cannot shrink the file like this.
54   */
55   if (_ma_readinfo(info,F_WRLCK,1))
56     DBUG_RETURN(my_errno);
57   log_record= share->now_transactional && !share->temporary;
58 
59   if (log_record)
60   {
61     /*
62       This record will be used by Recovery to finish the deletion if it
63       crashed. We force it to have a complete history in the log.
64     */
65     LEX_CUSTRING log_array[TRANSLOG_INTERNAL_PARTS + 1];
66     uchar log_data[FILEID_STORE_SIZE];
67     log_array[TRANSLOG_INTERNAL_PARTS + 0].str=    log_data;
68     log_array[TRANSLOG_INTERNAL_PARTS + 0].length= sizeof(log_data);
69     if (unlikely(translog_write_record(&lsn, LOGREC_REDO_DELETE_ALL,
70                                        info->trn, info, 0,
71                                        sizeof(log_array)/sizeof(log_array[0]),
72                                        log_array, log_data, NULL) ||
73                  translog_flush(lsn)))
74       goto err;
75     /*
76       If we fail in this function after this point, log and table will be
77       inconsistent.
78     */
79     if (_ma_mark_file_changed(share))
80       goto err;
81   }
82   else
83   {
84     if (_ma_mark_file_changed(share))
85       goto err;
86     /* Other branch called function below when writing log record, in hook */
87     _ma_reset_status(info);
88   }
89   /* Remove old history as the table is now empty for everyone */
90   _ma_reset_state(info);
91   share->state.changed= 0;
92 
93   /*
94     If we are using delayed keys or if the user has done changes to the tables
95     since it was locked then there may be key blocks in the page cache. Or
96     there may be data blocks there. We need to throw them away or they may
97     re-enter the emptied table or another table later.
98   */
99 
100 #ifdef HAVE_MMAP
101   if (mmap_file)
102     _ma_unmap_file(info);
103 #endif
104 
105   if (_ma_flush_table_files(info, MARIA_FLUSH_DATA|MARIA_FLUSH_INDEX,
106                             FLUSH_IGNORE_CHANGED, FLUSH_IGNORE_CHANGED) ||
107       mysql_file_chsize(info->dfile.file, 0, 0, MYF(MY_WME)) ||
108       mysql_file_chsize(share->kfile.file, share->base.keystart, 0, MYF(MY_WME)))
109     goto err;
110 
111   if (_ma_initialize_data_file(share, info->dfile.file))
112     goto err;
113 
114   if (log_record)
115   {
116     /*
117       Because LOGREC_REDO_DELETE_ALL does not operate on pages, it has the
118       following problem:
119       delete_all; inserts (redo_insert); all pages get flushed; checkpoint:
120       the dirty pages list will be empty. In recovery, delete_all is executed,
121       but redo_insert are skipped (dirty pages list is empty).
122       To avoid this, we need to set skip_redo_lsn now, and thus need to sync
123       files.
124       Also fixes the problem of:
125       bulk insert; insert; delete_all; crash:
126       "bulk insert" is skipped (no REDOs), so if "insert" would not be skipped
127       (if we didn't update skip_redo_lsn below) then "insert" would be tried
128       and fail, saying that it sees that the first page has to be created
129       though the inserted row has rownr>0.
130     */
131     my_bool error= _ma_state_info_write(share,
132                                         MA_STATE_INFO_WRITE_DONT_MOVE_OFFSET |
133                                         MA_STATE_INFO_WRITE_LOCK) ||
134       _ma_update_state_lsns(share, lsn, trnman_get_min_trid(), FALSE, FALSE) ||
135       _ma_sync_table_files(info);
136     info->trn->rec_lsn= LSN_IMPOSSIBLE;
137     if (error)
138       goto err;
139   }
140 
141   if (info->opt_flag & WRITE_CACHE_USED)
142     reinit_io_cache(&info->rec_cache, WRITE_CACHE, 0, 1, 1);
143 
144   _ma_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
145 #ifdef HAVE_MMAP
146   /* Map again */
147   if (mmap_file)
148     _ma_dynmap_file(info, (my_off_t) 0);
149 #endif
150   DBUG_RETURN(0);
151 
152 err:
153   {
154     int save_errno=my_errno;
155     _ma_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
156     info->update|=HA_STATE_WRITTEN;	/* Buffer changed */
157     DBUG_RETURN(my_errno=save_errno);
158   }
159 } /* maria_delete_all_rows */
160 
161 
162 /*
163   Reset status information
164 
165   SYNOPSIS
166     _ma_reset_status()
167     maria	Maria handler
168 
169   DESCRIPTION
170     Resets data and index file information as if the file would be empty
171     Files are not touched.
172 */
173 
_ma_reset_status(MARIA_HA * info)174 void _ma_reset_status(MARIA_HA *info)
175 {
176   MARIA_SHARE *share= info->s;
177   MARIA_STATE_INFO *state= &share->state;
178   uint i;
179   DBUG_ENTER("_ma_reset_status");
180 
181   state->split= 0;
182   state->state.records= state->state.del= 0;
183   state->changed=  0;                            /* File is optimized */
184   state->dellink= HA_OFFSET_ERROR;
185   state->sortkey=  (ushort) ~0;
186   state->state.key_file_length= share->base.keystart;
187   state->state.data_file_length= 0;
188   state->state.empty= state->state.key_empty= 0;
189   state->state.checksum= 0;
190   share->state.open_count= 0;
191   share->global_changed= 0;
192 
193   share->changed= 1;                            /* We must write state */
194 
195   *info->state= state->state;
196 
197   /* Drop the delete key chain. */
198   state->key_del= HA_OFFSET_ERROR;
199   /* Clear all keys */
200   for (i=0 ; i < share->base.keys ; i++)
201     state->key_root[i]= HA_OFFSET_ERROR;
202   DBUG_VOID_RETURN;
203 }
204