summaryrefslogtreecommitdiff
path: root/storage/maria/ma_state.c
blob: 2eff64ec7cd228f820ea7e33f3586ef4ede6af7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/* Copyright (C) 2008 Sun AB and Michael Widenius

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

/*
  Functions to maintain live statistics for Maria transactional tables
  and versioning for not transactional tables

  See WL#3138; Maria - fast "SELECT COUNT(*) FROM t;" and "CHECKSUM TABLE t"
  for details about live number of rows and live checksums

  TODO
   - Allocate MA_USED_TABLES and MA_HISTORY_STATE from a global pool (to
     avoid calls to malloc()
   - In trnamn_end_trans_hook(), don't call _ma_remove_not_visible_states()
     every time. One could for example call it if there has been more than
     10 ended transactions since last time it was called.
*/

#include "maria_def.h"
#include "trnman.h"
#include "ma_trnman.h"
#include "ma_blockrec.h"

/**
   @brief Setup initial start-of-transaction state for a table

   @fn     _ma_setup_live_state
   @param info		Maria handler

   @notes
     This function ensures that trn->used_tables contains a list of
     start and live states for tables that are part of the transaction
     and that info->state points to the current live state for the table.

   @TODO
     Change trn->table_list to a hash and share->state_history to a binary tree

   @return
   @retval 0  ok
   @retval 1  error (out of memory)
*/

my_bool _ma_setup_live_state(MARIA_HA *info)
{
  TRN *trn;
  MARIA_SHARE *share= info->s;
  MARIA_USED_TABLES *tables;
  MARIA_STATE_HISTORY *history;
  DBUG_ENTER("_ma_setup_live_state");
  DBUG_PRINT("enter", ("info: %p", info));

  DBUG_ASSERT(share->lock_key_trees);

  if (maria_create_trn_hook(info))
    DBUG_RETURN(1);

  trn= info->trn;
  for (tables= (MARIA_USED_TABLES*) trn->used_tables;
       tables;
       tables= tables->next)
  {
    if (tables->share == share)
    {
      /* Table is already used by transaction */
      goto end;
    }
  }

  /* Table was not used before, create new table state entry */
  if (!(tables= (MARIA_USED_TABLES*) my_malloc(sizeof(*tables),
                                               MYF(MY_WME | MY_ZEROFILL))))
    DBUG_RETURN(1);
  tables->next= trn->used_tables;
  trn->used_tables= tables;
  tables->share= share;

  mysql_mutex_lock(&share->intern_lock);
  share->in_trans++;
  DBUG_PRINT("info", ("share: %p  in_trans: %d",
                     share, share->in_trans));

  history= share->state_history;

  /*
    We must keep share locked to ensure that we don't access a history
    link that is deleted by concurrently running checkpoint.

    It's enough to compare trids here (instead of calling
    tranman_can_read_from) as history->trid is a commit_trid
  */
  while (trn->trid <= history->trid)
    history= history->next;
  mysql_mutex_unlock(&share->intern_lock);
  /* The current item can't be deleted as it's the first one visible for us */
  tables->state_start=  tables->state_current= history->state;
  tables->state_current.changed= tables->state_current.no_transid= 0;

  DBUG_PRINT("info", ("records: %ld", (ulong) tables->state_start.records));

end:
  info->state_start= &tables->state_start;
  info->state= &tables->state_current;
  info->used_tables= tables;
  tables->use_count++;

  /*
    Mark in transaction state if we are not using transid (versioning)
    on rows. If not, then we will in _ma_trnman_end_trans_hook()
    ensure that the state is visible for all at end of transaction
  */
  tables->state_current.no_transid|= !(info->row_flag & ROW_FLAG_TRANSID);

  DBUG_PRINT("exit", ("tables: %p  info->state: %p", tables, info->state));
  DBUG_RETURN(0);
}


