package world.hzq.easysql.storage.engine.datastruct;
import java.util.*;
public class BPlusTree<K extends Comparable<K>, E> {
private final int KEY_UPPER_BOUND;
private final int UNDERFLOW_BOUND;
private BPlusTreeNode root;
public BPlusTree(int order) {
this.KEY_UPPER_BOUND = order - 1;
this.UNDERFLOW_BOUND = KEY_UPPER_BOUND / 2;
}
public BPlusTree() {
this.KEY_UPPER_BOUND = 8;
this.UNDERFLOW_BOUND = KEY_UPPER_BOUND / 2;
}
public void insert(K entry, E value) {
if (root == null) {
root = new BPlusTreeLeafNode(asList(entry), asList(asSet(value)));
return;
}
BPlusTreeNode newChildNode = root.insert(entry, value);
if (newChildNode != null) {
K newRootEntry = newChildNode.getClass().equals(BPlusTreeLeafNode.class)
? newChildNode.entries.get(0)
: ((BPlusTreeNonLeafNode) newChildNode).findLeafEntry(newChildNode);
root = new BPlusTreeNonLeafNode(asList(newRootEntry), asList(root, newChildNode));
}
}
public E query(K entry) {
if (root == null) {
return null;
}
return root.query(entry);
}
public List<E> rangeQuery(K startInclude, K endExclude) {
if (root == null) {
return Collections.emptyList();
}
return root.rangeQuery(startInclude, endExclude);
}
public boolean update(K entry, E oldValue, E newValue) {
return root != null && root.update(entry, oldValue, newValue);
}
public boolean remove(K entry, E value) {
if (root == null) {
return false;
}
RemoveResult removeResult = root.remove(entry, value);
if (!removeResult.isRemoved) {
return false;
}
if (root.entries.isEmpty()) {
this.handleRootUnderflow();
}
return true;
}
public boolean remove(K entry) {
if (root == null) {
return false;
}
RemoveResult removeResult = root.remove(entry);
if (!removeResult.isRemoved) {
return false;
}
if (root.entries.isEmpty()) {
this.handleRootUnderflow();
}
return true;
}
private void handleRootUnderflow() {
root = root.getClass().equals(BPlusTreeLeafNode.class) ? null : ((BPlusTreeNonLeafNode) root).children.get(0);
}
@SafeVarargs
private final <T> List<T> asList(T... e) {
List<T> res = new ArrayList<>();
Collections.addAll(res, e);
return res;
}
@SafeVarargs
private final <T> Set<T> asSet(T... e) {
Set<T> res = new HashSet<>();
Collections.addAll(res, e);
return res;
}
@Override
public String toString() {
return root.toString();
}
private abstract class BPlusTreeNode {
protected List<K> entries;
protected boolean isFull() {
return entries.size() == KEY_UPPER_BOUND;
}
protected boolean isUnderflow() {
return entries.size() < UNDERFLOW_BOUND;
}
protected int getMedianIndex() {
return KEY_UPPER_BOUND / 2;
}
protected int findEntryIndex(K entry) {
int l = 0;
int r = entries.size() - 1;
int index = entries.size();
while (l <= r) {
int mid = l + ((r - l) >> 1);
if (entries.get(mid).compareTo(entry) >= 0) {
index = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return index;
}
public abstract List<E> rangeQuery(K startInclude, K endExclude);
public abstract E query(K entry);
public abstract BPlusTreeNode insert(K entry, E value);
public abstract boolean update(K entry, E oldValue, E newValue);
public abstract RemoveResult remove(K entry);
public abstract RemoveResult remove(K entry, E value);
public abstract void combine(BPlusTreeNode neighbor, K parentEntry);
public abstract void borrow(BPlusTreeNode neighbor, K parentEntry, boolean isLeft);
}
private class BPlusTreeNonLeafNode extends BPlusTreeNode {
public List<BPlusTreeNode> children;
public BPlusTreeNonLeafNode(List<K> entries, List<BPlusTreeNode> children) {
this.entries = entries;
this.children = children;
}
@Override
public List<E> rangeQuery(K startInclude, K endExclude) {
return children.get(findChildIndex(findEntryIndex(startInclude), startInclude)).rangeQuery(startInclude, endExclude);
}
@Override
public E query(K entry) {
return children.get(findChildIndex(findEntryIndex(entry), entry)).query(entry);
}
@Override
public boolean update(K entry, E oldValue, E newValue) {
return children.get(findChildIndex(findEntryIndex(entry), entry)).update(entry, oldValue, newValue);
}
@Override
public BPlusTreeNode insert(K entry, E value) {
BPlusTreeNode newChildNode = children.get(findChildIndex(findEntryIndex(entry), entry)).insert(entry, value);
if (newChildNode != null) {
K newEntry = findLeafEntry(newChildNode);
int newEntryIndex = findEntryIndex(newEntry);
if (isFull()) {
BPlusTreeNonLeafNode newNonLeafNode = split();
int medianIndex = getMedianIndex();
if (newEntryIndex < medianIndex) {
entries.add(newEntryIndex, newEntry);
children.add(newEntryIndex + 1, newChildNode);
} else {
int rightIndex = newNonLeafNode.findEntryIndex(newEntry);
newNonLeafNode.entries.add(rightIndex, newEntry);
newNonLeafNode.children.add(rightIndex, newChildNode);
}
newNonLeafNode.entries.remove(0);
return newNonLeafNode;
}
entries.add(newEntryIndex, newEntry);
children.add(newEntryIndex + 1, newChildNode);
}
return null;
}
@Override
public RemoveResult remove(K entry) {
int entryIndex = findEntryIndex(entry);
int childIndex = findChildIndex(entryIndex, entry);
BPlusTreeNode childNode = children.get(childIndex);
RemoveResult removeResult = childNode.remove(entry);
if (!removeResult.isRemoved) {
return removeResult;
}
if (removeResult.isUnderflow) {
this.handleUnderflow(childNode, childIndex, entryIndex);
}
return new RemoveResult(true, isUnderflow());
}
@Override
public RemoveResult remove(K entry, E value) {
int entryIndex = findEntryIndex(entry);
int childIndex = findChildIndex(entryIndex, entry);
BPlusTreeNode childNode = children.get(childIndex);
RemoveResult removeResult = childNode.remove(entry, value);
if (!removeResult.isRemoved) {
return removeResult;
}
if (removeResult.isUnderflow) {
this.handleUnderflow(childNode, childIndex, entryIndex);
}
return new RemoveResult(true, isUnderflow());
}
@Override
public void combine(BPlusTreeNode neighbor, K parentEntry) {
BPlusTreeNonLeafNode nonLeafNode = (BPlusTreeNonLeafNode) neighbor;
this.entries.add(parentEntry);
this.entries.addAll(nonLeafNode.entries);
this.children.addAll(nonLeafNode.children);
}
@O
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Java实现的MySQL数据库.zip (92个子文件)
easysql-master
src
world
hzq
easysql
executor
SelectExecutor.java 10KB
Executor.java 461B
SimpleExecutor.java 93B
ExecutorAdapter.java 1KB
InsertExecutor.java 12KB
CreateExecutor.java 2KB
entity
ResultEntity.java 206B
SelectResultEntity.java 3KB
AbstractExecutor.java 9KB
resolve
function
BranchHandle.java 488B
sql
constraint
ForeignKeyConstraint.java 4KB
ConstraintResolve.java 205B
PrimaryKeyConstraint.java 2KB
DefaultConstraint.java 1005B
ConstraintResolver.java 12KB
ConstraintType.java 682B
CheckConstraint.java 2KB
UniqueConstraint.java 2KB
AbstractConstraint.java 4KB
CommonConstraint.java 506B
tcl
RollbackSQLResolver.java 382B
CommitSQLResolver.java 401B
SavePointSQLResolver.java 341B
dml
dql
SelectSQLResolver.java 11KB
UpdateSQLResolver.java 5KB
DeleteSQLResolver.java 3KB
InsertSQLResolver.java 2KB
dcl
RevokeSQLResolver.java 437B
GrantSQLResolver.java 416B
common
SQLResolver.java 489B
AbstractSQLResolver.java 16KB
SQLResolverAdapter.java 2KB
OrderType.java 141B
SQLInfo.java 165B
SQLLanguageType.java 754B
SQLType.java 153B
SQLEntity.java 2KB
KeyWord.java 6KB
CommentSQLResolver.java 299B
Types.java 1KB
entity
DropSQLEntity.java 206B
CreateSQLEntity.java 3KB
SavePointSQLEntity.java 211B
CommitSQLEntity.java 208B
RevokeSQLEntity.java 208B
DeleteSQLEntity.java 1KB
OrderEntity.java 642B
SelectSQLEntity.java 7KB
RollbackSQLEntity.java 210B
GrantSQLEntity.java 207B
AlterSQLEntity.java 207B
UpdateSQLEntity.java 1KB
CommentSQLEntity.java 209B
TruncateSQLEntity.java 210B
InsertSQLEntity.java 2KB
ddl
TruncateSQLResolver.java 428B
AlterSQLResolver.java 425B
CreateSQLResolver.java 7KB
DropSQLResolver.java 395B
JDBCType.java 1011B
order
OrderResolver.java 3KB
config
Config.java 1KB
config.properties 19B
CommonCombinationUtil.java 765B
conn
Connection.java 4KB
test
TestStorage.java 820B
BPlusTreeTest.java 1KB
TestResolve.java 4KB
TestIO.java 2KB
TestExecutor.java 2KB
TestJDBC.java 1KB
Bean.java 692B
Test.java 4KB
TestResolve2.java 3KB
storage
index
IndexType.java 450B
type
ColumnType.java 921B
TableSpace.java 865B
Persistent.java 279B
Extent.java 414B
Page.java 9KB
ReadPageEntity.java 544B
engine
datastruct
BPlusTree.java 18KB
InnoDBEngine.java 224B
IndexDirectory.java 350B
PageType.java 498B
util
ByteUtil.java 4KB
IOUtil.java 9KB
FileUtil.java 2KB
AESUtil.java 6KB
struct
TableStruct.java 4KB
AppStart.java 478B
.gitignore 29B
共 92 条
- 1
资源评论
「已注销」
- 粉丝: 800
- 资源: 3612
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功