~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2002, 2004-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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* Update current record in heap-database */
17
18
#include "heapdef.h"
19
481 by Brian Aker
Remove all of uchar.
20
int heap_update(HP_INFO *info, const unsigned char *old_record, const unsigned char *new_record)
1 by brian
clean slate
21
{
22
  HP_KEYDEF *keydef, *end, *p_lastinx;
481 by Brian Aker
Remove all of uchar.
23
  unsigned char *pos;
280 by Brian Aker
Removed my_bool from heap engine.
24
  bool auto_key_changed= 0;
1 by brian
clean slate
25
  HP_SHARE *share= info->s;
482 by Brian Aker
Remove uint.
26
  uint32_t old_length, new_length;
27
  uint32_t old_chunk_count, new_chunk_count;
1 by brian
clean slate
28
29
  test_active(info);
30
  pos=info->current_ptr;
31
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
32
  if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,old_record))
51.3.1 by Jay Pipes
Removed all DBUG symbols from heap storage engine
33
    return(my_errno);				/* Record changed */
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
34
35
  old_length = hp_get_encoded_data_length(share, old_record, &old_chunk_count);
36
  new_length = hp_get_encoded_data_length(share, new_record, &new_chunk_count);
37
38
  if (new_chunk_count > old_chunk_count) {
39
    /* extend the old chunkset size as necessary, but do not shrink yet */
40
    if (hp_reallocate_chunkset(&share->recordspace, new_chunk_count, pos)) {
41
      return(my_errno);                          /* Out of memory or table space */
42
    }
43
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
44
1 by brian
clean slate
45
  if (--(share->records) < share->blength >> 1) share->blength>>= 1;
46
  share->changed=1;
47
48
  p_lastinx= share->keydef + info->lastinx;
49
  for (keydef= share->keydef, end= keydef + share->keys; keydef < end; keydef++)
50
  {
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
51
    if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
1 by brian
clean slate
52
    {
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
53
      if ((*keydef->delete_key)(info, keydef, old_record, pos, keydef == p_lastinx) ||
54
          (*keydef->write_key)(info, keydef, new_record, pos))
1 by brian
clean slate
55
        goto err;
56
      if (share->auto_key == (uint) (keydef - share->keydef + 1))
57
        auto_key_changed= 1;
58
    }
59
  }
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
60
  hp_copy_record_data_to_chunkset(share, new_record, pos);
1 by brian
clean slate
61
  if (++(share->records) == share->blength) share->blength+= share->blength;
62
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
63
  if (new_chunk_count < old_chunk_count) {
64
    /* Shrink the chunkset to its new size */
65
    hp_reallocate_chunkset(&share->recordspace, new_chunk_count, pos);
66
  }
67
1 by brian
clean slate
68
  if (auto_key_changed)
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
69
    heap_update_auto_increment(info, new_record);
51.3.1 by Jay Pipes
Removed all DBUG symbols from heap storage engine
70
  return(0);
1 by brian
clean slate
71
72
 err:
73
  if (my_errno == HA_ERR_FOUND_DUPP_KEY)
74
  {
75
    info->errkey = (int) (keydef - share->keydef);
76
    if (keydef->algorithm == HA_KEY_ALG_BTREE)
77
    {
78
      /* we don't need to delete non-inserted key from rb-tree */
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
79
      if ((*keydef->write_key)(info, keydef, old_record, pos))
1 by brian
clean slate
80
      {
81
        if (++(share->records) == share->blength)
82
	  share->blength+= share->blength;
51.3.1 by Jay Pipes
Removed all DBUG symbols from heap storage engine
83
        return(my_errno);
1 by brian
clean slate
84
      }
85
      keydef--;
86
    }
87
    while (keydef >= share->keydef)
88
    {
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
89
      if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
1 by brian
clean slate
90
      {
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
91
	if ((*keydef->delete_key)(info, keydef, new_record, pos, 0) ||
92
	    (*keydef->write_key)(info, keydef, old_record, pos))
1 by brian
clean slate
93
	  break;
94
      }
95
      keydef--;
96
    }
97
  }
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
98
1 by brian
clean slate
99
  if (++(share->records) == share->blength)
100
    share->blength+= share->blength;
244.1.1 by Harrison Fisk
Port Ebay/Google memory storage engine variable width columns.
101
102
  if (new_chunk_count > old_chunk_count) {
103
    /* Shrink the chunkset to its original size */
104
    hp_reallocate_chunkset(&share->recordspace, old_chunk_count, pos);
105
  }
106
51.3.1 by Jay Pipes
Removed all DBUG symbols from heap storage engine
107
  return(my_errno);
1 by brian
clean slate
108
} /* heap_update */