/**
   @brief Remove states that are not visible by anyone

   @fn   _ma_remove_not_visible_states()
   @param org_history    List to history
   @param all            1 if we should delete the first state if it's
                         visible for all.  For the moment this is only used
                         on close() of table.
   @param trnman_is_locked  Set to 1 if we have already a lock on trnman.

   @notes
     The assumption is that items in the history list is ordered by
     commit_trid.

     A state is not visible anymore if there is no new transaction
     that has been started between the commit_trid's of two states

     As long as some states exists, we keep the newest = (last commit)
     state as first state in the history.  This is to allow us to just move
     the history from the global list to the share when we open the table.

     Note that if 'all' is set trnman_is_locked must be 0, becasue
     trnman_get_min_trid() will take a lock on trnman.

   @return
   @retval Pointer to new history list
*/

MARIA_STATE_HISTORY
*_ma_remove_not_visible_states(MARIA_STATE_HISTORY *org_history,
                               my_bool all,
                               my_bool trnman_is_locked)
{
  TrID last_trid;
  MARIA_STATE_HISTORY *history, **parent, *next;
  DBUG_ENTER("_ma_remove_not_visible_states");

  if (!org_history)
    DBUG_RETURN(0);                          /* Not versioned table */

  last_trid= org_history->trid;
  parent= &org_history->next;
  for (history= org_history->next; history; history= next)
  {
    next= history->next;
    if (!trnman_exists_active_transactions(history->trid, last_trid,
                                           trnman_is_locked))
    {
      DBUG_PRINT("info", ("removing history->trid: %lu  next: %lu",
                          (ulong) history->trid, (ulong) last_trid));
      my_free(history);
      continue;
    }
    *parent= history;
    parent= &history->next;
    last_trid= history->trid;
  }
  *parent= 0;

  if (all && parent == &org_history->next)
  {
    /* There is only one state left. Delete this if it's visible for all */
    if (last_trid < trnman_get_min_trid())
    {
      my_free(org_history);
      org_history= 0;
    }
  }
  DBUG_RETURN(org_history);
}


/**
   @brief Remove not used state history

   @param share          Maria table information
   @param all            1 if we should delete the first state if it's
                         visible for all.  For the moment this is only used
                         on close() of table.

   @notes
   share and trnman are not locked.

   We must first lock trnman and then share->intern_lock. This is becasue
   _ma_trnman_end_trans_hook() has a lock on trnman and then
   takes share->intern_lock.
*/

void _ma_remove_not_visible_states_with_lock(MARIA_SHARE *share,
                                             my_bool all)
{
  my_bool is_lock_trman;
  if ((is_lock_trman= trman_is_inited()))
    trnman_lock();

  mysql_mutex_lock(&share->intern_lock);
  share->state_history=  _ma_remove_not_visible_states(share->state_history,
                                                       all, 1);
  mysql_mutex_unlock(&share->intern_lock);
  if (is_lock_trman)
    trnman_unlock();
}


/*
  Free state history information from share->history and reset information
  to current state.

  @notes
  Used after repair/rename/drop as then all rows are visible for everyone
*/

void _ma_reset_state(MARIA_HA *info)
{
  MARIA_SHARE *share= info->s;
  MARIA_STATE_HISTORY *history= share->state_history;
  DBUG_ENTER("_ma_reset_state");

  /* Always true if share->now_transactional is set */
  if (history && share->have_versioning)
  {
    MARIA_STATE_HISTORY *next;
    DBUG_PRINT("info", ("resetting history"));

    /* Set the current history to current state */
    share->state_history->state= share->state.state;
    /* Set current table handler to point to new history state */
    info->state= info->state_start= &share->state_history->state;
    for (history= history->next ; history ; history= next)
    {
      next= history->next;
      my_free(history);
    }
    share->state_history->next= 0;
    share->state_history->trid= 0;              /* Visible for all */
  }
  DBUG_VOID_RETURN;
}


/****************************************************************************
  The following functions are called by thr_lock() in threaded applications
  for not transactional tables
****************************************************************************/

/*
  Create a copy of the current status for the table

  SYNOPSIS
    _ma_get_status()
    param		Pointer to Myisam handler
    concurrent_insert	Set to 1 if we are going to do concurrent inserts
			(THR_WRITE_CONCURRENT_INSERT was used)
*/

