~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/internal/mf_iocache.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
{
71
71
 
72
72
static int _my_b_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count);
73
 
static int _my_b_read_r(register IO_CACHE *cache, unsigned char *Buffer, size_t Count);
74
 
static int _my_b_seq_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count);
75
73
static int _my_b_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count);
76
74
 
77
 
#define lock_append_buffer(info) \
78
 
 pthread_mutex_lock(&(info)->append_buffer_lock)
79
 
#define unlock_append_buffer(info) \
80
 
 pthread_mutex_unlock(&(info)->append_buffer_lock)
81
 
 
82
 
#define IO_ROUND_UP(X) (((X)+IO_SIZE-1) & ~(IO_SIZE-1))
83
 
#define IO_ROUND_DN(X) ( (X)            & ~(IO_SIZE-1))
84
 
 
85
 
/*
86
 
  Setup internal pointers inside IO_CACHE
87
 
 
88
 
  SYNOPSIS
89
 
    setup_io_cache()
90
 
    info                IO_CACHE handler
91
 
 
92
 
  NOTES
93
 
    This is called on automaticly on init or reinit of IO_CACHE
94
 
    It must be called externally if one moves or copies an IO_CACHE
95
 
    object.
96
 
*/
97
 
 
 
75
/**
 
76
 * @brief
 
77
 *   Lock appends for the IO_CACHE if required (need_append_buffer_lock)   
 
78
 */
 
79
inline
 
80
static void lock_append_buffer(register IO_CACHE *, int )
 
81
{
 
82
}
 
83
 
 
84
/**
 
85
 * @brief
 
86
 *   Unlock appends for the IO_CACHE if required (need_append_buffer_lock)   
 
87
 */
 
88
inline
 
89
static void unlock_append_buffer(register IO_CACHE *, int )
 
90
{
 
91
}
 
92
 
 
93
/**
 
94
 * @brief
 
95
 *   Round up to the nearest IO_SIZE boundary 
 
96
 */
 
97
inline
 
98
static size_t io_round_up(size_t x)
 
99
{
 
100
  return ((x+IO_SIZE-1) & ~(IO_SIZE-1));
 
101
}
 
102
 
 
103
/**
 
104
 * @brief
 
105
 *   Round down to the nearest IO_SIZE boundary   
 
106
 */
 
107
inline
 
108
static size_t io_round_dn(size_t x)
 
109
{
 
110
  return (x & ~(IO_SIZE-1));
 
111
}
 
112
 
 
113
 
 
114
/**
 
115
 * @brief 
 
116
 *   Setup internal pointers inside IO_CACHE
 
117
 * 
 
118
 * @details
 
119
 *   This is called on automatically on init or reinit of IO_CACHE
 
120
 *   It must be called externally if one moves or copies an IO_CACHE object.
 
121
 * 
 
122
 * @param info Cache handler to setup
 
123
 */
98
124
void setup_io_cache(IO_CACHE* info)
99
125
{
100
126
  /* Ensure that my_b_tell() and my_b_bytes_in_cache works */
125
151
      as myisamchk
126
152
    */
127
153
    break;
128
 
  case SEQ_READ_APPEND:
129
 
    info->read_function = _my_b_seq_read;
130
 
    info->write_function = 0;                   /* Force a core if used */
131
 
    break;
132
154
  default:
133
 
    info->read_function =
134
 
                          info->share ? _my_b_read_r :
135
 
                                        _my_b_read;
 
155
    info->read_function = _my_b_read;
136
156
    info->write_function = _my_b_write;
137
157
  }
138
158
 
139
159
  setup_io_cache(info);
140
160
}
141
161
 
142
 
 
143
 
/*
144
 
  Initialize an IO_CACHE object
145
 
 
146
 
  SYNOPSOS
147
 
    init_io_cache()
148
 
    info                cache handler to initialize
149
 
    file                File that should be associated to to the handler
150
 
                        If == -1 then real_open_cached_file()
151
 
                        will be called when it's time to open file.
152
 
    cachesize           Size of buffer to allocate for read/write
153
 
                        If == 0 then use my_default_record_cache_size
154
 
    type                Type of cache
155
 
    seek_offset         Where cache should start reading/writing
156
 
    use_async_io        Set to 1 of we should use async_io (if avaiable)
157
 
    cache_myflags       Bitmap of differnt flags
158
 
                        MY_WME | MY_FAE | MY_NABP | MY_FNABP |
159
 
                        MY_DONT_CHECK_FILESIZE
160
 
 
161
 
  RETURN
162
 
    0  ok
163
 
    #  error
164
 
*/
165
 
 
 
162
/**
 
163
 * @brief
 
164
 *   Initialize an IO_CACHE object
 
165
 *
 
166
 * @param info Cache handler to initialize
 
167
 * @param file File that should be associated with the handler
 
168
 *                 If == -1 then real_open_cached_file() will be called when it's time to open file.
 
169
 * @param cachesize Size of buffer to allocate for read/write
 
170
 *                      If == 0 then use my_default_record_cache_size
 
171
 * @param type Type of cache
 
172
 * @param seek_offset Where cache should start reading/writing
 
173
 * @param use_async_io Set to 1 if we should use async_io (if avaiable)
 
174
 * @param cache_myflags Bitmap of different flags
 
175
                            MY_WME | MY_FAE | MY_NABP | MY_FNABP | MY_DONT_CHECK_FILESIZE
 
176
 * 
 
177
 * @retval 0 success
 
178
 * @retval # error
 
179
 */
166
180
int init_io_cache(IO_CACHE *info, int file, size_t cachesize,
167
181
                  enum cache_type type, my_off_t seek_offset,
168
182
                  bool use_async_io, myf cache_myflags)
201
215
      info->seek_not_done= test(seek_offset != (my_off_t)pos);
202
216
  }
203
217
 
204
 
  info->share=0;
205
 
 
206
218
  if (!cachesize && !(cachesize= my_default_record_cache_size))
207
219
    return(1);                          /* No cache requested */
208
220
  min_cache=use_async_io ? IO_SIZE*4 : IO_SIZE*2;
209
 
  if (type == READ_CACHE || type == SEQ_READ_APPEND)
 
221
  if (type == READ_CACHE)
