22/**
33 * B-Tree删除
44 */
5- class BTree_Delete
5+ final class BTree_Delete
66 implements BTree_Command {
77
8- use BTree_Search;
8+ use BTree_Search,
9+ BTree_Traversal,
10+ BTree_CommandCommon;
911
1012 /**
1113 * 获取实例
@@ -90,7 +92,7 @@ private function _moveKeyToLeaf (BTree_Node $node, $key) {
9092 $ keyRight = $ leafRight ->leftBorderKey ();
9193 $ valueRight = $ leafRight ->match ($ keyRight );
9294 $ node ->replaceKey ($ key , $ keyRight , $ valueRight );
93- $ this ->_store ->writeNode ($ node );
95+ $ this ->_store () ->writeNode ($ node );
9496
9597 return array ($ leafRight , $ keyRight );
9698 }
@@ -113,7 +115,7 @@ private function _deleteKeyFromLeaf (BTree_Node $node, $key) {
113115 ) {
114116
115117 $ node ->delete ($ key );
116- $ this ->_store ->writeNode ($ node );
118+ $ this ->_store () ->writeNode ($ node );
117119
118120 return true ;
119121 }
@@ -161,9 +163,9 @@ private function _deleteMoveLeft (BTree_Node $node, $key) {
161163 $ node ->insert ($ keyParentLeft , $ valueParentLeft , $ neighborLeft ->rightBorderChild (), $ node ->leftBorderChild ());
162164 $ parentNode ->replaceKey ($ keyParentLeft , $ keyLeft , $ valueLeft );
163165 $ neighborLeft ->delete ($ keyLeft , BTree_Node::DELETE_FLAG_RIGHT );
164- $ this ->_store ->writeNode ($ node );
165- $ this ->_store ->writeNode ($ parentNode );
166- $ this ->_store ->writeNode ($ neighborLeft );
166+ $ this ->_store () ->writeNode ($ node );
167+ $ this ->_store () ->writeNode ($ parentNode );
168+ $ this ->_store () ->writeNode ($ neighborLeft );
167169
168170 return true ;
169171 }
@@ -201,9 +203,9 @@ private function _deleteMoveRight (BTree_Node $node, $key) {
201203 $ node ->insert ($ keyParentRight , $ valueParentRight , $ node ->rightBorderChild (), $ neighborRight ->leftBorderChild ());
202204 $ parentNode ->replaceKey ($ keyParentRight , $ keyRight , $ valueRight );
203205 $ neighborRight ->delete ($ keyRight , BTree_Node::DELETE_FLAG_LEFT );
204- $ this ->_store ->writeNode ($ node );
205- $ this ->_store ->writeNode ($ parentNode );
206- $ this ->_store ->writeNode ($ neighborRight );
206+ $ this ->_store () ->writeNode ($ node );
207+ $ this ->_store () ->writeNode ($ parentNode );
208+ $ this ->_store () ->writeNode ($ neighborRight );
207209
208210 return true ;
209211 }
@@ -293,7 +295,7 @@ private function _mergeStore (
293295 $ this ->_mergeStoreParent ($ parent , $ left , $ nextParent );
294296 }
295297
296- $ this ->_store ->writeNode ($ left );
298+ $ this ->_store () ->writeNode ($ left );
297299
298300 return $ left ;
299301 }
@@ -309,13 +311,13 @@ private function _mergeStoreParent (BTree_Node $parent, BTree_Node $left, BTree_
309311
310312 if (count ($ parent ->data ()) == 0 && $ nextParent ->isNewRoot ()) {
311313
312- $ this ->_store ->rootPointer ($ left ->pointer ());
313- $ this ->_store ->saveRootPointer ();
314+ $ this ->_store () ->rootPointer ($ left ->pointer ());
315+ $ this ->_store () ->saveRootPointer ();
314316
315317 return ;
316318 }
317319
318- $ this ->_store ->writeNode ($ parent );
320+ $ this ->_store () ->writeNode ($ parent );
319321 }
320322
321323 /**
@@ -331,7 +333,7 @@ private function _neighborRight (BTree_Node $node) {
331333
332334 if (BTree_Validate::pointer ($ pointerRight )) {
333335
334- return $ this ->_store ->readNode ($ pointerRight , $ parentNode );
336+ return $ this ->_store () ->readNode ($ pointerRight , $ parentNode );
335337 }
336338
337339 return false ;
@@ -350,7 +352,7 @@ private function _neighborLeft (BTree_Node $node) {
350352
351353 if (BTree_Validate::pointer ($ pointerLeft )) {
352354
353- return $ this ->_store ->readNode ($ pointerLeft , $ parentNode );
355+ return $ this ->_store () ->readNode ($ pointerLeft , $ parentNode );
354356 }
355357
356358 return false ;
@@ -363,44 +365,6 @@ private function _neighborLeft (BTree_Node $node) {
363365 */
364366 private function _leastNumberKeys () {
365367
366- return ceil ($ this ->_options ->numberSlots () / 2 ) - 1 ;
367- }
368-
369- /**
370- * 左邻叶节点
371- *
372- * @param int $pointer 指针
373- * @param BTree_Node $parentNode 上级节点
374- * @return BTree_Node 目标节点
375- */
376- private function _searchLeftBorderLeaf ($ pointer , BTree_Node $ parentNode ) {
377-
378- $ currentNode = $ this ->_store ->readNode ($ pointer , $ parentNode );
379-
380- if ($ currentNode ->isLeaf ()) {
381-
382- return $ currentNode ;
383- }
384-
385- return $ this ->_searchLeftBorderLeaf ($ currentNode ->leftBorderChild (), $ currentNode );
386- }
387-
388- /**
389- * 右邻叶节点
390- *
391- * @param int $pointer 指针
392- * @param BTree_Node $parentNode 上级节点
393- * @return BTree_Node 目标节点
394- */
395- private function _searchRightBorderLeaf ($ pointer , $ parentNode ) {
396-
397- $ currentNode = $ this ->_store ->readNode ($ pointer , $ parentNode );
398-
399- if ($ currentNode ->isLeaf ()) {
400-
401- return $ currentNode ;
402- }
403-
404- return $ this ->_searchRightBorderLeaf ($ currentNode ->rightBorderChild (), $ currentNode );
368+ return ceil ($ this ->_options ()->numberSlots () / 2 ) - 1 ;
405369 }
406370}
0 commit comments