void _ma_get_status(void* param, my_bool concurrent_insert)
{
  MARIA_HA *info=(MARIA_HA*) param;
  DBUG_ENTER("_ma_get_status");
  DBUG_PRINT("info",("key_file: %ld  data_file: %ld  concurrent_insert: %d",
		     (long) info->s->state.state.key_file_length,
		     (long) info->s->state.state.data_file_length,
                     concurrent_insert));
#ifndef DBUG_OFF
  if (info->state->key_file_length > info->s->state.state.key_file_length ||
      info->state->data_file_length > info->s->state.state.data_file_length)
    DBUG_PRINT("warning",("old info:  key_file: %ld  data_file: %ld",
			  (long) info->state->key_file_length,
			  (long) info->state->data_file_length));
#endif
  info->state_save= info->s->state.state;
  info->state= &info->state_save;
  info->state->changed= 0;
  info->append_insert_at_end= concurrent_insert;
  DBUG_VOID_RETURN;
}


void _ma_update_status(void* param)
{
  MARIA_HA *info=(MARIA_HA*) param;
  /*
    Because someone may have closed the table we point at, we only
    update the state if its our own state.  This isn't a problem as
    we are always pointing at our own lock or at a read lock.
    (This is enforced by thr_multi_lock.c)
  */
  if (info->state == &info->state_save)
  {
    MARIA_SHARE *share= info->s;
#ifndef DBUG_OFF
    DBUG_PRINT("info",("updating status:  key_file: %ld  data_file: %ld",
		       (long) info->state->key_file_length,
		       (long) info->state->data_file_length));
    if (info->state->key_file_length < share->state.state.key_file_length ||
	info->state->data_file_length < share->state.state.data_file_length)
      DBUG_PRINT("warning",("old info:  key_file: %ld  data_file: %ld",
			    (long) share->state.state.key_file_length,
			    (long) share->state.state.data_file_length));
#endif
    /*
      we are going to modify the state without lock's log, this would break
      recovery if done with a transactional table.
    */
    DBUG_ASSERT(!info->s->base.born_transactional);
    share->state.state= *info->state;
    info->state= &share->state.state;
#ifdef HAVE_QUERY_CACHE
    DBUG_PRINT("info", ("invalidator... '%s' (status update)",
                        info->s->data_file_name.str));
    DBUG_ASSERT(info->s->chst_invalidator != NULL);
    (*info->s->chst_invalidator)((const char *)info->s->data_file_name.str);
#endif

  }
  info->append_insert_at_end= 0;
}


/*
  Same as ma_update_status() but take a lock in the table lock, to protect
  against someone calling ma_get_status() from thr_lock() at the same time.
*/

void _ma_update_status_with_lock(MARIA_HA *info)
{
  my_bool locked= 0;
  if (info->state == &info->state_save)
  {
    locked= 1;
    mysql_mutex_lock(&info->s->lock.mutex);
  }
  (*info->s->lock.update_status)(info);
  if (locked)
    mysql_mutex_unlock(&info->s->lock.mutex);
}


void _ma_restore_status(void *param)
{
  MARIA_HA *info= (MARIA_HA*) param;
  info->state= &info->s->state.state;
  info->append_insert_at_end= 0;
}


void _ma_copy_status(void* to, void *from)
{
  ((MARIA_HA*) to)->state= &((MARIA_HA*) from)->state_save;
}


void _ma_reset_update_flag(void *param,
                           my_bool concurrent_insert __attribute__((unused)))
{
  MARIA_HA *info=(MARIA_HA*) param;
  info->state->changed= 0;
}

my_bool _ma_start_trans(void* param)
{
  MARIA_HA *info=(MARIA_HA*) param;
  if (!info->s->lock_key_trees)
  {
    info->state=  info->state_start;
    *info->state= info->s->state.state;
  }
  return 0;
}


/**
   @brief Check if should allow concurrent inserts

   @implementation
     Allow concurrent inserts if we don't have a hole in the table or
     if there is no active write lock and there is active read locks and
     maria_concurrent_insert == 2. In this last case the new
     row('s) are inserted at end of file instead of filling up the hole.

     The last case is to allow one to inserts into a heavily read-used table
     even if there is holes.

   @notes
     If there is a an rtree indexes in the table, concurrent inserts are
     disabled in maria_open()

  @return
  @retval 0  ok to use concurrent inserts
  @retval 1  not ok
*/