210
222
  {                                             /* Assume file isn't growing */
211
223
    if (!(cache_myflags & MY_DONT_CHECK_FILESIZE))
212
224
    {
235
247
      if (cachesize < min_cache)
236
248
        cachesize = min_cache;
237
249
      buffer_block= cachesize;
238
 
      if (type == SEQ_READ_APPEND)
239
 
        buffer_block *= 2;
240
250
      if ((info->buffer=
241
251
           (unsigned char*) malloc(buffer_block)) != 0)
242
252
      {
243
253
        info->write_buffer=info->buffer;
244
 
        if (type == SEQ_READ_APPEND)
245
 
          info->write_buffer = info->buffer + cachesize;
246
 
        info->alloced_buffer=1;
 
254
        info->alloced_buffer= true;
247
255
        break;                                  /* Enough memory found */
248
256
      }
249
257
      if (cachesize == min_cache)
256
264
  info->read_length=info->buffer_length=cachesize;
257
265
  info->myflags=cache_myflags & ~(MY_NABP | MY_FNABP);
258
266
  info->request_pos= info->read_pos= info->write_pos = info->buffer;
259
 
  if (type == SEQ_READ_APPEND)
260
 
  {
261
 
    info->append_read_pos = info->write_pos = info->write_buffer;
262
 
    info->write_end = info->write_buffer + info->buffer_length;
263
 
    pthread_mutex_init(&info->append_buffer_lock,MY_MUTEX_INIT_FAST);
264
 
  }
265
267
 
266
268
  if (type == WRITE_CACHE)
267
269
    info->write_end=
311
313
}
312
314
#endif
313
315
 
314
 
 
315
 
/*
316
 
  Use this to reset cache to re-start reading or to change the type
317
 
  between READ_CACHE <-> WRITE_CACHE
318
 
  If we are doing a reinit of a cache where we have the start of the file
319
 
  in the cache, we are reusing this memory without flushing it to disk.
320
 
*/
321
 
 
 
316
/**
 
317
 * @brief 
 
318
 *   Reset the cache
 
319
 * 
 
320
 * @detail
 
321
 *   Use this to reset cache to re-start reading or to change the type 
 
322
 *   between READ_CACHE <-> WRITE_CACHE
 
323
 *   If we are doing a reinit of a cache where we have the start of the file
 
324
 *   in the cache, we are reusing this memory without flushing it to disk.
 
325
 */
322
326
bool reinit_io_cache(IO_CACHE *info, enum cache_type type,
323
327
                        my_off_t seek_offset,
324
328
                        bool use_async_io,
326
330
{
327
331
  /* One can't do reinit with the following types */
328
332
  assert(type != READ_NET && info->type != READ_NET &&
329
 
              type != WRITE_NET && info->type != WRITE_NET &&
330
 
              type != SEQ_READ_APPEND && info->type != SEQ_READ_APPEND);
 
333
              type != WRITE_NET && info->type != WRITE_NET);
331
334
 
332
335
  /* If the whole file is in memory, avoid flushing to disk */
333
336
  if (! clear_cache &&
409
412
  return(0);
410
413
} /* reinit_io_cache */
411
414
 
412
 
 
413
 
 
414
 
/*
415
 
  Read buffered.
416
 
 
417
 
  SYNOPSIS
418
 
    _my_b_read()
419
 
      info                      IO_CACHE pointer
420
 
      Buffer                    Buffer to retrieve count bytes from file
421
 
      Count                     Number of bytes to read into Buffer
422
 
 
423
 
  NOTE
424
 
    This function is only called from the my_b_read() macro when there
425
 
    isn't enough characters in the buffer to satisfy the request.
426
 
 
427
 
  WARNING
428
 
 
429
 
    When changing this function, be careful with handling file offsets
430
 
    (end-of_file, pos_in_file). Do not cast them to possibly smaller
431
 
    types than my_off_t unless you can be sure that their value fits.
432
 
    Same applies to differences of file offsets.
433
 
 
434
 
    When changing this function, check _my_b_read_r(). It might need the
435
 
    same change.
436
 
 
437
 
  RETURN
438
 
    0      we succeeded in reading all data
439
 
    1      Error: can't read requested characters
440
 
*/
441
 
 
 
415
/**
 
416
 * @brief 
 
417
 *   Read buffered.
 
418
 * 
 
419
 * @detail
 
420
 *   This function is only called from the my_b_read() macro when there
 
421
 *   aren't enough characters in the buffer to satisfy the request.
 
422
 *
 
423
 * WARNING
 
424
 *   When changing this function, be careful with handling file offsets
 
425
 *   (end-of_file, pos_in_file). Do not cast them to possibly smaller
 
426
 *   types than my_off_t unless you can be sure that their value fits.
 
427
 *   Same applies to differences of file offsets.
 
428
 *
 
429
 * @param info IO_CACHE pointer @param Buffer Buffer to retrieve count bytes
 
430
 * from file @param Count Number of bytes to read into Buffer
 
431
 * 
 
432
 * @retval 0 We succeeded in reading all data
 
433
 * @retval 1 Error: can't read requested characters
 
434
 */
442
435
static int _my_b_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
443
436
{
444
437
  size_t length,diff_length,left_length, max_length;
537
530
}
538
531
 
539
532
 
540
 
/*
541
 
  Prepare IO_CACHE for shared use.
542
 
 
543
 
  SYNOPSIS
544
 
    init_io_cache_share()
545
 
      read_cache                A read cache. This will be copied for
546
 
                                every thread after setup.
547
 
      cshare                    The share.
548
 
      write_cache               If non-NULL a write cache that is to be
549
 
                                synchronized with the read caches.
550
 
      num_threads               Number of threads sharing the cache
551
 
                                including the write thread if any.
552
 
 
553
 
  DESCRIPTION
554
 
 
555
 
    The shared cache is used so: One IO_CACHE is initialized with
556
 
    init_io_cache(). This includes the allocation of a buffer. Then a
557
 
    share is allocated and init_io_cache_share() is called with the io
558
 
    cache and the share. Then the io cache is copied for each thread. So
559
 
    every thread has its own copy of IO_CACHE. But the allocated buffer
560
 
    is shared because cache->buffer is the same for all caches.
561
 
 
562
 
    One thread reads data from the file into the buffer. All threads
563
 
    read from the buffer, but every thread maintains its own set of
564
 
    pointers into the buffer. When all threads have used up the buffer
565
 
    contents, one of the threads reads the next block of data into the
566
 
    buffer. To accomplish this, each thread enters the cache lock before
567
 
    accessing the buffer. They wait in lock_io_cache() until all threads
568
 
    joined the lock. The last thread entering the lock is in charge of
569
 
    reading from file to buffer. It wakes all threads when done.
570
 
 
571
 
    Synchronizing a write cache to the read caches works so: Whenever
572
 
    the write buffer needs a flush, the write thread enters the lock and
573
 
    waits for all other threads to enter the lock too. They do this when
574
 
    they have used up the read buffer. When all threads are in the lock,
575
 
    the write thread copies the write buffer to the read buffer and
576
 
    wakes all threads.
577
 
 
578
 
    share->running_threads is the number of threads not being in the
579
 
    cache lock. When entering lock_io_cache() the number is decreased.
580
 
    When the thread that fills the buffer enters unlock_io_cache() the
581
 
    number is reset to the number of threads. The condition
582
 
    running_threads == 0 means that all threads are in the lock. Bumping
583
 
    up the number to the full count is non-intuitive. But increasing the
584
 
    number by one for each thread that leaves the lock could lead to a
585
 
    solo run of one thread. The last thread to join a lock reads from
586
 
    file to buffer, wakes the other threads, processes the data in the
587
 
    cache and enters the lock again. If no other thread left the lock
588
 
    meanwhile, it would think it's the last one again and read the next
589
 
    block...
590
 
 
591
 
    The share has copies of 'error', 'buffer', 'read_end', and
592
 
    'pos_in_file' from the thread that filled the buffer. We may not be
593
 
    able to access this information directly from its cache because the
594
 
    thread may be removed from the share before the variables could be
595
 
    copied by all other threads. Or, if a write buffer is synchronized,
596
 
    it would change its 'pos_in_file' after waking the other threads,
597
 
    possibly before they could copy its value.
598
 
 
599
 
    However, the 'buffer' variable in the share is for a synchronized
600
 
    write cache. It needs to know where to put the data. Otherwise it
601
 
    would need access to the read cache of one of the threads that is
602
 
    not yet removed from the share.
603
 
 
604
 
  RETURN
605
 
    void
606
 
*/
607
 
 
608
 
void init_io_cache_share(IO_CACHE *read_cache, IO_CACHE_SHARE *cshare,
609
 
                         IO_CACHE *write_cache, uint32_t num_threads)
610
 
{
611
 
  assert(num_threads > 1);
612
 
  assert(read_cache->type == READ_CACHE);
613
 
  assert(!write_cache || (write_cache->type == WRITE_CACHE));
614
 
 
615
 
  pthread_mutex_init(&cshare->mutex, MY_MUTEX_INIT_FAST);
616
 
  pthread_cond_init(&cshare->cond, 0);
617
 
  pthread_cond_init(&cshare->cond_writer, 0);
618
 
 
619
 
  cshare->running_threads= num_threads;
620
 
  cshare->total_threads=   num_threads;
621
 
  cshare->error=           0;    /* Initialize. */
622
 
  cshare->buffer=          read_cache->buffer;
623
 
  cshare->read_end=        NULL; /* See function comment of lock_io_cache(). */
624
 
  cshare->pos_in_file=     0;    /* See function comment of lock_io_cache(). */
625
 
  cshare->source_cache=    write_cache; /* Can be NULL. */
626
 
 
627
 
  read_cache->share=         cshare;
628
 
  read_cache->read_function= _my_b_read_r;
629
 
  read_cache->current_pos=   NULL;
630
 
  read_cache->current_end=   NULL;
631
 
 
632
 
  if (write_cache)
633
 
    write_cache->share= cshare;
634
 
 
635
 
  return;
636
 
}
637
 
 
638
 
 
639
 
/*
640
 
  Remove a thread from shared access to IO_CACHE.
641
 
 
642
 
  SYNOPSIS
643
 
    remove_io_thread()
644
 
      cache                     The IO_CACHE to be removed from the share.
645
 
 
646
 
  NOTE
647
 
 
648
 
    Every thread must do that on exit for not to deadlock other threads.
649
 
 
650
 
    The last thread destroys the pthread resources.
651
 
 
652
 
    A writer flushes its cache first.
653
 
 
654
 
  RETURN
655
 
    void
656
 
*/
657
 
 
658
 
void remove_io_thread(IO_CACHE *cache)
659
 
{
660
 
  IO_CACHE_SHARE *cshare= cache->share;
661
 
  uint32_t total;
662
 
 
663
 
  /* If the writer goes, it needs to flush the write cache. */
664
 
  if (cache == cshare->source_cache)
665
 
    flush_io_cache(cache);
666
 
 
667
 
  pthread_mutex_lock(&cshare->mutex);
668
 
 
669
 
  /* Remove from share. */
670
 
  total= --cshare->total_threads;
671
 
 
672
 
  /* Detach from share. */
673
 
  cache->share= NULL;
674
 
 
675
 
  /* If the writer goes, let the readers know. */
676
 
  if (cache == cshare->source_cache)
677
 
  {
678
 
    cshare->source_cache= NULL;
679
 
  }
680
 
 
681
 
  /* If all threads are waiting for me to join the lock, wake them. */
682
 
  if (!--cshare->running_threads)
683
 
  {
684
 
    pthread_cond_signal(&cshare->cond_writer);
685
 
    pthread_cond_broadcast(&cshare->cond);
686
 
  }
687
 
 
688
 
  pthread_mutex_unlock(&cshare->mutex);
689
 
 
690
 
  if (!total)
691
 
  {
692
 
    pthread_cond_destroy (&cshare->cond_writer);
693
 
    pthread_cond_destroy (&cshare->cond);
694
 
    pthread_mutex_destroy(&cshare->mutex);
695
 
  }
696
 
 
697
 
  return;
698
 
}
699
 
 
700
 
 
701
 
/*
702
 
  Lock IO cache and wait for all other threads to join.
703
 
 
704
 
  SYNOPSIS
705
 
    lock_io_cache()
706
 
      cache                     The cache of the thread entering the lock.
707
 
      pos                       File position of the block to read.
708
 
                                Unused for the write thread.
709
 
 
710
 
  DESCRIPTION
711
 
 
712
 
    Wait for all threads to finish with the current buffer. We want
713
 
    all threads to proceed in concert. The last thread to join
714
 
    lock_io_cache() will read the block from file and all threads start
715
 
    to use it. Then they will join again for reading the next block.
716
 
 
717
 
    The waiting threads detect a fresh buffer by comparing
718
 
    cshare->pos_in_file with the position they want to process next.
719
 
    Since the first block may start at position 0, we take
720
 
    cshare->read_end as an additional condition. This variable is
721
 
    initialized to NULL and will be set after a block of data is written
722
 
    to the buffer.
723
 
 
724
 
  RETURN
725
 
    1           OK, lock in place, go ahead and read.
726
 
    0           OK, unlocked, another thread did the read.
727
 
*/
728
 
 
729
 
static int lock_io_cache(IO_CACHE *cache, my_off_t pos)
730
 
{
731
 
  IO_CACHE_SHARE *cshare= cache->share;
732
 
 
733
 
  /* Enter the lock. */
734
 
  pthread_mutex_lock(&cshare->mutex);
735
 
  cshare->running_threads--;
736
 
 
737
 
  if (cshare->source_cache)
738
 
  {
739
 
    /* A write cache is synchronized to the read caches. */
740
 
 
741
 
    if (cache == cshare->source_cache)
742
 
    {
743
 
      /* The writer waits until all readers are here. */
744
 
      while (cshare->running_threads)
745
 
      {
746
 
        pthread_cond_wait(&cshare->cond_writer, &cshare->mutex);
747
 
      }
748
 
      /* Stay locked. Leave the lock later by unlock_io_cache(). */
749
 
      return(1);
750
 
    }
751
 
 
752
 
    /* The last thread wakes the writer. */
753
 
    if (!cshare->running_threads)
754
 
    {
755
 
      pthread_cond_signal(&cshare->cond_writer);
756
 
    }
757
 
 
758
 
    /*
759
 
      Readers wait until the data is copied from the writer. Another
760
 
      reason to stop waiting is the removal of the write thread. If this
761
 
      happens, we leave the lock with old data in the buffer.
762
 
    */
763
 
    while ((!cshare->read_end || (cshare->pos_in_file < pos)) &&
764
 
           cshare->source_cache)
765
 
    {
766
 
      pthread_cond_wait(&cshare->cond, &cshare->mutex);
767
 
    }
768
 
 
769
 
    /*
770
 
      If the writer was removed from the share while this thread was
771
 
      asleep, we need to simulate an EOF condition. The writer cannot
772
 
      reset the share variables as they might still be in use by readers
773
 
      of the last block. When we awake here then because the last
774
 
      joining thread signalled us. If the writer is not the last, it
775
 
      will not signal. So it is safe to clear the buffer here.
776
 
    */
777
 
    if (!cshare->read_end || (cshare->pos_in_file < pos))
778
 
    {
779
 
      cshare->read_end= cshare->buffer; /* Empty buffer. */
780
 
      cshare->error= 0; /* EOF is not an error. */
781
 
    }
782
 
  }
783
 
  else
784
 
  {
785
 
    /*
786
 
      There are read caches only. The last thread arriving in
787
 
      lock_io_cache() continues with a locked cache and reads the block.
788
 
    */
789
 
    if (!cshare->running_threads)
790
 
    {
791
 
      /* Stay locked. Leave the lock later by unlock_io_cache(). */
792
 
      return(1);
793
 
    }
794
 
 
795
 
    /*
796
 
      All other threads wait until the requested block is read by the
797
 
      last thread arriving. Another reason to stop waiting is the
798
 
      removal of a thread. If this leads to all threads being in the
799
 
      lock, we have to continue also. The first of the awaken threads
800
 
      will then do the read.
801
 
    */
802
 
    while ((!cshare->read_end || (cshare->pos_in_file < pos)) &&
803
 
           cshare->running_threads)
804
 
    {
805
 
      pthread_cond_wait(&cshare->cond, &cshare->mutex);
806
 
    }
807
 
 
808
 
    /* If the block is not yet read, continue with a locked cache and read. */
809
 
    if (!cshare->read_end || (cshare->pos_in_file < pos))
810
 
    {
811
 
      /* Stay locked. Leave the lock later by unlock_io_cache(). */
812
 
      return(1);
813
 
    }
814
 
 
815
 
    /* Another thread did read the block already. */
816
 
  }
817
 
 
818
 
  /*
819
 
    Leave the lock. Do not call unlock_io_cache() later. The thread that
820
 
    filled the buffer did this and marked all threads as running.
821
 
  */
822
 
  pthread_mutex_unlock(&cshare->mutex);
823
 
  return(0);
824
 
}
825
 
 
826
 
 
827
 
/*
828
 
  Unlock IO cache.
829
 
 
830
 
  SYNOPSIS
831
 
    unlock_io_cache()
832
 
      cache                     The cache of the thread leaving the lock.
833
 
 
834
 
  NOTE
835
 
    This is called by the thread that filled the buffer. It marks all
836
 
    threads as running and awakes them. This must not be done by any
837
 
    other thread.
838
 
 
839
 
    Do not signal cond_writer. Either there is no writer or the writer
840
 
    is the only one who can call this function.
841
 
 
842
 
    The reason for resetting running_threads to total_threads before
843
 
    waking all other threads is that it could be possible that this
844
 
    thread is so fast with processing the buffer that it enters the lock
845
 
    before even one other thread has left it. If every awoken thread
846
 
    would increase running_threads by one, this thread could think that
847
 
    he is again the last to join and would not wait for the other
848
 
    threads to process the data.
849
 
 
850
 
  RETURN
851
 
    void
852
 
*/
853
 
 
854
 
static void unlock_io_cache(IO_CACHE *cache)
855
 
{
856
 
  IO_CACHE_SHARE *cshare= cache->share;
857
 
 
858
 
  cshare->running_threads= cshare->total_threads;
859
 
  pthread_cond_broadcast(&cshare->cond);
860
 
  pthread_mutex_unlock(&cshare->mutex);
861
 
  return;
862
 
}
863
 
 
864
 
 
865
 
/*
866
 
  Read from IO_CACHE when it is shared between several threads.
867
 
 
868
 
  SYNOPSIS
869
 
    _my_b_read_r()
870
 
      cache                     IO_CACHE pointer
871
 
      Buffer                    Buffer to retrieve count bytes from file
872
 
      Count                     Number of bytes to read into Buffer
873
 
 
874
 
  NOTE
875
 
    This function is only called from the my_b_read() macro when there
876
 
    isn't enough characters in the buffer to satisfy the request.
877
 
 
878
 
  IMPLEMENTATION
879
 
 
880
 
    It works as follows: when a thread tries to read from a file (that
881
 
    is, after using all the data from the (shared) buffer), it just
882
 
    hangs on lock_io_cache(), waiting for other threads. When the very
883
 
    last thread attempts a read, lock_io_cache() returns 1, the thread
884
 
    does actual IO and unlock_io_cache(), which signals all the waiting
885
 
    threads that data is in the buffer.
886
 
 
887
 
  WARNING
888
 
 
889
 
    When changing this function, be careful with handling file offsets
890
 
    (end-of_file, pos_in_file). Do not cast them to possibly smaller
891
 
    types than my_off_t unless you can be sure that their value fits.
892
 
    Same applies to differences of file offsets. (Bug #11527)
893
 
 
894
 
    When changing this function, check _my_b_read(). It might need the
895
 
    same change.
896
 
 
897
 
  RETURN
898
 
    0      we succeeded in reading all data
899
 
    1      Error: can't read requested characters
900
 
*/
901
 
 
902
 
int _my_b_read_r(register IO_CACHE *cache, unsigned char *Buffer, size_t Count)
903
 
{
904
 
  my_off_t pos_in_file;
905
 
  size_t length, diff_length, left_length;
906
 
  IO_CACHE_SHARE *cshare= cache->share;
907
 
 
908
 
  if ((left_length= (size_t) (cache->read_end - cache->read_pos)))
909
 
  {
910
 
    assert(Count >= left_length);       /* User is not using my_b_read() */
911
 
    memcpy(Buffer, cache->read_pos, left_length);
912
 
    Buffer+= left_length;
913
 
    Count-= left_length;
914
 
  }
915
 
  while (Count)
916
 
  {
917
 
    size_t cnt, len;
918
 
 
919
 
    pos_in_file= cache->pos_in_file + (cache->read_end - cache->buffer);
920
 
    diff_length= (size_t) (pos_in_file & (IO_SIZE-1));
921
 
    length=IO_ROUND_UP(Count+diff_length)-diff_length;
922
 
    length= ((length <= cache->read_length) ?
923
 
             length + IO_ROUND_DN(cache->read_length - length) :
924
 
             length - IO_ROUND_UP(length - cache->read_length));
925
 
    if (cache->type != READ_FIFO &&
926
 
        (length > (cache->end_of_file - pos_in_file)))
927
 
      length= (size_t) (cache->end_of_file - pos_in_file);
928
 
    if (length == 0)
929
 
    {
930
 
      cache->error= (int) left_length;
931
 
      return(1);
932
 
    }
933
 
    if (lock_io_cache(cache, pos_in_file))
934
 
    {
935
 
      /* With a synchronized write/read cache we won't come here... */
936
 
      assert(!cshare->source_cache);
937
 
      /*
938
 
        ... unless the writer has gone before this thread entered the
939
 
        lock. Simulate EOF in this case. It can be distinguished by
940
 
        cache->file.
941
 
      */
942
 
      if (cache->file < 0)
943
 
        len= 0;
944
 
      else
945
 
      {
946
 
        /*
947
 
          Whenever a function which operates on IO_CACHE flushes/writes
948
 
          some part of the IO_CACHE to disk it will set the property
949
 
          "seek_not_done" to indicate this to other functions operating
950
 
          on the IO_CACHE.
951
 
        */
952
 
        if (cache->seek_not_done)
953
 
        {
954
 
          if (lseek(cache->file, pos_in_file, SEEK_SET) == MY_FILEPOS_ERROR)
955
 
          {
956
 
            cache->error= -1;
957
 
            unlock_io_cache(cache);
958
 
            return(1);
959
 
          }
960
 
        }
961
 
        len= my_read(cache->file, cache->buffer, length, cache->myflags);
962
 
      }
963
 
      cache->read_end=    cache->buffer + (len == (size_t) -1 ? 0 : len);
964
 
      cache->error=       (len == length ? 0 : (int) len);
965
 
      cache->pos_in_file= pos_in_file;
966
 
 
967
 
      /* Copy important values to the share. */
968
 
      cshare->error=       cache->error;
969
 
      cshare->read_end=    cache->read_end;
970
 
      cshare->pos_in_file= pos_in_file;
971
 
 
972
 
      /* Mark all threads as running and wake them. */
973
 
      unlock_io_cache(cache);
974
 
    }
975
 
    else
976
 
    {
977
 
      /*
978
 
        With a synchronized write/read cache readers always come here.
979
 
        Copy important values from the share.
980
 
      */
981
 
      cache->error=       cshare->error;
982
 
      cache->read_end=    cshare->read_end;
983
 
      cache->pos_in_file= cshare->pos_in_file;
984
 
 
985
 
      len= ((cache->error == -1) ? (size_t) -1 :
986
 
            (size_t) (cache->read_end - cache->buffer));
987
 
    }
988
 
    cache->read_pos=      cache->buffer;
989
 
    cache->seek_not_done= 0;
990
 
    if (len == 0 || len == (size_t) -1)
991
 
    {
992
 
      cache->error= (int) left_length;
993
 
      return(1);
994
 
    }
995
 
    cnt= (len > Count) ? Count : len;
996
 
    memcpy(Buffer, cache->read_pos, cnt);
997
 
    Count -= cnt;
998
 
    Buffer+= cnt;
999
 
    left_length+= cnt;
1000
 
    cache->read_pos+= cnt;
1001
 
  }
1002
 
  return(0);
1003
 
}
1004
 
 
1005
 
 
1006
 
/*
1007
 
  Copy data from write cache to read cache.
1008
 
 
1009
 
  SYNOPSIS
1010
 
    copy_to_read_buffer()
1011
 
      write_cache               The write cache.
1012
 
      write_buffer              The source of data, mostly the cache buffer.
1013
 
      write_length              The number of bytes to copy.
1014
 
 
1015
 
  NOTE
1016
 
    The write thread will wait for all read threads to join the cache
1017
 
    lock. Then it copies the data over and wakes the read threads.
1018
 
 
1019
 
  RETURN
1020
 
    void
1021
 
*/
1022
 
 
1023
 
static void copy_to_read_buffer(IO_CACHE *write_cache,
1024
 
                                const unsigned char *write_buffer, size_t write_length)
1025
 
{
1026
 
  IO_CACHE_SHARE *cshare= write_cache->share;
1027
 
 
1028
 
  assert(cshare->source_cache == write_cache);
1029
 
  /*
1030
 
    write_length is usually less or equal to buffer_length.
1031
 
    It can be bigger if _my_b_write() is called with a big length.
1032
 
  */
1033
 
  while (write_length)
1034
 
  {
1035
 
    size_t copy_length= min(write_length, write_cache->buffer_length);
1036
 
    int  rc;
1037
 
 
1038
 
    rc= lock_io_cache(write_cache, write_cache->pos_in_file);
1039
 
    /* The writing thread does always have the lock when it awakes. */
1040
 
    assert(rc);
1041
 
 
1042
 
    memcpy(cshare->buffer, write_buffer, copy_length);
1043
 
 
1044
 
    cshare->error=       0;
1045
 
    cshare->read_end=    cshare->buffer + copy_length;
1046
 
    cshare->pos_in_file= write_cache->pos_in_file;
1047
 
 
1048
 
    /* Mark all threads as running and wake them. */
1049
 
    unlock_io_cache(write_cache);
1050
 
 
1051
 
    write_buffer+= copy_length;
1052
 
    write_length-= copy_length;
1053
 
  }
1054
 
}
1055
 
 
1056
 
 
1057
 
/*
1058
 
  Do sequential read from the SEQ_READ_APPEND cache.
1059
 
 
1060
 
  We do this in three stages:
1061
 
   - first read from info->buffer
1062
 
   - then if there are still data to read, try the file descriptor
1063
 
   - afterwards, if there are still data to read, try append buffer
1064
 
 
1065
 
  RETURNS
1066
 
    0  Success
1067
 
    1  Failed to read
1068
 
*/
1069
 
 
1070
 
int _my_b_seq_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
1071
 
{
1072
 
  size_t length, diff_length, left_length, save_count, max_length;
1073
 
  my_off_t pos_in_file;
1074
 
  save_count=Count;
1075
 
 
1076
 
  /* first, read the regular buffer */
1077
 
  if ((left_length=(size_t) (info->read_end-info->read_pos)))
1078
 
  {
1079
 
    assert(Count > left_length);        /* User is not using my_b_read() */
1080
 
    memcpy(Buffer,info->read_pos, left_length);
1081
 
    Buffer+=left_length;
1082
 
    Count-=left_length;
1083
 
  }
1084
 
  lock_append_buffer(info);
1085
 
 
1086
 
  /* pos_in_file always point on where info->buffer was read */
1087
 
  if ((pos_in_file=info->pos_in_file +
1088
 
       (size_t) (info->read_end - info->buffer)) >= info->end_of_file)
1089
 
    goto read_append_buffer;
1090
 
 
1091
 
  /*
1092
 
    With read-append cache we must always do a seek before we read,
1093
 
    because the write could have moved the file pointer astray
1094
 
  */
1095
 
  if (lseek(info->file,pos_in_file,SEEK_SET) == MY_FILEPOS_ERROR)
1096
 
  {
1097
 
   info->error= -1;
1098
 
   unlock_append_buffer(info);
1099
 
   return (1);
1100
 
  }
1101
 
  info->seek_not_done=0;
1102
 
 
1103
 
  diff_length= (size_t) (pos_in_file & (IO_SIZE-1));
1104
 
 
1105
 
  /* now the second stage begins - read from file descriptor */
1106
 
  if (Count >= (size_t) (IO_SIZE+(IO_SIZE-diff_length)))
1107
 
  {
1108
 
    /* Fill first intern buffer */
1109
 
    size_t read_length;
1110
 
 
1111
 
    length=(Count & (size_t) ~(IO_SIZE-1))-diff_length;
1112
 
    if ((read_length= my_read(info->file,Buffer, length,
1113
 
                              info->myflags)) == (size_t) -1)
1114
 
    {
1115
 
      info->error= -1;
1116
 
      unlock_append_buffer(info);
1117
 
      return 1;
1118
 
    }
1119
 
    Count-=read_length;
1120
 
    Buffer+=read_length;
1121
 
    pos_in_file+=read_length;
1122
 
 
1123
 
    if (read_length != length)
1124
 
    {
1125
 
      /*
1126
 
        We only got part of data;  Read the rest of the data from the
1127
 
        write buffer
1128
 
      */
1129
 
      goto read_append_buffer;
1130
 
    }
1131
 
    left_length+=length;
1132
 
    diff_length=0;
1133
 
  }
1134
 
 
1135
 
  max_length= info->read_length-diff_length;
1136
 
  if (max_length > (info->end_of_file - pos_in_file))
1137
 
    max_length= (size_t) (info->end_of_file - pos_in_file);
1138
 
  if (!max_length)
1139
 
  {
1140
 
    if (Count)
1141
 
      goto read_append_buffer;
1142
 
    length=0;                           /* Didn't read any more chars */
1143
 
  }
1144
 
  else
1145
 
  {
1146
 
    length= my_read(info->file,info->buffer, max_length, info->myflags);
1147
 
    if (length == (size_t) -1)
1148
 
    {
1149
 
      info->error= -1;
1150
 
      unlock_append_buffer(info);
1151
 
      return 1;
1152
 
    }
1153
 
    if (length < Count)
1154
 
    {
1155
 
      memcpy(Buffer, info->buffer, length);
1156
 
      Count -= length;
1157
 
      Buffer += length;
1158
 
 
1159
 
      /*
1160
 
         added the line below to make
1161
 
         assert(pos_in_file==info->end_of_file) pass.
1162
 
         otherwise this does not appear to be needed
1163
 
      */
1164
 
      pos_in_file += length;
1165
 
      goto read_append_buffer;
1166
 
    }
1167
 
  }
1168
 
  unlock_append_buffer(info);
1169
 
  info->read_pos=info->buffer+Count;
1170
 
  info->read_end=info->buffer+length;
1171
 
  info->pos_in_file=pos_in_file;
1172
 
  memcpy(Buffer,info->buffer,(size_t) Count);
1173
 
  return 0;
1174
 
 
1175
 
read_append_buffer:
1176
 
 
1177
 
  /*
1178
 
     Read data from the current write buffer.
1179
 
     Count should never be == 0 here (The code will work even if count is 0)
1180
 
  */
1181
 
 
1182
 
  {
1183
 
    /* First copy the data to Count */
1184
 
    size_t len_in_buff = (size_t) (info->write_pos - info->append_read_pos);
1185
 
    size_t copy_len;
1186
 
    size_t transfer_len;
1187
 
 
1188
 
    assert(info->append_read_pos <= info->write_pos);
1189
 
    /*
1190
 
      TODO: figure out if the assert below is needed or correct.
1191
 
    */
1192
 
    assert(pos_in_file == info->end_of_file);
1193
 
    copy_len=min(Count, len_in_buff);
1194
 
    memcpy(Buffer, info->append_read_pos, copy_len);
1195
 
    info->append_read_pos += copy_len;
1196
 
    Count -= copy_len;
1197
 
    if (Count)
1198
 
      info->error = static_cast<int>(save_count - Count);
1199
 
 
1200
 
    /* Fill read buffer with data from write buffer */
1201
 
    memcpy(info->buffer, info->append_read_pos,
1202
 
           (size_t) (transfer_len=len_in_buff - copy_len));
1203
 
    info->read_pos= info->buffer;
1204
 
    info->read_end= info->buffer+transfer_len;
1205
 
    info->append_read_pos=info->write_pos;
1206
 
    info->pos_in_file=pos_in_file+copy_len;
1207
 
    info->end_of_file+=len_in_buff;
1208
 
  }
1209
 
  unlock_append_buffer(info);
1210
 
  return Count ? 1 : 0;
1211
 
}
1212
 
 
1213
 
 
1214
533
#ifdef HAVE_AIOWAIT
1215
534
 
1216
 
/*
1217
 
  Read from the IO_CACHE into a buffer and feed asynchronously
1218
 
  from disk when needed.
1219
 
 
1220
 
  SYNOPSIS
1221
 
    _my_b_async_read()
1222
 
      info                      IO_CACHE pointer
1223
 
      Buffer                    Buffer to retrieve count bytes from file
1224
 
      Count                     Number of bytes to read into Buffer
1225
 
 
1226
 
  RETURN VALUE
1227
 
    -1          An error has occurred; errno is set.
1228
 
     0          Success
1229
 
     1          An error has occurred; IO_CACHE to error state.
1230
 
*/
1231
 
 
 
535
/**
 
536
 * @brief
 
537
 *   Read from the IO_CACHE into a buffer and feed asynchronously from disk when needed.
 
538
 *
 
539
 * @param info IO_CACHE pointer
 
540
 * @param Buffer Buffer to retrieve count bytes from file
 
541
 * @param Count Number of bytes to read into Buffer
 
542
 * 
 
543
 * @retval -1 An error has occurred; errno is set.
 
544
 * @retval 0 Success
 
545
 * @retval 1 An error has occurred; IO_CACHE to error state.
 
546
 */
1232
547
int _my_b_async_read(register IO_CACHE *info, unsigned char *Buffer, size_t Count)
1233
548
{
1234
549
  size_t length,read_length,diff_length,left_length,use_length,org_Count;
1392
707
#endif
1393
708
 
1394
709
 
1395
 
/* Read one byte when buffer is empty */
1396
 
 
 
710
/**
 
711
 * @brief
 
712
 *   Read one byte when buffer is empty
 
713
 */
1397
714
int _my_b_get(IO_CACHE *info)
1398
715
{
1399
716
  unsigned char buff;
1407
724
  return (int) (unsigned char) buff;
1408
725
}
1409
726
 
1410
 
/*
1411
 
   Write a byte buffer to IO_CACHE and flush to disk
1412
 
   if IO_CACHE is full.
1413
 
 
1414
 
   RETURN VALUE
1415
 
    1 On error on write
1416
 
    0 On success
1417
 
   -1 On error; errno contains error code.
1418
 
*/
1419
 
 
 
727
/**
 
728
 * @brief
 
729
 *   Write a byte buffer to IO_CACHE and flush to disk if IO_CACHE is full.
 
730
 *
 
731
 * @retval -1 On error; errno contains error code.
 
732
 * @retval 0 On success
 
733
 * @retval 1 On error on write
 
734
 */
1420
735
int _my_b_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count)
1421
736
{
1422
737
  size_t rest_length,length;
1456
771
    if (my_write(info->file, Buffer, length, info->myflags | MY_NABP))
1457
772
      return info->error= -1;
1458
773
 
1459
 
    /*
1460
 
      In case of a shared I/O cache with a writer we normally do direct
1461
 
      write cache to read cache copy. Simulate this here by direct
1462
 
      caller buffer to read cache copy. Do it after the write so that
1463
 
      the cache readers actions on the flushed part can go in parallel
1464
 
      with the write of the extra stuff. copy_to_read_buffer()
1465
 
      synchronizes writer and readers so that after this call the
1466
 
      readers can act on the extra stuff while the writer can go ahead
1467
 
      and prepare the next output. copy_to_read_buffer() relies on
1468
 
      info->pos_in_file.
1469
 
    */
1470
 
    if (info->share)
1471
 
      copy_to_read_buffer(info, Buffer, length);
1472
 
 
1473
774
    Count-=length;
1474
775
    Buffer+=length;
1475
776
    info->pos_in_file+=length;
1479
780
  return 0;
1480
781
}
1481
782
 
1482
 
/*
1483
 
  Write a block to disk where part of the data may be inside the record
1484
 
  buffer.  As all write calls to the data goes through the cache,
1485
 
  we will never get a seek over the end of the buffer
1486
 
*/
1487
 
 
 
783
/**
 
784
 * @brief
 
785
 *   Write a block to disk where part of the data may be inside the record buffer.
 
786
 *   As all write calls to the data goes through the cache,
 
787
 *   we will never get a seek over the end of the buffer.
 
788
 */
1488
789
int my_block_write(register IO_CACHE *info, const unsigned char *Buffer, size_t Count,
1489
790
                   my_off_t pos)
1490
791
{
1491
792
  size_t length;
1492
793
  int error=0;
1493
794
 
1494
 
  /*
1495
 
    Assert that we cannot come here with a shared cache. If we do one
1496
 
    day, we might need to add a call to copy_to_read_buffer().
1497
 
  */
1498
 
  assert(!info->share);
1499
 
 
1500
795
  if (pos < info->pos_in_file)
1501
796
  {
1502
797
    /* Of no overlap, write everything without buffering */
1534
829
  return error;
1535
830
}
1536
831
 
1537
 
 
1538
 
        /* Flush write cache */
1539
 
 
1540
 
#define LOCK_APPEND_BUFFER if (need_append_buffer_lock) \
1541
 
  lock_append_buffer(info);
1542
 
#define UNLOCK_APPEND_BUFFER if (need_append_buffer_lock) \
1543
 
  unlock_append_buffer(info);
1544
 
 
 
832
/**
 
833
 * @brief
 
834
 *   Flush write cache 
 
835
 */
1545
836
int my_b_flush_io_cache(IO_CACHE *info, int need_append_buffer_lock)
1546
837
{
1547
838
  size_t length;
1548
 
  bool append_cache;
 
839
  bool append_cache= false;
1549
840
  my_off_t pos_in_file;
1550
841
 
1551
 
  if (!(append_cache = (info->type == SEQ_READ_APPEND)))
1552
 
    need_append_buffer_lock=0;
1553
 
 
1554
842
  if (info->type == WRITE_CACHE || append_cache)
1555
843
  {
1556
844
    if (info->file == -1)
1558
846
      if (real_open_cached_file(info))
1559
847
        return((info->error= -1));
1560
848
    }
1561
 
    LOCK_APPEND_BUFFER;
 
849
    lock_append_buffer(info, need_append_buffer_lock);
1562
850
 
1563
851
    if ((length=(size_t) (info->write_pos - info->write_buffer)))
1564
852
    {
1565
 
      /*
1566
 
        In case of a shared I/O cache with a writer we do direct write
1567
 
        cache to read cache copy. Do it before the write here so that
1568
 
        the readers can work in parallel with the write.
1569
 
        copy_to_read_buffer() relies on info->pos_in_file.
1570
 
      */
1571
 
      if (info->share)
1572
 
        copy_to_read_buffer(info, info->write_buffer, length);
1573
 
 
1574
853
      pos_in_file=info->pos_in_file;
1575
854
      /*
1576
855
        If we have append cache, we always open the file with
1580
859
      {                                 /* File touched, do seek */
1581
860
        if (lseek(info->file,pos_in_file,SEEK_SET) == MY_FILEPOS_ERROR)
1582
861
        {
1583
 
          UNLOCK_APPEND_BUFFER;
 
862
          unlock_append_buffer(info, need_append_buffer_lock);
1584
863
          return((info->error= -1));
1585
864
        }
1586
865
        if (!append_cache)
1608
887
      }
1609
888
 
1610
889
      info->append_read_pos=info->write_pos=info->write_buffer;
1611
 
      UNLOCK_APPEND_BUFFER;
 
890
      unlock_append_buffer(info, need_append_buffer_lock);
1612
891
      return(info->error);
1613
892
    }
1614
893
  }
1619
898
    info->inited=0;
1620
899
  }
1621
900
#endif
1622
 
  UNLOCK_APPEND_BUFFER;
 
901
  unlock_append_buffer(info, need_append_buffer_lock);
1623
902
  return(0);
1624
903
}
1625
904
 
