Drizzled Public API Documentation

mi_page.cc

00001 /* Copyright (C) 2000-2004, 2006 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /* Read and write key blocks */
00017 
00018 #include "myisam_priv.h"
00019 
00020 using namespace drizzled;
00021 
00022   /* Fetch a key-page in memory */
00023 
00024 unsigned char *_mi_fetch_keypage(register MI_INFO *info, MI_KEYDEF *keyinfo,
00025        internal::my_off_t page, int level,
00026                          unsigned char *buff, int return_buffer)
00027 {
00028   unsigned char *tmp;
00029   uint32_t page_size;
00030 
00031   tmp=(unsigned char*) key_cache_read(info->s->getKeyCache(),
00032                              info->s->kfile, page, level, (unsigned char*) buff,
00033            (uint) keyinfo->block_length,
00034            (uint) keyinfo->block_length,
00035            return_buffer);
00036   if (tmp == info->buff)
00037     info->buff_used=1;
00038   else if (!tmp)
00039   {
00040     info->last_keypage=HA_OFFSET_ERROR;
00041     mi_print_error(info->s, HA_ERR_CRASHED);
00042     errno=HA_ERR_CRASHED;
00043     return(0);
00044   }
00045   info->last_keypage=page;
00046   page_size=mi_getint(tmp);
00047   if (page_size < 4 || page_size > keyinfo->block_length)
00048   {
00049     info->last_keypage = HA_OFFSET_ERROR;
00050     mi_print_error(info->s, HA_ERR_CRASHED);
00051     errno = HA_ERR_CRASHED;
00052     tmp = 0;
00053   }
00054   return(tmp);
00055 } /* _mi_fetch_keypage */
00056 
00057 
00058   /* Write a key-page on disk */
00059 
00060 int _mi_write_keypage(register MI_INFO *info, register MI_KEYDEF *keyinfo,
00061           internal::my_off_t page, int level, unsigned char *buff)
00062 {
00063   register uint32_t length;
00064 
00065 #ifndef FAST          /* Safety check */
00066   if (page < info->s->base.keystart ||
00067       page+keyinfo->block_length > info->state->key_file_length ||
00068       (page & (MI_MIN_KEY_BLOCK_LENGTH-1)))
00069   {
00070     errno=EINVAL;
00071     return((-1));
00072   }
00073 #endif
00074 
00075   if ((length=keyinfo->block_length) > IO_SIZE*2 &&
00076       info->state->key_file_length != page+length)
00077     length= ((mi_getint(buff)+IO_SIZE-1) & (uint) ~(IO_SIZE-1));
00078 #ifdef HAVE_VALGRIND
00079   {
00080     length=mi_getint(buff);
00081     memset(buff+length, 0, keyinfo->block_length-length);
00082     length=keyinfo->block_length;
00083   }
00084 #endif
00085   return((key_cache_write(info->s->getKeyCache(),
00086                          info->s->kfile,page, level, (unsigned char*) buff,length,
00087        (uint) keyinfo->block_length,
00088        (int) ((info->lock_type != F_UNLCK) ||
00089         info->s->delay_key_write))));
00090 } /* mi_write_keypage */
00091 
00092 
00093   /* Remove page from disk */
00094 
00095 int _mi_dispose(register MI_INFO *info, MI_KEYDEF *keyinfo, internal::my_off_t pos,
00096                 int level)
00097 {
00098   internal::my_off_t old_link;
00099   unsigned char buff[8];
00100 
00101   old_link= info->s->state.key_del[keyinfo->block_size_index];
00102   info->s->state.key_del[keyinfo->block_size_index]= pos;
00103   mi_sizestore(buff,old_link);
00104   info->s->state.changed|= STATE_NOT_SORTED_PAGES;
00105   return(key_cache_write(info->s->getKeyCache(),
00106                               info->s->kfile, pos , level, buff,
00107             sizeof(buff),
00108             (uint) keyinfo->block_length,
00109             (int) (info->lock_type != F_UNLCK)));
00110 } /* _mi_dispose */
00111 
00112 
00113   /* Make new page on disk */
00114 
00115 internal::my_off_t _mi_new(register MI_INFO *info, MI_KEYDEF *keyinfo, int level)
00116 {
00117   internal::my_off_t pos;
00118   unsigned char buff[8];
00119 
00120   if ((pos= info->s->state.key_del[keyinfo->block_size_index]) ==
00121       HA_OFFSET_ERROR)
00122   {
00123     if (info->state->key_file_length >=
00124   info->s->base.max_key_file_length - keyinfo->block_length)
00125     {
00126       errno=HA_ERR_INDEX_FILE_FULL;
00127       return(HA_OFFSET_ERROR);
00128     }
00129     pos=info->state->key_file_length;
00130     info->state->key_file_length+= keyinfo->block_length;
00131   }
00132   else
00133   {
00134     if (!key_cache_read(info->s->getKeyCache(),
00135                         info->s->kfile, pos, level,
00136       buff,
00137       (uint) sizeof(buff),
00138       (uint) keyinfo->block_length,0))
00139       pos= HA_OFFSET_ERROR;
00140     else
00141       info->s->state.key_del[keyinfo->block_size_index]= mi_sizekorr(buff);
00142   }
00143   info->s->state.changed|= STATE_NOT_SORTED_PAGES;
00144   return(pos);
00145 } /* _mi_new */