📄 ibalance.c
字号:
else { it is the position of the leftmost pointer that must be deleted (together with its corresponding key to the left of the pointer) as a result of the previous level's balancing. }*/{ struct buffer_head * tbSh = PATH_H_PBUFFER (tb->tb_path, h); struct buffer_info bi; int order; /* we return this: it is 0 if there is no S[h], else it is tb->S[h]->b_item_order */ int insert_num, n, k; struct buffer_head * S_new; struct item_head new_insert_key; struct buffer_head * new_insert_ptr = NULL; struct item_head * new_insert_key_addr = insert_key; order = ( tbSh ) ? PATH_H_POSITION (tb->tb_path, h + 1)/*tb->S[h]->b_item_order*/ : 0; /* Using insert_size[h] calculate the number insert_num of items that must be inserted to or deleted from S[h]. */ insert_num = tb->insert_size[h]/((int)(KEY_SIZE + DC_SIZE)); /* Check whether insert_num is proper **/ /* Make balance in case insert_num < 0 */ if ( insert_num < 0 ) { balance_internal_when_delete (tb, h, child_pos); return order; } k = 0; if ( tb->lnum[h] > 0 ) { /* shift lnum[h] items from S[h] to the left neighbor L[h]. check how many of new items fall into L[h] or CFL[h] after shifting */ n = get_blkh_nr_items (B_BLK_HEAD(tb->L[h])); /* number of items in L[h] */ if ( tb->lnum[h] <= child_pos ) { /* new items don't fall into L[h] or CFL[h] */ internal_shift_left (INTERNAL_SHIFT_FROM_S_TO_L, tb, h, tb->lnum[h]); child_pos -= tb->lnum[h]; } else if ( tb->lnum[h] > child_pos + insert_num ) { /* all new items fall into L[h] */ internal_shift_left (INTERNAL_SHIFT_FROM_S_TO_L, tb, h, tb->lnum[h] - insert_num); /* insert insert_num keys and node-pointers into L[h] */ bi.bi_bh = tb->L[h]; bi.bi_parent = tb->FL[h]; bi.bi_position = get_left_neighbor_position (tb, h); internal_insert_childs (tb->tb_fs, &bi,/*tb->L[h], tb->S[h-1]->b_next*/ n + child_pos + 1, insert_num,insert_key,insert_ptr); insert_num = 0; } else { struct disk_child * dc; /* some items fall into L[h] or CFL[h], but some don't fall */ internal_shift1_left (tb, h, child_pos + 1); /* calculate number of new items that fall into L[h] */ k = tb->lnum[h] - child_pos - 1; bi.bi_bh = tb->L[h]; bi.bi_parent = tb->FL[h]; bi.bi_position = get_left_neighbor_position (tb, h); internal_insert_childs (tb->tb_fs, &bi,/*tb->L[h], tb->S[h-1]->b_next,*/ n + child_pos + 1,k, insert_key,insert_ptr); replace_lkey(tb, h, insert_key + k); /* replace the first node-ptr in S[h] by node-ptr to insert_ptr[k] */ dc = B_N_CHILD(tbSh, 0); set_dc(dc, MAX_CHILD_SIZE(insert_ptr[k]->b_size) - get_blkh_free_space (B_BLK_HEAD(insert_ptr[k])), insert_ptr[k]->b_blocknr); /* set_dc_child_size (dc, MAX_CHILD_SIZE(insert_ptr[k]->b_size) - get_blkh_free_space (B_BLK_HEAD(insert_ptr[k]))); set_dc_child_blocknr (dc, insert_ptr[k]->b_blocknr); */ mark_buffer_dirty (tbSh); k++; insert_key += k; insert_ptr += k; insert_num -= k; child_pos = 0; } } /* tb->lnum[h] > 0 */ if ( tb->rnum[h] > 0 ) { /*shift rnum[h] items from S[h] to the right neighbor R[h]*/ /* check how many of new items fall into R or CFR after shifting */ n = get_blkh_nr_items (B_BLK_HEAD (tbSh)); /* number of items in S[h] */ if ( n - tb->rnum[h] >= child_pos ) /* new items fall into S[h] */ /*internal_shift_right(tb,h,tbSh,tb->CFR[h],tb->rkey[h],tb->R[h],tb->rnum[h]);*/ internal_shift_right (INTERNAL_SHIFT_FROM_S_TO_R, tb, h, tb->rnum[h]); else if ( n + insert_num - tb->rnum[h] < child_pos ) { /* all new items fall into R[h] */ internal_shift_right (INTERNAL_SHIFT_FROM_S_TO_R, tb, h, tb->rnum[h] - insert_num); /* insert insert_num keys and node-pointers into R[h] */ bi.bi_bh = tb->R[h]; bi.bi_parent = tb->FR[h]; bi.bi_position = get_right_neighbor_position (tb, h); internal_insert_childs (tb->tb_fs, &bi, /*tb->R[h],tb->S[h-1]->b_next*/ child_pos - n - insert_num + tb->rnum[h] - 1, insert_num,insert_key,insert_ptr); insert_num = 0; } else { struct disk_child * dc; /* one of the items falls into CFR[h] */ internal_shift1_right(tb, h, n - child_pos + 1); /* calculate number of new items that fall into R[h] */ k = tb->rnum[h] - n + child_pos - 1; bi.bi_bh = tb->R[h]; bi.bi_parent = tb->FR[h]; bi.bi_position = get_right_neighbor_position (tb, h); internal_insert_childs (tb->tb_fs, &bi, /*tb->R[h], tb->R[h]->b_child,*/ 0, k, insert_key + 1, insert_ptr + 1); replace_rkey(tb, h, insert_key + insert_num - k - 1); /* replace the first node-ptr in R[h] by node-ptr insert_ptr[insert_num-k-1]*/ dc = B_N_CHILD(tb->R[h], 0); set_dc(dc, MAX_CHILD_SIZE(insert_ptr[insert_num-k-1]->b_size) - get_blkh_free_space (B_BLK_HEAD(insert_ptr[insert_num-k-1])), insert_ptr[insert_num-k-1]->b_blocknr); /* set_dc_child_size (dc, MAX_CHILD_SIZE(insert_ptr[insert_num-k-1]->b_size) - get_blkh_free_space (B_BLK_HEAD(insert_ptr[insert_num-k-1]))); set_dc_child_blocknr (dc, insert_ptr[insert_num-k-1]->b_blocknr); */ mark_buffer_dirty (tb->R[h]); insert_num -= (k + 1); } } /** Fill new node that appears instead of S[h] **/ if ( ! tb->blknum[h] ) { /* node S[h] is empty now */ /* Mark buffer as invalid and put it to head of free list. */ reiserfs_invalidate_buffer(tb, tbSh, 1);/* do not preserve, internal node*/ return order; } if ( ! tbSh ) { /* create new root */ struct disk_child * dc; struct buffer_head * tbSh_1 = PATH_H_PBUFFER (tb->tb_path, h - 1); struct reiserfs_super_block * sb; if ( tb->blknum[h] != 1 ) reiserfs_panic(0, "balance_internal", "One new node required for creating the new root"); /* S[h] = empty buffer from the list FEB. */ tbSh = get_FEB (tb); set_blkh_level (B_BLK_HEAD(tbSh), h + 1); /* Put the unique node-pointer to S[h] that points to S[h-1]. */ dc = B_N_CHILD(tbSh, 0); set_dc(dc, MAX_CHILD_SIZE (tbSh_1->b_size) - get_blkh_free_space (B_BLK_HEAD(tbSh_1)), tbSh_1->b_blocknr); /* set_dc_child_size (dc, MAX_CHILD_SIZE (tbSh_1->b_size) - get_blkh_free_space (B_BLK_HEAD(tbSh_1))); set_dc_child_blocknr (dc, tbSh_1->b_blocknr); */ tb->insert_size[h] -= DC_SIZE; set_blkh_free_space (B_BLK_HEAD(tbSh), get_blkh_free_space (B_BLK_HEAD(tbSh)) - DC_SIZE); mark_buffer_dirty (tbSh); /* put new root into path structure */ PATH_OFFSET_PBUFFER(tb->tb_path, ILLEGAL_PATH_ELEMENT_OFFSET) = tbSh; /* Change root in structure super block. */ sb = tb->tb_fs->fs_ondisk_sb; set_sb_root_block (sb, tbSh->b_blocknr); set_sb_tree_height (sb, get_sb_tree_height (sb) + 1); mark_buffer_dirty (tb->tb_fs->fs_super_bh); tb->tb_fs->fs_dirt = 1; } if ( tb->blknum[h] == 2 ) { int snum; struct buffer_info dest_bi, src_bi; /* S_new = free buffer from list FEB */ S_new = get_FEB(tb); set_blkh_level (B_BLK_HEAD(S_new), h + 1); dest_bi.bi_bh = S_new; dest_bi.bi_parent = 0; dest_bi.bi_position = 0; src_bi.bi_bh = tbSh; src_bi.bi_parent = PATH_H_PPARENT (tb->tb_path, h); src_bi.bi_position = PATH_H_POSITION (tb->tb_path, h + 1); n = get_blkh_nr_items (B_BLK_HEAD(tbSh)); /* number of items in S[h] */ snum = (insert_num + n + 1)/2; if ( n - snum >= child_pos ) { /* new items don't fall into S_new */ /* store the delimiting key for the next level */ /* new_insert_key = (n - snum)'th key in S[h] */ memcpy (&new_insert_key,B_N_PDELIM_KEY(tbSh,n - snum), KEY_SIZE); /* last parameter is del_par */ internal_move_pointers_items (tb->tb_fs, &dest_bi, &src_bi, LAST_TO_FIRST, snum, 0); } else if ( n + insert_num - snum < child_pos ) { /* all new items fall into S_new */ /* store the delimiting key for the next level */ /* new_insert_key = (n + insert_item - snum)'th key in S[h] */ memcpy(&new_insert_key,B_N_PDELIM_KEY(tbSh,n + insert_num - snum), KEY_SIZE); /* last parameter is del_par */ internal_move_pointers_items (tb->tb_fs, &dest_bi, &src_bi, LAST_TO_FIRST, snum - insert_num, 0); /* internal_move_pointers_items(S_new,tbSh,1,snum - insert_num,0);*/ /* insert insert_num keys and node-pointers into S_new */ internal_insert_childs (tb->tb_fs, &dest_bi, /*S_new,tb->S[h-1]->b_next,*/child_pos - n - insert_num + snum - 1, insert_num,insert_key,insert_ptr); insert_num = 0; } else { struct disk_child * dc; /* some items fall into S_new, but some don't fall */ /* last parameter is del_par */ internal_move_pointers_items (tb->tb_fs, &dest_bi, &src_bi, LAST_TO_FIRST, n - child_pos + 1, 1); /* internal_move_pointers_items(S_new,tbSh,1,n - child_pos + 1,1);*/ /* calculate number of new items that fall into S_new */ k = snum - n + child_pos - 1; internal_insert_childs (tb->tb_fs, &dest_bi, /*S_new,*/ 0, k, insert_key + 1, insert_ptr+1); /* new_insert_key = insert_key[insert_num - k - 1] */ memcpy(&new_insert_key,insert_key + insert_num - k - 1, KEY_SIZE); /* replace first node-ptr in S_new by node-ptr to insert_ptr[insert_num-k-1] */ dc = B_N_CHILD(S_new,0); set_dc(dc, MAX_CHILD_SIZE(insert_ptr[insert_num-k-1]->b_size) - get_blkh_free_space (B_BLK_HEAD(insert_ptr[insert_num-k-1])), insert_ptr[insert_num-k-1]->b_blocknr); /* set_dc_child_size (dc, MAX_CHILD_SIZE(insert_ptr[insert_num-k-1]->b_size) - get_blkh_free_space (B_BLK_HEAD(insert_ptr[insert_num-k-1]))); set_dc_child_blocknr (dc, insert_ptr[insert_num-k-1]->b_blocknr); */ mark_buffer_dirty (S_new); insert_num -= (k + 1); } /* new_insert_ptr = node_pointer to S_new */ new_insert_ptr = S_new; /* S_new->b_count --; */ /*brelse(S_new);*/ } n = get_blkh_nr_items (B_BLK_HEAD(tbSh)); /*number of items in S[h] */ if ( -1 <= child_pos && child_pos <= n && insert_num > 0 ) { bi.bi_bh = tbSh; bi.bi_parent = PATH_H_PPARENT (tb->tb_path, h); bi.bi_position = PATH_H_POSITION (tb->tb_path, h + 1); if (child_pos == -1) { /* this is a little different from original do_balance: here we insert the minimal keys in the tree, that has never happened when file system works */ if (tb->CFL[h-1] || insert_num != 1 || h != 1) die ("balance_internal: invalid child_pos"); /* insert_child (tb->S[h], tb->S[h-1], child_pos, insert_num, B_N_ITEM_HEAD(tb->S[0],0), insert_ptr);*/ internal_insert_childs (tb->tb_fs, &bi, child_pos, insert_num, B_N_PITEM_HEAD (PATH_PLAST_BUFFER (tb->tb_path), 0), insert_ptr); } else internal_insert_childs (tb->tb_fs, &bi, child_pos,insert_num,insert_key,insert_ptr); } memcpy (new_insert_key_addr,&new_insert_key,KEY_SIZE); insert_ptr[0] = new_insert_ptr; return order;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -