~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/hp_update.c

  • Committer: Stewart Smith
  • Date: 2008-07-13 06:56:15 UTC
  • mto: (210.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: stewart@flamingspork.com-20080713065615-vzok75kgnnviokl9
Move MD5() into a UDF

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* Update current record in heap-database */
17
17
 
18
 
#include "heap_priv.h"
19
 
 
20
 
using namespace drizzled;
21
 
 
22
 
int heap_update(HP_INFO *info, const unsigned char *old_record, const unsigned char *new_record)
 
18
#include "heapdef.h"
 
19
 
 
20
int heap_update(HP_INFO *info, const uchar *old, const uchar *heap_new)
23
21
{
24
22
  HP_KEYDEF *keydef, *end, *p_lastinx;
25
 
  unsigned char *pos;
26
 
  bool auto_key_changed= 0;
 
23
  uchar *pos;
 
24
  my_bool auto_key_changed= 0;
27
25
  HP_SHARE *share= info->s;
28
 
  uint32_t old_length, new_length;
29
 
  uint32_t old_chunk_count, new_chunk_count;
 
26
  DBUG_ENTER("heap_update");
30
27
 
31
28
  test_active(info);
32
29
  pos=info->current_ptr;
33
30
 
34
 
  if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,old_record))
35
 
    return(errno);                              /* Record changed */
36
 
 
37
 
  old_length = hp_get_encoded_data_length(share, old_record, &old_chunk_count);
38
 
  new_length = hp_get_encoded_data_length(share, new_record, &new_chunk_count);
39
 
 
40
 
  if (new_chunk_count > old_chunk_count) {
41
 
    /* extend the old chunkset size as necessary, but do not shrink yet */
42
 
    if (hp_reallocate_chunkset(&share->recordspace, new_chunk_count, pos)) {
43
 
      return(errno);                          /* Out of memory or table space */
44
 
    }
45
 
  }
46
 
 
 
31
  if (info->opt_flag & READ_CHECK_USED && hp_rectest(info,old))
 
32
    DBUG_RETURN(my_errno);                              /* Record changed */
47
33
  if (--(share->records) < share->blength >> 1) share->blength>>= 1;
48
34
  share->changed=1;
49
35
 
50
36
  p_lastinx= share->keydef + info->lastinx;
51
37
  for (keydef= share->keydef, end= keydef + share->keys; keydef < end; keydef++)
52
38
  {
53
 
    if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
 
39
    if (hp_rec_key_cmp(keydef, old, heap_new, 0))
54
40
    {
55
 
      if ((*keydef->delete_key)(info, keydef, old_record, pos, keydef == p_lastinx) ||
56
 
          (*keydef->write_key)(info, keydef, new_record, pos))
 
41
      if ((*keydef->delete_key)(info, keydef, old, pos, keydef == p_lastinx) ||
 
42
          (*keydef->write_key)(info, keydef, heap_new, pos))
57
43
        goto err;
58
44
      if (share->auto_key == (uint) (keydef - share->keydef + 1))
59
45
        auto_key_changed= 1;
60
46
    }
61
47
  }
62
 
  hp_copy_record_data_to_chunkset(share, new_record, pos);
 
48
 
 
49
  memcpy(pos,heap_new,(size_t) share->reclength);
63
50
  if (++(share->records) == share->blength) share->blength+= share->blength;
64
51
 
65
 
  if (new_chunk_count < old_chunk_count) {
66
 
    /* Shrink the chunkset to its new size */
67
 
    hp_reallocate_chunkset(&share->recordspace, new_chunk_count, pos);
68
 
  }
69
 
 
 
52
#if !defined(DBUG_OFF) && defined(EXTRA_HEAP_DEBUG)
 
53
  DBUG_EXECUTE("check_heap",heap_check_heap(info, 0););
 
54
#endif
70
55
  if (auto_key_changed)
71
 
    heap_update_auto_increment(info, new_record);
72
 
  return(0);
 
56
    heap_update_auto_increment(info, heap_new);
 
57
  DBUG_RETURN(0);
73
58
 
74
59
 err:
75
 
  if (errno == HA_ERR_FOUND_DUPP_KEY)
 
60
  if (my_errno == HA_ERR_FOUND_DUPP_KEY)
76
61
  {
77
62
    info->errkey = (int) (keydef - share->keydef);
78
63
    if (keydef->algorithm == HA_KEY_ALG_BTREE)
79
64
    {
80
65
      /* we don't need to delete non-inserted key from rb-tree */
81
 
      if ((*keydef->write_key)(info, keydef, old_record, pos))
 
66
      if ((*keydef->write_key)(info, keydef, old, pos))
82
67
      {
83
68
        if (++(share->records) == share->blength)
84
69
          share->blength+= share->blength;
85
 
        return(errno);
 
70
        DBUG_RETURN(my_errno);
86
71
      }
87
72
      keydef--;
88
73
    }
89
74
    while (keydef >= share->keydef)
90
75
    {
91
 
      if (hp_rec_key_cmp(keydef, old_record, new_record, 0))
 
76
      if (hp_rec_key_cmp(keydef, old, heap_new, 0))
92
77
      {
93
 
        if ((*keydef->delete_key)(info, keydef, new_record, pos, 0) ||
94
 
            (*keydef->write_key)(info, keydef, old_record, pos))
 
78
        if ((*keydef->delete_key)(info, keydef, heap_new, pos, 0) ||
 
79
            (*keydef->write_key)(info, keydef, old, pos))
95
80
          break;
96
81
      }
97
82
      keydef--;
98
83
    }
99
84
  }
100
 
 
101
85
  if (++(share->records) == share->blength)
102
86
    share->blength+= share->blength;
103
 
 
104
 
  if (new_chunk_count > old_chunk_count) {
105
 
    /* Shrink the chunkset to its original size */
106
 
    hp_reallocate_chunkset(&share->recordspace, old_chunk_count, pos);
107
 
  }
108
 
 
109
 
  return(errno);
 
87
  DBUG_RETURN(my_errno);
110
88
} /* heap_update */