my_bool _ma_check_status(void *param)
{
  MARIA_HA *info=(MARIA_HA*) param;
  /*
    The test for w_locks == 1 is here because this thread has already done an
    external lock (in other words: w_locks == 1 means no other threads has
    a write lock)
  */
  DBUG_PRINT("info",("dellink: %ld  r_locks: %u  w_locks: %u",
                     (long) info->s->state.dellink, (uint) info->s->r_locks,
                     (uint) info->s->w_locks));
  return (my_bool) !(info->s->state.dellink == HA_OFFSET_ERROR ||
                     (maria_concurrent_insert == 2 && info->s->r_locks &&
                      info->s->w_locks == 1));
}


/**
   @brief write hook at end of trans to store status for all used table

   @Notes
   This function must be called under trnman_lock in trnman_end_trn()
   because of the following reasons:
   - After trnman_end_trn() is called, the current transaction will be
   regarded as committed and all used tables state_history will be
   visible to other transactions.  To do this, we loop over all used
   tables and create/update a history entries that contains the correct
   state_history for them.
*/

my_bool _ma_trnman_end_trans_hook(TRN *trn, my_bool commit,
                                  my_bool active_transactions)
{
  my_bool error= 0;
  MARIA_USED_TABLES *tables, *next;
  DBUG_ENTER("_ma_trnman_end_trans_hook");
  DBUG_PRINT("enter", ("trn: %p  used_tables: %p", trn, trn->used_tables));

  for (tables= (MARIA_USED_TABLES*) trn->used_tables;
       tables;
       tables= next)
  {
    MARIA_SHARE *share= tables->share;
    next= tables->next;
    if (commit)
    {
      MARIA_STATE_HISTORY *history;

      mysql_mutex_lock(&share->intern_lock);

      /* We only have to update history state if something changed */
      if (tables->state_current.changed)
      {
        if (tables->state_current.no_transid)
        {
          /*
            The change was done without using transid on rows (like in
            bulk insert). In this case this thread is the only one
            that is using the table and all rows will be visble
            for all transactions.
          */
          _ma_reset_history(share);
        }
        else
        {
          if (active_transactions && share->now_transactional &&
              trnman_exists_active_transactions(share->state_history->trid,
                                                trn->commit_trid, 1))
          {
            /*
              There exist transactions that are still using the current
              share->state_history.  Create a new history item for this
              commit and add it first in the state_history list. This
              ensures that all history items are stored in the list in
              decresing trid order.
            */
            if (!(history= my_malloc(sizeof(*history), MYF(MY_WME))))
            {
              /* purecov: begin inspected */
              error= 1;
              mysql_mutex_unlock(&share->intern_lock);
              my_free(tables);
              continue;
              /* purecov: end */
            }
            history->state= share->state_history->state;
            history->next= share->state_history;
            share->state_history= history;
          }
          else
          {
            /* Previous history can't be seen by anyone, reuse old memory */
            history= share->state_history;
            DBUG_PRINT("info", ("removing history->trid: %lu  new: %lu",
                                (ulong) history->trid,
                                (ulong) trn->commit_trid));
          }

          history->state.records+= (tables->state_current.records -
                                    tables->state_start.records);
          history->state.checksum+= (tables->state_current.checksum -
                                     tables->state_start.checksum);
          history->trid= trn->commit_trid;

          share->state.last_change_trn= trn->commit_trid;

          if (history->next)
          {
            /* Remove not visible states */
            share->state_history= _ma_remove_not_visible_states(history, 0, 1);
          }
          DBUG_PRINT("info", ("share: %p in_trans: %d",
                              share, share->in_trans));
        }
      }
      share->in_trans--;
      mysql_mutex_unlock(&share->intern_lock);
    }
    else
    {
#ifdef DBUG_ASSERT_EXISTS
      /*
        We need to keep share->in_trans correct in the debug library
        because of the assert in maria_close()
      */
      mysql_mutex_lock(&share->intern_lock);
      share->in_trans--;
      mysql_mutex_unlock(&share->intern_lock);
#endif
    }
    my_free(tables);
  }
  trn->used_tables= 0;
  trn->used_instances= 0;
  DBUG_RETURN(error);
}


