~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/internal/mf_iocache.cc

  • Committer: Brian Aker
  • Date: 2010-11-06 15:43:10 UTC
  • mfrom: (1908.1.1 merge)
  • Revision ID: brian@tangent.org-20101106154310-g1jpjzwbc53pfc4f
Filesort encapsulation, plus modification to copy contructor

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
namespace internal
71
71
{
72
72
 
73
 
static int _my_b_read(register st_io_cache *info, unsigned char *Buffer, size_t Count);
74
 
static int _my_b_write(register st_io_cache *info, const unsigned char *Buffer, size_t Count);
 
73
static int _my_b_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count);
 
74
static int _my_b_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count);
75
75
 
76
76
/**
77
77
 * @brief
78
 
 *   Lock appends for the st_io_cache if required (need_append_buffer_lock)   
 
78
 *   Lock appends for the IO_CACHE if required (need_append_buffer_lock)   
79
79
 */
80
80
inline
81
 
static void lock_append_buffer(register st_io_cache *, int )
 
81
static void lock_append_buffer(register IO_CACHE *, int )
82
82
{
83
83
}
84
84
 
85
85
/**
86
86
 * @brief
87
 
 *   Unlock appends for the st_io_cache if required (need_append_buffer_lock)   
 
87
 *   Unlock appends for the IO_CACHE if required (need_append_buffer_lock)   
88
88
 */
89
89
inline
90
 
static void unlock_append_buffer(register st_io_cache *, int )
 
90
static void unlock_append_buffer(register IO_CACHE *, int )
91
91
{
92
92
}
93
93
 
114
114
 
115
115
/**
116
116
 * @brief 
117
 
 *   Setup internal pointers inside st_io_cache
 
117
 *   Setup internal pointers inside IO_CACHE
118
118
 * 
119
119
 * @details
120
 
 *   This is called on automatically on init or reinit of st_io_cache
121
 
 *   It must be called externally if one moves or copies an st_io_cache object.
 
120
 *   This is called on automatically on init or reinit of IO_CACHE
 
121
 *   It must be called externally if one moves or copies an IO_CACHE object.
122
122
 * 
123
123
 * @param info Cache handler to setup
124
124
 */
125
 
void st_io_cache::setup_io_cache()
 
125
void setup_io_cache(IO_CACHE* info)
126
126
{
127
127
  /* Ensure that my_b_tell() and my_b_bytes_in_cache works */
128
 
  if (type == WRITE_CACHE)
 
128
  if (info->type == WRITE_CACHE)
129
129
  {
130
 
    current_pos= &write_pos;
131
 
    current_end= &write_end;
 
130
    info->current_pos= &info->write_pos;
 
131
    info->current_end= &info->write_end;
132
132
  }
133
133
  else
134
134
  {
135
 
    current_pos= &read_pos;
136
 
    current_end= &read_end;
 
135
    info->current_pos= &info->read_pos;
 
136
    info->current_end= &info->read_end;
137
137
  }
138
138
}
139
139
 
140
140
 
141
 
void st_io_cache::init_functions()
 
141
static void
 
142
init_functions(IO_CACHE* info)
142
143
{
 
144
  enum cache_type type= info->type;
143
145
  switch (type) {
144
146
  case READ_NET:
145
147
    /*
151
153
    */
152
154
    break;
153
155
  default:
154
 
    read_function = _my_b_read;
155
 
    write_function = _my_b_write;
 
156
    info->read_function = _my_b_read;
 
157
    info->write_function = _my_b_write;
156
158
  }
157
159
 
158
 
  setup_io_cache();
 
160
  setup_io_cache(info);
159
161
}
160
162
 
161
163
/**
162
164
 * @brief
163
 
 *   Initialize an st_io_cache object
 
165
 *   Initialize an IO_CACHE object
164
166
 *
 
167
 * @param info Cache handler to initialize
165
168
 * @param file File that should be associated with the handler
166
169
 *                 If == -1 then real_open_cached_file() will be called when it's time to open file.
167
170
 * @param cachesize Size of buffer to allocate for read/write
175
178
 * @retval 0 success
176
179
 * @retval # error
177
180
 */
178
 
int st_io_cache::init_io_cache(int file_arg, size_t cachesize,
179
 
                               enum cache_type type_arg, my_off_t seek_offset,
180
 
                               bool use_async_io, myf cache_myflags)
 
181
int init_io_cache(IO_CACHE *info, int file, size_t cachesize,
 
182
                  enum cache_type type, my_off_t seek_offset,
 
183
                  bool use_async_io, myf cache_myflags)
