~jlukas79/+junk/mysql-server

« back to all changes in this revision

Viewing changes to storage/maria/ma_statrec.c

manual merge 6.0-main --> 6.0-bka-review

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 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; 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
        /* Functions to handle fixed-length-records */
 
17
 
 
18
#include "maria_def.h"
 
19
 
 
20
 
 
21
my_bool _ma_write_static_record(MARIA_HA *info, const uchar *record)
 
22
{
 
23
  uchar temp[8];                                 /* max pointer length */
 
24
  if (info->s->state.dellink != HA_OFFSET_ERROR &&
 
25
      !info->append_insert_at_end)
 
26
  {
 
27
    my_off_t filepos=info->s->state.dellink;
 
28
    info->rec_cache.seek_not_done=1;            /* We have done a seek */
 
29
    if (info->s->file_read(info, &temp[0],info->s->base.rec_reflength,
 
30
                info->s->state.dellink+1,
 
31
                 MYF(MY_NABP)))
 
32
      goto err;
 
33
    info->s->state.dellink= _ma_rec_pos(info->s, temp);
 
34
    info->state->del--;
 
35
    info->state->empty-=info->s->base.pack_reclength;
 
36
    if (info->s->file_write(info, record, info->s->base.reclength,
 
37
                            filepos, MYF(MY_NABP)))
 
38
      goto err;
 
39
  }
 
40
  else
 
41
  {
 
42
    if (info->state->data_file_length > info->s->base.max_data_file_length-
 
43
        info->s->base.pack_reclength)
 
44
    {
 
45
      my_errno=HA_ERR_RECORD_FILE_FULL;
 
46
      return(2);
 
47
    }
 
48
    if (info->opt_flag & WRITE_CACHE_USED)
 
49
    {                           /* Cash in use */
 
50
      if (my_b_write(&info->rec_cache, record,
 
51
                     info->s->base.reclength))
 
52
        goto err;
 
53
      if (info->s->base.pack_reclength != info->s->base.reclength)
 
54
      {
 
55
        uint length=info->s->base.pack_reclength - info->s->base.reclength;
 
56
        bzero(temp,length);
 
57
        if (my_b_write(&info->rec_cache, temp,length))
 
58
          goto err;
 
59
      }
 
60
    }
 
61
    else
 
62
    {
 
63
      info->rec_cache.seek_not_done=1;          /* We have done a seek */
 
64
      if (info->s->file_write(info, record, info->s->base.reclength,
 
65
                              info->state->data_file_length,
 
66
                              info->s->write_flag))
 
67
        goto err;
 
68
      if (info->s->base.pack_reclength != info->s->base.reclength)
 
69
      {
 
70
        uint length=info->s->base.pack_reclength - info->s->base.reclength;
 
71
        bzero(temp,length);
 
72
        if (info->s->file_write(info, temp,length,
 
73
                      info->state->data_file_length+
 
74
                      info->s->base.reclength,
 
75
                      info->s->write_flag))
 
76
    goto err;
 
77
      }
 
78
    }
 
79
    info->state->data_file_length+=info->s->base.pack_reclength;
 
80
    info->s->state.split++;
 
81
  }
 
82
  return 0;
 
83
 err:
 
84
  return 1;
 
85
}
 
86
 
 
87
my_bool _ma_update_static_record(MARIA_HA *info, MARIA_RECORD_POS pos,
 
88
                                 const uchar *oldrec __attribute__ ((unused)),
 
89
                                 const uchar *record)
 
90
{
 
91
  info->rec_cache.seek_not_done=1;              /* We have done a seek */
 
92
  return (info->s->file_write(info,
 
93
                              record, info->s->base.reclength,
 
94
                    pos,
 
95
                    MYF(MY_NABP)) != 0);
 
96
}
 
97
 
 
98
 
 
99
my_bool _ma_delete_static_record(MARIA_HA *info,
 
100
                                 const uchar *record __attribute__ ((unused)))
 
101
{
 
102
  uchar temp[9];                                 /* 1+sizeof(uint32) */
 
103
  info->state->del++;
 
104
  info->state->empty+=info->s->base.pack_reclength;
 
105
  temp[0]= '\0';                        /* Mark that record is deleted */
 
106
  _ma_dpointer(info->s, temp+1, info->s->state.dellink);
 
107
  info->s->state.dellink= info->cur_row.lastpos;
 
108
  info->rec_cache.seek_not_done=1;
 
109
  return (info->s->file_write(info, temp, 1+info->s->rec_reflength,
 
110
                    info->cur_row.lastpos, MYF(MY_NABP)) != 0);
 
111
}
 