/**
   Remove table from trnman_list

   @notes
     This is used when we unlock a table from a group of locked tables
     just before doing a rename or drop table.

     share->internal_lock must be locked when function is called
*/

void _ma_remove_table_from_trnman(MARIA_HA *info)
{
  MARIA_SHARE *share= info->s;
  TRN *trn= info->trn;
  MARIA_USED_TABLES *tables, **prev;
  DBUG_ENTER("_ma_remove_table_from_trnman");
  DBUG_PRINT("enter", ("trn: %p  used_tables: %p  share: %p  in_trans: %d",
                       trn, trn->used_tables, share, share->in_trans));

  mysql_mutex_assert_owner(&share->intern_lock);

  if (trn == &dummy_transaction_object)
    DBUG_VOID_RETURN;

  /* First remove share from used_tables */
  for (prev= (MARIA_USED_TABLES**) (char*) &trn->used_tables;
       (tables= *prev);
       prev= &tables->next)
  {
    if (tables->share == share)
    {
      *prev= tables->next;
      share->in_trans--;
      my_free(tables);
      break;
    }
  }
  if (!tables)
  {
    /*
      This can only happens in case of rename of intermediate table as
      part of alter table
    */
    DBUG_PRINT("warning", ("share: %p where not in used_tables_list", share));
  }

  /* Reset trn and remove table from used_instances */
  _ma_reset_trn_for_table(info);

  DBUG_VOID_RETURN;
}



/****************************************************************************
  The following functions are called by thr_lock() in threaded applications
  for transactional tables.
****************************************************************************/

/*
  Create a copy of the current status for the table

  SYNOPSIS
    _ma_get_status()
    param		Pointer to Aria handler
    concurrent_insert	Set to 1 if we are going to do concurrent inserts
			(THR_WRITE_CONCURRENT_INSERT was used)
*/

void _ma_block_get_status(void* param, my_bool concurrent_insert)
{
  MARIA_HA *info=(MARIA_HA*) param;
  DBUG_ENTER("_ma_block_get_status");
  DBUG_PRINT("enter", ("concurrent_insert %d", concurrent_insert));

  info->row_base_length= info->s->base_length;
  info->row_flag= info->s->base.default_row_flag;
  if (concurrent_insert)
  {
    DBUG_ASSERT(info->lock.type == TL_WRITE_CONCURRENT_INSERT);
    info->row_flag|= ROW_FLAG_TRANSID;
    info->row_base_length+= TRANSID_SIZE;
  }
  else
  {
    DBUG_ASSERT(info->lock.type != TL_WRITE_CONCURRENT_INSERT);
  }
  DBUG_VOID_RETURN;
}


my_bool _ma_block_start_trans(void* param)
{
  MARIA_HA *info=(MARIA_HA*) param;
  DBUG_ENTER("_ma_block_start_trans");

  if (info->s->lock_key_trees)
  {
    /*
      Assume for now that this doesn't fail (It can only fail in
      out of memory conditions)
      TODO: Fix this by having one extra state pre-allocated
    */
    DBUG_RETURN(_ma_setup_live_state(info));
  }
  else
  {
    /*
      We come here in the following cases:
      - The table is a temporary table
      - It's a table which is crash safe but not yet versioned, for
      example a table with fulltext or rtree keys

      Set the current state to point to save_state so that the
      block_format code don't count the same record twice.
      Copy also the current state. This may have been wrong if the
      same file was used several times in the last statement
    */
    info->state=  info->state_start;
    *info->state= info->s->state.state;
  }

  /*
    Info->trn is set if this table is already handled and we are
    called from maria_versioning()
  */
  if (info->s->base.born_transactional && !info->trn)
  {
    /*
      Assume for now that this doesn't fail (It can only fail in
      out of memory conditions)
    */
    DBUG_RETURN(maria_create_trn_hook(info) != 0);
  }
  DBUG_RETURN(0);
}


void _ma_block_update_status(void *param __attribute__((unused)))
{
}

void _ma_block_restore_status(void *param __attribute__((unused)))
{
}


/**
  Check if should allow concurrent inserts

  @return
  @retval 0  ok to use concurrent inserts
  @retval 1  not ok
*/