1626
 
/*
1627
 
  Free an IO_CACHE object
1628
 
 
1629
 
  SYNOPSOS
1630
 
    end_io_cache()
1631
 
    info                IO_CACHE Handle to free
1632
 
 
1633
 
  NOTES
1634
 
    It's currently safe to call this if one has called init_io_cache()
1635
 
    on the 'info' object, even if init_io_cache() failed.
1636
 
    This function is also safe to call twice with the same handle.
1637
 
 
1638
 
  RETURN
1639
 
   0  ok
1640
 
   #  Error
1641
 
*/
1642
 
 
 
905
/**
 
906
 * @brief
 
907
 *   Free an IO_CACHE object
 
908
 * 
 
909
 * @detail
 
910
 *   It's currently safe to call this if one has called init_io_cache()
 
911
 *   on the 'info' object, even if init_io_cache() failed.
 
912
 *   This function is also safe to call twice with the same handle.
 
913
 * 
 
914
 * @param info IO_CACHE Handle to free
 
915
 * 
 
916
 * @retval 0 ok
 
917
 * @retval # Error
 
918
 */
1643
919
int end_io_cache(IO_CACHE *info)
1644
920
{
1645
921
  int error=0;
1646
922
  IO_CACHE_CALLBACK pre_close;
1647
923
 
1648
 
  /*
1649
 
    Every thread must call remove_io_thread(). The last one destroys
1650
 
    the share elements.
1651
 
  */
1652
 
  assert(!info->share || !info->share->total_threads);
1653
 
 
1654
924
  if ((pre_close=info->pre_close))
1655
925
  {
1656
926
    (*pre_close)(info);
1664
934
    free((unsigned char*) info->buffer);
1665
935
    info->buffer=info->read_pos=(unsigned char*) 0;
1666
936
  }
1667
 
  if (info->type == SEQ_READ_APPEND)
1668
 
  {
1669
 
    /* Destroy allocated mutex */
1670
 
    info->type= TYPE_NOT_SET;
1671
 
    pthread_mutex_destroy(&info->append_buffer_lock);
1672
 
  }
 
937
 
1673
938
  return(error);
1674
939
} /* end_io_cache */
1675
940
 
