~vlad-lesin/percona-server/mysql-5.0.33-original

« back to all changes in this revision

Viewing changes to myisam/mi_preload.c

  • Committer: Vlad Lesin
  • Date: 2012-07-31 09:21:34 UTC
  • Revision ID: vladislav.lesin@percona.com-20120731092134-zfodx022b7992wsi
VirginĀ 5.0.33

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 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; either version 2 of the License, or
 
6
   (at your option) any later version.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; if not, write to the Free Software
 
15
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
16
 
 
17
/*
 
18
  Preload indexes into key cache
 
19
*/
 
20
 
 
21
#include "myisamdef.h"
 
22
 
 
23
 
 
24
/*
 
25
  Preload pages of the index file for a table into the key cache
 
26
 
 
27
  SYNOPSIS
 
28
    mi_preload()
 
29
      info          open table
 
30
      map           map of indexes to preload into key cache 
 
31
      ignore_leaves only non-leaves pages are to be preloaded
 
32
 
 
33
  RETURN VALUE
 
34
    0 if a success. error code - otherwise.
 
35
 
 
36
  NOTES.
 
37
    At present pages for all indexes are preloaded.
 
38
    In future only pages for indexes specified in the key_map parameter
 
39
    of the table will be preloaded.
 
40
*/
 
41
 
 
42
int mi_preload(MI_INFO *info, ulonglong key_map, my_bool ignore_leaves)
 
43
{
 
44
  uint i;
 
45
  ulong length, block_length= 0;
 
46
  uchar *buff= NULL;
 
47
  MYISAM_SHARE* share= info->s;
 
48
  uint keys= share->state.header.keys;
 
49
  MI_KEYDEF *keyinfo= share->keyinfo;
 
50
  my_off_t key_file_length= share->state.state.key_file_length;
 
51
  my_off_t pos= share->base.keystart;
 
52
  DBUG_ENTER("mi_preload");
 
53
 
 
54
  if (!keys || !mi_is_any_key_active(key_map) || key_file_length == pos)
 
55
    DBUG_RETURN(0);
 
56
 
 
57
  block_length= keyinfo[0].block_length;
 
58
 
 
59
  /* Check whether all indexes use the same block size */
 
60
  for (i= 1 ; i < keys ; i++)
 
61
  {
 
62
    if (keyinfo[i].block_length != block_length)
 
63
      DBUG_RETURN(my_errno= HA_ERR_NON_UNIQUE_BLOCK_SIZE);
 
64
  }
 
65
 
 
66
  length= info->preload_buff_size/block_length * block_length;
 
67
  set_if_bigger(length, block_length);
 
68
 
 
69
  if (!(buff= (uchar *) my_malloc(length, MYF(MY_WME))))
 
70
    DBUG_RETURN(my_errno= HA_ERR_OUT_OF_MEM);
 
71
 
 
72
  if (flush_key_blocks(share->key_cache,share->kfile, FLUSH_RELEASE))
 
73
    goto err;
 
74
 
 
75
  do
 
76
  {
 
77
    /* Read the next block of index file into the preload buffer */
 
78
    if ((my_off_t) length > (key_file_length-pos))
 
79
      length= (ulong) (key_file_length-pos);
 
80
    if (my_pread(share->kfile, (byte*) buff, length, pos, MYF(MY_FAE|MY_FNABP)))
 
81
      goto err;
 
82
 
 
83
    if (ignore_leaves)
 
84
    {
 
85
      uchar *end= buff+length;
 
86
      do
 
87
      {
 
88
        if (mi_test_if_nod(buff))
 
89
        {
 
90
          if (key_cache_insert(share->key_cache,
 
91
                               share->kfile, pos, DFLT_INIT_HITS,
 
92
                              (byte*) buff, block_length))
 
93
            goto err;
 
94
        }
 
95
        pos+= block_length;
 
96
      }
 
97
      while ((buff+= block_length) != end);
 
98
      buff= end-length;
 
99
    }
 
100
    else
 
101
    {
 
102
      if (key_cache_insert(share->key_cache,
 
103
                           share->kfile, pos, DFLT_INIT_HITS,
 
104
                           (byte*) buff, length))
 
105
        goto err;
 
106
      pos+= length;
 
107
    }
 
108
  }
 
109
  while (pos != key_file_length);
 
110
 
 
111
  my_free((char*) buff, MYF(0));
 
112
  DBUG_RETURN(0);
 
113
 
 
114
err:
 
115
  my_free((char*) buff, MYF(MY_ALLOW_ZERO_PTR));
 
116
  DBUG_RETURN(my_errno= errno);
 
117
}
 
118