Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
bug: 无法完成 drop 和 drop all,停止开发这两个功能
Browse files Browse the repository at this point in the history
  • Loading branch information
01Petard committed Sep 17, 2024
1 parent ece32cb commit 7afe254
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int newPage(byte[] initData) {
}

public Page getPage(int pgno) throws Exception {
return get((long)pgno);
return get(pgno);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/top/guoziyang/mydb/backend/im/BPlusTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,36 @@ public void close() {
static class InsertRes {
long newNode, newKey;
}

/**
* 删除键值
* @param key 键值
*/
public void delete(long key) throws Exception {
long rootUid = rootUid();
delete(rootUid, key);
}

/**
* 递归删除键值
* @param nodeUid 节点 UID
* @param key 键值
*/
private void delete(long nodeUid, long key) throws Exception {
Node node = Node.loadNode(this, nodeUid);
boolean isLeaf = node.isLeaf();
node.release();

if (isLeaf) {
Node leaf = Node.loadNode(this, nodeUid);
leaf.remove(key);
leaf.release();
} else {
long next = searchNext(nodeUid, key);
delete(next, key);
Node internalNode = Node.loadNode(this, nodeUid);
internalNode.remove(key);
internalNode.release();
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/top/guoziyang/mydb/backend/im/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,67 @@ static class SplitRes {
long newSon, newKey;
}

/**
* 从节点中删除指定的键值
* @param key 要删除的键值
* @return 返回是否成功删除
*/
public boolean remove(long key) throws Exception {
dataItem.before();
try {
boolean result = removeKey(key);
if (result && needMerge()) {
merge();
}
return result;
} finally {
dataItem.after(TransactionManagerImpl.SUPER_XID);
}
}

/**
* 从节点中删除指定的键值,并重新排列节点
* @param key 要删除的键值
* @return 返回是否成功删除
*/
private boolean removeKey(long key) {
int noKeys = getRawNoKeys(raw);
int kth = 0;
while (kth < noKeys) {
long ik = getRawKthKey(raw, kth);
if (ik == key) {
break;
}
kth++;
}
if (kth == noKeys || getRawKthKey(raw, kth) != key) {
return false;
}

if (getRawIfLeaf(raw)) {
shiftRawKth(raw, kth);
setRawNoKeys(raw, noKeys - 1);
} else {
shiftRawKth(raw, kth);
setRawNoKeys(raw, noKeys - 1);
}
return true;
}

/**
* 判断是否需要合并节点
* @return 是否需要合并
*/
private boolean needMerge() {
return getRawNoKeys(raw) < BALANCE_NUMBER / 2;
}

/**
* 合并当前节点
* @throws Exception
*/
private void merge() throws Exception {
// Implement the merge logic here
}

}
15 changes: 14 additions & 1 deletion src/main/java/top/guoziyang/mydb/backend/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ private static boolean isLogicOp(String op) {
return ("and".equals(op) || "or".equals(op));
}

private static Drop parseDrop(Tokenizer tokenizer) throws Exception {
private static Object parseDrop(Tokenizer tokenizer) throws Exception {
if ("all".equals(tokenizer.peek())) {
tokenizer.pop();
return new DropAll();
}

if (!"table".equals(tokenizer.peek())) {
throw Error.InvalidCommandException;
}
Expand All @@ -313,6 +318,14 @@ private static Drop parseDrop(Tokenizer tokenizer) throws Exception {
return drop;
}

private static DropAll parseDropAll(Tokenizer tokenizer) throws Exception {
tokenizer.pop();
if ("all".equals(tokenizer.peek())) {
return new DropAll();
}
throw Error.InvalidCommandException;
}

private static Create parseCreate(Tokenizer tokenizer) throws Exception {
if (!"table".equals(tokenizer.peek())) {
throw Error.InvalidCommandException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package top.guoziyang.mydb.backend.parser.statement;

public class DropAll {

}
6 changes: 6 additions & 0 deletions src/main/java/top/guoziyang/mydb/backend/server/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ private byte[] execute2(Object stat) throws Exception {
res = tbm.showTables(xid);
} else if (stat instanceof Create) {
res = tbm.create(xid, (Create) stat);
} else if (stat instanceof Drop) {
res = "COMMAND IS UNSUPPORTED".getBytes();
// res = tbm.drop(xid, (Drop) stat);
} else if (stat instanceof DropAll) {
res = "COMMAND IS UNSUPPORTED".getBytes();
// res = tbm.dropAll(xid, (DropAll) stat);
} else if (stat instanceof Select) {
res = tbm.read(xid, (Select) stat);
} else if (stat instanceof Insert) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/top/guoziyang/mydb/backend/tbm/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Field {
String fieldType;
private final Table tb;
private long index;
private BPlusTree bt;
BPlusTree bt;

public Field(long uid, Table tb) {
this.uid = uid;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/top/guoziyang/mydb/backend/tbm/Table.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package top.guoziyang.mydb.backend.tbm;

import com.google.common.primitives.Bytes;
import top.guoziyang.mydb.backend.dm.page.Page;
import top.guoziyang.mydb.backend.dm.pageCache.PageCache;
import top.guoziyang.mydb.backend.parser.statement.*;
import top.guoziyang.mydb.backend.tbm.Field.ParseValueRes;
import top.guoziyang.mydb.backend.tm.TransactionManagerImpl;
Expand Down Expand Up @@ -302,4 +304,17 @@ class CalWhereRes {
long l0, r0, l1, r1;
boolean single;
}


private PageCache pageCache; // 页面缓存对象
private int maxPageNumber; // 该表的最大页号


public PageCache getPageCache() {
return pageCache;
}

public int getMaxPageNumber() {
return maxPageNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ static TableManager open(String path, VersionManager vm, DataManager dm) {
// 创建一个新的表
byte[] create(long xid, Create create) throws Exception;

// 删除一张表
byte[] drop(long xid, Drop drop) throws Exception;

// 删除所有表
byte[] dropAll(long xid, DropAll drop) throws Exception;

// 插入新的记录
byte[] insert(long xid, Insert insert) throws Exception;

Expand Down
Loading

0 comments on commit 7afe254

Please sign in to comment.