my_bool _ma_block_check_status(void *param __attribute__((unused)))
{
  return (my_bool) 0;
}


/* Get status when transactional but not versioned */

my_bool _ma_block_start_trans_no_versioning(void* param)
{
  MARIA_HA *info=(MARIA_HA*) param;
  DBUG_ENTER("_ma_block_start_trans_no_versioning");
  DBUG_ASSERT(info->s->base.born_transactional && !info->s->lock_key_trees);

  info->state->changed= 0;              /* from _ma_reset_update_flag() */
  info->state=  info->state_start;
  *info->state= info->s->state.state;
  if (!info->trn)
  {
    /*
      Assume for now that this doesn't fail (It can only fail in
      out of memory conditions)
    */
    DBUG_RETURN(maria_create_trn_hook(info));
  }
  DBUG_RETURN(0);
}


/**
  Enable/disable versioning
*/

void maria_versioning(MARIA_HA *info, my_bool versioning)
{
  MARIA_SHARE *share= info->s;
  DBUG_ENTER("maria_versioning");

  /* For now, this is a hack */
  if (share->have_versioning)
  {
    enum thr_lock_type save_lock_type;
    share->lock_key_trees= versioning;
    /* Set up info->lock.type temporary for _ma_block_get_status() */
    save_lock_type= info->lock.type;
    info->lock.type= versioning ? TL_WRITE_CONCURRENT_INSERT : TL_WRITE;
    _ma_block_get_status((void*) info, versioning);
    info->lock.type= save_lock_type;
    if (versioning)
      info->state= &share->state.common;
    else
      info->state= &share->state.state;	/* Change global values by default */
    info->state_start= info->state;             /* Initial values */
  }
  DBUG_VOID_RETURN;
}


/**
   Update data_file_length to new length

   NOTES
     Only used by block records
*/

void _ma_set_share_data_file_length(MARIA_SHARE *share, ulonglong new_length)
{
  if (!share->internal_table)
    mysql_mutex_lock(&share->intern_lock);
  if (share->state.state.data_file_length < new_length)
  {
    share->state.state.data_file_length= new_length;
    if (new_length >= share->base.max_data_file_length)
    {
      /* Give an error on next insert */
      share->state.changed|= STATE_DATA_FILE_FULL;
    }
  }
  if (!share->internal_table)
  mysql_mutex_unlock(&share->intern_lock);
}


/**
   Copy state information that where updated while the table was used
   in not transactional mode
*/

void _ma_copy_nontrans_state_information(MARIA_HA *info)
{
  info->s->state.state.records=          info->state->records;
  info->s->state.state.checksum=         info->state->checksum;
}

/**
   Reset history
   This is only called during repair when we are the only one using the table.
*/

void _ma_reset_history(MARIA_SHARE *share)
{
  MARIA_STATE_HISTORY *history, *next;
  DBUG_ENTER("_ma_reset_history");

  share->state_history->trid= 0;          /* Visibly by all */
  share->state_history->state= share->state.state;
  history= share->state_history->next;
  share->state_history->next= 0;

  for (; history; history= next)
  {
    next= history->next;
    my_free(history);
  }
  DBUG_VOID_RETURN;
}


/****************************************************************************
  Virtual functions to check if row is visible
****************************************************************************/

/**
   Row is always visible
   This is for tables without concurrent insert
*/

my_bool _ma_row_visible_always(MARIA_HA *info __attribute__((unused)))
{
  return 1;
}


/**
   Row visibility for non transactional tables with concurrent insert

   @implementation
   When we got our table lock, we saved the current
   data_file_length. Concurrent inserts always go to the end of the
   file. So we can test if the found key references a new record.
*/

my_bool _ma_row_visible_non_transactional_table(MARIA_HA *info)
{
  return info->cur_row.lastpos < info->state->data_file_length;
}


/**
   Row visibility for transactional tables with versioning


   @TODO
   Add test if found key was marked deleted and it was deleted by
   us. In that case we should return 0
*/

my_bool _ma_row_visible_transactional_table(MARIA_HA *info)
{
  return trnman_can_read_from(info->trn, info->cur_row.trid);
}