112
 
 
113
 
 
114
my_bool _ma_cmp_static_record(register MARIA_HA *info,
 
115
                              register const uchar *old)
 
116
{
 
117
  DBUG_ENTER("_ma_cmp_static_record");
 
118
 
 
119
  /* We are going to do changes; dont let anybody disturb */
 
120
  dont_break();                         /* Dont allow SIGHUP or SIGINT */
 
121
 
 
122
  if (info->opt_flag & WRITE_CACHE_USED)
 
123
  {
 
124
    if (flush_io_cache(&info->rec_cache))
 
125
    {
 
126
      DBUG_RETURN(1);
 
127
    }
 
128
    info->rec_cache.seek_not_done=1;            /* We have done a seek */
 
129
  }
 
130
 
 
131
  if ((info->opt_flag & READ_CHECK_USED))
 
132
  {                                             /* If check isn't disabled  */
 
133
    info->rec_cache.seek_not_done=1;            /* We have done a seek */
 
134
    if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
 
135
                           info->cur_row.lastpos, MYF(MY_NABP)))
 
136
      DBUG_RETURN(1);
 
137
    if (memcmp(info->rec_buff, old, (uint) info->s->base.reclength))
 
138
    {
 
139
      DBUG_DUMP("read",old,info->s->base.reclength);
 
140
      DBUG_DUMP("disk",info->rec_buff,info->s->base.reclength);
 
141
      my_errno=HA_ERR_RECORD_CHANGED;           /* Record have changed */
 
142
      DBUG_RETURN(1);
 
143
    }
 
144
  }
 
145
  DBUG_RETURN(0);
 
146
}
 
147
 
 
148
 
 
149
my_bool _ma_cmp_static_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
 
150
                              const uchar *record, MARIA_RECORD_POS pos)
 
151
{
 
152
  DBUG_ENTER("_ma_cmp_static_unique");
 
153
 
 
154
  info->rec_cache.seek_not_done=1;              /* We have done a seek */
 
155
  if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
 
156
               pos, MYF(MY_NABP)))
 
157
    DBUG_RETURN(1);
 
158
  DBUG_RETURN(_ma_unique_comp(def, record, (uchar*) info->rec_buff,
 
159
                              def->null_are_equal));
 
160
}
 
161
 
 
162
 
 
163
/*
 
164
  Read a fixed-length-record
 
165
 
 
166
  RETURN
 
167
    0  Ok
 
168
    1  record delete
 
169
    -1 on read-error or locking-error
 
170
*/
 
171
 
 
172
int _ma_read_static_record(register MARIA_HA *info, register uchar *record,
 
173
                           MARIA_RECORD_POS pos)
 
174
{
 
175
  int error;
 
176
  DBUG_ENTER("_ma_read_static_record");
 
177
 
 
178
  if (pos != HA_OFFSET_ERROR)
 
179
  {
 
180
    if (info->opt_flag & WRITE_CACHE_USED &&
 
181
        info->rec_cache.pos_in_file <= pos &&
 
182
        flush_io_cache(&info->rec_cache))
 
183
      DBUG_RETURN(my_errno);
 
184
    info->rec_cache.seek_not_done=1;            /* We have done a seek */
 
185
 
 
186
    error= (int) info->s->file_read(info, record,info->s->base.reclength,
 
187
                                    pos, MYF(MY_NABP));
 
188
    if (! error)
 
189
    {
 
190
      fast_ma_writeinfo(info);
 
191
      if (!*record)
 
192
      {
 
193
        /* Record is deleted */
 
194
        DBUG_PRINT("warning", ("Record is deleted"));
 
195
        DBUG_RETURN((my_errno=HA_ERR_RECORD_DELETED));
 
196
      }
 
197
      info->update|= HA_STATE_AKTIV;            /* Record is read */
 
198
      DBUG_RETURN(0);
 
199
    }
 
200
  }
 
201
  fast_ma_writeinfo(info);                      /* No such record */
 
202
  DBUG_RETURN(my_errno);
 
203
}
 
