Drizzled Public API Documentation

hp_update.cc
00001 /* Copyright (C) 2000-2002, 2004-2005 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 /* Update current record in heap-database */
00017 
00018 #include "heap_priv.h"
00019 
00020 using namespace drizzled;
00021 
00022 int heap_update(HP_INFO *info, const unsigned char *old_record, const unsigned char *new_record)
00023 {
00024   HP_KEYDEF *keydef, *end, *p_lastinx;
00025   unsigned char *pos;
00026   bool auto_key_changed= 0;
00027   HP_SHARE *share= info->getShare();
00028 
00029   test_active(info);
00030   pos=info->current_ptr;
00031 
00032   if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,old_record))
00033     return(errno);        /* Record changed */
00034 
00035   if (--(share->records) < share->blength >> 1) share->blength>>= 1;
00036   share->changed=1;
00037 
00038   p_lastinx= share->keydef + info->lastinx;
00039   for (keydef= share->keydef, end= keydef + share->keys; keydef < end; keydef++)
00040   {
00041     if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
00042     {
00043       if (hp_delete_key(info, keydef, old_record, pos, keydef == p_lastinx) ||
00044           hp_write_key(info, keydef, new_record, pos))
00045       {
00046         goto err;
00047       }
00048       if (share->auto_key == (uint) (keydef - share->keydef + 1))
00049         auto_key_changed= 1;
00050     }
00051   }
00052   hp_copy_record_data_to_chunkset(share, new_record, pos);
00053   if (++(share->records) == share->blength) share->blength+= share->blength;
00054 
00055   if (auto_key_changed)
00056     heap_update_auto_increment(info, new_record);
00057   return(0);
00058 
00059  err:
00060   if (errno == HA_ERR_FOUND_DUPP_KEY)
00061   {
00062     info->errkey = (int) (keydef - share->keydef);
00063     while (keydef >= share->keydef)
00064     {
00065       if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
00066       {
00067   if (hp_delete_key(info, keydef, new_record, pos, 0) ||
00068       hp_write_key(info, keydef, old_record, pos))
00069     break;
00070       }
00071       keydef--;
00072     }
00073   }
00074 
00075   if (++(share->records) == share->blength)
00076     share->blength+= share->blength;
00077 
00078   return(errno);
00079 } /* heap_update */