Drizzled Public API Documentation

mi_statrec.cc
1 /* Copyright (C) 2000-2002, 2004-2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16  /* Functions to handle fixed-length-records */
17 
18 #include "myisam_priv.h"
19 
20 using namespace drizzled;
21 
22 int _mi_write_static_record(MI_INFO *info, const unsigned char *record)
23 {
24  unsigned char temp[8]; /* max pointer length */
25  if (info->s->state.dellink != HA_OFFSET_ERROR &&
26  !info->append_insert_at_end)
27  {
28  internal::my_off_t filepos=info->s->state.dellink;
29  info->rec_cache.seek_not_done=1; /* We have done a seek */
30  if (info->s->file_read(info, &temp[0],info->s->base.rec_reflength,
31  info->s->state.dellink+1,
32  MYF(MY_NABP)))
33  goto err;
34  info->s->state.dellink= _mi_rec_pos(info->s,temp);
35  info->state->del--;
36  info->state->empty-=info->s->base.pack_reclength;
37  if (info->s->file_write(info, record, info->s->base.reclength,
38  filepos,
39  MYF(MY_NABP)))
40  goto err;
41  }
42  else
43  {
44  if (info->state->data_file_length > info->s->base.max_data_file_length-
45  info->s->base.pack_reclength)
46  {
47  errno=HA_ERR_RECORD_FILE_FULL;
48  return(2);
49  }
50  if (info->opt_flag & WRITE_CACHE_USED)
51  { /* Cash in use */
52  if (info->rec_cache.write(record, info->s->base.reclength))
53  goto err;
54  if (info->s->base.pack_reclength != info->s->base.reclength)
55  {
56  uint32_t length=info->s->base.pack_reclength - info->s->base.reclength;
57  memset(temp, 0, length);
58  if (info->rec_cache.write(temp,length))
59  goto err;
60  }
61  }
62  else
63  {
64  info->rec_cache.seek_not_done=1; /* We have done a seek */
65  if (info->s->file_write(info, record, info->s->base.reclength,
66  info->state->data_file_length,
67  info->s->write_flag))
68  goto err;
69  if (info->s->base.pack_reclength != info->s->base.reclength)
70  {
71  uint32_t length=info->s->base.pack_reclength - info->s->base.reclength;
72  memset(temp, 0, length);
73  if (info->s->file_write(info, temp,length,
74  info->state->data_file_length+
75  info->s->base.reclength,
76  info->s->write_flag))
77  goto err;
78  }
79  }
80  info->state->data_file_length+=info->s->base.pack_reclength;
81  info->s->state.split++;
82  }
83  return 0;
84  err:
85  return 1;
86 }
87 
88 int _mi_update_static_record(MI_INFO *info, internal::my_off_t pos, const unsigned char *record)
89 {
90  info->rec_cache.seek_not_done=1; /* We have done a seek */
91  return (info->s->file_write(info,
92  record, info->s->base.reclength,
93  pos,
94  MYF(MY_NABP)) != 0);
95 }
96 
97 
98 int _mi_delete_static_record(MI_INFO *info)
99 {
100  unsigned char temp[9]; /* 1+sizeof(uint32) */
101 
102  info->state->del++;
103  info->state->empty+=info->s->base.pack_reclength;
104  temp[0]= '\0'; /* Mark that record is deleted */
105  _mi_dpointer(info,temp+1,info->s->state.dellink);
106  info->s->state.dellink = info->lastpos;
107  info->rec_cache.seek_not_done=1;
108  return (info->s->file_write(info,(unsigned char*) temp, 1+info->s->rec_reflength,
109  info->lastpos, MYF(MY_NABP)) != 0);
110 }
111 
112 
113 int _mi_cmp_static_record(register MI_INFO *info, register const unsigned char *old)
114 {
115  if (info->opt_flag & WRITE_CACHE_USED)
116  {
117  if (info->rec_cache.flush())
118  {
119  return(-1);
120  }
121  info->rec_cache.seek_not_done=1; /* We have done a seek */
122  }
123 
124  if ((info->opt_flag & READ_CHECK_USED))
125  { /* If check isn't disabled */
126  info->rec_cache.seek_not_done=1; /* We have done a seek */
127  if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
128  info->lastpos,
129  MYF(MY_NABP)))
130  return(-1);
131  if (memcmp(info->rec_buff, old,
132  (uint) info->s->base.reclength))
133  {
134  errno=HA_ERR_RECORD_CHANGED; /* Record have changed */
135  return(1);
136  }
137  }
138  return(0);
139 }
140 
141 
142 int _mi_cmp_static_unique(MI_INFO *info, MI_UNIQUEDEF *def,
143  const unsigned char *record, internal::my_off_t pos)
144 {
145  info->rec_cache.seek_not_done=1; /* We have done a seek */
146  if (info->s->file_read(info, info->rec_buff, info->s->base.reclength,
147  pos, MYF(MY_NABP)))
148  return(-1);
149  return(mi_unique_comp(def, record, info->rec_buff,
150  def->null_are_equal));
151 }
152 
153 
154  /* Read a fixed-length-record */
155  /* Returns 0 if Ok. */
156  /* 1 if record is deleted */
157  /* MY_FILE_ERROR on read-error or locking-error */
158 
159 int _mi_read_static_record(register MI_INFO *info, register internal::my_off_t pos,
160  register unsigned char *record)
161 {
162  if (pos != HA_OFFSET_ERROR)
163  {
164  if (info->opt_flag & WRITE_CACHE_USED &&
165  info->rec_cache.pos_in_file <= pos &&
166  info->rec_cache.flush())
167  return(-1);
168  info->rec_cache.seek_not_done=1; /* We have done a seek */
169 
170  int error= info->s->file_read(info, record, info->s->base.reclength, pos,MYF(MY_NABP)) != 0;
171  fast_mi_writeinfo(info);
172  if (! error)
173  {
174  if (!*record)
175  {
176  errno=HA_ERR_RECORD_DELETED;
177  return(1); /* Record is deleted */
178  }
179  info->update|= HA_STATE_AKTIV; /* Record is read */
180  return(0);
181  }
182  return(-1); /* Error on read */
183  }
184  fast_mi_writeinfo(info); /* No such record */
185  return(-1);
186 }
187 
188 
189 
190 int _mi_read_rnd_static_record(MI_INFO *info, unsigned char *buf,
191  register internal::my_off_t filepos,
192  bool skip_deleted_blocks)
193 {
194  int locked,error,cache_read;
195  MYISAM_SHARE *share=info->s;
196 
197  cache_read=0;
198  if (info->opt_flag & WRITE_CACHE_USED &&
199  (info->rec_cache.pos_in_file <= filepos || skip_deleted_blocks) &&
200  info->rec_cache.flush())
201  return(errno);
202  if (info->opt_flag & READ_CACHE_USED)
203  { /* Cache in use */
204  if (filepos == info->rec_cache.tell() &&
205  (skip_deleted_blocks || !filepos))
206  {
207  cache_read=1; /* Read record using cache */
208  }
209  else
210  info->rec_cache.seek_not_done=1; /* Filepos is changed */
211  }
212  locked=0;
213  if (info->lock_type == F_UNLCK)
214  {
215  if (filepos >= info->state->data_file_length)
216  { /* Test if new records */
217  if (_mi_readinfo(info,F_RDLCK,0))
218  return(errno);
219  locked=1;
220  }
221  else
222  { /* We don't nead new info */
223 #ifndef UNSAFE_LOCKING
224  locked=1;
225 #else
226  info->tmp_lock_type=F_RDLCK;
227 #endif
228  }
229  }
230  if (filepos >= info->state->data_file_length)
231  {
232  fast_mi_writeinfo(info);
233  return(errno=HA_ERR_END_OF_FILE);
234  }
235  info->lastpos= filepos;
236  info->nextpos= filepos+share->base.pack_reclength;
237 
238  if (! cache_read) /* No cacheing */
239  {
240  if ((error=_mi_read_static_record(info,filepos,buf)))
241  {
242  if (error > 0)
243  error=errno=HA_ERR_RECORD_DELETED;
244  else
245  error=errno;
246  }
247  return(error);
248  }
249 
250  /* Read record with cacheing */
251  error= info->rec_cache.read(buf, share->base.reclength);
252  if (info->s->base.pack_reclength != info->s->base.reclength && !error)
253  {
254  char tmp[8]; /* Skill fill bytes */
255  error= info->rec_cache.read(tmp, info->s->base.pack_reclength - info->s->base.reclength);
256  }
257  if (locked)
258  _mi_writeinfo(info,0); /* Unlock keyfile */
259  if (!error)
260  {
261  if (!buf[0])
262  { /* Record is removed */
263  return(errno=HA_ERR_RECORD_DELETED);
264  }
265  /* Found and may be updated */
266  info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
267  return(0);
268  }
269  /* errno should be set if rec_cache.error == -1 */
270  if (info->rec_cache.error != -1 || errno == 0)
271  errno=HA_ERR_WRONG_IN_RECORD;
272  return(errno); /* Something wrong (EOF?) */
273 }