summaryrefslogtreecommitdiffstats
path: root/src/H5Gdense.c
blob: 961560ad4ad9c1dd5ddc7ca7c5b3e9d4bee75dbf (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5Gdense.c
 *
 * Purpose:		Routines for operating on "dense" link storage for a
 *                      group in a file.
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/

#include "H5Gmodule.h" /* This source code file is part of the H5G module */

/***********/
/* Headers */
/***********/
#include "H5private.h"   /* Generic Functions			*/
#include "H5Eprivate.h"  /* Error handling		  	*/
#include "H5Gpkg.h"      /* Groups		  		*/
#include "H5MMprivate.h" /* Memory management			*/
#include "H5WBprivate.h" /* Wrapped Buffers                      */

/****************/
/* Local Macros */
/****************/

/* Fractal heap creation parameters for "dense" link storage */
#define H5G_FHEAP_MAN_WIDTH            4
#define H5G_FHEAP_MAN_START_BLOCK_SIZE 512
#define H5G_FHEAP_MAN_MAX_DIRECT_SIZE  (64 * 1024)
#define H5G_FHEAP_MAN_MAX_INDEX        32
#define H5G_FHEAP_MAN_START_ROOT_ROWS  1
#define H5G_FHEAP_CHECKSUM_DBLOCKS     TRUE
#define H5G_FHEAP_MAX_MAN_SIZE         (4 * 1024)

/* v2 B-tree creation macros for 'name' field index */
#define H5G_NAME_BT2_NODE_SIZE  512
#define H5G_NAME_BT2_MERGE_PERC 40
#define H5G_NAME_BT2_SPLIT_PERC 100

/* v2 B-tree creation macros for 'corder' field index */
#define H5G_CORDER_BT2_NODE_SIZE  512
#define H5G_CORDER_BT2_MERGE_PERC 40
#define H5G_CORDER_BT2_SPLIT_PERC 100

/* Size of stack buffer for serialized link */
#define H5G_LINK_BUF_SIZE 128

/******************/
/* Local Typedefs */
/******************/

/* Data exchange structure to use when building table of links in group */
typedef struct {
    H5G_link_table_t *ltable;   /* Pointer to link table to build */
    size_t            curr_lnk; /* Current link to operate on */
} H5G_dense_bt_ud_t;

/*
 * Data exchange structure to pass through the v2 B-tree layer for the
 * H5B2_iterate function when iterating over densely stored links.
 */
typedef struct {
    /* downward (internal) */
    H5F_t  *f;     /* Pointer to file that fractal heap is in */
    H5HF_t *fheap; /* Fractal heap handle               */
    hsize_t count; /* # of links examined               */

    /* downward (from application) */
    hsize_t           skip;    /* Number of links to skip           */
    H5G_lib_iterate_t op;      /* Callback for each link            */
    void             *op_data; /* Callback data for each link       */

    /* upward */
    int op_ret; /* Return value from callback        */
} H5G_bt2_ud_it_t;

/*
 * Data exchange structure to pass through the fractal heap layer for the
 * H5HF_op function when iterating over densely stored links.
 */
typedef struct {
    /* downward (internal) */
    H5F_t *f; /* Pointer to file that fractal heap is in */

    /* upward */
    H5O_link_t *lnk; /* Copy of link                      */
} H5G_fh_ud_it_t;

/*
 * Data exchange structure for dense link storage.  This structure is
 * passed through the v2 B-tree layer when removing links.
 */
typedef struct {
    /* downward */
    H5G_bt2_ud_common_t common;          /* Common info for B-tree user data (must be first) */
    hbool_t             rem_from_fheap;  /* Whether to remove the link from the fractal heap */
    haddr_t             corder_bt2_addr; /* Address of v2 B-tree indexing creation order */
    H5RS_str_t         *grp_full_path_r; /* Full path of group where link is removed */
    hbool_t             replace_names;   /* Whether to replace the names of open objects */
} H5G_bt2_ud_rm_t;

/*
 * Data exchange structure to pass through the fractal heap layer for the
 * H5HF_op function when removing a link from densely stored links.
 */
typedef struct {
    /* downward */
    H5F_t      *f;               /* Pointer to file that fractal heap is in */
    haddr_t     corder_bt2_addr; /* Address of v2 B-tree indexing creation order */
    H5RS_str_t *grp_full_path_r; /* Full path of group where link is removed */
    hbool_t     replace_names;   /* Whether to replace the names of open objects */
} H5G_fh_ud_rm_t;

/*
 * Data exchange structure for dense link storage.  This structure is
 * passed through the v2 B-tree layer when removing links by index.
 */
typedef struct {
    /* downward */
    H5F_t      *f;               /* Pointer to file that fractal heap is in */
    H5HF_t     *fheap;           /* Fractal heap handle               */
    H5_index_t  idx_type;        /* Primary index for removing link */
    haddr_t     other_bt2_addr;  /* Address of "other" v2 B-tree indexing link */
    H5RS_str_t *grp_full_path_r; /* Full path of group where link is removed */
} H5G_bt2_ud_rmbi_t;

/*
 * Data exchange structure to pass through the fractal heap layer for the
 * H5HF_op function when removing a link from densely stored links by index.
 */
typedef struct {
    /* downward */
    H5F_t *f; /* Pointer to file that fractal heap is in */

    /* upward */
    H5O_link_t *lnk; /* Pointer to link to remove */
} H5G_fh_ud_rmbi_t;

/*
 * Data exchange structure to pass through the v2 B-tree layer for the
 * H5B2_index function when retrieving the name of a link by index.
 */
typedef struct {
    /* downward (internal) */
    H5F_t  *f;     /* Pointer to file that fractal heap is in */
    H5HF_t *fheap; /* Fractal heap handle               */

    /* downward (from application) */
    char  *name;      /* Name buffer to fill               */
    size_t name_size; /* Size of name buffer to fill       */

    /* upward */
    size_t name_len; /* Full length of name               */
} H5G_bt2_ud_gnbi_t;

/*
 * Data exchange structure to pass through the fractal heap layer for the
 * H5HF_op function when retrieving the name of a link by index.
 */
typedef struct {
    /* downward (internal) */
    H5F_t *f; /* Pointer to file that fractal heap is in */

    /* downward (from application) */
    char  *name;      /* Name buffer to fill               */
    size_t name_size; /* Size of name buffer to fill       */

    /* upward */
    size_t name_len; /* Full length of name               */
} H5G_fh_ud_gnbi_t;

/*
 * Data exchange structure to pass through the v2 B-tree layer for the
 * H5B2_index function when retrieving a link by index.
 */
typedef struct {
    /* downward (internal) */
    H5F_t  *f;     /* Pointer to file that fractal heap is in */
    H5HF_t *fheap; /* Fractal heap handle               */

    /* upward */
    H5O_link_t *lnk; /* Pointer to link                   */
} H5G_bt2_ud_lbi_t;

/*
 * Data exchange structure to pass through the fractal heap layer for the
 * H5HF_op function when retrieving a link by index.
 */
typedef struct {
    /* downward (internal) */
    H5F_t *f; /* Pointer to file that fractal heap is in */

    /* upward */
    H5O_link_t *lnk; /* Pointer to link                   */
} H5G_fh_ud_lbi_t;

/********************/
/* Package Typedefs */
/********************/

/********************/
/* Local Prototypes */
/********************/

/*********************/
/* Package Variables */
/*********************/

/*****************************/
/* Library Private Variables */
/*****************************/

/*******************/
/* Local Variables */
/*******************/

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_create
 *
 * Purpose:	Creates dense link storage structures for a group
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_create(H5F_t *f, H5O_linfo_t *linfo, const H5O_pline_t *pline)
{
    H5HF_create_t fheap_cparam;        /* Fractal heap creation parameters */
    H5B2_create_t bt2_cparam;          /* v2 B-tree creation parameters */
    H5HF_t       *fheap      = NULL;   /* Fractal heap handle */
    H5B2_t       *bt2_name   = NULL;   /* v2 B-tree handle for names */
    H5B2_t       *bt2_corder = NULL;   /* v2 B-tree handle for creation order */
    size_t        fheap_id_len;        /* Fractal heap ID length */
    herr_t        ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);

    /* Set fractal heap creation parameters */
    /* XXX: Give some control of these to applications? */
    memset(&fheap_cparam, 0, sizeof(fheap_cparam));
    fheap_cparam.managed.width            = H5G_FHEAP_MAN_WIDTH;
    fheap_cparam.managed.start_block_size = H5G_FHEAP_MAN_START_BLOCK_SIZE;
    fheap_cparam.managed.max_direct_size  = H5G_FHEAP_MAN_MAX_DIRECT_SIZE;
    fheap_cparam.managed.max_index        = H5G_FHEAP_MAN_MAX_INDEX;
    fheap_cparam.managed.start_root_rows  = H5G_FHEAP_MAN_START_ROOT_ROWS;
    fheap_cparam.checksum_dblocks         = H5G_FHEAP_CHECKSUM_DBLOCKS;
    fheap_cparam.max_man_size             = H5G_FHEAP_MAX_MAN_SIZE;
    if (pline)
        fheap_cparam.pline = *pline;

    /* Create fractal heap for storing links */
    if (NULL == (fheap = H5HF_create(f, &fheap_cparam)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "unable to create fractal heap");

    /* Retrieve the heap's address in the file */
    if (H5HF_get_heap_addr(fheap, &(linfo->fheap_addr)) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "can't get fractal heap address");

    /* Retrieve the heap's ID length in the file */
    if (H5HF_get_id_len(fheap, &fheap_id_len) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTGETSIZE, FAIL, "can't get fractal heap ID length");
    assert(fheap_id_len == H5G_DENSE_FHEAP_ID_LEN);

    /* Create the name index v2 B-tree */
    memset(&bt2_cparam, 0, sizeof(bt2_cparam));
    bt2_cparam.cls       = H5G_BT2_NAME;
    bt2_cparam.node_size = (size_t)H5G_NAME_BT2_NODE_SIZE;
    H5_CHECK_OVERFLOW(fheap_id_len, /* From: */ hsize_t, /* To: */ uint32_t);
    bt2_cparam.rrec_size = 4 +                     /* Name's hash value */
                           (uint32_t)fheap_id_len; /* Fractal heap ID */
    bt2_cparam.split_percent = H5G_NAME_BT2_SPLIT_PERC;
    bt2_cparam.merge_percent = H5G_NAME_BT2_MERGE_PERC;
    if (NULL == (bt2_name = H5B2_create(f, &bt2_cparam, NULL)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "unable to create v2 B-tree for name index");

    /* Retrieve the v2 B-tree's address in the file */
    if (H5B2_get_addr(bt2_name, &(linfo->name_bt2_addr)) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "can't get v2 B-tree address for name index");

    /* Check if we should create a creation order index v2 B-tree */
    if (linfo->index_corder) {
        /* Create the creation order index v2 B-tree */
        memset(&bt2_cparam, 0, sizeof(bt2_cparam));
        bt2_cparam.cls       = H5G_BT2_CORDER;
        bt2_cparam.node_size = (size_t)H5G_CORDER_BT2_NODE_SIZE;
        H5_CHECK_OVERFLOW(fheap_id_len, /* From: */ hsize_t, /* To: */ uint32_t);
        bt2_cparam.rrec_size = 8 +                     /* Creation order value */
                               (uint32_t)fheap_id_len; /* Fractal heap ID */
        bt2_cparam.split_percent = H5G_CORDER_BT2_SPLIT_PERC;
        bt2_cparam.merge_percent = H5G_CORDER_BT2_MERGE_PERC;
        if (NULL == (bt2_corder = H5B2_create(f, &bt2_cparam, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "unable to create v2 B-tree for creation order index");

        /* Retrieve the v2 B-tree's address in the file */
        if (H5B2_get_addr(bt2_corder, &(linfo->corder_bt2_addr)) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "can't get v2 B-tree address for creation order index");
    } /* end if */

done:
    /* Close the open objects */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2_name && H5B2_close(bt2_name) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for name index");
    if (bt2_corder && H5B2_close(bt2_corder) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for creation order index");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_create() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_insert
 *
 * Purpose:	Insert a link into the  dense link storage structures for a group
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_insert(H5F_t *f, const H5O_linfo_t *linfo, const H5O_link_t *lnk)
{
    H5G_bt2_ud_ins_t udata;                       /* User data for v2 B-tree insertion */
    H5HF_t          *fheap      = NULL;           /* Fractal heap handle */
    H5B2_t          *bt2_name   = NULL;           /* v2 B-tree handle for name index */
    H5B2_t          *bt2_corder = NULL;           /* v2 B-tree handle for creation order index */
    size_t           link_size;                   /* Size of serialized link in the heap */
    H5WB_t          *wb = NULL;                   /* Wrapped buffer for link data */
    uint8_t          link_buf[H5G_LINK_BUF_SIZE]; /* Buffer for serializing link */
    void            *link_ptr  = NULL;            /* Pointer to serialized link */
    herr_t           ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);
    assert(lnk);

    /* Find out the size of buffer needed for serialized link */
    if ((link_size = H5O_msg_raw_size(f, H5O_LINK_ID, FALSE, lnk)) == 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTGETSIZE, FAIL, "can't get link size");

    /* Wrap the local buffer for serialized link */
    if (NULL == (wb = H5WB_wrap(link_buf, sizeof(link_buf))))
        HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, FAIL, "can't wrap buffer");

    /* Get a pointer to a buffer that's large enough for link */
    if (NULL == (link_ptr = H5WB_actual(wb, link_size)))
        HGOTO_ERROR(H5E_SYM, H5E_NOSPACE, FAIL, "can't get actual buffer");

    /* Create serialized form of link */
    if (H5O_msg_encode(f, H5O_LINK_ID, FALSE, (unsigned char *)link_ptr, lnk) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTENCODE, FAIL, "can't encode link");

    /* Open the fractal heap */
    if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

    /* Insert the serialized link into the fractal heap */
    if (H5HF_insert(fheap, link_size, link_ptr, udata.id) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTINSERT, FAIL, "unable to insert link into fractal heap");

    /* Open the name index v2 B-tree */
    if (NULL == (bt2_name = H5B2_open(f, linfo->name_bt2_addr, NULL)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for name index");

    /* Create the callback information for v2 B-tree record insertion */
    udata.common.f             = f;
    udata.common.fheap         = fheap;
    udata.common.name          = lnk->name;
    udata.common.name_hash     = H5_checksum_lookup3(lnk->name, HDstrlen(lnk->name), 0);
    udata.common.corder        = lnk->corder;
    udata.common.found_op      = NULL;
    udata.common.found_op_data = NULL;
    /* udata.id already set in H5HF_insert() call */

    /* Insert link into 'name' tracking v2 B-tree */
    if (H5B2_insert(bt2_name, &udata) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTINSERT, FAIL, "unable to insert record into v2 B-tree");

    /* Check if we should create a creation order index v2 B-tree record */
    if (linfo->index_corder) {
        /* Open the creation order index v2 B-tree */
        assert(H5_addr_defined(linfo->corder_bt2_addr));
        if (NULL == (bt2_corder = H5B2_open(f, linfo->corder_bt2_addr, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for creation order index");

        /* Insert the record into the creation order index v2 B-tree */
        if (H5B2_insert(bt2_corder, &udata) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTINSERT, FAIL, "unable to insert record into v2 B-tree");
    } /* end if */

done:
    /* Release resources */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2_name && H5B2_close(bt2_name) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for name index");
    if (bt2_corder && H5B2_close(bt2_corder) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for creation order index");
    if (wb && H5WB_unwrap(wb) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close wrapped buffer");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_insert() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_lookup_cb
 *
 * Purpose:	Callback when a link is located in an index
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_lookup_cb(const void *_lnk, void *_user_lnk)
{
    const H5O_link_t *lnk       = (const H5O_link_t *)_lnk; /* Record from B-tree */
    H5O_link_t       *user_lnk  = (H5O_link_t *)_user_lnk;  /* User data from v2 B-tree link lookup */
    herr_t            ret_value = SUCCEED;                  /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(lnk);
    assert(user_lnk);

    /* Copy link information */
    if (H5O_msg_copy(H5O_LINK_ID, lnk, user_lnk) == NULL)
        HGOTO_ERROR(H5E_SYM, H5E_CANTCOPY, H5_ITER_ERROR, "can't copy link message");

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_lookup_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_lookup
 *
 * Purpose:	Look up a link within a group that uses dense link storage
 *
 * Return:	SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_lookup(H5F_t *f, const H5O_linfo_t *linfo, const char *name, hbool_t *found, H5O_link_t *lnk)
{
    H5G_bt2_ud_common_t udata;               /* User data for v2 B-tree link lookup */
    H5HF_t             *fheap     = NULL;    /* Fractal heap handle */
    H5B2_t             *bt2_name  = NULL;    /* v2 B-tree handle for name index */
    herr_t              ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);
    assert(name && *name);
    assert(found);
    assert(lnk);

    /* Open the fractal heap */
    if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

    /* Open the name index v2 B-tree */
    if (NULL == (bt2_name = H5B2_open(f, linfo->name_bt2_addr, NULL)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for name index");

    /* Construct the user data for v2 B-tree callback */
    udata.f             = f;
    udata.fheap         = fheap;
    udata.name          = name;
    udata.name_hash     = H5_checksum_lookup3(name, HDstrlen(name), 0);
    udata.found_op      = H5G__dense_lookup_cb; /* v2 B-tree comparison callback */
    udata.found_op_data = lnk;

    /* Find & copy the named link in the 'name' index */
    if (H5B2_find(bt2_name, &udata, found, NULL, NULL) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTINSERT, FAIL, "unable to locate link in name index");

done:
    /* Release resources */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2_name && H5B2_close(bt2_name) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for name index");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_lookup() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_lookup_by_idx_fh_cb
 *
 * Purpose:	Callback for fractal heap operator, to make copy of link when
 *              when lookup up a link by index
 *
 * Return:	SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_lookup_by_idx_fh_cb(const void *obj, size_t obj_len, void *_udata)
{
    H5G_fh_ud_lbi_t *udata     = (H5G_fh_ud_lbi_t *)_udata; /* User data for fractal heap 'op' callback */
    H5O_link_t      *tmp_lnk   = NULL;                      /* Temporary pointer to link */
    herr_t           ret_value = SUCCEED;                   /* Return value */

    FUNC_ENTER_PACKAGE

    /* Decode link information & keep a copy */
    if (NULL == (tmp_lnk = (H5O_link_t *)H5O_msg_decode(udata->f, NULL, H5O_LINK_ID, obj_len,
                                                        (const unsigned char *)obj)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTDECODE, FAIL, "can't decode link");

    /* Copy link information */
    if (NULL == H5O_msg_copy(H5O_LINK_ID, tmp_lnk, udata->lnk))
        HGOTO_ERROR(H5E_SYM, H5E_CANTCOPY, H5_ITER_ERROR, "can't copy link message");

done:
    /* Release the space allocated for the link */
    if (tmp_lnk)
        H5O_msg_free(H5O_LINK_ID, tmp_lnk);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_lookup_by_idx_fh_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_lookup_by_idx_bt2_cb
 *
 * Purpose:	v2 B-tree callback for dense link storage lookup by index
 *
 * Return:	H5_ITER_ERROR/H5_ITER_CONT/H5_ITER_STOP
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_lookup_by_idx_bt2_cb(const void *_record, void *_bt2_udata)
{
    const H5G_dense_bt2_name_rec_t *record    = (const H5G_dense_bt2_name_rec_t *)_record;
    H5G_bt2_ud_lbi_t               *bt2_udata = (H5G_bt2_ud_lbi_t *)_bt2_udata; /* User data for callback */
    H5G_fh_ud_lbi_t                 fh_udata;                 /* User data for fractal heap 'op' callback */
    int                             ret_value = H5_ITER_CONT; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Prepare user data for callback */
    /* down */
    fh_udata.f   = bt2_udata->f;
    fh_udata.lnk = bt2_udata->lnk;

    /* Call fractal heap 'op' routine, to copy the link information */
    if (H5HF_op(bt2_udata->fheap, record->id, H5G__dense_lookup_by_idx_fh_cb, &fh_udata) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPERATE, H5_ITER_ERROR, "link found callback failed");

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_lookup_by_idx_bt2_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_lookup_by_idx
 *
 * Purpose:	Look up a link within a group that uses dense link storage,
 *              according to the order of an index
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_lookup_by_idx(H5F_t *f, const H5O_linfo_t *linfo, H5_index_t idx_type, H5_iter_order_t order,
                         hsize_t n, H5O_link_t *lnk)
{
    H5HF_t          *fheap  = NULL;       /* Fractal heap handle */
    H5G_link_table_t ltable = {0, NULL};  /* Table of links */
    H5B2_t          *bt2    = NULL;       /* v2 B-tree handle for index */
    haddr_t          bt2_addr;            /* Address of v2 B-tree to use for lookup */
    herr_t           ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);
    assert(lnk);

    /* Determine the address of the index to use */
    if (idx_type == H5_INDEX_NAME) {
        /* Since names are hashed, getting them in strictly increasing or
         *      decreasing order requires building a table and sorting it.
         *      If the order is native, use the B-tree for names.
         */
        bt2_addr = HADDR_UNDEF;
    } /* end if */
    else {
        assert(idx_type == H5_INDEX_CRT_ORDER);

        /* This address may not be defined if creation order is tracked, but
         *      there's no index on it.  If there's no v2 B-tree that indexes
         *      the links and the order is native, use the B-tree for names.
         *      Otherwise, build a table.
         */
        bt2_addr = linfo->corder_bt2_addr;
    } /* end else */

    /* If the order is native and there's no B-tree for indexing the links,
     * use the B-tree for names instead of building a table to speed up the
     * process.
     */
    if (order == H5_ITER_NATIVE && !H5_addr_defined(bt2_addr)) {
        bt2_addr = linfo->name_bt2_addr;
        assert(H5_addr_defined(bt2_addr));
    } /* end if */

    /* If there is an index defined for the field, use it */
    if (H5_addr_defined(bt2_addr)) {
        H5G_bt2_ud_lbi_t udata; /* User data for v2 B-tree link lookup */

        /* Open the fractal heap */
        if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

        /* Open the index v2 B-tree */
        if (NULL == (bt2 = H5B2_open(f, bt2_addr, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for index");

        /* Construct the user data for v2 B-tree callback */
        udata.f     = f;
        udata.fheap = fheap;
        udata.lnk   = lnk;

        /* Find & copy the link in the appropriate index */
        if (H5B2_index(bt2, order, n, H5G__dense_lookup_by_idx_bt2_cb, &udata) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTINSERT, FAIL, "unable to locate link in index");
    }      /* end if */
    else { /* Otherwise, we need to build a table of the links and sort it */
        /* Build the table of links for this group */
        if (H5G__dense_build_table(f, linfo, idx_type, order, &ltable) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "error building table of links");

        /* Check for going out of bounds */
        if (n >= ltable.nlinks)
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "index out of bound");

        /* Copy link information */
        if (NULL == H5O_msg_copy(H5O_LINK_ID, &ltable.lnks[n], lnk))
            HGOTO_ERROR(H5E_SYM, H5E_CANTCOPY, H5_ITER_ERROR, "can't copy link message");
    } /* end else */

done:
    /* Release resources */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2 && H5B2_close(bt2) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for index");
    if (ltable.lnks && H5G__link_release_table(&ltable) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CANTFREE, FAIL, "unable to release link table");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_lookup_by_idx() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_build_table_cb
 *
 * Purpose:	Callback routine for building table of links from dense
 *              link storage.
 *
 * Return:	Success:        Non-negative
 *		Failure:	Negative
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_build_table_cb(const H5O_link_t *lnk, void *_udata)
{
    H5G_dense_bt_ud_t *udata     = (H5G_dense_bt_ud_t *)_udata; /* 'User data' passed in */
    herr_t             ret_value = H5_ITER_CONT;                /* Return value */

    FUNC_ENTER_PACKAGE

    /* check arguments */
    assert(lnk);
    assert(udata);
    assert(udata->curr_lnk < udata->ltable->nlinks);

    /* Copy link information */
    if (H5O_msg_copy(H5O_LINK_ID, lnk, &(udata->ltable->lnks[udata->curr_lnk])) == NULL)
        HGOTO_ERROR(H5E_SYM, H5E_CANTCOPY, H5_ITER_ERROR, "can't copy link message");

    /* Increment number of links stored */
    udata->curr_lnk++;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_build_table_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_build_table
 *
 * Purpose:     Builds a table containing a sorted list of links for a group
 *
 * Note:	Used for building table of links in non-native iteration order
 *		for an index
 *
 * Return:	Success:        Non-negative
 *		Failure:	Negative
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_build_table(H5F_t *f, const H5O_linfo_t *linfo, H5_index_t idx_type, H5_iter_order_t order,
                       H5G_link_table_t *ltable)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity check */
    assert(f);
    assert(linfo);
    assert(ltable);

    /* Set size of table */
    H5_CHECK_OVERFLOW(linfo->nlinks, /* From: */ hsize_t, /* To: */ size_t);
    ltable->nlinks = (size_t)linfo->nlinks;

    /* Allocate space for the table entries */
    if (ltable->nlinks > 0) {
        H5G_dense_bt_ud_t udata; /* User data for iteration callback */

        /* Allocate the table to store the links */
        if ((ltable->lnks = (H5O_link_t *)H5MM_malloc(sizeof(H5O_link_t) * ltable->nlinks)) == NULL)
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");

        /* Set up user data for iteration */
        udata.ltable   = ltable;
        udata.curr_lnk = 0;

        /* Iterate over the links in the group, building a table of the link messages */
        if (H5G__dense_iterate(f, linfo, H5_INDEX_NAME, H5_ITER_NATIVE, (hsize_t)0, NULL,
                               H5G__dense_build_table_cb, &udata) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTNEXT, FAIL, "error iterating over links");

        /* Sort link table in correct iteration order */
        if (H5G__link_sort_table(ltable, idx_type, order) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTSORT, FAIL, "error sorting link messages");
    } /* end if */
    else
        ltable->lnks = NULL;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_build_table() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_iterate_fh_cb
 *
 * Purpose:	Callback for fractal heap operator, to make user's callback
 *              when iterating over links
 *
 * Return:	SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_iterate_fh_cb(const void *obj, size_t obj_len, void *_udata)
{
    H5G_fh_ud_it_t *udata     = (H5G_fh_ud_it_t *)_udata; /* User data for fractal heap 'op' callback */
    herr_t          ret_value = SUCCEED;                  /* Return value */

    FUNC_ENTER_PACKAGE

    /* Decode link information & keep a copy */
    /* (we make a copy instead of calling the user/library callback directly in
     *  this routine because this fractal heap 'op' callback routine is called
     *  with the direct block protected and if the callback routine invokes an
     *  HDF5 routine, it could attempt to re-protect that direct block for the
     *  heap, causing the HDF5 routine called to fail - QAK)
     */
    if (NULL == (udata->lnk = (H5O_link_t *)H5O_msg_decode(udata->f, NULL, H5O_LINK_ID, obj_len,
                                                           (const unsigned char *)obj)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTDECODE, FAIL, "can't decode link");

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_iterate_fh_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_iterate_bt2_cb
 *
 * Purpose:	v2 B-tree callback for dense link storage iterator
 *
 * Return:	H5_ITER_ERROR/H5_ITER_CONT/H5_ITER_STOP
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_iterate_bt2_cb(const void *_record, void *_bt2_udata)
{
    const H5G_dense_bt2_name_rec_t *record    = (const H5G_dense_bt2_name_rec_t *)_record;
    H5G_bt2_ud_it_t                *bt2_udata = (H5G_bt2_ud_it_t *)_bt2_udata; /* User data for callback */
    herr_t                          ret_value = H5_ITER_CONT;                  /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check for skipping links */
    if (bt2_udata->skip > 0)
        --bt2_udata->skip;
    else {
        H5G_fh_ud_it_t fh_udata; /* User data for fractal heap 'op' callback */

        /* Prepare user data for callback */
        /* down */
        fh_udata.f = bt2_udata->f;

        /* Call fractal heap 'op' routine, to copy the link information */
        if (H5HF_op(bt2_udata->fheap, record->id, H5G__dense_iterate_fh_cb, &fh_udata) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPERATE, H5_ITER_ERROR, "heap op callback failed");

        /* Make the callback */
        ret_value = (bt2_udata->op)(fh_udata.lnk, bt2_udata->op_data);

        /* Release the space allocated for the link */
        H5O_msg_free(H5O_LINK_ID, fh_udata.lnk);
    } /* end else */

    /* Increment the number of entries passed through */
    /* (whether we skipped them or not) */
    bt2_udata->count++;

    /* Check for callback failure and pass along return value */
    if (ret_value < 0)
        HERROR(H5E_SYM, H5E_CANTNEXT, "iteration operator failed");

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_iterate_bt2_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_iterate
 *
 * Purpose:	Iterate over the objects in a group using dense link storage
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_iterate(H5F_t *f, const H5O_linfo_t *linfo, H5_index_t idx_type, H5_iter_order_t order,
                   hsize_t skip, hsize_t *last_lnk, H5G_lib_iterate_t op, void *op_data)
{
    H5HF_t          *fheap  = NULL;      /* Fractal heap handle */
    H5G_link_table_t ltable = {0, NULL}; /* Table of links */
    H5B2_t          *bt2    = NULL;      /* v2 B-tree handle for index */
    haddr_t          bt2_addr;           /* Address of v2 B-tree to use for lookup */
    herr_t           ret_value = FAIL;   /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);
    assert(op);

    /* Determine the address of the index to use */
    if (idx_type == H5_INDEX_NAME) {
        /* Since names are hashed, getting them in strictly increasing or
         * decreasing order requires building a table and sorting it. If
         * the order is native, use the B-tree for names.
         */
        bt2_addr = HADDR_UNDEF;
    } /* end if */
    else {
        assert(idx_type == H5_INDEX_CRT_ORDER);

        /* This address may not be defined if creation order is tracked, but
         *      there's no index on it.  If there's no v2 B-tree that indexes
         *      the links and the order is native, use the B-tree for names.
         *      Otherwise, build a table.
         */
        bt2_addr = linfo->corder_bt2_addr;
    } /* end else */

    /* If the order is native and there's no B-tree for indexing the links,
     * use the B-tree for names instead of building a table to speed up the
     * process.
     */
    if (order == H5_ITER_NATIVE && !H5_addr_defined(bt2_addr)) {
        assert(H5_addr_defined(linfo->name_bt2_addr));
        bt2_addr = linfo->name_bt2_addr;
    } /* end if */

    /* Check on iteration order */
    if (order == H5_ITER_NATIVE) {
        H5G_bt2_ud_it_t udata; /* User data for iterator callback */

        /* Sanity check */
        assert(H5_addr_defined(bt2_addr));

        /* Open the fractal heap */
        if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

        /* Open the index v2 B-tree */
        if (NULL == (bt2 = H5B2_open(f, bt2_addr, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for index");

        /* Construct the user data for v2 B-tree iterator callback */
        udata.f       = f;
        udata.fheap   = fheap;
        udata.skip    = skip;
        udata.count   = 0;
        udata.op      = op;
        udata.op_data = op_data;

        /* Iterate over the records in the v2 B-tree's "native" order */
        /* (by hash of name) */
        if ((ret_value = H5B2_iterate(bt2, H5G__dense_iterate_bt2_cb, &udata)) < 0)
            HERROR(H5E_SYM, H5E_BADITER, "link iteration failed");

        /* Update the last link examined, if requested */
        if (last_lnk)
            *last_lnk = udata.count;
    } /* end if */
    else {
        /* Build the table of links for this group */
        if (H5G__dense_build_table(f, linfo, idx_type, order, &ltable) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "error building table of links");

        /* Iterate over links in table */
        if ((ret_value = H5G__link_iterate_table(&ltable, skip, last_lnk, op, op_data)) < 0)
            HERROR(H5E_SYM, H5E_CANTNEXT, "iteration operator failed");
    } /* end else */

done:
    /* Release resources */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2 && H5B2_close(bt2) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for index");
    if (ltable.lnks && H5G__link_release_table(&ltable) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CANTFREE, FAIL, "unable to release link table");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_iterate() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_get_name_by_idx_fh_cb
 *
 * Purpose:	Callback for fractal heap operator, to retrieve name according
 *              to an index
 *
 * Return:	SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_get_name_by_idx_fh_cb(const void *obj, size_t obj_len, void *_udata)
{
    H5G_fh_ud_gnbi_t *udata = (H5G_fh_ud_gnbi_t *)_udata; /* User data for fractal heap 'op' callback */
    H5O_link_t       *lnk;                                /* Pointer to link created from heap object */
    herr_t            ret_value = SUCCEED;                /* Return value */

    FUNC_ENTER_PACKAGE

    /* Decode link information */
    if (NULL == (lnk = (H5O_link_t *)H5O_msg_decode(udata->f, NULL, H5O_LINK_ID, obj_len,
                                                    (const unsigned char *)obj)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTDECODE, FAIL, "can't decode link");

    /* Get the length of the name */
    udata->name_len = HDstrlen(lnk->name);

    /* Copy the name into the user's buffer, if given */
    if (udata->name) {
        HDstrncpy(udata->name, lnk->name, MIN((udata->name_len + 1), udata->name_size));
        if (udata->name_len >= udata->name_size)
            udata->name[udata->name_size - 1] = '\0';
    } /* end if */

    /* Release the space allocated for the link */
    H5O_msg_free(H5O_LINK_ID, lnk);

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_get_name_by_idx_fh_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_get_name_by_idx_bt2_cb
 *
 * Purpose:	v2 B-tree callback for dense link storage 'get name by idx' call
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_get_name_by_idx_bt2_cb(const void *_record, void *_bt2_udata)
{
    const H5G_dense_bt2_name_rec_t *record    = (const H5G_dense_bt2_name_rec_t *)_record;
    H5G_bt2_ud_gnbi_t              *bt2_udata = (H5G_bt2_ud_gnbi_t *)_bt2_udata; /* User data for callback */
    H5G_fh_ud_gnbi_t                fh_udata;            /* User data for fractal heap 'op' callback */
    herr_t                          ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Prepare user data for callback */
    /* down */
    fh_udata.f         = bt2_udata->f;
    fh_udata.name      = bt2_udata->name;
    fh_udata.name_size = bt2_udata->name_size;

    /* Call fractal heap 'op' routine, to perform user callback */
    if (H5HF_op(bt2_udata->fheap, record->id, H5G__dense_get_name_by_idx_fh_cb, &fh_udata) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPERATE, FAIL, "link found callback failed");

    /* Set the name's full length to return */
    bt2_udata->name_len = fh_udata.name_len;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_get_name_by_idx_bt2_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_get_name_by_idx
 *
 * Purpose:     Returns the name of objects in the group by giving index.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_get_name_by_idx(H5F_t *f, H5O_linfo_t *linfo, H5_index_t idx_type, H5_iter_order_t order,
                           hsize_t n, char *name, size_t name_size, size_t *name_len)
{
    H5HF_t          *fheap  = NULL;       /* Fractal heap handle */
    H5G_link_table_t ltable = {0, NULL};  /* Table of links */
    H5B2_t          *bt2    = NULL;       /* v2 B-tree handle for index */
    haddr_t          bt2_addr;            /* Address of v2 B-tree to use for lookup */
    herr_t           ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);

    /* Determine the address of the index to use */
    if (idx_type == H5_INDEX_NAME) {
        /* Since names are hashed, getting them in strictly increasing or
         * decreasing order requires building a table and sorting it.  If
         * the order is native, use the B-tree for names.
         */
        bt2_addr = HADDR_UNDEF;
    } /* end if */
    else {
        assert(idx_type == H5_INDEX_CRT_ORDER);

        /* This address may not be defined if creation order is tracked, but
         *      there's no index on it.  If there's no v2 B-tree that indexes
         *      the links and the order is native, use the B-tree for names.
         *      Otherwise, build a table.
         */
        bt2_addr = linfo->corder_bt2_addr;
    } /* end else */

    /* If the order is native and there's no B-tree for indexing the links,
     * use the B-tree for names instead of building a table to speed up the
     * process.
     */
    if (order == H5_ITER_NATIVE && !H5_addr_defined(bt2_addr)) {
        bt2_addr = linfo->name_bt2_addr;
        assert(H5_addr_defined(bt2_addr));
    } /* end if */

    /* If there is an index defined for the field, use it */
    if (H5_addr_defined(bt2_addr)) {
        H5G_bt2_ud_gnbi_t udata; /* User data for v2 B-tree callback */

        /* Open the fractal heap */
        if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

        /* Open the index v2 B-tree */
        if (NULL == (bt2 = H5B2_open(f, bt2_addr, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for index");

        /* Set up the user data for the v2 B-tree 'record remove' callback */
        udata.f         = f;
        udata.fheap     = fheap;
        udata.name      = name;
        udata.name_size = name_size;

        /* Retrieve the name according to the v2 B-tree's index order */
        if (H5B2_index(bt2, order, n, H5G__dense_get_name_by_idx_bt2_cb, &udata) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTLIST, FAIL, "can't locate object in v2 B-tree");

        /* Set return value */
        *name_len = udata.name_len;
    }      /* end if */
    else { /* Otherwise, we need to build a table of the links and sort it */
        /* Build the table of links for this group */
        if (H5G__dense_build_table(f, linfo, idx_type, order, &ltable) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "error building table of links");

        /* Check for going out of bounds */
        if (n >= ltable.nlinks)
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "index out of bound");

        /* Get the length of the name */
        *name_len = HDstrlen(ltable.lnks[n].name);

        /* Copy the name into the user's buffer, if given */
        if (name) {
            HDstrncpy(name, ltable.lnks[n].name, MIN((*name_len + 1), name_size));
            if (*name_len >= name_size)
                name[name_size - 1] = '\0';
        } /* end if */
    }     /* end else */

done:
    /* Release resources */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2 && H5B2_close(bt2) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for index");
    if (ltable.lnks && H5G__link_release_table(&ltable) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CANTFREE, FAIL, "unable to release link table");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_get_name_by_idx() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_remove_fh_cb
 *
 * Purpose:	Callback for fractal heap operator when removing links
 *
 * Return:	SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_remove_fh_cb(const void *obj, size_t obj_len, void *_udata)
{
    H5G_fh_ud_rm_t *udata     = (H5G_fh_ud_rm_t *)_udata; /* User data for fractal heap 'op' callback */
    H5O_link_t     *lnk       = NULL;                     /* Pointer to link created from heap object */
    H5B2_t         *bt2       = NULL;                     /* v2 B-tree handle for index */
    herr_t          ret_value = SUCCEED;                  /* Return value */

    FUNC_ENTER_PACKAGE

    /* Decode link information */
    if (NULL == (lnk = (H5O_link_t *)H5O_msg_decode(udata->f, NULL, H5O_LINK_ID, obj_len,
                                                    (const unsigned char *)obj)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTDECODE, FAIL, "can't decode link");

    /* Check for removing the link from the creation order index */
    if (H5_addr_defined(udata->corder_bt2_addr)) {
        H5G_bt2_ud_common_t bt2_udata; /* Info for B-tree callbacks */

        /* Open the creation order index v2 B-tree */
        if (NULL == (bt2 = H5B2_open(udata->f, udata->corder_bt2_addr, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for creation order index");

        /* Set up the user data for the v2 B-tree 'record remove' callback */
        assert(lnk->corder_valid);
        bt2_udata.corder = lnk->corder;

        /* Remove the record from the name index v2 B-tree */
        if (H5B2_remove(bt2, &bt2_udata, NULL, NULL) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTREMOVE, FAIL,
                        "unable to remove link from creation order index v2 B-tree");
    } /* end if */

    /* Replace open objects' names, if requested */
    if (udata->replace_names)
        if (H5G__link_name_replace(udata->f, udata->grp_full_path_r, lnk) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTRENAME, FAIL, "unable to rename open objects");

    /* Perform the deletion action on the link, if requested */
    /* (call message "delete" callback directly: *ick* - QAK) */
    if (H5O_link_delete(udata->f, NULL, lnk) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTDELETE, FAIL, "unable to delete link");

done:
    /* Release resources */
    if (bt2 && H5B2_close(bt2) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for creation order index");
    if (lnk)
        H5O_msg_free(H5O_LINK_ID, lnk);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_remove_fh_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_remove_bt2_cb
 *
 * Purpose:	v2 B-tree callback for dense link storage record removal
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_remove_bt2_cb(const void *_record, void *_bt2_udata)
{
    const H5G_dense_bt2_name_rec_t *record    = (const H5G_dense_bt2_name_rec_t *)_record;
    H5G_bt2_ud_rm_t                *bt2_udata = (H5G_bt2_ud_rm_t *)_bt2_udata; /* User data for callback */
    H5G_fh_ud_rm_t                  fh_udata;            /* User data for fractal heap 'op' callback */
    herr_t                          ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Set up the user data for fractal heap 'op' callback */
    fh_udata.f               = bt2_udata->common.f;
    fh_udata.corder_bt2_addr = bt2_udata->corder_bt2_addr;
    fh_udata.grp_full_path_r = bt2_udata->grp_full_path_r;
    fh_udata.replace_names   = bt2_udata->replace_names;

    /* Call fractal heap 'op' routine, to perform user callback */
    if (H5HF_op(bt2_udata->common.fheap, record->id, H5G__dense_remove_fh_cb, &fh_udata) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPERATE, FAIL, "link removal callback failed");

    /* Remove record from fractal heap, if requested */
    if (bt2_udata->rem_from_fheap)
        if (H5HF_remove(bt2_udata->common.fheap, record->id) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTREMOVE, FAIL, "unable to remove link from fractal heap");

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_remove_bt2_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_remove
 *
 * Purpose:	Remove a link from the dense storage of a group
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_remove(H5F_t *f, const H5O_linfo_t *linfo, H5RS_str_t *grp_full_path_r, const char *name)
{
    H5HF_t         *fheap = NULL;        /* Fractal heap handle */
    H5G_bt2_ud_rm_t udata;               /* User data for v2 B-tree record removal */
    H5B2_t         *bt2       = NULL;    /* v2 B-tree handle for index */
    herr_t          ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);
    assert(name && *name);

    /* Open the fractal heap */
    if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

    /* Open the name index v2 B-tree */
    if (NULL == (bt2 = H5B2_open(f, linfo->name_bt2_addr, NULL)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for name index");

    /* Set up the user data for the v2 B-tree 'record remove' callback */
    udata.common.f             = f;
    udata.common.fheap         = fheap;
    udata.common.name          = name;
    udata.common.name_hash     = H5_checksum_lookup3(name, HDstrlen(name), 0);
    udata.common.found_op      = NULL;
    udata.common.found_op_data = NULL;
    udata.rem_from_fheap       = TRUE;
    udata.corder_bt2_addr      = linfo->corder_bt2_addr;
    udata.grp_full_path_r      = grp_full_path_r;
    udata.replace_names        = TRUE;

    /* Remove the record from the name index v2 B-tree */
    if (H5B2_remove(bt2, &udata, H5G__dense_remove_bt2_cb, &udata) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTREMOVE, FAIL, "unable to remove link from name index v2 B-tree");

done:
    /* Release resources */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2 && H5B2_close(bt2) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for name index");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_remove() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_remove_by_idx_fh_cb
 *
 * Purpose:	Callback for fractal heap operator when removing links by index
 *
 * Return:	SUCCEED/FAIL
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_remove_by_idx_fh_cb(const void *obj, size_t obj_len, void *_udata)
{
    H5G_fh_ud_rmbi_t *udata     = (H5G_fh_ud_rmbi_t *)_udata; /* User data for fractal heap 'op' callback */
    herr_t            ret_value = SUCCEED;                    /* Return value */

    FUNC_ENTER_PACKAGE

    /* Decode link information */
    if (NULL == (udata->lnk = (H5O_link_t *)H5O_msg_decode(udata->f, NULL, H5O_LINK_ID, obj_len,
                                                           (const unsigned char *)obj)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTDECODE, H5_ITER_ERROR, "can't decode link");

    /* Can't operate on link here because the fractal heap block is locked */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_remove_by_idx_fh_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_remove_by_idx_bt2_cb
 *
 * Purpose:	v2 B-tree callback for dense link storage record removal by index
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5G__dense_remove_by_idx_bt2_cb(const void *_record, void *_bt2_udata)
{
    H5G_bt2_ud_rmbi_t *bt2_udata = (H5G_bt2_ud_rmbi_t *)_bt2_udata; /* User data for callback */
    H5G_fh_ud_rmbi_t   fh_udata;            /* User data for fractal heap 'op' callback */
    H5B2_t            *bt2 = NULL;          /* v2 B-tree handle for index */
    const uint8_t     *heap_id;             /* Heap ID for link */
    herr_t             ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Determine the index being used */
    if (bt2_udata->idx_type == H5_INDEX_NAME) {
        const H5G_dense_bt2_name_rec_t *record = (const H5G_dense_bt2_name_rec_t *)_record;

        /* Set the heap ID to operate on */
        heap_id = record->id;
    } /* end if */
    else {
        const H5G_dense_bt2_corder_rec_t *record = (const H5G_dense_bt2_corder_rec_t *)_record;

        assert(bt2_udata->idx_type == H5_INDEX_CRT_ORDER);

        /* Set the heap ID to operate on */
        heap_id = record->id;
    } /* end else */

    /* Set up the user data for fractal heap 'op' callback */
    fh_udata.f   = bt2_udata->f;
    fh_udata.lnk = NULL;

    /* Call fractal heap 'op' routine, to perform user callback */
    if (H5HF_op(bt2_udata->fheap, heap_id, H5G__dense_remove_by_idx_fh_cb, &fh_udata) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTOPERATE, FAIL, "link removal callback failed");
    assert(fh_udata.lnk);

    /* Check for removing the link from the "other" index (creation order, when name used and vice versa) */
    if (H5_addr_defined(bt2_udata->other_bt2_addr)) {
        H5G_bt2_ud_common_t other_bt2_udata; /* Info for B-tree callbacks */

        /* Determine the index being used */
        if (bt2_udata->idx_type == H5_INDEX_NAME) {
            /* Set up the user data for the v2 B-tree 'record remove' callback */
            other_bt2_udata.corder = fh_udata.lnk->corder;
        } /* end if */
        else {
            assert(bt2_udata->idx_type == H5_INDEX_CRT_ORDER);

            /* Set up the user data for the v2 B-tree 'record remove' callback */
            other_bt2_udata.f     = bt2_udata->f;
            other_bt2_udata.fheap = bt2_udata->fheap;
            other_bt2_udata.name  = fh_udata.lnk->name;
            other_bt2_udata.name_hash =
                H5_checksum_lookup3(fh_udata.lnk->name, HDstrlen(fh_udata.lnk->name), 0);
            other_bt2_udata.found_op      = NULL;
            other_bt2_udata.found_op_data = NULL;
        } /* end else */

        /* Open the index v2 B-tree */
        if (NULL == (bt2 = H5B2_open(bt2_udata->f, bt2_udata->other_bt2_addr, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for 'other' index");

        /* Set the common information for the v2 B-tree remove operation */

        /* Remove the record from the name index v2 B-tree */
        if (H5B2_remove(bt2, &other_bt2_udata, NULL, NULL) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTREMOVE, H5_ITER_ERROR,
                        "unable to remove link from 'other' index v2 B-tree");
    } /* end if */

    /* Replace open objects' names */
    if (H5G__link_name_replace(bt2_udata->f, bt2_udata->grp_full_path_r, fh_udata.lnk) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTRENAME, FAIL, "unable to rename open objects");

    /* Perform the deletion action on the link */
    /* (call link message "delete" callback directly: *ick* - QAK) */
    if (H5O_link_delete(bt2_udata->f, NULL, fh_udata.lnk) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTDELETE, FAIL, "unable to delete link");

    /* Release the space allocated for the link */
    H5O_msg_free(H5O_LINK_ID, fh_udata.lnk);

    /* Remove record from fractal heap */
    if (H5HF_remove(bt2_udata->fheap, heap_id) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTREMOVE, FAIL, "unable to remove link from fractal heap");

done:
    /* Release resources */
    if (bt2 && H5B2_close(bt2) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for 'other' index");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_remove_by_idx_bt2_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_remove_by_idx
 *
 * Purpose:	Remove a link from the dense storage of a group, according to
 *              to the offset in an indexed order
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_remove_by_idx(H5F_t *f, const H5O_linfo_t *linfo, H5RS_str_t *grp_full_path_r, H5_index_t idx_type,
                         H5_iter_order_t order, hsize_t n)
{
    H5HF_t          *fheap  = NULL;       /* Fractal heap handle */
    H5G_link_table_t ltable = {0, NULL};  /* Table of links */
    H5B2_t          *bt2    = NULL;       /* v2 B-tree handle for index */
    haddr_t          bt2_addr;            /* Address of v2 B-tree to use for lookup */
    herr_t           ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);

    /* Determine the address of the index to use */
    if (idx_type == H5_INDEX_NAME) {
        /* Since names are hashed, getting them in strictly increasing or
         * decreasing order requires building a table and sorting it.  If
         * the order is native, use the B-tree for names.
         */
        bt2_addr = HADDR_UNDEF;
    } /* end if */
    else {
        assert(idx_type == H5_INDEX_CRT_ORDER);

        /* This address may not be defined if creation order is tracked, but
         *      there's no index on it.  If there's no v2 B-tree that indexes
         *      the links and the order is native, use the B-tree for names.
         *      Otherwise, build a table.
         */
        bt2_addr = linfo->corder_bt2_addr;
    } /* end else */

    /* If the order is native and there's no B-tree for indexing the links,
     * use the B-tree for names instead of building a table to speed up the
     * process.
     */
    if (order == H5_ITER_NATIVE && !H5_addr_defined(bt2_addr)) {
        bt2_addr = linfo->name_bt2_addr;
        assert(H5_addr_defined(bt2_addr));
    } /* end if */

    /* If there is an index defined for the field, use it */
    if (H5_addr_defined(bt2_addr)) {
        H5G_bt2_ud_rmbi_t udata; /* User data for v2 B-tree record removal */

        /* Open the fractal heap */
        if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

        /* Open the index v2 B-tree */
        if (NULL == (bt2 = H5B2_open(f, bt2_addr, NULL)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for index");

        /* Set up the user data for the v2 B-tree 'remove by index' callback */
        udata.f               = f;
        udata.fheap           = fheap;
        udata.idx_type        = idx_type;
        udata.other_bt2_addr  = idx_type == H5_INDEX_NAME ? linfo->corder_bt2_addr : linfo->name_bt2_addr;
        udata.grp_full_path_r = grp_full_path_r;

        /* Remove the record from the name index v2 B-tree */
        if (H5B2_remove_by_idx(bt2, order, n, H5G__dense_remove_by_idx_bt2_cb, &udata) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTREMOVE, FAIL, "unable to remove link from indexed v2 B-tree");
    }      /* end if */
    else { /* Otherwise, we need to build a table of the links and sort it */
        /* Build the table of links for this group */
        if (H5G__dense_build_table(f, linfo, idx_type, order, &ltable) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTGET, FAIL, "error building table of links");

        /* Check for going out of bounds */
        if (n >= ltable.nlinks)
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "index out of bound");

        /* Remove the appropriate link from the dense storage */
        if (H5G__dense_remove(f, linfo, grp_full_path_r, ltable.lnks[n].name) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTREMOVE, FAIL, "unable to remove link from dense storage");
    } /* end else */

done:
    /* Release resources */
    if (fheap && H5HF_close(fheap) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    if (bt2 && H5B2_close(bt2) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close v2 B-tree for index");
    if (ltable.lnks && H5G__link_release_table(&ltable) < 0)
        HDONE_ERROR(H5E_SYM, H5E_CANTFREE, FAIL, "unable to release link table");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_remove_by_idx() */

/*-------------------------------------------------------------------------
 * Function:	H5G__dense_delete
 *
 * Purpose:	Delete the dense storage for a group
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G__dense_delete(H5F_t *f, H5O_linfo_t *linfo, hbool_t adj_link)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    assert(f);
    assert(linfo);

    /* Check if we are to adjust the ref. count for all the links */
    /* (we adjust the ref. count when deleting a group and we _don't_ adjust
     *  the ref. count when transitioning back to compact storage)
     */
    if (adj_link) {
        H5HF_t         *fheap = NULL; /* Fractal heap handle */
        H5G_bt2_ud_rm_t udata;        /* User data for v2 B-tree record removal */

        /* Open the fractal heap */
        if (NULL == (fheap = H5HF_open(f, linfo->fheap_addr)))
            HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");

        /* Set up the user data for the v2 B-tree 'record remove' callback */
        udata.common.f             = f;
        udata.common.fheap         = fheap;
        udata.common.name          = NULL;
        udata.common.name_hash     = 0;
        udata.common.found_op      = NULL;
        udata.common.found_op_data = NULL;
        udata.rem_from_fheap       = FALSE; /* handled in "bulk" below by deleting entire heap */
        udata.corder_bt2_addr      = linfo->corder_bt2_addr;
        udata.grp_full_path_r      = NULL;
        udata.replace_names        = FALSE;

        /* Delete the name index, adjusting the ref. count on links removed */
        if (H5B2_delete(f, linfo->name_bt2_addr, NULL, H5G__dense_remove_bt2_cb, &udata) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTDELETE, FAIL, "unable to delete v2 B-tree for name index");

        /* Close the fractal heap */
        if (H5HF_close(fheap) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CLOSEERROR, FAIL, "can't close fractal heap");
    } /* end if */
    else {
        /* Delete the name index, without adjusting the ref. count on the links  */
        if (H5B2_delete(f, linfo->name_bt2_addr, NULL, NULL, NULL) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTDELETE, FAIL, "unable to delete v2 B-tree for name index");
    } /* end else */
    linfo->name_bt2_addr = HADDR_UNDEF;

    /* Check if we should delete the creation order index v2 B-tree */
    if (linfo->index_corder) {
        /* Delete the creation order index, without adjusting the ref. count on the links  */
        assert(H5_addr_defined(linfo->corder_bt2_addr));
        if (H5B2_delete(f, linfo->corder_bt2_addr, NULL, NULL, NULL) < 0)
            HGOTO_ERROR(H5E_SYM, H5E_CANTDELETE, FAIL, "unable to delete v2 B-tree for creation order index");
        linfo->corder_bt2_addr = HADDR_UNDEF;
    } /* end if */
    else
        assert(!H5_addr_defined(linfo->corder_bt2_addr));

    /* Delete the fractal heap */
    if (H5HF_delete(f, linfo->fheap_addr) < 0)
        HGOTO_ERROR(H5E_SYM, H5E_CANTDELETE, FAIL, "unable to delete fractal heap");
    linfo->fheap_addr = HADDR_UNDEF;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5G__dense_delete() */