1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2    Use is subject to license terms.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
23 
24 /*
25   remove all records from database
26   Identical as hp_create() and hp_open() but used HP_SHARE* instead of name and
27   database remains open.
28 */
29 
30 #include "heapdef.h"
31 
heap_clear(HP_INFO * info)32 void heap_clear(HP_INFO *info)
33 {
34   hp_clear(info->s);
35 }
36 
hp_clear(HP_SHARE * info)37 void hp_clear(HP_SHARE *info)
38 {
39   DBUG_ENTER("hp_clear");
40 
41   if (info->block.levels)
42     (void) hp_free_level(&info->block,info->block.levels,info->block.root,
43 			(uchar*) 0);
44   info->block.levels=0;
45   hp_clear_keys(info);
46   info->records= info->deleted= 0;
47   info->data_length= 0;
48   info->blength=1;
49   info->changed=0;
50   info->del_link=0;
51   DBUG_VOID_RETURN;
52 }
53 
54 
55 /*
56   Clear all keys.
57 
58   SYNOPSIS
59     heap_clear_keys()
60     info      A pointer to the heap storage engine HP_INFO struct.
61 
62   DESCRIPTION
63     Delete all trees of all indexes and leave them empty.
64 
65   RETURN
66     void
67 */
68 
heap_clear_keys(HP_INFO * info)69 void heap_clear_keys(HP_INFO *info)
70 {
71   hp_clear(info->s);
72 }
73 
74 
75 /*
76   Clear all keys.
77 
78   SYNOPSIS
79     hp_clear_keys()
80     info      A pointer to the heap storage engine HP_SHARE struct.
81 
82   DESCRIPTION
83     Delete all trees of all indexes and leave them empty.
84 
85   RETURN
86     void
87 */
88 
hp_clear_keys(HP_SHARE * info)89 void hp_clear_keys(HP_SHARE *info)
90 {
91   uint key;
92   DBUG_ENTER("hp_clear_keys");
93 
94   for (key=0 ; key < info->keys ; key++)
95   {
96     HP_KEYDEF *keyinfo = info->keydef + key;
97     if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
98     {
99       delete_tree(&keyinfo->rb_tree);
100     }
101     else
102     {
103       HP_BLOCK *block= &keyinfo->block;
104       if (block->levels)
105         (void) hp_free_level(block,block->levels,block->root,(uchar*) 0);
106       block->levels=0;
107       block->last_allocated=0;
108       keyinfo->hash_buckets= 0;
109     }
110   }
111   info->index_length=0;
112   DBUG_VOID_RETURN;
113 }
114 
115 
116 /*
117   Disable all indexes.
118 
119   SYNOPSIS
120     heap_disable_indexes()
121     info      A pointer to the heap storage engine HP_INFO struct.
122 
123   DESCRIPTION
124     Disable and clear (remove contents of) all indexes.
125 
126   RETURN
127     0  ok
128 */
129 
heap_disable_indexes(HP_INFO * info)130 int heap_disable_indexes(HP_INFO *info)
131 {
132   HP_SHARE *share= info->s;
133 
134   if (share->keys)
135   {
136     hp_clear_keys(share);
137     share->currently_disabled_keys= share->keys;
138     share->keys= 0;
139   }
140   return 0;
141 }
142 
143 
144 /*
145   Enable all indexes
146 
147   SYNOPSIS
148     heap_enable_indexes()
149     info      A pointer to the heap storage engine HP_INFO struct.
150 
151   DESCRIPTION
152     Enable all indexes. The indexes might have been disabled
153     by heap_disable_index() before.
154     The function works only if both data and indexes are empty,
155     since the heap storage engine cannot repair the indexes.
156     To be sure, call handler::delete_all_rows() before.
157 
158   RETURN
159     0  ok
160     HA_ERR_CRASHED data or index is non-empty.
161 */
162 
heap_enable_indexes(HP_INFO * info)163 int heap_enable_indexes(HP_INFO *info)
164 {
165   int error= 0;
166   HP_SHARE *share= info->s;
167 
168   if (share->data_length || share->index_length)
169     error= HA_ERR_CRASHED;
170   else
171     if (share->currently_disabled_keys)
172     {
173       share->keys= share->currently_disabled_keys;
174       share->currently_disabled_keys= 0;
175     }
176   return error;
177 }
178 
179 
180 /*
181   Test if indexes are disabled.
182 
183   SYNOPSIS
184     heap_indexes_are_disabled()
185     info      A pointer to the heap storage engine HP_INFO struct.
186 
187   DESCRIPTION
188     Test if indexes are disabled.
189 
190   RETURN
191     0  indexes are not disabled
192     1  all indexes are disabled
193    [2  non-unique indexes are disabled - NOT YET IMPLEMENTED]
194 */
195 
heap_indexes_are_disabled(HP_INFO * info)196 int heap_indexes_are_disabled(HP_INFO *info)
197 {
198   HP_SHARE *share= info->s;
199 
200   return (! share->keys && share->currently_disabled_keys);
201 }
202 
203