-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
H5B.c
1962 lines (1721 loc) · 77.3 KB
/
H5B.c
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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 *
* [email protected]. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------
*
* Created: H5B.c
*
* Purpose: Implements balanced, sibling-linked, N-ary trees
* capable of storing any type of data with unique key
* values.
*
* A B-link-tree is a balanced tree where each node has
* a pointer to its left and right siblings. A
* B-link-tree is a rooted tree having the following
* properties:
*
* 1. Every node, x, has the following fields:
*
* a. level[x], the level in the tree at which node
* x appears. Leaf nodes are at level zero.
*
* b. n[x], the number of children pointed to by the
* node. Internal nodes point to subtrees while
* leaf nodes point to arbitrary data.
*
* c. The child pointers themselves, child[x,i] such
* that 0 <= i < n[x].
*
* d. n[x]+1 key values stored in increasing
* order:
*
* key[x,0] < key[x,1] < ... < key[x,n[x]].
*
* e. left[x] is a pointer to the node's left sibling
* or the null pointer if this is the left-most
* node at this level in the tree.
*
* f. right[x] is a pointer to the node's right
* sibling or the null pointer if this is the
* right-most node at this level in the tree.
*
* 3. The keys key[x,i] partition the key spaces of the
* children of x:
*
* key[x,i] <= key[child[x,i],j] <= key[x,i+1]
*
* for any valid combination of i and j.
*
* 4. There are lower and upper bounds on the number of
* child pointers a node can contain. These bounds
* can be expressed in terms of a fixed integer k>=2
* called the `minimum degree' of the B-tree.
*
* a. Every node other than the root must have at least
* k child pointers and k+1 keys. If the tree is
* nonempty, the root must have at least one child
* pointer and two keys.
*
* b. Every node can contain at most 2k child pointers
* and 2k+1 keys. A node is `full' if it contains
* exactly 2k child pointers and 2k+1 keys.
*
* 5. When searching for a particular value, V, and
* key[V] = key[x,i] for some node x and entry i,
* then:
*
* a. If i=0 the child[0] is followed.
*
* b. If i=n[x] the child[n[x]-1] is followed.
*
* c. Otherwise, the child that is followed
* (either child[x,i-1] or child[x,i]) is
* determined by the type of object to which the
* leaf nodes of the tree point and is controlled
* by the key comparison function registered for
* that type of B-tree.
*
*
*-------------------------------------------------------------------------
*/
/****************/
/* Module Setup */
/****************/
#include "H5Bmodule.h" /* This source code file is part of the H5B module */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#include "H5Bpkg.h" /* B-link trees */
#include "H5CXprivate.h" /* API Contexts */
#include "H5Eprivate.h" /* Error handling */
#include "H5FLprivate.h" /* Free Lists */
#include "H5MFprivate.h" /* File memory management */
#include "H5MMprivate.h" /* Memory management */
/****************/
/* Local Macros */
/****************/
#define H5B_SIZEOF_HDR(F) \
(H5_SIZEOF_MAGIC + /*magic number */ \
4 + /*type, level, num entries */ \
2 * H5F_SIZEOF_ADDR(F)) /*left and right sibling addresses */
/* Default initializer for H5B_ins_ud_t */
#define H5B_INS_UD_T_NULL \
{ \
NULL, HADDR_UNDEF, H5AC__NO_FLAGS_SET \
}
/******************/
/* Local Typedefs */
/******************/
/* "user data" for iterating over B-tree (collects B-tree metadata size) */
typedef struct H5B_iter_ud_t {
H5B_info_t *bt_info; /* Information about B-tree */
void *udata; /* Node type's 'udata' for loading & iterator callback */
} H5B_info_ud_t;
/* Convenience struct for the arguments needed to unprotect a b-tree after a
* call to H5B__iterate_helper() or H5B__split() */
typedef struct H5B_ins_ud_t {
H5B_t *bt; /* B-tree */
haddr_t addr; /* B-tree address */
unsigned cache_flags; /* Cache flags for H5AC_unprotect() */
} H5B_ins_ud_t;
/********************/
/* Local Prototypes */
/********************/
static H5B_ins_t H5B__insert_helper(H5F_t *f, H5B_ins_ud_t *bt_ud, const H5B_class_t *type, uint8_t *lt_key,
bool *lt_key_changed, uint8_t *md_key, void *udata, uint8_t *rt_key,
bool *rt_key_changed, H5B_ins_ud_t *split_bt_ud /*out*/);
static herr_t H5B__insert_child(H5B_t *bt, unsigned *bt_flags, unsigned idx, haddr_t child, H5B_ins_t anchor,
const void *md_key);
static herr_t H5B__split(H5F_t *f, H5B_ins_ud_t *bt_ud, unsigned idx, void *udata,
H5B_ins_ud_t *split_bt_ud /*out*/);
static H5B_t *H5B__copy(const H5B_t *old_bt);
/*********************/
/* Package Variables */
/*********************/
/* Declare a free list to manage the haddr_t sequence information */
H5FL_SEQ_DEFINE(haddr_t);
/* Declare a PQ free list to manage the native block information */
H5FL_BLK_DEFINE(native_block);
/* Declare a free list to manage the H5B_t struct */
H5FL_DEFINE(H5B_t);
/*****************************/
/* Library Private Variables */
/*****************************/
/*******************/
/* Local Variables */
/*******************/
/* Declare a free list to manage the H5B_shared_t struct */
H5FL_DEFINE_STATIC(H5B_shared_t);
/* Declare a free list to manage the raw page information */
H5FL_BLK_DEFINE_STATIC(page);
/* Declare a free list to manage the native key offset sequence information */
H5FL_SEQ_DEFINE_STATIC(size_t);
/*-------------------------------------------------------------------------
* Function: H5B_create
*
* Purpose: Creates a new empty B-tree leaf node. The UDATA pointer is
* passed as an argument to the sizeof_rkey() method for the
* B-tree.
*
* Return: Success: Non-negative, and the address of new node is
* returned through the ADDR_P argument.
*
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
herr_t
H5B_create(H5F_t *f, const H5B_class_t *type, void *udata, haddr_t *addr_p /*out*/)
{
H5B_t *bt = NULL;
H5B_shared_t *shared = NULL; /* Pointer to shared B-tree info */
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI(FAIL)
/*
* Check arguments.
*/
assert(f);
assert(type);
assert(addr_p);
/*
* Allocate file and memory data structures.
*/
if (NULL == (bt = H5FL_MALLOC(H5B_t)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTALLOC, FAIL, "memory allocation failed for B-tree root node");
memset(&bt->cache_info, 0, sizeof(H5AC_info_t));
bt->level = 0;
bt->left = HADDR_UNDEF;
bt->right = HADDR_UNDEF;
bt->nchildren = 0;
if (NULL == (bt->rc_shared = (type->get_shared)(f, udata)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "can't retrieve B-tree node buffer");
H5UC_INC(bt->rc_shared);
shared = (H5B_shared_t *)H5UC_GET_OBJ(bt->rc_shared);
assert(shared);
if (NULL == (bt->native = H5FL_BLK_MALLOC(native_block, shared->sizeof_keys)) ||
NULL == (bt->child = H5FL_SEQ_MALLOC(haddr_t, (size_t)shared->two_k)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTALLOC, FAIL, "memory allocation failed for B-tree root node");
if (HADDR_UNDEF == (*addr_p = H5MF_alloc(f, H5FD_MEM_BTREE, (hsize_t)shared->sizeof_rnode)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTALLOC, FAIL, "file allocation failed for B-tree root node");
/*
* Cache the new B-tree node.
*/
if (H5AC_insert_entry(f, H5AC_BT, *addr_p, bt, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "can't add B-tree root node to cache");
done:
if (ret_value < 0) {
if (shared && shared->sizeof_rnode > 0) {
H5_CHECK_OVERFLOW(shared->sizeof_rnode, size_t, hsize_t);
(void)H5MF_xfree(f, H5FD_MEM_BTREE, *addr_p, (hsize_t)shared->sizeof_rnode);
} /* end if */
if (bt)
/* Destroy B-tree node */
if (H5B__node_dest(bt) < 0)
HDONE_ERROR(H5E_BTREE, H5E_CANTFREE, FAIL, "unable to destroy B-tree node");
} /* end if */
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B_create() */
/*-------------------------------------------------------------------------
* Function: H5B_find
*
* Purpose: Locate the specified information in a B-tree and return
* that information by filling in fields of the caller-supplied
* UDATA pointer depending on the type of leaf node
* requested. The UDATA can point to additional data passed
* to the key comparison function.
*
* Note: This function does not follow the left/right sibling
* pointers since it assumes that all nodes can be reached
* from the parent node.
*
* Return: Non-negative (true/false) on success (if found, values returned
* through the UDATA argument). Negative on failure (if not found,
* UDATA is undefined).
*
*-------------------------------------------------------------------------
*/
herr_t
H5B_find(H5F_t *f, const H5B_class_t *type, haddr_t addr, bool *found, void *udata)
{
H5B_t *bt = NULL;
H5UC_t *rc_shared; /* Ref-counted shared info */
H5B_shared_t *shared; /* Pointer to shared B-tree info */
H5B_cache_ud_t cache_udata; /* User-data for metadata cache callback */
unsigned idx = 0, lt = 0, rt; /* Final, left & right key indices */
int cmp = 1; /* Key comparison value */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI(FAIL)
/*
* Check arguments.
*/
assert(f);
assert(type);
assert(type->decode);
assert(type->cmp3);
assert(type->found);
assert(H5_addr_defined(addr));
/* Get shared info for B-tree */
if (NULL == (rc_shared = (type->get_shared)(f, udata)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "can't retrieve B-tree's shared ref. count object");
shared = (H5B_shared_t *)H5UC_GET_OBJ(rc_shared);
assert(shared);
/*
* Perform a binary search to locate the child which contains
* the thing for which we're searching.
*/
cache_udata.f = f;
cache_udata.type = type;
cache_udata.rc_shared = rc_shared;
if (NULL == (bt = (H5B_t *)H5AC_protect(f, H5AC_BT, addr, &cache_udata, H5AC__READ_ONLY_FLAG)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load B-tree node");
rt = bt->nchildren;
while (lt < rt && cmp) {
idx = (lt + rt) / 2;
/* compare */
if ((cmp = (type->cmp3)(H5B_NKEY(bt, shared, idx), udata, H5B_NKEY(bt, shared, (idx + 1)))) < 0)
rt = idx;
else
lt = idx + 1;
} /* end while */
/* Check if not found */
if (cmp)
*found = false;
else {
/*
* Follow the link to the subtree or to the data node.
*/
assert(idx < bt->nchildren);
if (bt->level > 0) {
if ((ret_value = H5B_find(f, type, bt->child[idx], found, udata)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_NOTFOUND, FAIL, "can't lookup key in subtree");
} /* end if */
else {
if ((ret_value = (type->found)(f, bt->child[idx], H5B_NKEY(bt, shared, idx), found, udata)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_NOTFOUND, FAIL, "can't lookup key in leaf node");
} /* end else */
} /* end else */
done:
if (bt && H5AC_unprotect(f, H5AC_BT, addr, bt, H5AC__NO_FLAGS_SET) < 0)
HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release node");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B_find() */
/*-------------------------------------------------------------------------
* Function: H5B__split
*
* Purpose: Split a single node into two nodes. The old node will
* contain the left children and the new node will contain the
* right children.
*
* The UDATA pointer is passed to the sizeof_rkey() method but is
* otherwise unused.
*
* The BT_UD argument is a pointer to a protected B-tree
* node.
*
* Return: Non-negative on success (The address of the new node is
* returned through the NEW_ADDR argument). Negative on failure.
*
*-------------------------------------------------------------------------
*/
static herr_t
H5B__split(H5F_t *f, H5B_ins_ud_t *bt_ud, unsigned idx, void *udata, H5B_ins_ud_t *split_bt_ud /*out*/)
{
H5B_shared_t *shared; /* Pointer to shared B-tree info */
H5B_cache_ud_t cache_udata; /* User-data for metadata cache callback */
unsigned nleft, nright; /* Number of keys in left & right halves */
double split_ratios[3]; /* B-tree split ratios */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/*
* Check arguments.
*/
assert(f);
assert(bt_ud);
assert(bt_ud->bt);
assert(H5_addr_defined(bt_ud->addr));
assert(split_bt_ud);
assert(!split_bt_ud->bt);
/*
* Initialize variables.
*/
shared = (H5B_shared_t *)H5UC_GET_OBJ(bt_ud->bt->rc_shared);
assert(shared);
assert(bt_ud->bt->nchildren == shared->two_k);
/* Get B-tree split ratios */
if (H5CX_get_btree_split_ratios(split_ratios) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "can't retrieve B-tree split ratios");
/*
* Decide how to split the children of the old node among the old node
* and the new node.
*/
if (!H5_addr_defined(bt_ud->bt->right))
nleft = (unsigned)((double)shared->two_k * split_ratios[2]); /*right*/
else if (!H5_addr_defined(bt_ud->bt->left))
nleft = (unsigned)((double)shared->two_k * split_ratios[0]); /*left*/
else
nleft = (unsigned)((double)shared->two_k * split_ratios[1]); /*middle*/
/*
* Keep the new child in the same node as the child that split. This can
* result in nodes that have an unused child when data is written
* sequentially, but it simplifies stuff below.
*/
if (idx < nleft && nleft == shared->two_k)
--nleft;
else if (idx >= nleft && 0 == nleft)
nleft++;
nright = shared->two_k - nleft;
/*
* Create the new B-tree node.
*/
if (H5B_create(f, shared->type, udata, &split_bt_ud->addr /*out*/) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to create B-tree");
cache_udata.f = f;
cache_udata.type = shared->type;
cache_udata.rc_shared = bt_ud->bt->rc_shared;
if (NULL == (split_bt_ud->bt =
(H5B_t *)H5AC_protect(f, H5AC_BT, split_bt_ud->addr, &cache_udata, H5AC__NO_FLAGS_SET)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree");
split_bt_ud->bt->level = bt_ud->bt->level;
/*
* Copy data from the old node to the new node.
*/
split_bt_ud->cache_flags = H5AC__DIRTIED_FLAG;
H5MM_memcpy(split_bt_ud->bt->native, bt_ud->bt->native + nleft * shared->type->sizeof_nkey,
(nright + 1) * shared->type->sizeof_nkey);
H5MM_memcpy(split_bt_ud->bt->child, &bt_ud->bt->child[nleft], nright * sizeof(haddr_t));
split_bt_ud->bt->nchildren = nright;
/*
* Truncate the old node.
*/
bt_ud->cache_flags |= H5AC__DIRTIED_FLAG;
bt_ud->bt->nchildren = nleft;
/*
* Update other sibling pointers.
*/
split_bt_ud->bt->left = bt_ud->addr;
split_bt_ud->bt->right = bt_ud->bt->right;
if (H5_addr_defined(bt_ud->bt->right)) {
H5B_t *tmp_bt;
if (NULL ==
(tmp_bt = (H5B_t *)H5AC_protect(f, H5AC_BT, bt_ud->bt->right, &cache_udata, H5AC__NO_FLAGS_SET)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to load right sibling");
tmp_bt->left = split_bt_ud->addr;
if (H5AC_unprotect(f, H5AC_BT, bt_ud->bt->right, tmp_bt, H5AC__DIRTIED_FLAG) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node");
} /* end if */
bt_ud->bt->right = split_bt_ud->addr;
assert(bt_ud->cache_flags & H5AC__DIRTIED_FLAG);
done:
if (ret_value < 0) {
if (split_bt_ud->bt &&
H5AC_unprotect(f, H5AC_BT, split_bt_ud->addr, split_bt_ud->bt, split_bt_ud->cache_flags) < 0)
HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node");
split_bt_ud->bt = NULL;
split_bt_ud->addr = HADDR_UNDEF;
split_bt_ud->cache_flags = H5AC__NO_FLAGS_SET;
} /* end if */
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B__split() */
/*-------------------------------------------------------------------------
* Function: H5B_insert
*
* Purpose: Adds a new item to the B-tree.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5B_insert(H5F_t *f, const H5B_class_t *type, haddr_t addr, void *udata)
{
/*
* These are defined this way to satisfy alignment constraints.
*/
uint64_t _lt_key[128], _md_key[128], _rt_key[128];
uint8_t *lt_key = (uint8_t *)_lt_key;
uint8_t *md_key = (uint8_t *)_md_key;
uint8_t *rt_key = (uint8_t *)_rt_key;
bool lt_key_changed = false, rt_key_changed = false;
haddr_t old_root_addr = HADDR_UNDEF;
unsigned level;
H5B_ins_ud_t bt_ud = H5B_INS_UD_T_NULL; /* (Old) root node */
H5B_ins_ud_t split_bt_ud = H5B_INS_UD_T_NULL; /* Split B-tree node */
H5B_t *new_root_bt = NULL; /* New root node */
H5UC_t *rc_shared; /* Ref-counted shared info */
H5B_shared_t *shared; /* Pointer to shared B-tree info */
H5B_cache_ud_t cache_udata; /* User-data for metadata cache callback */
H5B_ins_t my_ins = H5B_INS_ERROR;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI(FAIL)
/* Check arguments. */
assert(f);
assert(type);
assert(type->sizeof_nkey <= sizeof _lt_key);
assert(H5_addr_defined(addr));
/* Get shared info for B-tree */
if (NULL == (rc_shared = (type->get_shared)(f, udata)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "can't retrieve B-tree's shared ref. count object");
shared = (H5B_shared_t *)H5UC_GET_OBJ(rc_shared);
assert(shared);
/* Protect the root node */
cache_udata.f = f;
cache_udata.type = type;
cache_udata.rc_shared = rc_shared;
bt_ud.addr = addr;
if (NULL == (bt_ud.bt = (H5B_t *)H5AC_protect(f, H5AC_BT, addr, &cache_udata, H5AC__NO_FLAGS_SET)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to locate root of B-tree");
/* Insert the object */
if ((int)(my_ins = H5B__insert_helper(f, &bt_ud, type, lt_key, <_key_changed, md_key, udata, rt_key,
&rt_key_changed, &split_bt_ud /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to insert key");
/* Check if the root node split */
if (H5B_INS_NOOP == my_ins) {
/* The root node did not split - just return */
assert(!split_bt_ud.bt);
HGOTO_DONE(SUCCEED);
} /* end if */
assert(H5B_INS_RIGHT == my_ins);
assert(split_bt_ud.bt);
assert(H5_addr_defined(split_bt_ud.addr));
/* Get level of old root */
level = bt_ud.bt->level;
/* update left and right keys */
if (!lt_key_changed)
H5MM_memcpy(lt_key, H5B_NKEY(bt_ud.bt, shared, 0), type->sizeof_nkey);
if (!rt_key_changed)
H5MM_memcpy(rt_key, H5B_NKEY(split_bt_ud.bt, shared, split_bt_ud.bt->nchildren), type->sizeof_nkey);
/*
* Copy the old root node to some other file location and make the new root
* at the old root's previous address. This prevents the B-tree from
* "moving".
*/
H5_CHECK_OVERFLOW(shared->sizeof_rnode, size_t, hsize_t);
if (HADDR_UNDEF == (old_root_addr = H5MF_alloc(f, H5FD_MEM_BTREE, (hsize_t)shared->sizeof_rnode)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTALLOC, FAIL, "unable to allocate file space to move root");
/*
* Move the node to the new location
*/
/* Make a copy of the old root information */
if (NULL == (new_root_bt = H5B__copy(bt_ud.bt)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTCOPY, FAIL, "unable to copy old root");
/* Unprotect the old root so we can move it. Also force it to be marked
* dirty so it is written to the new location. */
if (H5AC_unprotect(f, H5AC_BT, bt_ud.addr, bt_ud.bt, H5AC__DIRTIED_FLAG) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release old root");
bt_ud.bt = NULL; /* Make certain future references will be caught */
/* Move the location of the old root on the disk */
if (H5AC_move_entry(f, H5AC_BT, bt_ud.addr, old_root_addr) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to move B-tree root node");
bt_ud.addr = old_root_addr;
/* Update the split b-tree's left pointer to point to the new location */
split_bt_ud.bt->left = bt_ud.addr;
split_bt_ud.cache_flags |= H5AC__DIRTIED_FLAG;
/* clear the old root info at the old address (we already copied it) */
new_root_bt->left = HADDR_UNDEF;
new_root_bt->right = HADDR_UNDEF;
/* Set the new information for the copy */
new_root_bt->level = level + 1;
new_root_bt->nchildren = 2;
new_root_bt->child[0] = bt_ud.addr;
H5MM_memcpy(H5B_NKEY(new_root_bt, shared, 0), lt_key, shared->type->sizeof_nkey);
new_root_bt->child[1] = split_bt_ud.addr;
H5MM_memcpy(H5B_NKEY(new_root_bt, shared, 1), md_key, shared->type->sizeof_nkey);
H5MM_memcpy(H5B_NKEY(new_root_bt, shared, 2), rt_key, shared->type->sizeof_nkey);
/* Insert the modified copy of the old root into the file again */
if (H5AC_insert_entry(f, H5AC_BT, addr, new_root_bt, H5AC__NO_FLAGS_SET) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTFLUSH, FAIL, "unable to add old B-tree root node to cache");
done:
if (ret_value < 0)
if (new_root_bt && H5B__node_dest(new_root_bt) < 0)
HDONE_ERROR(H5E_BTREE, H5E_CANTRELEASE, FAIL, "unable to free B-tree root node");
if (bt_ud.bt)
if (H5AC_unprotect(f, H5AC_BT, bt_ud.addr, bt_ud.bt, bt_ud.cache_flags) < 0)
HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to unprotect old root");
if (split_bt_ud.bt)
if (H5AC_unprotect(f, H5AC_BT, split_bt_ud.addr, split_bt_ud.bt, split_bt_ud.cache_flags) < 0)
HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to unprotect new child");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B_insert() */
/*-------------------------------------------------------------------------
* Function: H5B__insert_child
*
* Purpose: Insert a child to the left or right of child[IDX] depending
* on whether ANCHOR is H5B_INS_LEFT or H5B_INS_RIGHT. The BT
* argument is a pointer to a protected B-tree node.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5B__insert_child(H5B_t *bt, unsigned *bt_flags, unsigned idx, haddr_t child, H5B_ins_t anchor,
const void *md_key)
{
H5B_shared_t *shared; /* Pointer to shared B-tree info */
uint8_t *base; /* Base offset for move */
FUNC_ENTER_PACKAGE_NOERR
assert(bt);
assert(bt_flags);
assert(H5_addr_defined(child));
shared = (H5B_shared_t *)H5UC_GET_OBJ(bt->rc_shared);
assert(shared);
assert(bt->nchildren < shared->two_k);
/* Check for inserting right-most key into node (common when just appending
* records to an unlimited dimension chunked dataset)
*/
base = H5B_NKEY(bt, shared, (idx + 1));
if ((idx + 1) == bt->nchildren) {
/* Make room for the new key */
H5MM_memcpy(base + shared->type->sizeof_nkey, base,
shared->type->sizeof_nkey); /* No overlap possible - memcpy() OK */
H5MM_memcpy(base, md_key, shared->type->sizeof_nkey);
/* The MD_KEY is the left key of the new node */
if (H5B_INS_RIGHT == anchor)
idx++; /* Don't have to memmove() child addresses down, just add new child */
else
/* Make room for the new child address */
bt->child[idx + 1] = bt->child[idx];
} /* end if */
else {
/* Make room for the new key */
memmove(base + shared->type->sizeof_nkey, base, (bt->nchildren - idx) * shared->type->sizeof_nkey);
H5MM_memcpy(base, md_key, shared->type->sizeof_nkey);
/* The MD_KEY is the left key of the new node */
if (H5B_INS_RIGHT == anchor)
idx++;
/* Make room for the new child address */
memmove(bt->child + idx + 1, bt->child + idx, (bt->nchildren - idx) * sizeof(haddr_t));
} /* end if */
bt->child[idx] = child;
bt->nchildren += 1;
/* Mark node as dirty */
*bt_flags |= H5AC__DIRTIED_FLAG;
FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5B_insert_child() */
/*-------------------------------------------------------------------------
* Function: H5B__insert_helper
*
* Purpose: Inserts the item UDATA into the tree rooted at ADDR and having
* the specified type.
*
* On return, if LT_KEY_CHANGED is non-zero, then LT_KEY is
* the new native left key. Similarly for RT_KEY_CHANGED
* and RT_KEY.
*
* If the node splits, then MD_KEY contains the key that
* was split between the two nodes (that is, the key that
* appears as the max key in the left node and the min key
* in the right node).
*
* Return: Success: A B-tree operation. The address of the new
* node, if the node splits, is returned through
* the NEW_NODE_P argument. The new node is always
* to the right of the previous node. This
* function is called recursively and the return
* value influences the behavior of the caller.
* See also, declaration of H5B_ins_t.
*
* Failure: H5B_INS_ERROR
*
*-------------------------------------------------------------------------
*/
static H5B_ins_t
H5B__insert_helper(H5F_t *f, H5B_ins_ud_t *bt_ud, const H5B_class_t *type, uint8_t *lt_key,
bool *lt_key_changed, uint8_t *md_key, void *udata, uint8_t *rt_key, bool *rt_key_changed,
H5B_ins_ud_t *split_bt_ud /*out*/)
{
H5B_t *bt; /* Convenience pointer to B-tree */
H5UC_t *rc_shared; /* Ref-counted shared info */
H5B_shared_t *shared; /* Pointer to shared B-tree info */
H5B_cache_ud_t cache_udata; /* User-data for metadata cache callback */
unsigned lt = 0, idx = 0, rt; /* Left, final & right index values */
int cmp = -1; /* Key comparison value */
H5B_ins_ud_t child_bt_ud = H5B_INS_UD_T_NULL; /* Child B-tree */
H5B_ins_ud_t new_child_bt_ud = H5B_INS_UD_T_NULL; /* Newly split child B-tree */
H5B_ins_t my_ins = H5B_INS_ERROR;
H5B_ins_t ret_value = H5B_INS_ERROR; /* Return value */
FUNC_ENTER_PACKAGE
/*
* Check arguments
*/
assert(f);
assert(bt_ud);
assert(bt_ud->bt);
assert(H5_addr_defined(bt_ud->addr));
assert(type);
assert(type->decode);
assert(type->cmp3);
assert(type->new_node);
assert(lt_key);
assert(lt_key_changed);
assert(rt_key);
assert(rt_key_changed);
assert(split_bt_ud);
assert(!split_bt_ud->bt);
assert(!H5_addr_defined(split_bt_ud->addr));
assert(split_bt_ud->cache_flags == H5AC__NO_FLAGS_SET);
bt = bt_ud->bt;
*lt_key_changed = false;
*rt_key_changed = false;
/* Get shared info for B-tree */
if (NULL == (rc_shared = (type->get_shared)(f, udata)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, H5B_INS_ERROR,
"can't retrieve B-tree's shared ref. count object");
shared = (H5B_shared_t *)H5UC_GET_OBJ(rc_shared);
assert(shared);
/*
* Use a binary search to find the child that will receive the new
* data. When the search completes IDX points to the child that
* should get the new data.
*/
rt = bt->nchildren;
while (lt < rt && cmp) {
idx = (lt + rt) / 2;
if ((cmp = (type->cmp3)(H5B_NKEY(bt, shared, idx), udata, H5B_NKEY(bt, shared, idx + 1))) < 0)
rt = idx;
else
lt = idx + 1;
} /* end while */
/* Set up user data for cache callbacks */
cache_udata.f = f;
cache_udata.type = type;
cache_udata.rc_shared = rc_shared;
if (0 == bt->nchildren) {
/*
* The value being inserted will be the only value in this tree. We
* must necessarily be at level zero.
*/
assert(0 == bt->level);
if ((type->new_node)(f, H5B_INS_FIRST, H5B_NKEY(bt, shared, 0), udata, H5B_NKEY(bt, shared, 1),
bt->child + 0 /*out*/) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, H5B_INS_ERROR, "unable to create leaf node");
bt->nchildren = 1;
bt_ud->cache_flags |= H5AC__DIRTIED_FLAG;
idx = 0;
if (type->follow_min) {
if ((int)(my_ins = (type->insert)(f, bt->child[idx], H5B_NKEY(bt, shared, idx), lt_key_changed,
md_key, udata, H5B_NKEY(bt, shared, idx + 1), rt_key_changed,
&new_child_bt_ud.addr /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "unable to insert first leaf node");
} /* end if */
else
my_ins = H5B_INS_NOOP;
}
else if (cmp < 0 && idx == 0) {
if (bt->level > 0) {
/*
* The value being inserted is less than any value in this tree.
* Follow the minimum branch out of this node to a subtree.
*/
child_bt_ud.addr = bt->child[idx];
if (NULL == (child_bt_ud.bt = (H5B_t *)H5AC_protect(f, H5AC_BT, child_bt_ud.addr, &cache_udata,
H5AC__NO_FLAGS_SET)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, H5B_INS_ERROR, "unable to load node");
if ((int)(my_ins = H5B__insert_helper(
f, &child_bt_ud, type, H5B_NKEY(bt, shared, idx), lt_key_changed, md_key, udata,
H5B_NKEY(bt, shared, idx + 1), rt_key_changed, &new_child_bt_ud /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert minimum subtree");
}
else if (type->follow_min) {
/*
* The value being inserted is less than any leaf node out of this
* current node. Follow the minimum branch to a leaf node and let
* the subclass handle the problem.
*/
if ((int)(my_ins = (type->insert)(f, bt->child[idx], H5B_NKEY(bt, shared, idx), lt_key_changed,
md_key, udata, H5B_NKEY(bt, shared, idx + 1), rt_key_changed,
&new_child_bt_ud.addr /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert minimum leaf node");
}
else {
/*
* The value being inserted is less than any leaf node out of the
* current node. Create a new minimum leaf node out of this B-tree
* node. This node is not empty (handled above).
*/
my_ins = H5B_INS_LEFT;
H5MM_memcpy(md_key, H5B_NKEY(bt, shared, idx), type->sizeof_nkey);
if ((type->new_node)(f, H5B_INS_LEFT, H5B_NKEY(bt, shared, idx), udata, md_key,
&new_child_bt_ud.addr /*out*/) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert minimum leaf node");
*lt_key_changed = true;
} /* end else */
#ifdef H5_STRICT_FORMAT_CHECKS
/* Since we are to the left of the leftmost key there must not be a left
* sibling */
if (H5_addr_defined(bt->left))
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR,
"internal error: likely corrupt key values");
#endif /* H5_STRICT_FORMAT_CHECKS */
}
else if (cmp > 0 && idx + 1 >= bt->nchildren) {
if (bt->level > 0) {
/*
* The value being inserted is larger than any value in this tree.
* Follow the maximum branch out of this node to a subtree.
*/
idx = bt->nchildren - 1;
child_bt_ud.addr = bt->child[idx];
if (NULL == (child_bt_ud.bt = (H5B_t *)H5AC_protect(f, H5AC_BT, child_bt_ud.addr, &cache_udata,
H5AC__NO_FLAGS_SET)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, H5B_INS_ERROR, "unable to load node");
if ((int)(my_ins = H5B__insert_helper(
f, &child_bt_ud, type, H5B_NKEY(bt, shared, idx), lt_key_changed, md_key, udata,
H5B_NKEY(bt, shared, idx + 1), rt_key_changed, &new_child_bt_ud /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert maximum subtree");
}
else if (type->follow_max) {
/*
* The value being inserted is larger than any leaf node out of the
* current node. Follow the maximum branch to a leaf node and let
* the subclass handle the problem.
*/
idx = bt->nchildren - 1;
if ((int)(my_ins = (type->insert)(f, bt->child[idx], H5B_NKEY(bt, shared, idx), lt_key_changed,
md_key, udata, H5B_NKEY(bt, shared, idx + 1), rt_key_changed,
&new_child_bt_ud.addr /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert maximum leaf node");
}
else {
/*
* The value being inserted is larger than any leaf node out of the
* current node. Create a new maximum leaf node out of this B-tree
* node.
*/
idx = bt->nchildren - 1;
my_ins = H5B_INS_RIGHT;
H5MM_memcpy(md_key, H5B_NKEY(bt, shared, idx + 1), type->sizeof_nkey);
if ((type->new_node)(f, H5B_INS_RIGHT, md_key, udata, H5B_NKEY(bt, shared, idx + 1),
&new_child_bt_ud.addr /*out*/) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert maximum leaf node");
*rt_key_changed = true;
} /* end else */
#ifdef H5_STRICT_FORMAT_CHECKS
/* Since we are to the right of the rightmost key there must not be a
* right sibling */
if (H5_addr_defined(bt->right))
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR,
"internal error: likely corrupt key values");
#endif /* H5_STRICT_FORMAT_CHECKS */
}
else if (cmp) {
/* We couldn't figure out which branch to follow out of this node */
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR,
"internal error: could not determine which branch to follow out of this node");
}
else if (bt->level > 0) {
/*
* Follow a branch out of this node to another subtree.
*/
assert(idx < bt->nchildren);
child_bt_ud.addr = bt->child[idx];
if (NULL == (child_bt_ud.bt = (H5B_t *)H5AC_protect(f, H5AC_BT, child_bt_ud.addr, &cache_udata,
H5AC__NO_FLAGS_SET)))
HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, H5B_INS_ERROR, "unable to load node");
if ((int)(my_ins = H5B__insert_helper(f, &child_bt_ud, type, H5B_NKEY(bt, shared, idx),
lt_key_changed, md_key, udata, H5B_NKEY(bt, shared, idx + 1),
rt_key_changed, &new_child_bt_ud /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert subtree");
}
else {
/*
* Follow a branch out of this node to a leaf node of some other type.
*/
assert(idx < bt->nchildren);
if ((int)(my_ins = (type->insert)(f, bt->child[idx], H5B_NKEY(bt, shared, idx), lt_key_changed,
md_key, udata, H5B_NKEY(bt, shared, idx + 1), rt_key_changed,
&new_child_bt_ud.addr /*out*/)) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, H5B_INS_ERROR, "can't insert leaf node");
}
assert((int)my_ins >= 0);
/*
* Update the left and right keys of the current node.
*/
if (*lt_key_changed) {
bt_ud->cache_flags |= H5AC__DIRTIED_FLAG;
if (idx > 0) {
assert(type->critical_key == H5B_LEFT);
assert(!(H5B_INS_LEFT == my_ins || H5B_INS_RIGHT == my_ins));
*lt_key_changed = false;
} /* end if */
else
H5MM_memcpy(lt_key, H5B_NKEY(bt, shared, idx), type->sizeof_nkey);
} /* end if */
if (*rt_key_changed) {
bt_ud->cache_flags |= H5AC__DIRTIED_FLAG;
if (idx + 1 < bt->nchildren) {
assert(type->critical_key == H5B_RIGHT);
assert(!(H5B_INS_LEFT == my_ins || H5B_INS_RIGHT == my_ins));
*rt_key_changed = false;
} /* end if */
else
H5MM_memcpy(rt_key, H5B_NKEY(bt, shared, idx + 1), type->sizeof_nkey);
} /* end if */
/*
* Handle changes/additions to children
*/
assert(!(bt->level == 0) != !(child_bt_ud.bt));
if (H5B_INS_CHANGE == my_ins) {
/*
* The insertion simply changed the address for the child.
*/
assert(!child_bt_ud.bt);
assert(bt->level == 0);
bt->child[idx] = new_child_bt_ud.addr;
bt_ud->cache_flags |= H5AC__DIRTIED_FLAG;
}
else if (H5B_INS_LEFT == my_ins || H5B_INS_RIGHT == my_ins) {
unsigned *tmp_bt_flags_ptr = NULL;
H5B_t *tmp_bt;
/*
* If this node is full then split it before inserting the new child.
*/
if (bt->nchildren == shared->two_k) {
if (H5B__split(f, bt_ud, idx, udata, split_bt_ud /*out*/) < 0)
HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, H5B_INS_ERROR, "unable to split node");
if (idx < bt->nchildren) {
tmp_bt = bt;
tmp_bt_flags_ptr = &bt_ud->cache_flags;
}
else {
idx -= bt->nchildren;