1676
941
} /* namespace internal */
1677
942
} /* namespace drizzled */
1678
 
 
1679
 
/**********************************************************************
1680
 
 Testing of MF_IOCACHE
1681
 
**********************************************************************/
1682
 
 
1683
 
#ifdef MAIN
1684
 
 
1685
 
void die(const char* fmt, ...)
1686
 
{
1687
 
  va_list va_args;
1688
 
  va_start(va_args,fmt);
1689
 
  fprintf(stderr,"Error:");
1690
 
  vfprintf(stderr, fmt,va_args);
1691
 
  fprintf(stderr,", errno=%d\n", errno);
1692
 
  exit(1);
1693
 
}
1694
 
 
1695
 
int open_file(const char* fname, IO_CACHE* info, int cache_size)
1696
 
{
1697
 
  int fd;
1698
 
  if ((fd=my_open(fname,O_CREAT | O_RDWR,MYF(MY_WME))) < 0)
1699
 
    die("Could not open %s", fname);
1700
 
  if (init_io_cache(info, fd, cache_size, SEQ_READ_APPEND, 0,0,MYF(MY_WME)))
1701
 
    die("failed in init_io_cache()");
1702
 
  return fd;
1703
 
}
1704
 
 
1705
 
void close_file(IO_CACHE* info)
1706
 
