1 /* Copyright (c) 2003, 2010, 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 St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /*
24   Preload indexes into key cache
25 */
26 
27 #include "myisamdef.h"
28 
29 
30 /*
31   Preload pages of the index file for a table into the key cache
32 
33   SYNOPSIS
34     mi_preload()
35       info          open table
36       map           map of indexes to preload into key cache
37       ignore_leaves only non-leaves pages are to be preloaded
38 
39   RETURN VALUE
40     0 if a success. error code - otherwise.
41 
42   NOTES.
43     At present pages for all indexes are preloaded.
44     In future only pages for indexes specified in the key_map parameter
45     of the table will be preloaded.
46 */
47 
mi_preload(MI_INFO * info,ulonglong key_map,my_bool ignore_leaves)48 int mi_preload(MI_INFO *info, ulonglong key_map, my_bool ignore_leaves)
49 {
50   uint i;
51   ulong length, block_length= 0;
52   uchar *buff= NULL;
53   MYISAM_SHARE* share= info->s;
54   uint keys= share->state.header.keys;
55   MI_KEYDEF *keyinfo= share->keyinfo;
56   my_off_t key_file_length= share->state.state.key_file_length;
57   my_off_t pos= share->base.keystart;
58   DBUG_ENTER("mi_preload");
59 
60   if (!keys || !mi_is_any_key_active(key_map) || key_file_length == pos)
61     DBUG_RETURN(0);
62 
63   /* Preload into a non initialized key cache should never happen. */
64   DBUG_ASSERT(share->key_cache->key_cache_inited);
65 
66   block_length= keyinfo[0].block_length;
67 
68   if (ignore_leaves)
69   {
70     /* Check whether all indexes use the same block size */
71     for (i= 1 ; i < keys ; i++)
72     {
73       if (keyinfo[i].block_length != block_length)
74         DBUG_RETURN(my_errno= HA_ERR_NON_UNIQUE_BLOCK_SIZE);
75     }
76   }
77   else
78     block_length= share->key_cache->key_cache_block_size;
79 
80   length= info->preload_buff_size/block_length * block_length;
81   set_if_bigger(length, block_length);
82 
83   if (!(buff= (uchar *) my_malloc(length, MYF(MY_WME))))
84     DBUG_RETURN(my_errno= HA_ERR_OUT_OF_MEM);
85 
86   if (flush_key_blocks(share->key_cache,share->kfile, FLUSH_RELEASE))
87     goto err;
88 
89   do
90   {
91     /* Read the next block of index file into the preload buffer */
92     if ((my_off_t) length > (key_file_length-pos))
93       length= (ulong) (key_file_length-pos);
94     if (mysql_file_pread(share->kfile, (uchar*) buff, length, pos,
95                          MYF(MY_FAE|MY_FNABP)))
96       goto err;
97 
98     if (ignore_leaves)
99     {
100       uchar *end= buff+length;
101       do
102       {
103         if (mi_test_if_nod(buff))
104         {
105           if (key_cache_insert(share->key_cache,
106                                share->kfile, pos, DFLT_INIT_HITS,
107                               (uchar*) buff, block_length))
108 	    goto err;
109 	}
110         pos+= block_length;
111       }
112       while ((buff+= block_length) != end);
113       buff= end-length;
114     }
115     else
116     {
117       if (key_cache_insert(share->key_cache,
118                            share->kfile, pos, DFLT_INIT_HITS,
119                            (uchar*) buff, length))
120 	goto err;
121       pos+= length;
122     }
123   }
124   while (pos != key_file_length);
125 
126   my_free(buff);
127   DBUG_RETURN(0);
128 
129 err:
130   my_free(buff);
131   DBUG_RETURN(my_errno= errno);
132 }
133 
134