181
184
{
182
185
  size_t min_cache;
183
186
  off_t pos;
184
 
  my_off_t end_of_file_local= ~(my_off_t) 0;
 
187
  my_off_t end_of_file= ~(my_off_t) 0;
185
188
 
186
 
  file= file_arg;
187
 
  type= TYPE_NOT_SET;       /* Don't set it until mutex are created */
188
 
  pos_in_file= seek_offset;
189
 
  pre_close = pre_read = post_read = 0;
190
 
  arg = 0;
191
 
  alloced_buffer = 0;
192
 
  buffer=0;
193
 
  seek_not_done= 0;
 
189
  info->file= file;
 
190
  info->type= TYPE_NOT_SET;         /* Don't set it until mutex are created */
 
191
  info->pos_in_file= seek_offset;
 
192
  info->pre_close = info->pre_read = info->post_read = 0;
 
193
  info->arg = 0;
 
194
  info->alloced_buffer = 0;
 
195
  info->buffer=0;
 
196
  info->seek_not_done= 0;
194
197
 
195
198
  if (file >= 0)
196
199
  {
201
204
         This kind of object doesn't support seek() or tell(). Don't set a
202
205
         flag that will make us again try to seek() later and fail.
203
206
      */
204
 
      seek_not_done= 0;
 
207
      info->seek_not_done= 0;
205
208
      /*
206
209
        Additionally, if we're supposed to start somewhere other than the
207
210
        the beginning of whatever this file is, then somebody made a bad
210
213
      assert(seek_offset == 0);
211
214
    }
212
215
    else
213
 
      seek_not_done= test(seek_offset != (my_off_t)pos);
 
216
      info->seek_not_done= test(seek_offset != (my_off_t)pos);
214
217
  }
215
218
 
216
219
  if (!cachesize && !(cachesize= my_default_record_cache_size))
217
 
    return 1;                           /* No cache requested */
 
220
    return(1);                          /* No cache requested */
218
221
  min_cache=use_async_io ? IO_SIZE*4 : IO_SIZE*2;
219
 
  if (type_arg == READ_CACHE)
 
222
  if (type == READ_CACHE)
220
223
  {                                             /* Assume file isn't growing */
221
224
    if (!(cache_myflags & MY_DONT_CHECK_FILESIZE))
222
225
    {
223
226
      /* Calculate end of file to avoid allocating oversized buffers */
224
 
      end_of_file_local=lseek(file,0L,SEEK_END);
 
227
      end_of_file=lseek(file,0L,SEEK_END);
225
228
      /* Need to reset seek_not_done now that we just did a seek. */
226
 
      seek_not_done= end_of_file_local == seek_offset ? 0 : 1;
227
 
      if (end_of_file_local < seek_offset)
228
 
        end_of_file_local=seek_offset;
 
229
      info->seek_not_done= end_of_file == seek_offset ? 0 : 1;
 
230
      if (end_of_file < seek_offset)
 
231
        end_of_file=seek_offset;
229
232
      /* Trim cache size if the file is very small */
230
 
      if ((my_off_t) cachesize > end_of_file_local-seek_offset+IO_SIZE*2-1)
 
233
      if ((my_off_t) cachesize > end_of_file-seek_offset+IO_SIZE*2-1)
231
234
      {
232
 
        cachesize= (size_t) (end_of_file_local-seek_offset)+IO_SIZE*2-1;
 
235
        cachesize= (size_t) (end_of_file-seek_offset)+IO_SIZE*2-1;
233
236
        use_async_io=0;                         /* No need to use async */
234
237
      }
235
238
    }
236
239
  }
237
240
  cache_myflags &= ~MY_DONT_CHECK_FILESIZE;
238
 
  if (type_arg != READ_NET && type_arg != WRITE_NET)
 
241
  if (type != READ_NET && type != WRITE_NET)
239
242
  {
240
243
    /* Retry allocating memory in smaller blocks until we get one */
241
244
    cachesize= ((cachesize + min_cache-1) & ~(min_cache-1));
245
248
      if (cachesize < min_cache)
246
249
        cachesize = min_cache;
247
250
      buffer_block= cachesize;
248
 
      if ((type_arg == READ_CACHE) and (not global_read_buffer.add(buffer_block)))
 
251
      if ((type == READ_CACHE) and (not global_read_buffer.add(buffer_block)))
249
252
      {
250
253
        my_error(ER_OUT_OF_GLOBAL_READMEMORY, MYF(ME_ERROR+ME_WAITTANG));
251
254
        return 2;
252
255
      }
253
256
 
254
 
      if ((buffer=
 
257
      if ((info->buffer=
255
258
           (unsigned char*) malloc(buffer_block)) != 0)
256
259
      {
257
 
        write_buffer=buffer;
258
 
        alloced_buffer= true;
 
260
        info->write_buffer=info->buffer;
 
261
        info->alloced_buffer= true;
259
262
        break;                                  /* Enough memory found */
260
263
      }
261
264
      if (cachesize == min_cache)
262
265
      {
263
 
        if (type_arg == READ_CACHE)
 
266
        if (type == READ_CACHE)
264
267
          global_read_buffer.sub(buffer_block);
265
 
        return 2;                               /* Can't alloc cache */
 
268
        return(2);                              /* Can't alloc cache */
266
269
      }
267
270
      /* Try with less memory */
268
 
      if (type_arg == READ_CACHE)
 
271
      if (type == READ_CACHE)
269
272
        global_read_buffer.sub(buffer_block);
270
273
      cachesize= (cachesize*3/4 & ~(min_cache-1));
271
274
    }
272
275
  }
273
276
 
274
 
  read_length=buffer_length=cachesize;
275
 
  myflags=cache_myflags & ~(MY_NABP | MY_FNABP);
276
 
  request_pos= read_pos= write_pos = buffer;
 
277
  info->read_length=info->buffer_length=cachesize;
 
278
  info->myflags=cache_myflags & ~(MY_NABP | MY_FNABP);
 
279
  info->request_pos= info->read_pos= info->write_pos = info->buffer;
277
280
 
278
 
  if (type_arg == WRITE_CACHE)
279
 
    write_end=
280
 
      buffer+buffer_length- (seek_offset & (IO_SIZE-1));
 
281
  if (type == WRITE_CACHE)
 
282
    info->write_end=
 
283
      info->buffer+info->buffer_length- (seek_offset & (IO_SIZE-1));
281
284
  else
282
 
    read_end=buffer;            /* Nothing in cache */
 
285
    info->read_end=info->buffer;                /* Nothing in cache */
283
286
 
284
287
  /* End_of_file may be changed by user later */
285
 
  end_of_file= end_of_file_local;
286
 
  error= 0;
287
 
  type= type_arg;
288
 
  init_functions();
 
288
  info->end_of_file= end_of_file;
 
289
  info->error=0;
 
290
  info->type= type;
 
291
  init_functions(info);
289
292
#ifdef HAVE_AIOWAIT
290
293
  if (use_async_io && ! my_disable_async_io)
291
294
  {
292
 
    read_length/=2;
293
 
    read_function=_my_b_async_read;
 
295
    info->read_length/=2;
 
296
    info->read_function=_my_b_async_read;
294
297
  }
295
 
  inited= aio_result.pending= 0;
 
298
  info->inited=info->aio_result.pending=0;
296
299
#endif
297
 
  return 0;
 
300
  return(0);
298
301
}                                               /* init_io_cache */
299
302
 
300
303
        /* Wait until current request is ready */
319
322
        break;
320
323
    }
321
324
  }
 
