home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / isam / _page.c < prev    next >
C/C++ Source or Header  |  2000-08-31  |  4KB  |  138 lines

  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. /* L{ser och skriver nyckelblock */
  18.  
  19. #include "isamdef.h"
  20. #ifdef    __WIN__
  21. #include <errno.h>
  22. #endif
  23.  
  24.     /* Fetch a key-page in memory */
  25.  
  26. uchar *_nisam_fetch_keypage(register N_INFO *info, N_KEYDEF *keyinfo,
  27.                 my_off_t page, uchar *buff, int return_buffer)
  28. {
  29.   uchar *tmp;
  30.   tmp=(uchar*) key_cache_read(info->s->kfile,page,(byte*) buff,
  31.                  (uint) keyinfo->base.block_length,
  32.                  (uint) keyinfo->base.block_length,
  33.                  return_buffer);
  34.   if (tmp == info->buff)
  35.   {
  36.     info->update|=HA_STATE_BUFF_SAVED;
  37.     info->int_pos=(ulong) page;
  38.     info->buff_used=1;
  39.   }
  40.   else
  41.   {
  42.     info->update&= ~HA_STATE_BUFF_SAVED;
  43.     if (tmp)
  44.       info->int_pos=(ulong) page;
  45.     else
  46.     {
  47.       info->int_pos=NI_POS_ERROR;
  48.       DBUG_PRINT("error",("Got errno: %d from key_cache_read",my_errno));
  49.       my_errno=HA_ERR_CRASHED;
  50.     }
  51.   }
  52.   return tmp;
  53. } /* _nisam_fetch_keypage */
  54.  
  55.  
  56.     /* Write a key-page on disk */
  57.  
  58. int _nisam_write_keypage(register N_INFO *info, register N_KEYDEF *keyinfo,
  59.               my_off_t page, uchar *buff)
  60. {
  61.   reg3 uint length;
  62. #ifndef QQ                    /* Safety check */
  63.   if (page < info->s->base.keystart ||
  64.       page+keyinfo->base.block_length > info->s->state.key_file_length ||
  65.       page & (nisam_block_size-1))
  66.   {
  67.     DBUG_PRINT("error",("Trying to write outside key region: %lu",
  68.             (long) page));
  69.     my_errno=EINVAL;
  70.     return(-1);
  71.   }
  72.   DBUG_PRINT("page",("write page at: %lu",(long) page,buff));
  73.   DBUG_DUMP("buff",(byte*) buff,getint(buff));
  74. #endif
  75.  
  76.   if ((length=keyinfo->base.block_length) > IO_SIZE*2 &&
  77.        info->s->state.key_file_length != page+length)
  78.     length= ((getint(buff)+IO_SIZE-1) & (uint) ~(IO_SIZE-1));
  79. #ifdef HAVE_purify
  80.   {
  81.     length=getint(buff);
  82.     bzero((byte*) buff+length,keyinfo->base.block_length-length);
  83.     length=keyinfo->base.block_length;
  84.   }
  85. #endif
  86.   return (key_cache_write(info->s->kfile,page,(byte*) buff,length,
  87.              (uint) keyinfo->base.block_length,
  88.              (int) (info->lock_type != F_UNLCK)));
  89. } /* nisam_write_keypage */
  90.  
  91.  
  92.     /* Remove page from disk */
  93.  
  94. int _nisam_dispose(register N_INFO *info, N_KEYDEF *keyinfo, my_off_t pos)
  95. {
  96.   uint keynr= (uint) (keyinfo - info->s->keyinfo);
  97.   ulong old_link;                /* ulong is ok here */
  98.   DBUG_ENTER("_nisam_dispose");
  99.  
  100.   old_link=info->s->state.key_del[keynr];
  101.   info->s->state.key_del[keynr]=(ulong) pos;
  102.   DBUG_RETURN(key_cache_write(info->s->kfile,pos,(byte*) &old_link,
  103.                   sizeof(long),
  104.                   (uint) keyinfo->base.block_length,
  105.                   (int) (info->lock_type != F_UNLCK)));
  106. } /* _nisam_dispose */
  107.  
  108.  
  109.     /* Make new page on disk */
  110.  
  111. ulong _nisam_new(register N_INFO *info, N_KEYDEF *keyinfo)
  112. {
  113.   uint keynr= (uint) (keyinfo - info->s->keyinfo);
  114.   ulong pos;
  115.   DBUG_ENTER("_nisam_new");
  116.  
  117.   if ((pos=info->s->state.key_del[keynr]) == NI_POS_ERROR)
  118.   {
  119.     if (info->s->state.key_file_length >= info->s->base.max_key_file_length)
  120.     {
  121.       my_errno=HA_ERR_INDEX_FILE_FULL;
  122.       DBUG_RETURN(NI_POS_ERROR);
  123.     }
  124.     pos=info->s->state.key_file_length;
  125.     info->s->state.key_file_length+= keyinfo->base.block_length;
  126.   }
  127.   else
  128.   {
  129.     if (!key_cache_read(info->s->kfile,pos,
  130.             (byte*) &info->s->state.key_del[keynr],
  131.             (uint) sizeof(long),
  132.             (uint) keyinfo->base.block_length,0))
  133.       pos= NI_POS_ERROR;
  134.   }
  135.   DBUG_PRINT("exit",("Pos: %d",pos));
  136.   DBUG_RETURN(pos);
  137. } /* _nisam_new */
  138.