{
1707
 
  end_io_cache(info);
1708
 
  my_close(info->file, MYF(MY_WME));
1709
 
}
1710
 
 
1711
 
int main(int argc, char** argv)
1712
 
{
1713
 
  IO_CACHE sra_cache; /* SEQ_READ_APPEND */
1714
 
  MY_STAT status;
1715
 
  const char* fname="/tmp/iocache.test";
1716
 
  int cache_size=16384;
1717
 
  char llstr_buf[22];
1718
 
  int max_block,total_bytes=0;
1719
 
  int i,num_loops=100,error=0;
1720
 
  char *p;
1721
 
  char* block, *block_end;
1722
 
  MY_INIT(argv[0]);
1723
 
  max_block = cache_size*3;
1724
 
  if (!(block=(char*)malloc(max_block)))
1725
 
    die("Not enough memory to allocate test block");
1726
 
  block_end = block + max_block;
1727
 
  for (p = block,i=0; p < block_end;i++)
1728
 
  {
1729
 
    *p++ = (char)i;
1730
 
  }
1731
 
  if (my_stat(fname,&status, MYF(0)) &&
1732
 
      my_delete(fname,MYF(MY_WME)))
1733
 
    {
1734
 
      die("Delete of %s failed, aborting", fname);
1735
 
    }
1736
 
  open_file(fname,&sra_cache, cache_size);
1737
 
  for (i = 0; i < num_loops; i++)
1738
 
  {
1739
 
    char buf[4];
1740
 
    int block_size = abs(rand() % max_block);
1741
 
    int4store(buf, block_size);
1742
 
    if (my_b_append(&sra_cache,buf,4) ||
1743
 
        my_b_append(&sra_cache, block, block_size))
1744
 
      die("write failed");
1745
 
    total_bytes += 4+block_size;
1746
 
  }
1747
 
  close_file(&sra_cache);
1748
 
  free(block);
1749
 
  if (!my_stat(fname,&status,MYF(MY_WME)))
1750
 
    die("%s failed to stat, but I had just closed it,\
1751
 
 wonder how that happened");
1752
 
  printf("Final size of %s is %s, wrote %d bytes\n",fname,
1753
 
         llstr(status.st_size,llstr_buf),
1754
 
         total_bytes);
1755
 
  my_delete(fname, MYF(MY_WME));
1756
 
  /* check correctness of tests */
1757
 
  if (total_bytes != status.st_size)
1758
 
  {
1759
 
    fprintf(stderr,"Not the same number of bytes acutally  in file as bytes \
1760
 
supposedly written\n");
1761
 
    error=1;
1762
 
  }
1763
 
  exit(error);
1764
 
  return 0;
1765
 
}
1766
 
#endif