325
  return;
322
326
}
323
327
#endif
324
328
 
332
336
 *   If we are doing a reinit of a cache where we have the start of the file
333
337
 *   in the cache, we are reusing this memory without flushing it to disk.
334
338
 */
335
 
bool st_io_cache::reinit_io_cache(enum cache_type type_arg,
336
 
                                  my_off_t seek_offset,
337
 
                                  bool use_async_io,
338
 
                                  bool clear_cache)
 
339
bool reinit_io_cache(IO_CACHE *info, enum cache_type type,
 
340
                        my_off_t seek_offset,
 
341
                        bool use_async_io,
 
342
                        bool clear_cache)
339
343
{
340
344
  /* One can't do reinit with the following types */
341
 
  assert(type_arg != READ_NET && type != READ_NET &&
342
 
              type_arg != WRITE_NET && type != WRITE_NET);
 
345
  assert(type != READ_NET && info->type != READ_NET &&
 
346
              type != WRITE_NET && info->type != WRITE_NET);
343
347
 
344
348
  /* If the whole file is in memory, avoid flushing to disk */
345
349
  if (! clear_cache &&
346
 
      seek_offset >= pos_in_file &&
347
 
      seek_offset <= my_b_tell(this))
 
350
      seek_offset >= info->pos_in_file &&
 
351
      seek_offset <= my_b_tell(info))
