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