1 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 /* Check that heap-structure is ok */
24 
25 #include "heapdef.h"
26 
27 static int check_one_key(HP_KEYDEF *keydef, uint keynr, ulong records,
28 			 ulong blength, my_bool print_status);
29 static int check_one_rb_key(HP_INFO *info, uint keynr, ulong records,
30 			    my_bool print_status);
31 
32 
33 /*
34   Check if keys and rows are ok in a heap table
35 
36   SYNOPSIS
37     heap_check_heap()
38     info		Table handler
39     print_status	Prints some extra status
40 
41   NOTES
42     Doesn't change the state of the table handler
43 
44   RETURN VALUES
45     0	ok
46     1 error
47 */
48 
heap_check_heap(HP_INFO * info,my_bool print_status)49 int heap_check_heap(HP_INFO *info, my_bool print_status)
50 {
51   int error;
52   uint key;
53   ulong records=0, deleted=0, pos, next_block;
54   HP_SHARE *share=info->s;
55   HP_INFO save_info= *info;			/* Needed because scan_init */
56   DBUG_ENTER("heap_check_heap");
57 
58   for (error=key= 0 ; key < share->keys ; key++)
59   {
60     if (share->keydef[key].algorithm == HA_KEY_ALG_BTREE)
61       error|= check_one_rb_key(info, key, share->records, print_status);
62     else
63       error|= check_one_key(share->keydef + key, key, share->records,
64 			    share->blength, print_status);
65   }
66   /*
67     This is basicly the same code as in hp_scan, but we repeat it here to
68     get shorter DBUG log file.
69   */
70   for (pos=next_block= 0 ; ; pos++)
71   {
72     if (pos < next_block)
73     {
74       info->current_ptr+= share->block.recbuffer;
75     }
76     else
77     {
78       next_block+= share->block.records_in_block;
79       if (next_block >= share->records+share->deleted)
80       {
81 	next_block= share->records+share->deleted;
82 	if (pos >= next_block)
83 	  break;				/* End of file */
84       }
85     }
86     hp_find_record(info,pos);
87 
88     if (!info->current_ptr[share->reclength])
89       deleted++;
90     else
91       records++;
92   }
93 
94   if (records != share->records || deleted != share->deleted)
95   {
96     DBUG_PRINT("error",("Found rows: %lu (%lu)  deleted %lu (%lu)",
97 			records, (ulong) share->records,
98                         deleted, (ulong) share->deleted));
99     error= 1;
100   }
101   *info= save_info;
102   DBUG_RETURN(error);
103 }
104 
105 
check_one_key(HP_KEYDEF * keydef,uint keynr,ulong records,ulong blength,my_bool print_status)106 static int check_one_key(HP_KEYDEF *keydef, uint keynr, ulong records,
107 			 ulong blength, my_bool print_status)
108 {
109   int error;
110   ulong i,found,max_links,seek,links;
111   ulong rec_link;				/* Only used with debugging */
112   ulong hash_buckets_found;
113   HASH_INFO *hash_info;
114 
115   error=0;
116   hash_buckets_found= 0;
117   for (i=found=max_links=seek=0 ; i < records ; i++)
118   {
119     hash_info=hp_find_hash(&keydef->block,i);
120     if (hp_mask(hash_info->hash, blength,records) == i)
121     {
122       found++;
123       seek++;
124       links=1;
125       while ((hash_info=hash_info->next_key) && found < records + 1)
126       {
127 	seek+= ++links;
128 	if (i != (rec_link= hp_mask(hash_info->hash, blength, records)))
129 	{
130 	  DBUG_PRINT("error",
131                      ("Record in wrong link: Link %lu  Record: 0x%lx  Record-link %lu",
132                       i, (long) hash_info->ptr_to_rec, rec_link));
133 	  error=1;
134 	}
135 	else
136 	  found++;
137       }
138       if (links > max_links) max_links=links;
139       hash_buckets_found++;
140     }
141   }
142   if (found != records)
143   {
144     DBUG_PRINT("error",("Found %ld of %ld records", found, records));
145     error=1;
146   }
147   if (keydef->hash_buckets != hash_buckets_found)
148   {
149     DBUG_PRINT("error",("Found %ld buckets, stats shows %ld buckets",
150                         hash_buckets_found, (long) keydef->hash_buckets));
151     error=1;
152   }
153   DBUG_PRINT("info",
154 	     ("records: %ld   seeks: %lu   max links: %lu   hitrate: %.2f   "
155               "buckets: %lu",
156 	      records,seek,max_links,
157 	      (float) seek / (float) (records ? records : 1),
158               hash_buckets_found));
159   if (print_status)
160     printf("Key: %d  records: %ld   seeks: %lu   max links: %lu   "
161            "hitrate: %.2f   buckets: %lu\n",
162 	   keynr, records, seek, max_links,
163 	   (float) seek / (float) (records ? records : 1),
164            hash_buckets_found);
165   return error;
166 }
167 
check_one_rb_key(HP_INFO * info,uint keynr,ulong records,my_bool print_status)168 static int check_one_rb_key(HP_INFO *info, uint keynr, ulong records,
169 			    my_bool print_status)
170 {
171   HP_KEYDEF *keydef= info->s->keydef + keynr;
172   int error= 0;
173   ulong found= 0;
174   uchar *key, *recpos;
175   uint key_length;
176   uint not_used[2];
177 
178   if ((key= tree_search_edge(&keydef->rb_tree, info->parents,
179 			     &info->last_pos, offsetof(TREE_ELEMENT, left))))
180   {
181     do
182     {
183       memcpy(&recpos, key + (*keydef->get_key_length)(keydef,key), sizeof(uchar*));
184       key_length= hp_rb_make_key(keydef, info->recbuf, recpos, 0);
185       if (ha_key_cmp(keydef->seg, (uchar*) info->recbuf, (uchar*) key,
186 		     key_length, SEARCH_FIND | SEARCH_SAME, not_used))
187       {
188 	error= 1;
189 	DBUG_PRINT("error",("Record in wrong link:  key: %u  Record: 0x%lx\n",
190 			    keynr, (long) recpos));
191       }
192       else
193 	found++;
194       key= tree_search_next(&keydef->rb_tree, &info->last_pos,
195 			    offsetof(TREE_ELEMENT, left),
196 			    offsetof(TREE_ELEMENT, right));
197     } while (key);
198   }
199   if (found != records)
200   {
201     DBUG_PRINT("error",("Found %lu of %lu records", found, records));
202     error= 1;
203   }
204   if (print_status)
205     printf("Key: %d  records: %ld\n", keynr, records);
206   return error;
207 }
208