348
352
  {
349
353
    /* Reuse current buffer without flushing it to disk */
350
354
    unsigned char *pos;
351
 
    if (type == WRITE_CACHE && type_arg == READ_CACHE)
 
355
    if (info->type == WRITE_CACHE && type == READ_CACHE)
352
356
    {
353
 
      read_end=write_pos;
354
 
      end_of_file=my_b_tell(this);
 
357
      info->read_end=info->write_pos;
 
358
      info->end_of_file=my_b_tell(info);
355
359
      /*
356
360
        Trigger a new seek only if we have a valid
357
361
        file handle.
358
362
      */
359
 
      seek_not_done= (file != -1);
 
363
      info->seek_not_done= (info->file != -1);
360
364
    }
361
 
    else if (type_arg == WRITE_CACHE)
 
365
    else if (type == WRITE_CACHE)
362
366
    {
363
 
      if (type == READ_CACHE)
 
367
      if (info->type == READ_CACHE)
364
368
      {
365
 
        write_end=write_buffer+buffer_length;
366
 
        seek_not_done=1;
 
369
        info->write_end=info->write_buffer+info->buffer_length;
 
370
        info->seek_not_done=1;
367
371
      }
368
 
      end_of_file = ~(my_off_t) 0;
 
372
      info->end_of_file = ~(my_off_t) 0;
369
373
    }
370
 
    pos=request_pos+(seek_offset-pos_in_file);
371
 
    if (type_arg == WRITE_CACHE)
372
 
      write_pos=pos;
 
374
    pos=info->request_pos+(seek_offset-info->pos_in_file);
 
375
    if (type == WRITE_CACHE)
 
376
      info->write_pos=pos;
373
377
    else
374
 
      read_pos= pos;
 
378
      info->read_pos= pos;
375
379
#ifdef HAVE_AIOWAIT
376
 
    my_aiowait(&aio_result);            /* Wait for outstanding req */
 
380
    my_aiowait(&info->aio_result);              /* Wait for outstanding req */
377
381
#endif
378
382
  }
379
383
  else
382
386
      If we change from WRITE_CACHE to READ_CACHE, assume that everything
383
387
      after the current positions should be ignored
384
388
    */
385
 
    if (type == WRITE_CACHE && type_arg == READ_CACHE)
386
 
      end_of_file=my_b_tell(this);
 
389
    if (info->type == WRITE_CACHE && type == READ_CACHE)
 
390
      info->end_of_file=my_b_tell(info);
387
391
    /* flush cache if we want to reuse it */
388
 
    if (!clear_cache && my_b_flush_io_cache(this, 1))
389
 
      return 1;
390
 
    pos_in_file=seek_offset;
 
392
    if (!clear_cache && my_b_flush_io_cache(info,1))
 
393
      return(1);
 
394
    info->pos_in_file=seek_offset;
391
395
    /* Better to do always do a seek */
392
 
    seek_not_done=1;
393
 
    request_pos=read_pos=write_pos=buffer;
394
 
    if (type_arg == READ_CACHE)
 
396
    info->seek_not_done=1;
 
397
    info->request_pos=info->read_pos=info->write_pos=info->buffer;
 
398
    if (type == READ_CACHE)
395
399
    {
396
 
      read_end=buffer;          /* Nothing in cache */
 
400
      info->read_end=info->buffer;              /* Nothing in cache */
397
401
    }
398
402
    else
399
403
    {
400
 
      write_end=(buffer + buffer_length -
 
404
      info->write_end=(info->buffer + info->buffer_length -
401
405
                       (seek_offset & (IO_SIZE-1)));
402
 
      end_of_file= ~(my_off_t) 0;
 
406
      info->end_of_file= ~(my_off_t) 0;
403
407
    }
404
408
  }
405
 
  type= type_arg;
406
 
  error=0;
407
 
  init_functions();
 
409
  info->type=type;
 
410
  info->error=0;
 
411
  init_functions(info);
408
412
 
409
413
#ifdef HAVE_AIOWAIT
410
414
  if (use_async_io && ! my_disable_async_io &&
411
 
      ((uint32_t) buffer_length <
412
 
       (uint32_t) (end_of_file - seek_offset)))
 
415
      ((uint32_t) info->buffer_length <
 
416
       (uint32_t) (info->end_of_file - seek_offset)))
413
417
  {
414
 
    read_length=buffer_length/2;
415
 
    read_function=_my_b_async_read;
 
418
    info->read_length=info->buffer_length/2;
 
419
    info->read_function=_my_b_async_read;
416
420
  }
417
 
  inited= 0;
 
421
  info->inited=0;
418
422
#else
419
423
  (void)use_async_io;
420
424
#endif
421
 
  return 0;
 
425
  return(0);
422
426
} /* reinit_io_cache */
423
427
 
