~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/myisam/mi_preload.cc

  • Committer: Mark Atwood
  • Date: 2011-09-14 03:30:42 UTC
  • mfrom: (2409.2.6 refactor7)
  • Revision ID: me@mark.atwood.name-20110914033042-2u0s8foaigvf62g2
mergeĀ lp:~olafvdspek/drizzle/refactor7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2003, 2005 MySQL 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 St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
/*
17
 
  Preload indexes into key cache
18
 
*/
19
 
 
20
 
#include "myisam_priv.h"
21
 
#include <stdlib.h>
22
 
#include <drizzled/util/test.h>
23
 
 
24
 
using namespace drizzled;
25
 
 
26
 
/*
27
 
  Preload pages of the index file for a table into the key cache
28
 
 
29
 
  SYNOPSIS
30
 
    mi_preload()
31
 
      info          open table
32
 
      map           map of indexes to preload into key cache
33
 
      ignore_leaves only non-leaves pages are to be preloaded
34
 
 
35
 
  RETURN VALUE
36
 
    0 if a success. error code - otherwise.
37
 
 
38
 
  NOTES.
39
 
    At present pages for all indexes are preloaded.
40
 
    In future only pages for indexes specified in the key_map parameter
41
 
    of the table will be preloaded.
42
 
*/
43
 
 
44
 
int mi_preload(MI_INFO *info, uint64_t key_map, bool ignore_leaves)
45
 
{
46
 
  uint32_t i;
47
 
  uint32_t length, block_length= 0;
48
 
  unsigned char *buff= NULL;
49
 
  MYISAM_SHARE* share= info->s;
50
 
  uint32_t keys= share->state.header.keys;
51
 
  MI_KEYDEF *keyinfo= share->keyinfo;
52
 
  internal::my_off_t key_file_length= share->state.state.key_file_length;
53
 
  internal::my_off_t pos= share->base.keystart;
54
 
 
55
 
  if (!keys || !mi_is_any_key_active(key_map) || key_file_length == pos)
56
 
    return(0);
57
 
 
58
 
  block_length= keyinfo[0].block_length;
59
 
 
60
 
  if (ignore_leaves)
61
 
  {
62
 
    /* Check whether all indexes use the same block size */
63
 
    for (i= 1 ; i < keys ; i++)
64
 
    {
65
 
      if (keyinfo[i].block_length != block_length)
66
 
        return(errno= HA_ERR_NON_UNIQUE_BLOCK_SIZE);
67
 
    }
68
 
  }
69
 
  else
70
 
    block_length= share->getKeyCache()->key_cache_block_size;
71
 
 
72
 
  length= info->preload_buff_size/block_length * block_length;
73
 
  set_if_bigger(length, block_length);
74
 
 
75
 
  if (!(buff= (unsigned char *) malloc(length)))
76
 
    return(errno= HA_ERR_OUT_OF_MEM);
77
 
 
78
 
  if (flush_key_blocks(share->getKeyCache(), share->kfile, FLUSH_RELEASE))
79
 
    goto err;
80
 
 
81
 
  do
82
 
  {
83
 
    /* Read the next block of index file into the preload buffer */
84
 
    if ((internal::my_off_t) length > (key_file_length-pos))
85
 
      length= (uint32_t) (key_file_length-pos);
86
 
    if (my_pread(share->kfile, (unsigned char*) buff, length, pos, MYF(MY_FAE|MY_FNABP)))
87
 
      goto err;
88
 
 
89
 
    if (ignore_leaves)
90
 
    {
91
 
      unsigned char *end= buff+length;
92
 
      do
93
 
      {
94
 
        if (mi_test_if_nod(buff))
95
 
        {
96
 
          if (key_cache_insert(share->getKeyCache(),
97
 
                               share->kfile, pos, DFLT_INIT_HITS,
98
 
                              (unsigned char*) buff, block_length))
99
 
            goto err;
100
 
        }
101
 
        pos+= block_length;
102
 
      }
103
 
      while ((buff+= block_length) != end);
104
 
      buff= end-length;
105
 
    }
106
 
    else
107
 
    {
108
 
      if (key_cache_insert(share->getKeyCache(),
109
 
                           share->kfile, pos, DFLT_INIT_HITS,
110
 
                           (unsigned char*) buff, length))
111
 
        goto err;
112
 
      pos+= length;
113
 
    }
114
 
  }
115
 
  while (pos != key_file_length);
116
 
 
117
 
  free((char*) buff);
118
 
  return(0);
119
 
 
120
 
err:
121
 
  free((char*) buff);
122
 
  return(errno= errno);
123
 
}
124