提交 227fbad7 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 cf7264a1
...@@ -156,4 +156,12 @@ abstract class PageData { ...@@ -156,4 +156,12 @@ abstract class PageData {
this.parentPageId = id; this.parentPageId = id;
} }
/**
* Remove a row.
*
* @param key the key of the row to remove
* @return true if this page is now empty
*/
abstract boolean remove(int key) throws SQLException;
} }
...@@ -8,8 +8,13 @@ package org.h2.index; ...@@ -8,8 +8,13 @@ package org.h2.index;
import java.sql.SQLException; import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.jdbc.JdbcSQLException;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow;
import org.h2.store.DataPageBinary; import org.h2.store.DataPageBinary;
/** /**
...@@ -159,7 +164,7 @@ class PageDataLeaf extends PageData { ...@@ -159,7 +164,7 @@ class PageDataLeaf extends PageData {
return 0; return 0;
} }
private void removeRow(int index) throws SQLException { private void removeRow(int i) throws SQLException {
entryCount--; entryCount--;
if (entryCount <= 0) { if (entryCount <= 0) {
Message.getInternalError(); Message.getInternalError();
...@@ -167,12 +172,12 @@ class PageDataLeaf extends PageData { ...@@ -167,12 +172,12 @@ class PageDataLeaf extends PageData {
int[] newOffsets = new int[entryCount]; int[] newOffsets = new int[entryCount];
int[] newKeys = new int[entryCount]; int[] newKeys = new int[entryCount];
Row[] newRows = new Row[entryCount]; Row[] newRows = new Row[entryCount];
System.arraycopy(offsets, 0, newOffsets, 0, index); System.arraycopy(offsets, 0, newOffsets, 0, i);
System.arraycopy(keys, 0, newKeys, 0, index); System.arraycopy(keys, 0, newKeys, 0, i);
System.arraycopy(rows, 0, newRows, 0, index); System.arraycopy(rows, 0, newRows, 0, i);
System.arraycopy(offsets, index + 1, newOffsets, index, entryCount - index); System.arraycopy(offsets, i + 1, newOffsets, i, entryCount - i);
System.arraycopy(keys, index + 1, newKeys, index, entryCount - index); System.arraycopy(keys, i + 1, newKeys, i, entryCount - i);
System.arraycopy(rows, index + 1, newRows, index, entryCount - index); System.arraycopy(rows, i + 1, newRows, i, entryCount - i);
start -= 6; start -= 6;
offsets = newOffsets; offsets = newOffsets;
keys = newKeys; keys = newKeys;
...@@ -231,4 +236,37 @@ class PageDataLeaf extends PageData { ...@@ -231,4 +236,37 @@ class PageDataLeaf extends PageData {
return this; return this;
} }
boolean remove(int key) throws SQLException {
int i = find(key);
if (keys[i] != key) {
throw Message.getSQLException(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, index.getSQL());
}
if (entryCount == 1) {
return true;
}
// if (pageData.size() == 1 && !root) {
// // the last row has been deleted
// return oldRow;
// }
// pageData.remove(i);
// updateRealByteCount(false, row);
// index.updatePage(session, this);
// if (i > 0) {
// // the first row didn't change
// return null;
// }
// if (pageData.size() == 0) {
// return null;
// }
// return getData(0);
// }
// if (comp > 0) {
// r = i;
// } else {
// l = i + 1;
// }
// }
throw Message.getSQLException(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, index.getSQL());
}
} }
...@@ -8,6 +8,9 @@ package org.h2.index; ...@@ -8,6 +8,9 @@ package org.h2.index;
import java.sql.SQLException; import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.engine.Constants;
import org.h2.message.Message;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
import org.h2.store.DataPageBinary; import org.h2.store.DataPageBinary;
...@@ -142,4 +145,48 @@ class PageDataNode extends PageData { ...@@ -142,4 +145,48 @@ class PageDataNode extends PageData {
return index.getPage(child).getFirstLeaf(); return index.getPage(child).getFirstLeaf();
} }
private void removeRow(int i) throws SQLException {
entryCount--;
if (entryCount <= 0) {
Message.getInternalError();
}
int[] newKeys = new int[entryCount];
int[] newChildPageIds = new int[entryCount + 1];
System.arraycopy(keys, 0, newKeys, 0, i);
System.arraycopy(childPageIds, 0, newChildPageIds, 0, i);
System.arraycopy(keys, i + 1, newKeys, i, entryCount - i);
System.arraycopy(childPageIds, i + 1, newChildPageIds, i, entryCount - i + 1);
keys = newKeys;
childPageIds = newChildPageIds;
}
boolean remove(int key) throws SQLException {
int todo;
int at = find(key);
// merge is not implemented to allow concurrent usage of btrees
// TODO maybe implement merge
PageData page = index.getPage(childPageIds[at]);
boolean empty = page.remove(key);
if (!empty) {
// the first row didn't change - nothing to do
return false;
}
// this child is now empty
if (entryCount == 1) {
// no more children - this page is empty as well
// it can't be the root otherwise the index would have been
// truncated
return true;
}
if (at == 0) {
// the first child is empty - then the first row of this subtree
// has changed
removeRow(at);
} else {
// otherwise the first row didn't change
removeRow(at - 1);
}
return false;
}
} }
...@@ -143,8 +143,14 @@ public class PageScanIndex extends BaseIndex implements RowIndex { ...@@ -143,8 +143,14 @@ public class PageScanIndex extends BaseIndex implements RowIndex {
public void remove(Session session, Row row) throws SQLException { public void remove(Session session, Row row) throws SQLException {
int invalidateRowCount; int invalidateRowCount;
int todo; // setChanged(session);
rowCount++; if (rowCount == 1) {
truncate(session);
} else {
PageData root = getPage(headPos);
root.remove(row.getPos());
rowCount--;
}
} }
public void remove(Session session) throws SQLException { public void remove(Session session) throws SQLException {
......
...@@ -284,6 +284,7 @@ java org.h2.test.TestAll timer ...@@ -284,6 +284,7 @@ java org.h2.test.TestAll timer
/* /*
is in-memory scan index re-using ids?
don't store default values (store a special value) don't store default values (store a special value)
build.sh from mac (test in Ubuntu) build.sh from mac (test in Ubuntu)
btree: maybe split at the insertion point btree: maybe split at the insertion point
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论