424
428
/**
435
439
 *   types than my_off_t unless you can be sure that their value fits.
436
440
 *   Same applies to differences of file offsets.
437
441
 *
438
 
 * @param info st_io_cache pointer @param Buffer Buffer to retrieve count bytes
 
442
 * @param info IO_CACHE pointer @param Buffer Buffer to retrieve count bytes
439
443
 * from file @param Count Number of bytes to read into Buffer
440
444
 * 
441
445
 * @retval 0 We succeeded in reading all data
442
446
 * @retval 1 Error: can't read requested characters
443
447
 */
444
 
static int _my_b_read(register st_io_cache *info, unsigned char *Buffer, size_t Count)
 
448
static int _my_b_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
445
449
{
446
 
  size_t length_local,diff_length,left_length, max_length;
447
 
  my_off_t pos_in_file_local;
 
450
  size_t length,diff_length,left_length, max_length;
 
451
  my_off_t pos_in_file;
448
452
 
449
453
  if ((left_length= (size_t) (info->read_end-info->read_pos)))
450
454
  {
455
459
  }
456
460
 
457
461
  /* pos_in_file always point on where info->buffer was read */
458
 
  pos_in_file_local=info->pos_in_file+ (size_t) (info->read_end - info->buffer);
 
462
  pos_in_file=info->pos_in_file+ (size_t) (info->read_end - info->buffer);
459
463
 
460
464
  /*
461
 
    Whenever a function which operates on st_io_cache flushes/writes
462
 
    some part of the st_io_cache to disk it will set the property
 
465
    Whenever a function which operates on IO_CACHE flushes/writes
 
466
    some part of the IO_CACHE to disk it will set the property
463
467
    "seek_not_done" to indicate this to other functions operating
464
 
    on the st_io_cache.
 
468
    on the IO_CACHE.
465
469
  */
466
470
  if (info->seek_not_done)
467
471
  {
468
 
    if ((lseek(info->file,pos_in_file_local,SEEK_SET) != MY_FILEPOS_ERROR))
 
472
    if ((lseek(info->file,pos_in_file,SEEK_SET) != MY_FILEPOS_ERROR))
469
473
    {
470
474
      /* No error, reset seek_not_done flag. */
471
475
      info->seek_not_done= 0;
483
487
    }
484
488
  }
485
489
 
486
 
  diff_length= (size_t) (pos_in_file_local & (IO_SIZE-1));
 
490
  diff_length= (size_t) (pos_in_file & (IO_SIZE-1));
487
491
  if (Count >= (size_t) (IO_SIZE+(IO_SIZE-diff_length)))
488
492
  {                                     /* Fill first intern buffer */
489
493
    size_t read_length;
490
 
    if (info->end_of_file <= pos_in_file_local)
 
494
    if (info->end_of_file <= pos_in_file)
491
495
    {                                   /* End of file */
492
496
      info->error= (int) left_length;
493
497
      return(1);
494
498
    }
495
 
    length_local=(Count & (size_t) ~(IO_SIZE-1))-diff_length;
496
 
    if ((read_length= my_read(info->file,Buffer, length_local, info->myflags)) != length_local)
 
499
    length=(Count & (size_t) ~(IO_SIZE-1))-diff_length;
 
500
    if ((read_length= my_read(info->file,Buffer, length, info->myflags))
 
501
        != length)
497
502
    {
498
503
      info->error= (read_length == (size_t) -1 ? -1 :
499
504
                    (int) (read_length+left_length));
500
505
      return(1);
501
506
    }
502
 
    Count-= length_local;
503
 
    Buffer+= length_local;
504
 
    pos_in_file_local+= length_local;
505
 
    left_length+= length_local;
 
507
    Count-=length;
 
508
    Buffer+=length;
 
509
    pos_in_file+=length;
 
510
    left_length+=length;
506
511
    diff_length=0;
507
512
  }
508
513
 
509
514
  max_length= info->read_length-diff_length;
510
515
  if (info->type != READ_FIFO &&
511
 
      max_length > (info->end_of_file - pos_in_file_local))
512
 
    max_length= (size_t) (info->end_of_file - pos_in_file_local);
 
516
      max_length > (info->end_of_file - pos_in_file))
 
517
    max_length= (size_t) (info->end_of_file - pos_in_file);
513
518
  if (!max_length)
514
519
  {
515
520
    if (Count)
517
522
      info->error= static_cast<int>(left_length);       /* We only got this many char */
518
523
      return(1);
519
524
    }
520
 
     length_local=0;                            /* Didn't read any chars */
 
525
    length=0;                           /* Didn't read any chars */
521
526
  }
