Skip to content

Commit

Permalink
删除列的RowKey属性,不再依赖RowKey来判断语句是否是批量操作
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Sep 30, 2018
1 parent 7db2300 commit c7b7f88
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 211 deletions.
Expand Up @@ -41,7 +41,7 @@ public String[] assignEndpoints(IDatabase db) {
@Override @Override
public Set<NetEndpoint> getLiveEndpoints() { public Set<NetEndpoint> getLiveEndpoints() {
HashSet<NetEndpoint> set = new HashSet<>(1); HashSet<NetEndpoint> set = new HashSet<>(1);
set.add(NetEndpoint.getLocalTcpEndpoint()); set.add(NetEndpoint.getLocalP2pEndpoint());
return set; return set;
} }


Expand Down
Expand Up @@ -34,8 +34,6 @@ public interface PreparedStatement extends SQLStatement {


boolean isLocal(); boolean isLocal();


boolean isBatch();

void setObjectId(int i); void setObjectId(int i);


void checkCanceled(); void checkCanceled();
Expand Down
5 changes: 0 additions & 5 deletions lealone-db/src/main/java/org/lealone/db/ServerSession.java
Expand Up @@ -1203,11 +1203,6 @@ public Transaction getTransaction(PreparedStatement p) {
if (transaction != null) if (transaction != null)
return transaction; return transaction;


boolean autoCommit = this.autoCommit;
if (autoCommit && p != null && !p.isLocal() && p.isBatch()) { // 批量操作会当成一个分布式事务处理
autoCommit = false;
}

boolean isShardingMode = isShardingMode(); boolean isShardingMode = isShardingMode();
Transaction transaction = database.getTransactionEngine().beginTransaction(autoCommit, isShardingMode); Transaction transaction = database.getTransactionEngine().beginTransaction(autoCommit, isShardingMode);
transaction.setValidator(this); transaction.setValidator(this);
Expand Down
24 changes: 1 addition & 23 deletions lealone-db/src/main/java/org/lealone/db/result/Row.java
Expand Up @@ -27,30 +27,13 @@ public class Row implements SearchRow {
private int memory; private int memory;
private int version; private int version;
private boolean deleted; private boolean deleted;
private Value rowKey;
private Table table; private Table table;


@Override
public Value getRowKey() {
return rowKey;
}

@Override
public void setRowKey(Value rowKey) {
this.rowKey = rowKey;
}

public Row(Value[] data, int memory) { public Row(Value[] data, int memory) {
this.data = data; this.data = data;
this.memory = memory; this.memory = memory;
} }


public Row(Value rowKey, Value[] data, int memory) {
this.rowKey = rowKey;
this.data = data;
this.memory = memory;
}

/** /**
* Get a copy of the row that is distinct from (not equal to) this row. * Get a copy of the row that is distinct from (not equal to) this row.
* This is used for FOR UPDATE to allow pseudo-updating a row. * This is used for FOR UPDATE to allow pseudo-updating a row.
Expand Down Expand Up @@ -93,18 +76,13 @@ public void setKey(long key) {


@Override @Override
public Value getValue(int i) { public Value getValue(int i) {
return i == -1 ? ValueLong.get(key) : (i == -2 ? rowKey : data[i]); return i == -1 ? ValueLong.get(key) : data[i];
} }


@Override @Override
public void setValue(int i, Value v, Column c) { public void setValue(int i, Value v, Column c) {
if (c != null && c.isRowKeyColumn())
this.rowKey = v;
if (i == -1) { if (i == -1) {
this.key = v.getLong(); this.key = v.getLong();
this.rowKey = v;
} else if (i == -2) {
this.rowKey = v;
} else { } else {
data[i] = v; data[i] = v;
} }
Expand Down
4 changes: 0 additions & 4 deletions lealone-db/src/main/java/org/lealone/db/result/SearchRow.java
Expand Up @@ -80,8 +80,4 @@ public interface SearchRow {
*/ */
int getMemory(); int getMemory();


void setRowKey(Value rowKey);

Value getRowKey();

} }
14 changes: 0 additions & 14 deletions lealone-db/src/main/java/org/lealone/db/result/SimpleRow.java
Expand Up @@ -20,7 +20,6 @@ public class SimpleRow implements SearchRow {
private int version; private int version;
private final Value[] data; private final Value[] data;
private int memory; private int memory;
private Value rowKey;


public SimpleRow(Value[] data) { public SimpleRow(Value[] data) {
this.data = data; this.data = data;
Expand Down Expand Up @@ -59,8 +58,6 @@ public void setValue(int idx, Value v) {


@Override @Override
public void setValue(int idx, Value v, Column c) { public void setValue(int idx, Value v, Column c) {
if (c != null && c.isRowKeyColumn())
this.rowKey = v;
data[idx] = v; data[idx] = v;
} }


Expand Down Expand Up @@ -98,15 +95,4 @@ public int getMemory() {
} }
return memory; return memory;
} }

@Override
public void setRowKey(Value rowKey) {
this.rowKey = rowKey;
}

@Override
public Value getRowKey() {
return rowKey;
}

} }
14 changes: 0 additions & 14 deletions lealone-db/src/main/java/org/lealone/db/result/SimpleRowValue.java
Expand Up @@ -20,7 +20,6 @@ public class SimpleRowValue implements SearchRow {
private int index; private int index;
private final int virtualColumnCount; private final int virtualColumnCount;
private Value data; private Value data;
private Value rowKey;


public SimpleRowValue(int columnCount) { public SimpleRowValue(int columnCount) {
this.virtualColumnCount = columnCount; this.virtualColumnCount = columnCount;
Expand Down Expand Up @@ -64,8 +63,6 @@ public void setValue(int idx, Value v) {


@Override @Override
public void setValue(int idx, Value v, Column c) { public void setValue(int idx, Value v, Column c) {
if (c != null && c.isRowKeyColumn())
this.rowKey = v;
index = idx; index = idx;
data = v; data = v;
} }
Expand All @@ -79,15 +76,4 @@ public String toString() {
public int getMemory() { public int getMemory() {
return Constants.MEMORY_OBJECT + (data == null ? 0 : data.getMemory()); return Constants.MEMORY_OBJECT + (data == null ? 0 : data.getMemory());
} }

@Override
public void setRowKey(Value rowKey) {
this.rowKey = rowKey;
}

@Override
public Value getRowKey() {
return rowKey;
}

} }
13 changes: 0 additions & 13 deletions lealone-db/src/main/java/org/lealone/db/table/Column.java
Expand Up @@ -85,16 +85,6 @@ public class Column {
private String comment; private String comment;
private boolean primaryKey; private boolean primaryKey;


private boolean isRowKeyColumn;

public void setRowKeyColumn(boolean isRowKeyColumn) {
this.isRowKeyColumn = isRowKeyColumn;
}

public boolean isRowKeyColumn() {
return isRowKeyColumn;
}

public Column(String name, int type) { public Column(String name, int type) {
this(name, type, -1, -1, -1); this(name, type, -1, -1, -1);
} }
Expand Down Expand Up @@ -444,9 +434,6 @@ public String getCreateSQL(boolean isAlter) {
} }
} }
} }
if (isRowKeyColumn) {
buff.append(" ROW KEY");
}
if (!nullable) { if (!nullable) {
buff.append(" NOT NULL"); buff.append(" NOT NULL");
} }
Expand Down
7 changes: 1 addition & 6 deletions lealone-sql/src/main/java/org/lealone/sql/Parser.java
Expand Up @@ -5657,7 +5657,6 @@ private Column parseColumn(Schema schema, CreateTable command, String tableName)
Column column = parseColumnForTable(columnName, true); Column column = parseColumnForTable(columnName, true);
if (column.isAutoIncrement() && column.isPrimaryKey()) { if (column.isAutoIncrement() && column.isPrimaryKey()) {
column.setPrimaryKey(false); column.setPrimaryKey(false);
column.setRowKeyColumn(true);
IndexColumn[] cols = { new IndexColumn() }; IndexColumn[] cols = { new IndexColumn() };
cols[0].columnName = column.getName(); cols[0].columnName = column.getName();
cols[0].column = column; cols[0].column = column;
Expand All @@ -5672,12 +5671,8 @@ private Column parseColumn(Schema schema, CreateTable command, String tableName)
if (readIf("CONSTRAINT")) { if (readIf("CONSTRAINT")) {
constraintName = readColumnIdentifier(); constraintName = readColumnIdentifier();
} }
if (readIf("ROW")) { if (readIf("PRIMARY")) {
read("KEY");
column.setRowKeyColumn(true);
} else if (readIf("PRIMARY")) {
read("KEY"); read("KEY");
column.setRowKeyColumn(true);
boolean hash = readIf("HASH"); boolean hash = readIf("HASH");
IndexColumn[] cols = { new IndexColumn() }; IndexColumn[] cols = { new IndexColumn() };
cols[0].columnName = column.getName(); cols[0].columnName = column.getName();
Expand Down
30 changes: 0 additions & 30 deletions lealone-sql/src/main/java/org/lealone/sql/StatementBase.java
Expand Up @@ -23,7 +23,6 @@
import org.lealone.db.async.AsyncHandler; import org.lealone.db.async.AsyncHandler;
import org.lealone.db.async.AsyncResult; import org.lealone.db.async.AsyncResult;
import org.lealone.db.result.Result; import org.lealone.db.result.Result;
import org.lealone.db.result.SearchRow;
import org.lealone.db.value.Value; import org.lealone.db.value.Value;
import org.lealone.sql.expression.Expression; import org.lealone.sql.expression.Expression;
import org.lealone.sql.expression.Parameter; import org.lealone.sql.expression.Parameter;
Expand Down Expand Up @@ -445,13 +444,6 @@ public ServerSession getSession() {
return session; return session;
} }


// 多值insert、不带等号PartitionKey条件的delete/update都是一种批量操作,
// 这类批量操作会当成一个分布式事务处理
@Override
public boolean isBatch() {
return false;
}

/** /**
* Whether the statement is already closed (in which case it can be re-used). * Whether the statement is already closed (in which case it can be re-used).
* *
Expand Down Expand Up @@ -525,28 +517,6 @@ protected static String getSQL(Expression[] list) {
return buff.toString(); return buff.toString();
} }


public static boolean containsEqualPartitionKeyComparisonType(TableFilter tableFilter) {
return getPartitionKey(tableFilter) != null;
}

public static Value getPartitionKey(TableFilter tableFilter) {
SearchRow startRow = tableFilter.getStartSearchRow();
SearchRow endRow = tableFilter.getEndSearchRow();

Value startPK = getPartitionKey(startRow);
Value endPK = getPartitionKey(endRow);
if (startPK != null && endPK != null && startPK == endPK)
return startPK;

return null;
}

public static Value getPartitionKey(SearchRow row) {
if (row == null)
return null;
return row.getRowKey();
}

protected double cost; protected double cost;


@Override @Override
Expand Down
Expand Up @@ -122,11 +122,6 @@ public int getType() {
return statement.getType(); return statement.getType();
} }


@Override
public boolean isBatch() {
return statement.isBatch();
}

@Override @Override
public int hashCode() { public int hashCode() {
return statement.hashCode(); return statement.hashCode();
Expand Down
Expand Up @@ -57,7 +57,7 @@ protected boolean isTargetEndpoint(Database db) {
} }


protected void executeDatabaseStatement(Database db) { protected void executeDatabaseStatement(Database db) {
if (session.isRoot()) { if (session.isRoot() && !this.isLocal()) {
SQLRouter.executeDatabaseStatement(db, session, this); SQLRouter.executeDatabaseStatement(db, session, this);
} }
} }
Expand Down
5 changes: 0 additions & 5 deletions lealone-sql/src/main/java/org/lealone/sql/dml/Delete.java
Expand Up @@ -53,11 +53,6 @@ public void setLimit(Expression limit) {
this.limitExpr = limit; this.limitExpr = limit;
} }


@Override
public boolean isBatch() {
return !containsEqualPartitionKeyComparisonType(tableFilter);
}

public void setTableFilter(TableFilter tableFilter) { public void setTableFilter(TableFilter tableFilter) {
this.tableFilter = tableFilter; this.tableFilter = tableFilter;
} }
Expand Down
7 changes: 0 additions & 7 deletions lealone-sql/src/main/java/org/lealone/sql/dml/Insert.java
Expand Up @@ -73,13 +73,6 @@ public void addRow(Expression[] expr) {
list.add(expr); list.add(expr);
} }


@Override
public boolean isBatch() {
// 因为GlobalUniqueIndex是通过独立的唯一索引表实现的,如果包含GlobalUniqueIndex,
// 那么每次往主表中增加一条记录时,都会同时往唯一索引表中加一条记录,所以也是批量的
return (query != null && query.isBatchForInsert()) || list.size() > 1 || table.containsGlobalUniqueIndex();
}

@Override @Override
public void setLocal(boolean local) { public void setLocal(boolean local) {
super.setLocal(local); super.setLocal(local);
Expand Down
7 changes: 0 additions & 7 deletions lealone-sql/src/main/java/org/lealone/sql/dml/Merge.java
Expand Up @@ -55,13 +55,6 @@ public boolean isCacheable() {
return true; return true;
} }


@Override
public boolean isBatch() {
// 因为GlobalUniqueIndex是通过独立的唯一索引表实现的,如果包含GlobalUniqueIndex,
// 那么每次往主表中增加一条记录时,都会同时往唯一索引表中加一条记录,所以也是批量的
return (query != null && query.isBatchForInsert()) || list.size() > 1 || table.containsGlobalUniqueIndex();
}

@Override @Override
public void setLocal(boolean local) { public void setLocal(boolean local) {
super.setLocal(local); super.setLocal(local);
Expand Down
8 changes: 0 additions & 8 deletions lealone-sql/src/main/java/org/lealone/sql/dml/Query.java
Expand Up @@ -562,14 +562,6 @@ public final long getMaxDataModificationId() {


public abstract List<TableFilter> getTopFilters(); public abstract List<TableFilter> getTopFilters();


// Query单独出现时不管涉及多少行记录都认为不是批量的,只有Query用于Insert时才有效
@Override
public boolean isBatch() {
return false;
}

public abstract boolean isBatchForInsert();

protected List<PageKey> pageKeys; protected List<PageKey> pageKeys;


@Override @Override
Expand Down
5 changes: 0 additions & 5 deletions lealone-sql/src/main/java/org/lealone/sql/dml/Select.java
Expand Up @@ -1510,11 +1510,6 @@ public SortOrder getSortOrder() {
return sort; return sort;
} }


@Override
public boolean isBatchForInsert() {
return !containsEqualPartitionKeyComparisonType(topTableFilter);
}

@Override @Override
public void addGlobalCondition(CommandParameter param, int columnId, int indexConditionType) { public void addGlobalCondition(CommandParameter param, int columnId, int indexConditionType) {
int comparisonType = 0; int comparisonType = 0;
Expand Down
Expand Up @@ -445,11 +445,6 @@ public List<TableFilter> getTopFilters() {
return filters; return filters;
} }


@Override
public boolean isBatchForInsert() {
return left.isBatchForInsert() || right.isBatchForInsert();
}

@Override @Override
public void addGlobalCondition(CommandParameter param, int columnId, int indexConditionType) { public void addGlobalCondition(CommandParameter param, int columnId, int indexConditionType) {
this.addGlobalCondition((Parameter) param, columnId, indexConditionType); this.addGlobalCondition((Parameter) param, columnId, indexConditionType);
Expand Down
5 changes: 0 additions & 5 deletions lealone-sql/src/main/java/org/lealone/sql/dml/Update.java
Expand Up @@ -66,11 +66,6 @@ public void setLimit(Expression limit) {
this.limitExpr = limit; this.limitExpr = limit;
} }


@Override
public boolean isBatch() {
return !containsEqualPartitionKeyComparisonType(tableFilter);
}

public void setTableFilter(TableFilter tableFilter) { public void setTableFilter(TableFilter tableFilter) {
this.tableFilter = tableFilter; this.tableFilter = tableFilter;
} }
Expand Down
Expand Up @@ -179,9 +179,7 @@ private SearchRow getSearchRow(ServerSession session, Column column, SearchRow r
} else { } else {
v = getMax(session, row.getValue(id), v, max); v = getMax(session, row.getValue(id), v, max);
} }
if (id == -2) { if (id < 0) {
row.setRowKey(v);
} else if (id < 0) {
row.setKey(v.getLong()); row.setKey(v.getLong());
} else { } else {
row.setValue(id, v, column); row.setValue(id, v, column);
Expand Down
Expand Up @@ -894,8 +894,6 @@ public Value getValue(Column column) {
int columnId = column.getColumnId(); int columnId = column.getColumnId();
if (columnId == -1) { if (columnId == -1) {
return ValueLong.get(currentSearchRow.getKey()); return ValueLong.get(currentSearchRow.getKey());
} else if (columnId == -2) {
return currentSearchRow.getRowKey();
} }
if (current == null) { if (current == null) {
Value v = currentSearchRow.getValue(columnId); Value v = currentSearchRow.getValue(columnId);
Expand Down

0 comments on commit c7b7f88

Please sign in to comment.