204
 
 
205
 
 
206
/**
 
207
   @brief  Read record from given position or next record
 
208
 
 
209
   @note
 
210
     When scanning, this function will return HA_ERR_RECORD_DELETED
 
211
     for deleted rows even if skip_deleted_blocks is set.
 
212
     The reason for this is to allow the caller to calculate the record
 
213
     position without having to do call maria_position() for each record.
 
214
*/
 
215
 
 
216
int _ma_read_rnd_static_record(MARIA_HA *info, uchar *buf,
 
217
                               MARIA_RECORD_POS filepos,
 
218
                               my_bool skip_deleted_blocks)
 
219
{
 
220
  int locked,error,cache_read;
 
221
  uint cache_length;
 
222
  MARIA_SHARE *share= info->s;
 
223
  DBUG_ENTER("_ma_read_rnd_static_record");
 
224
 
 
225
  cache_read=0;
 
226
  cache_length=0;
 
227
  if (info->opt_flag & READ_CACHE_USED)
 
228
  {                                             /* Cache in use */
 
229
    if (filepos == my_b_tell(&info->rec_cache) &&
 
230
        (skip_deleted_blocks || !filepos))
 
231
    {
 
232
      cache_read=1;                             /* Read record using cache */
 
233
      cache_length= (uint) (info->rec_cache.read_end -
 
234
                            info->rec_cache.read_pos);
 
235
    }
 
236
    else
 
237
      info->rec_cache.seek_not_done=1;          /* Filepos is changed */
 
238
  }
 
239
  locked=0;
 
240
  if (info->lock_type == F_UNLCK)
 
241
  {
 
242
    if (filepos >= info->state->data_file_length)
 
243
    {                                           /* Test if new records */
 
244
      if (_ma_readinfo(info,F_RDLCK,0))
 
245
        DBUG_RETURN(my_errno);
 
246
      locked=1;
 
247
    }
 
248
    else
 
249
    {                                           /* We don't nead new info */
 
250
#ifndef UNSAFE_LOCKING
 
251
      if ((! cache_read || share->base.reclength > cache_length) &&
 
252
          share->tot_locks == 0)
 
253
      {                                         /* record not in cache */
 
254
        locked=1;
 
255
      }
 
256
#else
 
257
      info->tmp_lock_type=F_RDLCK;
 
258
#endif
 
259
    }
 
260
  }
 
261
  if (filepos >= info->state->data_file_length)
 
262
  {
 
263
    DBUG_PRINT("test",("filepos: %ld (%ld)  records: %ld  del: %ld",
 
264
                       (long) filepos/share->base.reclength, (long) filepos,
 
265
                       (long) info->state->records, (long) info->state->del));
 
266
    fast_ma_writeinfo(info);
 
267
    DBUG_RETURN(my_errno=HA_ERR_END_OF_FILE);
 
268
  }
 
269
  info->cur_row.lastpos= filepos;
 
270
  info->cur_row.nextpos= filepos+share->base.pack_reclength;
 
271
 
 
272
  if (! cache_read)                     /* No cacheing */
 
273
  {
 
274
    error= _ma_read_static_record(info, buf, filepos);
 
275
    DBUG_RETURN(error);
 
276
  }
 
277
 
 
278
        /* Read record with cacheing */
 
279
  error=my_b_read(&info->rec_cache,(uchar*) buf,share->base.reclength);
 
280
  if (info->s->base.pack_reclength != info->s->base.reclength && !error)
 
281
  {
 
282
    char tmp[8];                                /* Skill fill bytes */
 
283
    error=my_b_read(&info->rec_cache,(uchar*) tmp,
 
284
                    info->s->base.pack_reclength - info->s->base.reclength);
 
285
  }
 
286
  if (locked)
 
287
    (void)(_ma_writeinfo(info,0));              /* Unlock keyfile */
 
288
  if (!error)
 
289
  {
 
290
    if (!buf[0])
 
291
    {                                           /* Record is removed */
 
292
      DBUG_RETURN(my_errno=HA_ERR_RECORD_DELETED);
 
293
    }
 
294
                                                /* Found and may be updated */
 
295
    info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
 
296
    DBUG_RETURN(0);
 
297
  }
 
298
  /* my_errno should be set if rec_cache.error == -1 */
 
299
  if (info->rec_cache.error != -1 || my_errno == 0)
 
300
    my_errno=HA_ERR_WRONG_IN_RECORD;
 
301
  DBUG_RETURN(my_errno);                        /* Something wrong (EOF?) */
 
302
}