522
 
  else if (( length_local= my_read(info->file,info->buffer, max_length,
 
527
  else if ((length= my_read(info->file,info->buffer, max_length,
523
528
                            info->myflags)) < Count ||
524
 
            length_local == (size_t) -1)
 
529
           length == (size_t) -1)
525
530
  {
526
 
    if ( length_local != (size_t) -1)
527
 
      memcpy(Buffer, info->buffer,  length_local);
528
 
    info->pos_in_file= pos_in_file_local;
529
 
    info->error=  length_local == (size_t) -1 ? -1 : (int) ( length_local+left_length);
 
531
    if (length != (size_t) -1)
 
532
      memcpy(Buffer, info->buffer, length);
 
533
    info->pos_in_file= pos_in_file;
 
534
    info->error= length == (size_t) -1 ? -1 : (int) (length+left_length);
530
535
    info->read_pos=info->read_end=info->buffer;
531
536
    return(1);
532
537
  }
533
538
  info->read_pos=info->buffer+Count;
534
 
  info->read_end=info->buffer+ length_local;
535
 
  info->pos_in_file=pos_in_file_local;
 
539
  info->read_end=info->buffer+length;
 
540
  info->pos_in_file=pos_in_file;
536
541
  memcpy(Buffer, info->buffer, Count);
537
542
  return(0);
538
543
}
542
547
 
543
548
/**
544
549
 * @brief
545
 
 *   Read from the st_io_cache into a buffer and feed asynchronously from disk when needed.
 
550
 *   Read from the IO_CACHE into a buffer and feed asynchronously from disk when needed.
546
551
 *
547
 
 * @param info st_io_cache pointer
 
552
 * @param info IO_CACHE pointer
548
553
 * @param Buffer Buffer to retrieve count bytes from file
549
554
 * @param Count Number of bytes to read into Buffer
550
555
 * 
551
556
 * @retval -1 An error has occurred; errno is set.
552
557
 * @retval 0 Success
553
 
 * @retval 1 An error has occurred; st_io_cache to error state.
 
558
 * @retval 1 An error has occurred; IO_CACHE to error state.
554
559
 */
555
 
int _my_b_async_read(register st_io_cache *info, unsigned char *Buffer, size_t Count)
 
560
int _my_b_async_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
556
561
{
557
 
  size_t length_local,read_length,diff_length,left_length,use_length,org_Count;
 
562
  size_t length,read_length,diff_length,left_length,use_length,org_Count;
558
563
  size_t max_length;
559
564
  my_off_t next_pos_in_file;
560
565
  unsigned char *read_buffer;
615
620
      }
616
621
    }
617
622
        /* Copy found bytes to buffer */
618
 
    length_local=min(Count,read_length);
619
 
    memcpy(Buffer,info->read_pos,(size_t) length_local);
620
 
    Buffer+=length_local;
621
 
    Count-=length_local;
622
 
    left_length+=length_local;
 
623
    length=min(Count,read_length);
 
624
    memcpy(Buffer,info->read_pos,(size_t) length);
 
625
    Buffer+=length;
 
626
    Count-=length;
 
627
    left_length+=length;
623
628
    info->read_end=info->rc_pos+read_length;
624
 
    info->read_pos+=length_local;
 
629
    info->read_pos+=length;
625
630
  }
626
631
  else
