summaryrefslogtreecommitdiffstats
path: root/storage/maria/ma_delete.c
blob: b24cfcc684270cd800cdaa01a78c8e24d7d4abb7 (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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
/* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
   Copyright (C) 2009-2010 Monty Program Ab

   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 */

#include "ma_fulltext.h"
#include "ma_rt_index.h"
#include "trnman.h"
#include "ma_key_recover.h"

static int d_search(MARIA_HA *info, MARIA_KEY *key, uint32 comp_flag,
                    MARIA_PAGE *page);
static int del(MARIA_HA *info, MARIA_KEY *key,
               MARIA_PAGE *anc_page, MARIA_PAGE *leaf_page,
	       uchar *keypos, my_off_t next_block, uchar *ret_key_buff);
static int underflow(MARIA_HA *info, MARIA_KEYDEF *keyinfo,
		     MARIA_PAGE *anc_page, MARIA_PAGE *leaf_page,
		     uchar *keypos);
static uint remove_key(MARIA_KEYDEF *keyinfo, uint page_flag, uint nod_flag,
                       uchar *keypos, uchar *lastkey, uchar *page_end,
		       my_off_t *next_block, MARIA_KEY_PARAM *s_temp);

/* @breif Remove a row from a MARIA table */

int maria_delete(MARIA_HA *info,const uchar *record)
{
  uint i;
  uchar *old_key;
  int save_errno;
  char lastpos[8];
  MARIA_SHARE *share= info->s;
  MARIA_KEYDEF *keyinfo;
  DBUG_ENTER("maria_delete");

  /* Test if record is in datafile */
  DBUG_EXECUTE_IF("maria_pretend_crashed_table_on_usage",
                  _ma_print_error(info, HA_ERR_CRASHED, 0);
                  DBUG_RETURN(my_errno= HA_ERR_CRASHED););
  DBUG_EXECUTE_IF("my_error_test_undefined_error",
                  _ma_print_error(info, INT_MAX, 0);
                  DBUG_RETURN(my_errno= INT_MAX););
  if (!(info->update & HA_STATE_AKTIV))
  {
    DBUG_RETURN(my_errno=HA_ERR_KEY_NOT_FOUND);	/* No database read */
  }
  if (share->options & HA_OPTION_READ_ONLY_DATA)
  {
    DBUG_RETURN(my_errno=EACCES);
  }
  if (_ma_readinfo(info,F_WRLCK,1))
    DBUG_RETURN(my_errno);
  if ((*share->compare_record)(info,record))
    goto err;				/* Error on read-check */

  if (_ma_mark_file_changed(share))
    goto err;

  /* Ensure we don't change the autoincrement value */
  info->last_auto_increment= ~(ulonglong) 0;
  /* Remove all keys from the index file */

  old_key= info->lastkey_buff2;

  for (i=0, keyinfo= share->keyinfo ; i < share->base.keys ; i++, keyinfo++)
  {
    if (maria_is_key_active(share->state.key_map, i))
    {
      keyinfo->version++;
      if (keyinfo->flag & HA_FULLTEXT)
      {
        if (_ma_ft_del(info, i, old_key, record, info->cur_row.lastpos))
          goto err;
      }
      else
      {
        MARIA_KEY key;
        if (keyinfo->ck_delete(info,
                               (*keyinfo->make_key)(info, &key, i, old_key,
                                                    record,
                                                    info->cur_row.lastpos,
                                                    info->cur_row.trid)))
          goto err;
      }
      /* The above changed info->lastkey2. Inform maria_rnext_same(). */
      info->update&= ~HA_STATE_RNEXT_SAME;
    }
  }

  if (share->calc_checksum)
  {
    /*
      We can't use the row based checksum as this doesn't have enough
      precision.
    */
    info->cur_row.checksum= (*share->calc_checksum)(info, record);
  }

  if ((*share->delete_record)(info, record))
    goto err;				/* Remove record from database */

  info->state->checksum-= info->cur_row.checksum;
  info->state->records--;
  info->update= HA_STATE_CHANGED+HA_STATE_DELETED+HA_STATE_ROW_CHANGED;
  info->row_changes++;
  share->state.changed|= (STATE_NOT_OPTIMIZED_ROWS | STATE_NOT_MOVABLE |
                          STATE_NOT_ZEROFILLED);
  info->state->changed=1;

  mi_sizestore(lastpos, info->cur_row.lastpos);
  _ma_writeinfo(info, WRITEINFO_UPDATE_KEYFILE);
  if (info->invalidator != 0)
  {
    DBUG_PRINT("info", ("invalidator... '%s' (delete)",
                        share->open_file_name.str));
    (*info->invalidator)(share->open_file_name.str);
    info->invalidator=0;
  }
  DBUG_RETURN(0);

err:
  save_errno= my_errno;
  DBUG_ASSERT(save_errno);
  if (!save_errno)
    save_errno= HA_ERR_INTERNAL_ERROR;          /* Should never happen */

  mi_sizestore(lastpos, info->cur_row.lastpos);
  (void) _ma_writeinfo(info,WRITEINFO_UPDATE_KEYFILE);
  info->update|=HA_STATE_WRITTEN;	/* Buffer changed */
  if (save_errno != HA_ERR_RECORD_CHANGED)
  {
    _ma_set_fatal_error(info, HA_ERR_CRASHED);
    save_errno= HA_ERR_CRASHED;
  }
  DBUG_RETURN(my_errno= save_errno);
} /* maria_delete */


/*
  Remove a key from the btree index

  TODO:
   Change ma_ck_real_delete() to use another buffer for changed keys instead
   of key->data. This would allows us to remove the copying of the key here.
*/

my_bool _ma_ck_delete(MARIA_HA *info, MARIA_KEY *key)
{
  MARIA_SHARE *share= info->s;
  int res;
  my_bool buff_alloced;
  LSN lsn= LSN_IMPOSSIBLE;
  my_off_t new_root= share->state.key_root[key->keyinfo->key_nr];
  uchar *key_buff, *save_key_data;
  MARIA_KEY org_key;
  DBUG_ENTER("_ma_ck_delete");

  LINT_INIT_STRUCT(org_key);

  alloc_on_stack(*info->stack_end_ptr, key_buff, buff_alloced,
                 key->keyinfo->max_store_length);
  if (!key_buff)
    DBUG_RETURN(1);

  save_key_data= key->data;
  if (share->now_transactional)
  {
    /* Save original value as the key may change */
    memcpy(key_buff, key->data, key->data_length + key->ref_length);
    org_key= *key;
    key->data= key_buff;
  }

  if ((res= _ma_ck_real_delete(info, key, &new_root)))
  {
    /* We have to mark the table crashed before unpin_all_pages() */
    maria_mark_crashed(info);
  }

  key->data= save_key_data;
  if (!res && share->now_transactional)
    res= _ma_write_undo_key_delete(info, &org_key, new_root, &lsn);
  else
  {
    share->state.key_root[key->keyinfo->key_nr]= new_root;
    _ma_fast_unlock_key_del(info);
  }
  _ma_unpin_all_pages_and_finalize_row(info, lsn);

  stack_alloc_free(key_buff, buff_alloced);
  DBUG_RETURN(res != 0);
} /* _ma_ck_delete */


my_bool _ma_ck_real_delete(register MARIA_HA *info, MARIA_KEY *key,
                           my_off_t *root)
{
  int error;
  my_bool result= 0, buff_alloced;
  my_off_t old_root;
  uchar *root_buff;
  MARIA_KEYDEF *keyinfo= key->keyinfo;
  MARIA_PAGE page;
  DBUG_ENTER("_ma_ck_real_delete");

  if ((old_root=*root) == HA_OFFSET_ERROR)
  {
    _ma_set_fatal_error(info, HA_ERR_CRASHED);
    DBUG_RETURN(1);
  }

  alloc_on_stack(*info->stack_end_ptr, root_buff, buff_alloced,
                 (keyinfo->block_length + keyinfo->max_store_length*2));
  if (!root_buff)
    DBUG_RETURN(1);

  DBUG_PRINT("info",("root_page: %lu",
                     (ulong) (old_root / keyinfo->block_length)));
  if (_ma_fetch_keypage(&page, info, keyinfo, old_root,
                        PAGECACHE_LOCK_WRITE, DFLT_INIT_HITS, root_buff, 0))
  {
    result= 1;
    goto err;
  }
  if ((error= d_search(info, key, (keyinfo->flag & HA_FULLTEXT ?
                                   SEARCH_FIND | SEARCH_UPDATE | SEARCH_INSERT:
                                   SEARCH_SAME),
                       &page)))
  {
    if (error < 0)
      result= 1;
    else if (error == 2)
    {
      DBUG_PRINT("test",("Enlarging of root when deleting"));
      if (_ma_enlarge_root(info, key, root))
        result= 1;
    }
    else /* error == 1 */
    {
      MARIA_SHARE *share= info->s;

      page_mark_changed(info, &page);

      if (page.size <= page.node + share->keypage_header + 1)
      {
        DBUG_ASSERT(page.size == page.node + share->keypage_header);
	if (page.node)
	  *root= _ma_kpos(page.node, root_buff +share->keypage_header +
                          page.node);
	else
	  *root=HA_OFFSET_ERROR;
	if (_ma_dispose(info, old_root, 0))
	  result= 1;
      }
      else if (_ma_write_keypage(&page, PAGECACHE_LOCK_LEFT_WRITELOCKED,
                                 DFLT_INIT_HITS))
        result= 1;
    }
  }
err:
  stack_alloc_free(root_buff, buff_alloced);
  DBUG_PRINT("exit",("Return: %d",result));
  DBUG_RETURN(result);
} /* _ma_ck_real_delete */


/**
   @brief Remove key below key root

   @param key  Key to delete.  Will contain new key if block was enlarged

   @return
   @retval 0   ok (anc_page is not changed)
   @retval 1   If data on page is too small; In this case anc_buff is not saved
   @retval 2   If data on page is too big
   @retval -1  On errors
*/

static int d_search(MARIA_HA *info, MARIA_KEY *key, uint32 comp_flag,
                    MARIA_PAGE *anc_page)
{
  int flag,ret_value,save_flag;
  uint nod_flag, page_flag;
  my_bool last_key, buff_alloced= 0, lastkey_alloced;
  uchar *leaf_buff=0, *keypos, *lastkey;
  MARIA_KEY_PARAM s_temp;
  MARIA_SHARE *share= info->s;
  MARIA_KEYDEF *keyinfo= key->keyinfo;
  MARIA_PAGE leaf_page;
  DBUG_ENTER("d_search");
  DBUG_DUMP("page", anc_page->buff, anc_page->size);

  alloc_on_stack(*info->stack_end_ptr, lastkey, lastkey_alloced,
                 keyinfo->max_store_length);
  if (!lastkey)
    DBUG_RETURN(1);

  flag=(*keyinfo->bin_search)(key, anc_page, comp_flag, &keypos, lastkey,
                              &last_key);
  if (flag == MARIA_FOUND_WRONG_KEY)
  {
    DBUG_PRINT("error",("Found wrong key"));
    goto err;
  }
  page_flag= anc_page->flag;
  nod_flag=  anc_page->node;

  if (!flag && (keyinfo->flag & HA_FULLTEXT))
  {
    uint off;
    int  subkeys;

    get_key_full_length_rdonly(off, lastkey);
    subkeys=ft_sintXkorr(lastkey+off);
    DBUG_ASSERT(info->ft1_to_ft2==0 || subkeys >=0);
    comp_flag=SEARCH_SAME;
    if (subkeys >= 0)
    {
      /* normal word, one-level tree structure */
      if (info->ft1_to_ft2)
      {
        /* we're in ft1->ft2 conversion mode. Saving key data */
        insert_dynamic(info->ft1_to_ft2, (lastkey+off));
      }
      else
      {
        /* we need exact match only if not in ft1->ft2 conversion mode */
        flag=(*keyinfo->bin_search)(key, anc_page, comp_flag, &keypos,
                                    lastkey, &last_key);
      }
      /* fall through to normal delete */
    }
    else
    {
      /* popular word. two-level tree. going down */
      uint tmp_key_length;
      my_off_t root;
      uchar *kpos=keypos;
      MARIA_KEY tmp_key;

      tmp_key.data=    lastkey;
      tmp_key.keyinfo= keyinfo;

      if (!(tmp_key_length=(*keyinfo->get_key)(&tmp_key, page_flag, nod_flag,
                                               &kpos)))
      {
        _ma_set_fatal_error(info, HA_ERR_CRASHED);
        goto err;
      }
      root= _ma_row_pos_from_key(&tmp_key);
      if (subkeys == -1)
      {
        /* the last entry in sub-tree */
        if (_ma_dispose(info, root, 1))
          goto err;
        /* fall through to normal delete */
      }
      else
      {
        MARIA_KEY word_key;
        keyinfo=&share->ft2_keyinfo;
        /* we'll modify key entry 'in vivo' */
        kpos-=keyinfo->keylength+nod_flag;
        get_key_full_length_rdonly(off, key->data);

        word_key.data=        key->data + off;
        word_key.keyinfo=     &share->ft2_keyinfo;
        word_key.data_length= HA_FT_WLEN;
        word_key.ref_length= 0;
        word_key.flag= 0;
        ret_value= _ma_ck_real_delete(info, &word_key, &root);
        _ma_dpointer(share, kpos+HA_FT_WLEN, root);
        subkeys++;
        ft_intXstore(kpos, subkeys);
        if (!ret_value)
        {
          page_mark_changed(info, anc_page);
          ret_value= _ma_write_keypage(anc_page,
                                       PAGECACHE_LOCK_LEFT_WRITELOCKED,
                                       DFLT_INIT_HITS);
        }
        goto end;
      }
    }
  }
  if (nod_flag)
  {
    /* Read left child page */
    leaf_page.pos= _ma_kpos(nod_flag,keypos);

    alloc_on_stack(*info->stack_end_ptr, leaf_buff, buff_alloced,
                   (keyinfo->block_length + keyinfo->max_store_length*2));
    if (!leaf_buff)
      goto err;

    if (_ma_fetch_keypage(&leaf_page, info,keyinfo, leaf_page.pos,
                          PAGECACHE_LOCK_WRITE, DFLT_INIT_HITS, leaf_buff,
                          0))
      goto err;
  }

  if (flag != 0)
  {
    if (!nod_flag)
    {
      /* This should newer happend */
      DBUG_PRINT("error",("Didn't find key"));
      _ma_set_fatal_error(info, HA_ERR_CRASHED);
      goto err;
    }
    save_flag=0;
    ret_value= d_search(info, key, comp_flag, &leaf_page);
  }
  else
  {						/* Found key */
    uint tmp;
    uint anc_buff_length= anc_page->size;
    uint anc_page_flag=   anc_page->flag;
    my_off_t next_block;

    if (!(tmp= remove_key(keyinfo, anc_page_flag, nod_flag, keypos, lastkey,
                          anc_page->buff + anc_buff_length,
                          &next_block, &s_temp)))
      goto err;

    page_mark_changed(info, anc_page);
    anc_buff_length-= tmp;
    anc_page->size= anc_buff_length;
    page_store_size(share, anc_page);

    /*
      Log initial changes on pages
      If there is an underflow, there will be more changes logged to the
      page
    */
    if (share->now_transactional &&
        _ma_log_delete(anc_page, s_temp.key_pos,
                       s_temp.changed_length, s_temp.move_length,
                       0, KEY_OP_DEBUG_LOG_DEL_CHANGE_1))
      goto err;

    if (!nod_flag)
    {						/* On leaf page */
      if (anc_buff_length <= (info->quick_mode ?
                              MARIA_MIN_KEYBLOCK_LENGTH :
                              (uint) keyinfo->underflow_block_length))
      {
        /* Page will be written by caller if we return 1 */
        ret_value= 1;
        goto end;
      }
      if (_ma_write_keypage(anc_page,
                            PAGECACHE_LOCK_LEFT_WRITELOCKED, DFLT_INIT_HITS))
	goto err;

      ret_value= 0;                             /* Return ok */
      goto end;
    }
    save_flag=1;                         /* Mark that anc_buff is changed */
    ret_value= del(info, key, anc_page, &leaf_page,
                   keypos, next_block, lastkey);
  }
  if (ret_value >0)
  {
    save_flag= 2;
    if (ret_value == 1)
      ret_value= underflow(info, keyinfo, anc_page, &leaf_page, keypos);
    else
    {
      /* This can only happen with variable length keys */
      MARIA_KEY last_key;
      DBUG_PRINT("test",("Enlarging of key when deleting"));

      last_key.data=    lastkey;
      last_key.keyinfo= keyinfo;
      if (!_ma_get_last_key(&last_key, anc_page, keypos))
	goto err;
      ret_value= _ma_insert(info, key, anc_page, keypos,
                            last_key.data,
                            (MARIA_PAGE*) 0, (uchar*) 0, (my_bool) 0);

      if (_ma_write_keypage(&leaf_page, PAGECACHE_LOCK_LEFT_WRITELOCKED,
                            DFLT_INIT_HITS))
        ret_value= -1;
    }
  }
  if (ret_value == 0 && anc_page->size > share->max_index_block_size)
  {
    /*
      parent buffer got too big ; We have to split the page.
      The | 2 is there to force write of anc page below
    */
    save_flag= 3;
    ret_value= _ma_split_page(info, key, anc_page,
                              share->max_index_block_size,
                              (uchar*) 0, 0, 0, lastkey, 0) | 2;
    DBUG_ASSERT(anc_page->org_size == anc_page->size);
  }
  if (save_flag && ret_value != 1)
  {
    page_mark_changed(info, anc_page);
    if (_ma_write_keypage(anc_page, PAGECACHE_LOCK_LEFT_WRITELOCKED,
                          DFLT_INIT_HITS))
      ret_value= -1;
  }
  else
  {
    DBUG_DUMP("page", anc_page->buff, anc_page->size);
  }

end:
  stack_alloc_free(leaf_buff, buff_alloced);
  stack_alloc_free(lastkey, lastkey_alloced);
  DBUG_PRINT("exit",("Return: %d",ret_value));
  DBUG_RETURN(ret_value);

err:
  stack_alloc_free(leaf_buff, buff_alloced);
  stack_alloc_free(lastkey, lastkey_alloced);
  DBUG_PRINT("exit",("Error: %d",my_errno));
  DBUG_RETURN (-1);
} /* d_search */


/**
   @brief Remove a key that has a page-reference

   @param info		 Maria handler
   @param key		 Buffer for key to be inserted at upper level
   @param anc_page	 Page address for page where deleted key was
   @param anc_buff       Page buffer (nod) where deleted key was
   @param leaf_page      Page address for nod before the deleted key
   @param leaf_buff      Buffer for leaf_page
   @param leaf_buff_link Pinned page link for leaf_buff
   @param keypos         Pos to where deleted key was on anc_buff
   @param next_block	 Page adress for nod after deleted key
   @param ret_key_buff	 Key before keypos in anc_buff

   @notes
      leaf_page must be written to disk if retval > 0
      anc_page  is not updated on disk. Caller should do this

   @return
   @retval < 0   Error
   @retval 0     OK.    leaf_buff is written to disk

   @retval 1     key contains key to upper level (from balance page)
                 leaf_buff has underflow
   @retval 2     key contains key to upper level (from split space)
*/

static int del(MARIA_HA *info, MARIA_KEY *key,
               MARIA_PAGE *anc_page, MARIA_PAGE *leaf_page,
	       uchar *keypos, my_off_t next_block, uchar *ret_key_buff)
{
  int ret_value,length;
  uint a_length, page_flag, nod_flag, leaf_length, new_leaf_length;
  uchar *keybuff,*endpos,*next_buff,*key_start, *prev_key;
  uchar *anc_buff;
  my_bool buff_alloced= 0, keybuff_alloced;
  MARIA_KEY_PARAM s_temp;
  MARIA_KEY tmp_key;
  MARIA_SHARE *share= info->s;
  MARIA_KEYDEF *keyinfo= key->keyinfo;
  MARIA_KEY ret_key;
  MARIA_PAGE next_page;
  DBUG_ENTER("del");
  DBUG_PRINT("enter",("leaf_page: %lu  keypos: %p",
                      (ulong) (leaf_page->pos / share->block_size),
		      keypos));
  DBUG_DUMP("leaf_buff", leaf_page->buff, leaf_page->size);

  alloc_on_stack(*info->stack_end_ptr, keybuff, keybuff_alloced,
                 keyinfo->max_store_length);
  if (!keybuff)
    DBUG_RETURN(1);

  page_flag=   leaf_page->flag;
  leaf_length= leaf_page->size;
  nod_flag=    leaf_page->node;

  endpos= leaf_page->buff + leaf_length;
  tmp_key.keyinfo= keyinfo;
  tmp_key.data=    keybuff;
  next_buff= 0;

  if (!(key_start= _ma_get_last_key(&tmp_key, leaf_page, endpos)))
    goto err;

  if (nod_flag)
  {
    next_page.pos= _ma_kpos(nod_flag,endpos);

    alloc_on_stack(*info->stack_end_ptr, next_buff, buff_alloced,
                   (keyinfo->block_length + keyinfo->max_store_length*2));
    if (!next_buff)
      goto err;

    if (_ma_fetch_keypage(&next_page, info, keyinfo, next_page.pos,
                          PAGECACHE_LOCK_WRITE, DFLT_INIT_HITS, next_buff, 0))
      ret_value= -1;
    else
    {
      DBUG_DUMP("next_page", next_page.buff, next_page.size);
      if ((ret_value= del(info, key, anc_page, &next_page,
                          keypos, next_block, ret_key_buff)) >0)
      {
        /* Get new length after key was deleted */
	endpos= leaf_page->buff+ leaf_page->size;
	if (ret_value == 1)
	{
          /* underflow writes "next_page" to disk */
	  ret_value= underflow(info, keyinfo, leaf_page, &next_page,
                               endpos);
          if (ret_value < 0)
            goto err;
	  if (leaf_page->size > share->max_index_block_size)
	  {
            DBUG_ASSERT(ret_value == 0);
	    ret_value= (_ma_split_page(info, key, leaf_page,
                                       share->max_index_block_size,
                                       (uchar*) 0, 0, 0,
                                       ret_key_buff, 0) | 2);
	  }
	}
	else
	{
          if (_ma_write_keypage(&next_page, PAGECACHE_LOCK_LEFT_WRITELOCKED,
                                DFLT_INIT_HITS))
            goto err;
	  DBUG_PRINT("test",("Inserting of key when deleting"));
	  if (!_ma_get_last_key(&tmp_key, leaf_page, endpos))
	    goto err;
	  ret_value= _ma_insert(info, key, leaf_page, endpos,
                                tmp_key.data, (MARIA_PAGE *) 0, (uchar*) 0,
                                0);
	}
      }
      page_mark_changed(info, leaf_page);
      /*
        If ret_value <> 0, then leaf_page underflowed and caller will have
        to handle underflow and write leaf_page to disk.
        We can't write it here, as if leaf_page is empty we get an assert
        in _ma_write_keypage.
      */
      if (ret_value == 0 && _ma_write_keypage(leaf_page,
                                              PAGECACHE_LOCK_LEFT_WRITELOCKED,
                                              DFLT_INIT_HITS))
	goto err;
    }
    stack_alloc_free(next_buff, buff_alloced);
    stack_alloc_free(keybuff, keybuff_alloced);
    DBUG_ASSERT(leaf_page->size <= share->max_index_block_size);
    DBUG_RETURN(ret_value);
  }

  /*
    Remove last key from leaf page
    Note that leaf_page page may only have had one key (can normally only
    happen in quick mode), in which ase it will now temporary have 0 keys
    on it. This will be corrected by the caller as we will return 0.
  */
  new_leaf_length= (uint) (key_start - leaf_page->buff);
  leaf_page->size= new_leaf_length;
  page_store_size(share, leaf_page);

  if (share->now_transactional &&
      _ma_log_suffix(leaf_page, leaf_length, new_leaf_length))
    goto err;

  page_mark_changed(info, leaf_page);           /* Safety */
  if (new_leaf_length <= (info->quick_mode ? MARIA_MIN_KEYBLOCK_LENGTH :
                          (uint) keyinfo->underflow_block_length))
  {
    /* Underflow, leaf_page will be written by caller */
    ret_value= 1;
  }
  else
  {
    ret_value= 0;
    if (_ma_write_keypage(leaf_page, PAGECACHE_LOCK_LEFT_WRITELOCKED,
                          DFLT_INIT_HITS))
      goto err;
  }

  /* Place last key in ancestor page on deleted key position */
  a_length= anc_page->size;
  anc_buff= anc_page->buff;
  endpos=   anc_buff + a_length;

  ret_key.keyinfo= keyinfo;
  ret_key.data=    ret_key_buff;

  prev_key= 0;
  if (keypos != anc_buff+share->keypage_header + share->base.key_reflength)
  {
    if (!_ma_get_last_key(&ret_key, anc_page, keypos))
      goto err;
    prev_key= ret_key.data;
  }
  length= (*keyinfo->pack_key)(&tmp_key, share->base.key_reflength,
                               keypos == endpos ? (uchar*) 0 : keypos,
                               prev_key, prev_key,
                               &s_temp);
  if (length > 0)
    bmove_upp(endpos+length,endpos,(uint) (endpos-keypos));
  else
    bmove(keypos,keypos-length, (int) (endpos-keypos)+length);
  (*keyinfo->store_key)(keyinfo,keypos,&s_temp);
  key_start= keypos;
  if (tmp_key.flag & (SEARCH_USER_KEY_HAS_TRANSID |
                      SEARCH_PAGE_KEY_HAS_TRANSID))
    _ma_mark_page_with_transid(share, anc_page);

  /* Save pointer to next leaf on parent page */
  if (!(*keyinfo->get_key)(&ret_key, page_flag, share->base.key_reflength,
                           &keypos))
    goto err;
  _ma_kpointer(info,keypos - share->base.key_reflength,next_block);
  anc_page->size= a_length + length;
  page_store_size(share, anc_page);

  if (share->now_transactional &&
      _ma_log_add(anc_page, a_length,
                  key_start, s_temp.changed_length, s_temp.move_length, 1,
                  KEY_OP_DEBUG_LOG_ADD_2))
    goto err;

  DBUG_ASSERT(leaf_page->size <= share->max_index_block_size);
  stack_alloc_free(next_buff, buff_alloced);
  stack_alloc_free(keybuff, keybuff_alloced);
  DBUG_RETURN(new_leaf_length <=
              (info->quick_mode ? MARIA_MIN_KEYBLOCK_LENGTH :
               (uint) keyinfo->underflow_block_length));

err:
  stack_alloc_free(next_buff, buff_alloced);
  stack_alloc_free(keybuff, keybuff_alloced);
  DBUG_RETURN(-1);
} /* del */


/**
   @brief Balances adjacent pages if underflow occours

   @fn    underflow()
   @param anc_buff        Anchestor page data
   @param leaf_page       Leaf page (page that underflowed)
   @param leaf_page_link  Pointer to pin information about leaf page
   @param keypos          Position after current key in anc_buff

   @note
     This function writes redo entries for all changes
     leaf_page is saved to disk
     Caller must save anc_buff

     For the algoritm to work, we have to ensure for packed keys that
     key_length + (underflow_length + max_block_length + key_length) / 2
     <= block_length.
     From which follows that underflow_length <= block_length - key_length *3
     For not packed keys we have:
     (underflow_length + max_block_length + key_length) / 2 <= block_length
     From which follows that underflow_length < block_length - key_length
     This is ensured by setting of underflow_block_length.

   @return
   @retval  0  ok
   @retval  1  ok, but anc_page did underflow
   @retval -1  error
 */

static int underflow(MARIA_HA *info, MARIA_KEYDEF *keyinfo,
		     MARIA_PAGE *anc_page, MARIA_PAGE *leaf_page,
		     uchar *keypos)
{
  int t_length;
  uint anc_length,buff_length,leaf_length,p_length,s_length,nod_flag;
  uint next_buff_length, new_buff_length, key_reflength;
  uint unchanged_leaf_length, new_leaf_length, new_anc_length;
  uint anc_page_flag, page_flag;
  uchar *anc_key_buff, *leaf_key_buff;
  uchar *endpos, *next_keypos, *anc_pos, *half_pos, *prev_key;
  uchar *anc_buff, *leaf_buff;
  uchar *after_key, *anc_end_pos;
  MARIA_KEY_PARAM key_deleted, key_inserted;
  MARIA_SHARE *share= info->s;
  my_bool first_key, buff_alloced;
  MARIA_KEY tmp_key, anc_key, leaf_key;
  MARIA_PAGE next_page;
  DBUG_ENTER("underflow");
  DBUG_PRINT("enter",("leaf_page: %lu  keypos: %p",
                      (ulong) (leaf_page->pos / share->block_size),
		      keypos));
  DBUG_DUMP("anc_buff", anc_page->buff,  anc_page->size);
  DBUG_DUMP("leaf_buff", leaf_page->buff, leaf_page->size);

  alloc_on_stack(*info->stack_end_ptr, anc_key_buff, buff_alloced,
                 keyinfo->max_store_length*2);
  if (!anc_key_buff)
    DBUG_RETURN(1);

  leaf_key_buff= anc_key_buff+ keyinfo->max_store_length;

  anc_page_flag= anc_page->flag;
  anc_buff= anc_page->buff;
  leaf_buff= leaf_page->buff;
  info->keyread_buff_used=1;
  next_keypos=keypos;
  nod_flag= leaf_page->node;
  p_length= nod_flag+share->keypage_header;
  anc_length= anc_page->size;
  leaf_length= leaf_page->size;
  key_reflength= share->base.key_reflength;
  if (share->keyinfo+info->lastinx == keyinfo)
    info->page_changed=1;
  first_key= keypos == anc_buff + share->keypage_header + key_reflength;

  tmp_key.data=  info->buff;
  anc_key.data=  anc_key_buff;
  leaf_key.data= leaf_key_buff;
  tmp_key.keyinfo= leaf_key.keyinfo= anc_key.keyinfo= keyinfo;

  if ((keypos < anc_buff + anc_length && (info->state->records & 1)) ||
      first_key)
  {
    uint tmp_length;
    uint next_page_flag;
    /* Use page right of anc-page */
    DBUG_PRINT("test",("use right page"));

    /*
      Calculate position after the current key. Note that keydata itself is
      not used
    */
    if (keyinfo->flag & HA_BINARY_PACK_KEY)
    {
      if (!(next_keypos= _ma_get_key(&tmp_key, anc_page, keypos)))
	goto err;
    }
    else
    {
      /* Avoid length error check if packed key */
      tmp_key.data[0]= tmp_key.data[1]= 0;
      /* Got to end of found key */
      if (!(*keyinfo->get_key)(&tmp_key, anc_page_flag, key_reflength,
                               &next_keypos))
        goto err;
    }
    next_page.pos= _ma_kpos(key_reflength, next_keypos);
    if (_ma_fetch_keypage(&next_page, info, keyinfo, next_page.pos,
                          PAGECACHE_LOCK_WRITE, DFLT_INIT_HITS, info->buff, 0))
      goto err;
    next_buff_length= next_page.size;
    next_page_flag=   next_page.flag;
    DBUG_DUMP("next", next_page.buff, next_page.size);

    /* find keys to make a big key-page */
    bmove(next_keypos-key_reflength, next_page.buff + share->keypage_header,
          key_reflength);

    if (!_ma_get_last_key(&anc_key, anc_page, next_keypos) ||
	!_ma_get_last_key(&leaf_key, leaf_page, leaf_buff+leaf_length))
      goto err;

    /* merge pages and put parting key from anc_page between */
    prev_key= (leaf_length == p_length ? (uchar*) 0 : leaf_key.data);
    t_length= (*keyinfo->pack_key)(&anc_key, nod_flag, next_page.buff+p_length,
                                   prev_key, prev_key, &key_inserted);
    tmp_length= next_buff_length - p_length;
    endpos= next_page.buff + tmp_length + leaf_length + t_length;
    /* next_page.buff will always be larger than before !*/
    bmove_upp(endpos, next_page.buff + next_buff_length, tmp_length);
    memcpy(next_page.buff, leaf_buff,(size_t) leaf_length);
    (*keyinfo->store_key)(keyinfo, next_page.buff+leaf_length, &key_inserted);
    buff_length= (uint) (endpos - next_page.buff);

    /* Set page flag from combination of both key pages and parting key */
    page_flag= next_page_flag | leaf_page->flag;
    if (anc_key.flag & (SEARCH_USER_KEY_HAS_TRANSID |
                        SEARCH_PAGE_KEY_HAS_TRANSID))
      page_flag|= KEYPAGE_FLAG_HAS_TRANSID;

    next_page.size= buff_length;
    next_page.flag= page_flag;
    page_store_info(share, &next_page);

    /* remove key from anc_page */
    if (!(s_length=remove_key(keyinfo, anc_page_flag, key_reflength, keypos,
                              anc_key_buff, anc_buff+anc_length,
                              (my_off_t *) 0, &key_deleted)))
      goto err;

    new_anc_length= anc_length - s_length;
    anc_page->size= new_anc_length;
    page_store_size(share, anc_page);

    if (buff_length <= share->max_index_block_size)
    {
      /* All keys fitted into one page */
      page_mark_changed(info, &next_page);
      if (_ma_dispose(info, next_page.pos, 0))
       goto err;

      memcpy(leaf_buff, next_page.buff, (size_t) buff_length);
      leaf_page->size= next_page.size;
      leaf_page->flag= next_page.flag;

      if (share->now_transactional)
      {
        /*
          Log changes to parent page. Note that this page may have been
          temporarily bigger than block_size.
         */
        if (_ma_log_delete(anc_page, key_deleted.key_pos,
                           key_deleted.changed_length,
                           key_deleted.move_length,
                           anc_length - anc_page->org_size,
                           KEY_OP_DEBUG_LOG_DEL_CHANGE_2))
          goto err;
        /*
          Log changes to leaf page. Data for leaf page is in leaf_buff
          which contains original leaf_buff, parting key and next_buff
        */
        if (_ma_log_suffix(leaf_page, leaf_length, buff_length))
          goto err;
      }
    }
    else
    {
      /*
        Balancing didn't free a page, so we have to split 'buff' into two
        pages:
        - Find key in middle of buffer
        - Store everything before key in 'leaf_page'
        - Pack key into anc_page at position of deleted key
          Note that anc_page may overflow! (is handled by caller)
        - Store remaining keys in next_page (buff)
      */
      MARIA_KEY_PARAM anc_key_inserted;

      anc_end_pos= anc_buff + new_anc_length;

      DBUG_PRINT("test",("anc_buff:%p anc_end_pos:%p",
                         anc_buff, anc_end_pos));

      if (!first_key && !_ma_get_last_key(&anc_key, anc_page, keypos))
	goto err;
      if (!(half_pos= _ma_find_half_pos(&leaf_key, &next_page, &after_key)))
	goto err;
      new_leaf_length= (uint) (half_pos - next_page.buff);
      memcpy(leaf_buff, next_page.buff, (size_t) new_leaf_length);

      leaf_page->size= new_leaf_length;
      leaf_page->flag= page_flag;
      page_store_info(share, leaf_page);

      /* Correct new keypointer to leaf_page */
      half_pos=after_key;
      _ma_kpointer(info,
                   leaf_key.data + leaf_key.data_length + leaf_key.ref_length,
                   next_page.pos);

      /* Save key in anc_page */
      prev_key= (first_key  ? (uchar*) 0 : anc_key.data);
      t_length= (*keyinfo->pack_key)(&leaf_key, key_reflength,
                                     (keypos == anc_end_pos ? (uchar*) 0 :
                                      keypos),
                                     prev_key, prev_key, &anc_key_inserted);
      if (t_length >= 0)
	bmove_upp(anc_end_pos+t_length, anc_end_pos,
                  (uint) (anc_end_pos - keypos));
      else
	bmove(keypos,keypos-t_length,(uint) (anc_end_pos-keypos)+t_length);
      (*keyinfo->store_key)(keyinfo,keypos, &anc_key_inserted);
      new_anc_length+= t_length;
      anc_page->size= new_anc_length;
      page_store_size(share, anc_page);

      if (leaf_key.flag & (SEARCH_USER_KEY_HAS_TRANSID |
                           SEARCH_PAGE_KEY_HAS_TRANSID))
        _ma_mark_page_with_transid(share, anc_page);

      /* Store key first in new page */
      if (nod_flag)
	bmove(next_page.buff + share->keypage_header, half_pos-nod_flag,
              (size_t) nod_flag);
      if (!(*keyinfo->get_key)(&leaf_key, page_flag, nod_flag, &half_pos))
	goto err;
      t_length=(int) (*keyinfo->pack_key)(&leaf_key, nod_flag, (uchar*) 0,
					  (uchar*) 0, (uchar*) 0,
					  &key_inserted);
      /* t_length will always be > 0 for a new page !*/
      tmp_length= (uint) ((next_page.buff + buff_length) - half_pos);
      bmove(next_page.buff + p_length + t_length, half_pos, tmp_length);
      (*keyinfo->store_key)(keyinfo, next_page.buff + p_length, &key_inserted);
      new_buff_length= tmp_length + t_length + p_length;
      next_page.size= new_buff_length;
      page_store_size(share, &next_page);
      /* keypage flag is already up to date */

      if (share->now_transactional)
      {
        /*
          Log changes to parent page
          This has one key deleted from it and one key inserted to it at
          keypos

          ma_log_add ensures that we don't log changes that is outside of
          key block size, as the REDO code can't handle that
        */
        if (_ma_log_add(anc_page, anc_length, keypos,
                        anc_key_inserted.move_length +
                        MY_MAX(anc_key_inserted.changed_length -
                            anc_key_inserted.move_length,
                            key_deleted.changed_length),
                        anc_key_inserted.move_length -
                        key_deleted.move_length, 1,
                        KEY_OP_DEBUG_LOG_ADD_3))
          goto err;

        /*
          Log changes to leaf page.
          This contains original data with new data added at end
        */
        DBUG_ASSERT(leaf_length <= new_leaf_length);
        if (_ma_log_suffix(leaf_page, leaf_length, new_leaf_length))
          goto err;
        /*
          Log changes to next page

          This contains original data with some prefix data deleted and
          some compressed data at start possible extended

          Data in buff was originally:
          org_leaf_buff     [leaf_length]
          separator_key     [buff_key_inserted.move_length]
          next_key_changes  [buff_key_inserted.changed_length -move_length]
          next_page_data    [next_buff_length - p_length -
                            (buff_key_inserted.changed_length -move_length)]

          After changes it's now:
          unpacked_key      [key_inserted.changed_length]
          next_suffix       [next_buff_length - key_inserted.changed_length]

        */
        DBUG_ASSERT(new_buff_length <= next_buff_length);
        if (_ma_log_prefix(&next_page, key_inserted.changed_length,
                           (int) (new_buff_length - next_buff_length),
                           KEY_OP_DEBUG_LOG_PREFIX_1))
          goto err;
      }
      page_mark_changed(info, &next_page);
      if (_ma_write_keypage(&next_page,
                            PAGECACHE_LOCK_LEFT_WRITELOCKED, DFLT_INIT_HITS))
	goto err;
    }

    page_mark_changed(info, leaf_page);
    if (_ma_write_keypage(leaf_page,
                          PAGECACHE_LOCK_LEFT_WRITELOCKED, DFLT_INIT_HITS))
      goto err;
    stack_alloc_free(anc_key_buff, buff_alloced);
    DBUG_RETURN(new_anc_length <=
                ((info->quick_mode ? MARIA_MIN_KEYBLOCK_LENGTH :
                  (uint) keyinfo->underflow_block_length)));
  }

  DBUG_PRINT("test",("use left page"));

  keypos= _ma_get_last_key(&anc_key, anc_page, keypos);
  if (!keypos)
    goto err;
  next_page.pos= _ma_kpos(key_reflength,keypos);
  if (_ma_fetch_keypage(&next_page, info, keyinfo, next_page.pos,
                        PAGECACHE_LOCK_WRITE, DFLT_INIT_HITS, info->buff, 0))
    goto err;
  buff_length= next_page.size;
  endpos= next_page.buff + buff_length;
  DBUG_DUMP("prev", next_page.buff, next_page.size);

  /* find keys to make a big key-page */
  bmove(next_keypos - key_reflength, leaf_buff + share->keypage_header,
        key_reflength);
  next_keypos=keypos;
  if (!(*keyinfo->get_key)(&anc_key, anc_page_flag, key_reflength,
                           &next_keypos))
    goto err;
  if (!_ma_get_last_key(&leaf_key, &next_page, endpos))
    goto err;

  /* merge pages and put parting key from anc_page between */
  prev_key= (leaf_length == p_length ? (uchar*) 0 : leaf_key.data);
  t_length=(*keyinfo->pack_key)(&anc_key, nod_flag,
				(leaf_length == p_length ?
                                 (uchar*) 0 : leaf_buff+p_length),
				prev_key, prev_key,
				&key_inserted);
  if (t_length >= 0)
    bmove(endpos+t_length, leaf_buff+p_length,
          (size_t) (leaf_length-p_length));
  else						/* We gained space */
    bmove(endpos,leaf_buff+((int) p_length-t_length),
	  (size_t) (leaf_length-p_length+t_length));
  (*keyinfo->store_key)(keyinfo,endpos, &key_inserted);

  /* Remember for logging how many bytes of leaf_buff that are not changed */
  DBUG_ASSERT((int) key_inserted.changed_length >= key_inserted.move_length);
  unchanged_leaf_length= (leaf_length - p_length -
                          (key_inserted.changed_length -
                           key_inserted.move_length));

  new_buff_length= buff_length + leaf_length - p_length + t_length;

#ifdef EXTRA_DEBUG
  /* Ensure that unchanged_leaf_length is correct */
  DBUG_ASSERT(bcmp(next_page.buff + new_buff_length - unchanged_leaf_length,
                   leaf_buff + leaf_length - unchanged_leaf_length,
                   unchanged_leaf_length) == 0);
#endif

  page_flag= next_page.flag | leaf_page->flag;
  if (anc_key.flag & (SEARCH_USER_KEY_HAS_TRANSID |
                       SEARCH_PAGE_KEY_HAS_TRANSID))
    page_flag|= KEYPAGE_FLAG_HAS_TRANSID;

  next_page.size= new_buff_length;
  next_page.flag= page_flag;
  page_store_info(share, &next_page);

  /* remove key from anc_page */
  if (!(s_length= remove_key(keyinfo, anc_page_flag, key_reflength, keypos,
                             anc_key_buff,
                             anc_buff+anc_length, (my_off_t *) 0,
                             &key_deleted)))
    goto err;

  new_anc_length= anc_length - s_length;
  anc_page->size= new_anc_length;
  page_store_size(share, anc_page);

  if (new_buff_length <= share->max_index_block_size)
  {
    /* All keys fitted into one page */
    page_mark_changed(info, leaf_page);
    if (_ma_dispose(info, leaf_page->pos, 0))
      goto err;

    if (share->now_transactional)
    {
      /*
        Log changes to parent page. Note that this page may have been
        temporarily bigger than block_size.
      */
      if (_ma_log_delete(anc_page, key_deleted.key_pos,
                         key_deleted.changed_length, key_deleted.move_length,
                         anc_length - anc_page->org_size,
                         KEY_OP_DEBUG_LOG_DEL_CHANGE_3))
        goto err;
      /*
        Log changes to next page. Data for leaf page is in buff
        that contains original leaf_buff, parting key and next_buff
      */
      if (_ma_log_suffix(&next_page, buff_length, new_buff_length))
        goto err;
    }
  }
  else
  {
    /*
      Balancing didn't free a page, so we have to split 'next_page' into two
      pages
      - Find key in middle of buffer (buff)
      - Pack key at half_buff into anc_page at position of deleted key
        Note that anc_page may overflow! (is handled by caller)
      - Move everything after middlekey to 'leaf_buff'
      - Shorten buff at 'endpos'
    */
    MARIA_KEY_PARAM anc_key_inserted;
    size_t tmp_length;

    if (keypos == anc_buff + share->keypage_header + key_reflength)
      anc_pos= 0;				/* First key */
    else
    {
      if (!_ma_get_last_key(&anc_key, anc_page, keypos))
        goto err;
      anc_pos= anc_key.data;
    }
    if (!(endpos= _ma_find_half_pos(&leaf_key, &next_page, &half_pos)))
      goto err;

    /* Correct new keypointer to leaf_page */
    _ma_kpointer(info,leaf_key.data + leaf_key.data_length +
                 leaf_key.ref_length, leaf_page->pos);

    /* Save parting key found by _ma_find_half_pos() in anc_page */
    DBUG_DUMP("anc_buff", anc_buff, new_anc_length);
    DBUG_DUMP_KEY("key_to_anc", &leaf_key);
    anc_end_pos= anc_buff + new_anc_length;
    t_length=(*keyinfo->pack_key)(&leaf_key, key_reflength,
				  keypos == anc_end_pos ? (uchar*) 0
				  : keypos,
				  anc_pos, anc_pos,
				  &anc_key_inserted);
    if (t_length >= 0)
      bmove_upp(anc_end_pos+t_length, anc_end_pos,
                (uint) (anc_end_pos-keypos));
    else
      bmove(keypos,keypos-t_length,(uint) (anc_end_pos-keypos)+t_length);
    (*keyinfo->store_key)(keyinfo,keypos, &anc_key_inserted);
    new_anc_length+= t_length;
    anc_page->size= new_anc_length;
    page_store_size(share, anc_page);

    if (leaf_key.flag & (SEARCH_USER_KEY_HAS_TRANSID |
                         SEARCH_PAGE_KEY_HAS_TRANSID))
      _ma_mark_page_with_transid(share, anc_page);

    /* Store first key on new page */
    if (nod_flag)
      bmove(leaf_buff + share->keypage_header, half_pos-nod_flag,
            (size_t) nod_flag);
    if (!(*keyinfo->get_key)(&leaf_key, page_flag, nod_flag, &half_pos))
      goto err;
    DBUG_DUMP_KEY("key_to_leaf", &leaf_key);
    t_length=(*keyinfo->pack_key)(&leaf_key, nod_flag, (uchar*) 0,
				  (uchar*) 0, (uchar*) 0, &key_inserted);
    /* t_length will always be > 0 for a new page !*/
    tmp_length= (size_t) ((next_page.buff + new_buff_length) - half_pos);
    DBUG_PRINT("info",("t_length: %d  length: %d",t_length, (int) tmp_length));
    bmove(leaf_buff+p_length+t_length, half_pos, tmp_length);
    (*keyinfo->store_key)(keyinfo,leaf_buff+p_length, &key_inserted);
    new_leaf_length= (uint)(tmp_length + t_length + p_length);
    DBUG_ASSERT(new_leaf_length <= share->max_index_block_size);

    leaf_page->size= new_leaf_length;
    leaf_page->flag= page_flag;
    page_store_info(share, leaf_page);

    new_buff_length= (uint) (endpos - next_page.buff);
    next_page.size= new_buff_length;
    page_store_size(share, &next_page);

    if (share->now_transactional)
    {
      /*
        Log changes to parent page
        This has one key deleted from it and one key inserted to it at
        keypos

        ma_log_add() ensures that we don't log changes that is outside of
        key block size, as the REDO code can't handle that
      */
      if (_ma_log_add(anc_page, anc_length, keypos,
                      anc_key_inserted.move_length +
                      MY_MAX(anc_key_inserted.changed_length -
                          anc_key_inserted.move_length,
                          key_deleted.changed_length),
                      anc_key_inserted.move_length -
                      key_deleted.move_length, 1,KEY_OP_DEBUG_LOG_ADD_4))
        goto err;

      /*
        Log changes to leaf page.
        This contains original data with new data added first
      */
      DBUG_ASSERT(leaf_length <= new_leaf_length);
      DBUG_ASSERT(new_leaf_length >= unchanged_leaf_length);
      if (_ma_log_prefix(leaf_page, new_leaf_length - unchanged_leaf_length,
                         (int) (new_leaf_length - leaf_length),
                         KEY_OP_DEBUG_LOG_PREFIX_2))
        goto err;
      /*
        Log changes to next page
        This contains original data with some suffix data deleted
      */
      DBUG_ASSERT(new_buff_length <= buff_length);
      if (_ma_log_suffix(&next_page, buff_length, new_buff_length))
        goto err;
    }

    page_mark_changed(info, leaf_page);
    if (_ma_write_keypage(leaf_page,
                          PAGECACHE_LOCK_LEFT_WRITELOCKED, DFLT_INIT_HITS))
      goto err;
  }
  page_mark_changed(info, &next_page);
  if (_ma_write_keypage(&next_page,
                        PAGECACHE_LOCK_LEFT_WRITELOCKED, DFLT_INIT_HITS))
    goto err;

  stack_alloc_free(anc_key_buff, buff_alloced);
  DBUG_RETURN(new_anc_length <=
              ((info->quick_mode ? MARIA_MIN_KEYBLOCK_LENGTH :
                (uint) keyinfo->underflow_block_length)));

err:
  stack_alloc_free(anc_key_buff, buff_alloced);
  DBUG_RETURN(-1);
} /* underflow */


/**
  @brief Remove a key from page

  @fn remove_key()
    keyinfo	          Key handle
    nod_flag              Length of node ptr
    keypos	          Where on page key starts
    lastkey	          Buffer for storing keys to be removed
    page_end	          Pointer to end of page
    next_block	          If <> 0 and node-page, this is set to address of
    		          next page
    s_temp	          Information about what changes was done one the page:
    s_temp.key_pos        Start of key
    s_temp.move_length    Number of bytes removed at keypos
    s_temp.changed_length Number of bytes changed at keypos

  @todo
    The current code doesn't handle the case that the next key may be
    packed better against the previous key if there is a case difference

  @return
  @retval 0  error
  @retval #  How many chars was removed
*/

static uint remove_key(MARIA_KEYDEF *keyinfo, uint page_flag, uint nod_flag,
		       uchar *keypos, uchar *lastkey,
		       uchar *page_end, my_off_t *next_block,
                       MARIA_KEY_PARAM *s_temp)
{
  int s_length;
  uchar *start;
  DBUG_ENTER("remove_key");
  DBUG_PRINT("enter", ("keypos:%p page_end: %p",
                       keypos, page_end));

  start= s_temp->key_pos= keypos;
  s_temp->changed_length= 0;
  if (!(keyinfo->flag &
	(HA_PACK_KEY | HA_SPACE_PACK_USED | HA_VAR_LENGTH_KEY |
	 HA_BINARY_PACK_KEY)) &&
      !(page_flag & KEYPAGE_FLAG_HAS_TRANSID))
  {
    /* Static length key */
    s_length=(int) (keyinfo->keylength+nod_flag);
    if (next_block && nod_flag)
      *next_block= _ma_kpos(nod_flag,keypos+s_length);
  }
  else
  {
    /* Let keypos point at next key */
    MARIA_KEY key;

    /* Calculate length of key */
    key.keyinfo= keyinfo;
    key.data=    lastkey;
    if (!(*keyinfo->get_key)(&key, page_flag, nod_flag, &keypos))
      DBUG_RETURN(0);				/* Error */

    if (next_block && nod_flag)
      *next_block= _ma_kpos(nod_flag,keypos);
    s_length=(int) (keypos-start);
    if (keypos != page_end)
    {
      if (keyinfo->flag & HA_BINARY_PACK_KEY)
      {
	uchar *old_key= start;
	uint next_length,prev_length,prev_pack_length;

        /* keypos points here on start of next key */
	get_key_length(next_length,keypos);
	get_key_pack_length(prev_length,prev_pack_length,old_key);
	if (next_length > prev_length)
	{
          uint diff= (next_length-prev_length);
	  /* We have to copy data from the current key to the next key */
	  keypos-= diff + prev_pack_length;
	  store_key_length(keypos, prev_length);
          bmove(keypos + prev_pack_length, lastkey + prev_length, diff);
	  s_length=(int) (keypos-start);
          s_temp->changed_length= diff + prev_pack_length;
	}
      }
      else
      {
	/* Check if a variable length first key part */
	if ((keyinfo->seg->flag & HA_PACK_KEY) && *keypos & 128)
	{
	  /* Next key is packed against the current one */
	  uint next_length,prev_length,prev_pack_length,lastkey_length,
	    rest_length;
	  if (keyinfo->seg[0].length >= 127)
	  {
	    if (!(prev_length=mi_uint2korr(start) & 32767))
	      goto end;
	    next_length=mi_uint2korr(keypos) & 32767;
	    keypos+=2;
	    prev_pack_length=2;
	  }
	  else
	  {
	    if (!(prev_length= *start & 127))
	      goto end;				/* Same key as previous*/
	    next_length= *keypos & 127;
	    keypos++;
	    prev_pack_length=1;
	  }
	  if (!(*start & 128))
	    prev_length=0;			/* prev key not packed */
	  if (keyinfo->seg[0].flag & HA_NULL_PART)
	    lastkey++;				/* Skip null marker */
	  get_key_length(lastkey_length,lastkey);
	  if (!next_length)			/* Same key after */
	  {
	    next_length=lastkey_length;
	    rest_length=0;
	  }
	  else
	    get_key_length(rest_length,keypos);

	  if (next_length >= prev_length)
	  {
            /* Next key is based on deleted key */
            uint pack_length;
            uint diff= (next_length-prev_length);

            /* keypos points to data of next key (after key length) */
	    bmove(keypos - diff, lastkey + prev_length, diff);
	    rest_length+= diff;
	    pack_length= prev_length ? get_pack_length(rest_length): 0;
	    keypos-= diff + pack_length + prev_pack_length;
	    s_length=(int) (keypos-start);
	    if (prev_length)			/* Pack against prev key */
	    {
	      *keypos++= start[0];
	      if (prev_pack_length == 2)
		*keypos++= start[1];
	      store_key_length(keypos,rest_length);
	    }
	    else
	    {
	      /* Next key is not packed anymore */
	      if (keyinfo->seg[0].flag & HA_NULL_PART)
	      {
		rest_length++;			/* Mark not null */
	      }
	      if (prev_pack_length == 2)
	      {
		mi_int2store(keypos,rest_length);
	      }
	      else
		*keypos= rest_length;
	    }
            s_temp->changed_length= diff + pack_length + prev_pack_length;
	  }
	}
      }
    }
  }
  end:
  bmove(start, start+s_length, (uint) (page_end-start-s_length));
  s_temp->move_length= s_length;
  DBUG_RETURN((uint) s_length);
} /* remove_key */


/****************************************************************************
  Logging of redos
****************************************************************************/

/**
   @brief
   log entry where some parts are deleted and some things are changed
   and some data could be added last.

   @fn _ma_log_delete()
   @param info		  Maria handler
   @param page	          Pageaddress for changed page
   @param buff		  Page buffer
   @param key_pos         Start of change area
   @param changed_length  How many bytes where changed at key_pos
   @param move_length     How many bytes where deleted at key_pos
   @param append_length	  Length of data added last
		          This is taken from end of ma_page->buff

   This is mainly used when a key is deleted. The append happens
   when we delete a key from a page with data > block_size kept in
   memory and we have to add back the data that was stored > block_size
*/

my_bool _ma_log_delete(MARIA_PAGE *ma_page, const uchar *key_pos,
                       uint changed_length, uint move_length,
                       uint append_length __attribute__((unused)),
                       enum en_key_debug debug_marker __attribute__((unused)))
{
  LSN lsn;
  uchar log_data[FILEID_STORE_SIZE + PAGE_STORE_SIZE + 2 + 5+ 2 + 3 + 3 + 6 + 3 + 7];
  uchar *log_pos;
  LEX_CUSTRING log_array[TRANSLOG_INTERNAL_PARTS + 7];
  uint translog_parts, current_size, extra_length;
  uint offset= (uint) (key_pos - ma_page->buff);
  MARIA_HA *info= ma_page->info;
  MARIA_SHARE *share= info->s;
  my_off_t page= ma_page->pos / share->block_size;
  DBUG_ENTER("_ma_log_delete");
  DBUG_PRINT("enter", ("page: %lu  offset: %u  changed_length: %u  move_length: %u  append_length: %u  page_size: %u",
                       (ulong) page, offset, changed_length, move_length,
                       append_length, ma_page->size));
  DBUG_ASSERT(share->now_transactional && move_length);
  DBUG_ASSERT(offset + changed_length <= ma_page->size);
  DBUG_ASSERT(ma_page->org_size - move_length + append_length == ma_page->size);
  DBUG_ASSERT(move_length <= ma_page->org_size - share->keypage_header);

  /* Store address of new root page */
  page_store(log_data + FILEID_STORE_SIZE, page);
  log_pos= log_data+ FILEID_STORE_SIZE + PAGE_STORE_SIZE;
  current_size= ma_page->org_size;

#ifdef EXTRA_DEBUG_KEY_CHANGES
  *log_pos++= KEY_OP_DEBUG;
  *log_pos++= debug_marker;

  *log_pos++= KEY_OP_DEBUG_2;
  int2store(log_pos,   ma_page->org_size);
  int2store(log_pos+2, ma_page->size);
  log_pos+=4;
#endif

  /* Store keypage_flag */
  *log_pos++= KEY_OP_SET_PAGEFLAG;
  *log_pos++= _ma_get_keypage_flag(info->s, ma_page->buff);

  log_pos[0]= KEY_OP_OFFSET;
  int2store(log_pos+1, offset);
  log_pos+= 3;
  translog_parts= TRANSLOG_INTERNAL_PARTS + 1;
  extra_length= 0;

  if (changed_length)
  {
    if (offset + changed_length >= share->max_index_block_size)
    {
      changed_length= share->max_index_block_size - offset;
      move_length= 0;                           /* Nothing to move */
      current_size= share->max_index_block_size;
    }

    log_pos[0]= KEY_OP_CHANGE;
    int2store(log_pos+1, changed_length);
    log_pos+= 3;
    log_array[translog_parts].str=    ma_page->buff + offset;
    log_array[translog_parts].length= changed_length;
    translog_parts++;

    /* We only have to move things after offset+changed_length */
    offset+= changed_length;
  }

  log_array[TRANSLOG_INTERNAL_PARTS + 0].str=    log_data;
  log_array[TRANSLOG_INTERNAL_PARTS + 0].length= (uint) (log_pos - log_data);

  if (move_length)
  {
    uint log_length;
    if (offset + move_length < share->max_index_block_size)
    {
      /*
        Move down things that is on page.
        page_offset in apply_redo_inxed() will be at original offset
        + changed_length.
      */
      log_pos[0]= KEY_OP_SHIFT;
      int2store(log_pos+1, - (int) move_length);
      log_length= 3;
      current_size-= move_length;
    }
    else
    {
      /* Delete to end of page */
      uint tmp= current_size - offset;
      current_size= offset;
      log_pos[0]= KEY_OP_DEL_SUFFIX;
      int2store(log_pos+1, tmp);
      log_length= 3;
    }
    log_array[translog_parts].str=    log_pos;
    log_array[translog_parts].length= log_length;
    translog_parts++;
    log_pos+= log_length;
    extra_length+= log_length;
  }

  if (current_size != ma_page->size &&
      current_size != share->max_index_block_size)
  {
    /* Append data that didn't fit on the page before */
    uint length= (MY_MIN(ma_page->size, share->max_index_block_size) -
                  current_size);
    uchar *data= ma_page->buff + current_size;

    DBUG_ASSERT(length <= append_length);

    log_pos[0]= KEY_OP_ADD_SUFFIX;
    int2store(log_pos+1, length);
    log_array[translog_parts].str=        log_pos;
    log_array[translog_parts].length=     3;
    log_array[translog_parts + 1].str=    data;
    log_array[translog_parts + 1].length= length;
    log_pos+= 3;
    translog_parts+= 2;
    current_size+= length;
    extra_length+= 3 + length;
  }

  _ma_log_key_changes(ma_page,
                      log_array + translog_parts,
                      log_pos, &extra_length, &translog_parts);
  /* Remember new page length for future log entires for same page */
  ma_page->org_size= current_size;

  if (translog_write_record(&lsn, LOGREC_REDO_INDEX,
                            info->trn, info,
                            (translog_size_t)
                            log_array[TRANSLOG_INTERNAL_PARTS].length +
                            changed_length + extra_length, translog_parts,
                            log_array, log_data, NULL))
    DBUG_RETURN(1);

  DBUG_RETURN(0);
}


/****************************************************************************
  Logging of undos
****************************************************************************/

my_bool _ma_write_undo_key_delete(MARIA_HA *info, const MARIA_KEY *key,
                                  my_off_t new_root, LSN *res_lsn)
{
  MARIA_SHARE *share= info->s;
  uchar log_data[LSN_STORE_SIZE + FILEID_STORE_SIZE +
                 KEY_NR_STORE_SIZE + PAGE_STORE_SIZE], *log_pos;
  LEX_CUSTRING log_array[TRANSLOG_INTERNAL_PARTS + 2];
  struct st_msg_to_write_hook_for_undo_key msg;
  enum translog_record_type log_type= LOGREC_UNDO_KEY_DELETE;
  uint keynr= key->keyinfo->key_nr;

  lsn_store(log_data, info->trn->undo_lsn);
  key_nr_store(log_data + LSN_STORE_SIZE + FILEID_STORE_SIZE, keynr);
  log_pos= log_data + LSN_STORE_SIZE + FILEID_STORE_SIZE + KEY_NR_STORE_SIZE;

  /**
    @todo BUG if we had concurrent insert/deletes, reading state's key_root
    like this would be unsafe.
  */
  if (new_root != share->state.key_root[keynr])
  {
    my_off_t page;
    page= ((new_root == HA_OFFSET_ERROR) ? IMPOSSIBLE_PAGE_NO :
           new_root / share->block_size);
    page_store(log_pos, page);
    log_pos+= PAGE_STORE_SIZE;
    log_type= LOGREC_UNDO_KEY_DELETE_WITH_ROOT;
  }

  log_array[TRANSLOG_INTERNAL_PARTS + 0].str=    log_data;
  log_array[TRANSLOG_INTERNAL_PARTS + 0].length= (uint) (log_pos - log_data);
  log_array[TRANSLOG_INTERNAL_PARTS + 1].str=    key->data;
  log_array[TRANSLOG_INTERNAL_PARTS + 1].length= (key->data_length +
                                                  key->ref_length);

  msg.root= &share->state.key_root[keynr];
  msg.value= new_root;
  /*
    set autoincrement to 1 if this is an auto_increment key
    This is only used if we are now in a rollback of a duplicate key
  */
  msg.auto_increment= share->base.auto_key == keynr + 1;

  return translog_write_record(res_lsn, log_type,
                               info->trn, info,
                               (translog_size_t)
                               (log_array[TRANSLOG_INTERNAL_PARTS + 0].length +
                                log_array[TRANSLOG_INTERNAL_PARTS + 1].length),
                               TRANSLOG_INTERNAL_PARTS + 2, log_array,
                               log_data + LSN_STORE_SIZE, &msg) ? -1 : 0;
}