Skip to content

Commit 1d1dd73

Browse files
committed
add compare logic for get iterator
1 parent 35d69dd commit 1d1dd73

File tree

12 files changed

+584
-87
lines changed

12 files changed

+584
-87
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* 命令公共成员
4+
*/
5+
trait BTree_CommandCommon {
6+
7+
/**
8+
* 存储
9+
*/
10+
protected $_store;
11+
12+
/**
13+
* 配置数据
14+
*/
15+
protected $_options;
16+
17+
/**
18+
* 获取存储实例
19+
*
20+
* @return BTree_Store 存储实例
21+
*/
22+
protected function _store () {
23+
24+
return $this->_store;
25+
}
26+
27+
/**
28+
* 获取配置实例
29+
*
30+
* @return BTree_Options 配置实例
31+
*/
32+
protected function _options () {
33+
34+
return $this->_options;
35+
}
36+
}

B-Tree/BTree/Compare.class.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* 比较查询逻辑
4+
*/
5+
final class BTree_Compare implements
6+
BTree_Command {
7+
8+
use BTree_Search,
9+
BTree_CommandCommon;
10+
11+
/**
12+
* 获取实例
13+
*
14+
* @param BTree_Store $store 存储
15+
* @param BTree_Options $options 配置
16+
* @return BTree_Insert 本类实例
17+
*/
18+
public static function getInstance (BTree_Store $store, BTree_Options $options) {
19+
20+
return new self($store, $options);
21+
}
22+
23+
/**
24+
* 构造函数
25+
*
26+
* @param BTree_Store $store 存储
27+
* @param BTree_Options $options 配置
28+
*/
29+
private function __construct (BTree_Store $store, BTree_Options $options) {
30+
31+
$this->_store = $store;
32+
$this->_options = $options;
33+
}
34+
35+
/**
36+
* 克隆
37+
*/
38+
private function __clone () {
39+
}
40+
41+
/**
42+
* 执行
43+
*
44+
* @param $params 参数
45+
*/
46+
public function call ($params) {
47+
48+
$enumOperator = array(BTree_Iterator::OPERATOR_GREATER_THAN, BTree_Iterator::OPERATOR_LESS_THAN);
49+
$key = $params['key'];
50+
$operator = isset($params['operator']) && in_array($params['operator'], $enumOperator)
51+
? $params['operator']
52+
: BTree_Iterator::OPERATOR_GREATER_THAN;
53+
$node = $this->_searchNode($key);
54+
55+
if (!($node instanceof BTree_Node)) {
56+
57+
return false;
58+
}
59+
60+
return new BTree_Iterator($this->_store(), $node, $key, $operator);
61+
}
62+
}

B-Tree/BTree/Debug.class.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
class BTree_Debug implements BTree_Command {
66

7-
use BTree_Search;
7+
use BTree_CommandCommon;
88

99
/**
1010
* 获取实例
@@ -30,15 +30,20 @@ private function __construct (BTree_Store $store, BTree_Options $options) {
3030
$this->_options = $options;
3131
}
3232

33+
/**
34+
* 调用本命令逻辑
35+
*
36+
* @param array $params 命令参数
37+
*/
3338
public function call ($params) {
3439

35-
$rootPointer = $this->_store->rootPointer();
40+
$rootPointer = $this->_store()->rootPointer();
3641
$pointer = isset($params['pointer']) && $params['pointer'] > 0
3742
? $params['pointer']
3843
: $rootPointer;
3944
$data = array(
4045
'pointer_root' => $rootPointer,
41-
'target' => $this->_store->readNode($pointer),
46+
'target' => $this->_store()->readNode($pointer),
4247
);
4348

4449
var_dump($data);

B-Tree/BTree/Delete.class.php

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
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
}

B-Tree/BTree/Insert.class.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
final class BTree_Insert
66
implements BTree_Command {
77

8-
use BTree_Search;
8+
use BTree_Search,
9+
BTree_CommandCommon;
910

1011
/**
1112
* 获取实例
@@ -76,7 +77,7 @@ private function _insertNode ($currentNode, $key, $value, $pointerLeft = 0, $poi
7677

7778
$currentNode->insert($key, $value, $pointerLeft, $pointerRight);
7879

79-
return $this->_store->writeNode($currentNode, $this->_options);
80+
return $this->_store()->writeNode($currentNode, $this->_options);
8081
}
8182

8283
/**
@@ -100,8 +101,8 @@ private function _insertNodeBeforeSeparate ($currentNode, $key, $value, $pointer
100101
$rightNode->insert($key, $value, $pointerLeft, $pointerRight);
101102
}
102103

103-
$pointerLeftMid = $this->_store->writeNode($currentNode, $this->_options);
104-
$pointerRightMid = $this->_store->writeNode($rightNode, $this->_options);
104+
$pointerLeftMid = $this->_store()->writeNode($currentNode, $this->_options);
105+
$pointerRightMid = $this->_store()->writeNode($rightNode, $this->_options);
105106

106107
return $this->_insertNode($currentNode->parent(), $keyMid, $valueMid, $pointerLeftMid, $pointerRightMid);
107108
}
@@ -119,8 +120,8 @@ private function _insertNodeAfterSeparate ($currentNode, $key, $value, $pointerL
119120

120121
$currentNode->insert($key, $value, $pointerLeft, $pointerRight);
121122
list($keyMid, $valueMid, $rightNode) = $currentNode->separateRight();
122-
$pointerLeftMid = $this->_store->writeNode($currentNode);
123-
$pointerRightMid = $this->_store->writeNode($rightNode);
123+
$pointerLeftMid = $this->_store()->writeNode($currentNode);
124+
$pointerRightMid = $this->_store()->writeNode($rightNode);
124125

125126
return $this->_insertNode($currentNode->parent(), $keyMid, $valueMid, $pointerLeftMid, $pointerRightMid);
126127
}
@@ -132,7 +133,7 @@ private function _insertNodeAfterSeparate ($currentNode, $key, $value, $pointerL
132133
*/
133134
private function _isSeparateFirst () {
134135

135-
return $this->_options->numberSlots() % 2 == 1;
136+
return $this->_options()->numberSlots() % 2 == 1;
136137
}
137138

138139
/**
@@ -142,6 +143,6 @@ private function _isSeparateFirst () {
142143
*/
143144
private function _isFullNode (BTree_Node $node) {
144145

145-
return count($node->data()) >= $this->_options->numberSlots();
146+
return count($node->data()) >= $this->_options()->numberSlots();
146147
}
147148
}

0 commit comments

Comments
 (0)