627
632
    next_pos_in_file=(info->pos_in_file+ (size_t)
719
724
 * @brief
720
725
 *   Read one byte when buffer is empty
721
726
 */
722
 
int _my_b_get(st_io_cache *info)
 
727
int _my_b_get(IO_CACHE *info)
723
728
{
724
729
  unsigned char buff;
725
730
  IO_CACHE_CALLBACK pre_read,post_read;
734
739
 
735
740
/**
736
741
 * @brief
737
 
 *   Write a byte buffer to st_io_cache and flush to disk if st_io_cache is full.
 
742
 *   Write a byte buffer to IO_CACHE and flush to disk if IO_CACHE is full.
738
743
 *
739
744
 * @retval -1 On error; errno contains error code.
740
745
 * @retval 0 On success
741
746
 * @retval 1 On error on write
742
747
 */
743
 
int _my_b_write(register st_io_cache *info, const unsigned char *Buffer, size_t Count)
 
748
int _my_b_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count)
744
749
{
745
 
  size_t rest_length,length_local;
 
750
  size_t rest_length,length;
746
751
 
747
752
  if (info->pos_in_file+info->buffer_length > info->end_of_file)
748
753
  {
760
765
    return 1;
761
766
  if (Count >= IO_SIZE)
762
767
  {                                     /* Fill first intern buffer */
763
 
    length_local=Count & (size_t) ~(IO_SIZE-1);
 
768
    length=Count & (size_t) ~(IO_SIZE-1);
764
769
    if (info->seek_not_done)
765
770
    {
766
771
      /*
767
 
        Whenever a function which operates on st_io_cache flushes/writes
768
 
        some part of the st_io_cache to disk it will set the property
 
772
        Whenever a function which operates on IO_CACHE flushes/writes
 
773
        some part of the IO_CACHE to disk it will set the property
769
774
        "seek_not_done" to indicate this to other functions operating
770
 
        on the st_io_cache.
 
775
        on the IO_CACHE.
771
776
      */
772
777
      if (lseek(info->file,info->pos_in_file,SEEK_SET))
773
778
      {
776
781
      }
777
782
      info->seek_not_done=0;
778
783
    }
779
 
    if (my_write(info->file, Buffer, length_local, info->myflags | MY_NABP))
 
784
    if (my_write(info->file, Buffer, length, info->myflags | MY_NABP))
780
785
      return info->error= -1;
781
786
 
782
 
    Count-=length_local;
783
 
    Buffer+=length_local;
784
 
    info->pos_in_file+=length_local;
 
787
    Count-=length;
 
788
    Buffer+=length;
 
789
    info->pos_in_file+=length;
785
790
  }
786
791
  memcpy(info->write_pos,Buffer,(size_t) Count);
787
792
  info->write_pos+=Count;
794
799
 *   As all write calls to the data goes through the cache,
795
800
 *   we will never get a seek over the end of the buffer.
796
801
 */
797
 
int my_block_write(register st_io_cache *info, const unsigned char *Buffer, size_t Count,
 
802
int my_block_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count,
798
803
                   my_off_t pos)
799
804
{
800
 
  size_t length_local;
 
805
  size_t length;
801
806
  int error=0;
802
807
 
803
808
  if (pos < info->pos_in_file)
806
811
    if (pos + Count <= info->pos_in_file)
807
812
      return (pwrite(info->file, Buffer, Count, pos) == 0);
808
813
    /* Write the part of the block that is before buffer */
809
 
    length_local= (uint32_t) (info->pos_in_file - pos);
810
 
    if (pwrite(info->file, Buffer, length_local, pos) == 0)
 
814
    length= (uint32_t) (info->pos_in_file - pos);
 
815
    if (pwrite(info->file, Buffer, length, pos) == 0)
811
816
      info->error= error= -1;
812
 
    Buffer+=length_local;
813
 
    pos+=  length_local;
814
 
    Count-= length_local;
 
817
    Buffer+=length;
 
818
    pos+=  length;
 
819
    Count-= length;
815
820
  }
816
821
 
817
822
  /* Check if we want to write inside the used part of the buffer.*/
818
 
  length_local= (size_t) (info->write_end - info->buffer);
819
 
  if (pos < info->pos_in_file + length_local)
 
823
  length= (size_t) (info->write_end - info->buffer);
 
824
  if (pos < info->pos_in_file + length)
820
825
  {
821
826
    size_t offset= (size_t) (pos - info->pos_in_file);
822
 
    length_local-=offset;
823
 
    if (length_local > Count)
824
 
      length_local=Count;
825
 
    memcpy(info->buffer+offset, Buffer, length_local);
826
 
    Buffer+=length_local;
827
 
    Count-= length_local;
828
 
    /* Fix length_local of buffer if the new data was larger */
829
 
    if (info->buffer+length_local > info->write_pos)
830
 
      info->write_pos=info->buffer+length_local;
 
827
    length-=offset;
 
828
    if (length > Count)
 
829
      length=Count;
 
830
    memcpy(info->buffer+offset, Buffer, length);
 
831
    Buffer+=length;
 
832
    Count-= length;
 
833
    /* Fix length of buffer if the new data was larger */
 
834
    if (info->buffer+length > info->write_pos)
 
835
      info->write_pos=info->buffer+length;
831
836
    if (!Count)
832
837
      return (error);
833
838
  }
841
846
 * @brief
842
847
 *   Flush write cache 
843
848
 */
844
 
int my_b_flush_io_cache(st_io_cache *info, int need_append_buffer_lock)
 
849
int my_b_flush_io_cache(IO_CACHE *info, int need_append_buffer_lock)
845
850
{
846
 
  size_t length_local;
 
851
  size_t length;
847
852
  bool append_cache= false;
848
 
  my_off_t pos_in_file_local;
 
853
  my_off_t pos_in_file;
849
854
 
850
855
  if (info->type == WRITE_CACHE || append_cache)
851
856
  {
852
857
    if (info->file == -1)
853
858
    {
854
 
      if (info->real_open_cached_file())
 
859
      if (real_open_cached_file(info))
855
860
        return((info->error= -1));
856
861
    }
857
862
    lock_append_buffer(info, need_append_buffer_lock);
858
863
 
859
 
    if ((length_local=(size_t) (info->write_pos - info->write_buffer)))
 
864
    if ((length=(size_t) (info->write_pos - info->write_buffer)))
860
865
    {
861
 
      pos_in_file_local=info->pos_in_file;
 
866
      pos_in_file=info->pos_in_file;
862
867
      /*
863
868
        If we have append cache, we always open the file with
864
869
        O_APPEND which moves the pos to EOF automatically on every write
865
870
      */
866
871
      if (!append_cache && info->seek_not_done)
867
872
      {                                 /* File touched, do seek */
868
 
        if (lseek(info->file,pos_in_file_local,SEEK_SET) == MY_FILEPOS_ERROR)
 
873
        if (lseek(info->file,pos_in_file,SEEK_SET) == MY_FILEPOS_ERROR)
869
874
        {
870
875
          unlock_append_buffer(info, need_append_buffer_lock);
871
876
          return((info->error= -1));
874
879
          info->seek_not_done=0;
875
880
      }
876
881
      if (!append_cache)
877
 
        info->pos_in_file+=length_local;
 
882
        info->pos_in_file+=length;
878
883
      info->write_end= (info->write_buffer+info->buffer_length-
879
 
                        ((pos_in_file_local+length_local) & (IO_SIZE-1)));
 
884
                        ((pos_in_file+length) & (IO_SIZE-1)));
880
885
 
881
 
      if (my_write(info->file,info->write_buffer,length_local,
 
886
      if (my_write(info->file,info->write_buffer,length,
882
887
                   info->myflags | MY_NABP))
883
888
        info->error= -1;
884
889
      else
885
890
        info->error= 0;
886
891
      if (!append_cache)
887
892
      {
888
 
        set_if_bigger(info->end_of_file,(pos_in_file_local+length_local));
 
893
        set_if_bigger(info->end_of_file,(pos_in_file+length));
889
894
      }
890
895
      else
891
896
      {
912
917
 
913
918
/**
914
919
 * @brief
915
 
 *   Free an st_io_cache object
 
920
 *   Free an IO_CACHE object
916
921
 * 
917
922
 * @detail
918
923
 *   It's currently safe to call this if one has called init_io_cache()
919
924
 *   on the 'info' object, even if init_io_cache() failed.
920
925
 *   This function is also safe to call twice with the same handle.
921
926
 * 
922
 
 * @param info st_io_cache Handle to free
 
927
 * @param info IO_CACHE Handle to free
923
928
 * 
924
929
 * @retval 0 ok
925
930
 * @retval # Error
926
931
 */
927
 
int st_io_cache::end_io_cache()
 
932
int end_io_cache(IO_CACHE *info)
928
933
{
929
 
  int _error=0;
930
 
 
931
 
  if (pre_close)
932
 
  {
933
 
    (*pre_close)(this);
934
 
    pre_close= 0;
935
 
  }
936
 
  if (alloced_buffer)
937
 
  {
938
 
    if (type == READ_CACHE)
939
 
      global_read_buffer.sub(buffer_length);
940
 
    alloced_buffer=0;
941
 
    if (file != -1)                     /* File doesn't exist */
942
 
      _error= my_b_flush_io_cache(this, 1);
943
 
    free((unsigned char*) buffer);
944
 
    buffer=read_pos=(unsigned char*) 0;
945
 
  }
946
 
 
947
 
  return _error;
 
934
  int error=0;
 
935
  IO_CACHE_CALLBACK pre_close;
 
936
 
 
937
  if ((pre_close=info->pre_close))
 
938
  {
 
939
    (*pre_close)(info);
 
940
    info->pre_close= 0;
 
941
  }
 
942
  if (info->alloced_buffer)
 
943
  {
 
944
    if (info->type == READ_CACHE)
 
945
      global_read_buffer.sub(info->buffer_length);
 
946
    info->alloced_buffer=0;
 
947
    if (info->file != -1)                       /* File doesn't exist */
 
948
      error= my_b_flush_io_cache(info,1);
 
949
    free((unsigned char*) info->buffer);
 
950
    info->buffer=info->read_pos=(unsigned char*) 0;
 
951
  }
 
952
 
 
953
  return(error);
948
954
} /* end_io_cache */
949
955
 
950
956
} /* namespace internal */