提交 a4f95b2b authored 作者: Thomas Mueller's avatar Thomas Mueller

Smaller changes (mainly for Android).

上级 7692231b
...@@ -49,7 +49,7 @@ public class CommandContainer extends Command { ...@@ -49,7 +49,7 @@ public class CommandContainer extends Command {
long mod = prepared.getModificationMetaId(); long mod = prepared.getModificationMetaId();
prepared.setModificationMetaId(0); prepared.setModificationMetaId(0);
ArrayList<Parameter> newParams = prepared.getParameters(); ArrayList<Parameter> newParams = prepared.getParameters();
for (int i = 0; i < newParams.size(); i++) { for (int i = 0, size = newParams.size(); i < size; i++) {
Parameter old = oldParams.get(i); Parameter old = oldParams.get(i);
if (old.isValueSet()) { if (old.isValueSet()) {
Value v = old.getValue(session); Value v = old.getValue(session);
......
...@@ -651,7 +651,7 @@ public class Parser { ...@@ -651,7 +651,7 @@ public class Parser {
read(")"); read(")");
read("="); read("=");
Expression expression = readExpression(); Expression expression = readExpression();
for (int i = 0; i < columns.size(); i++) { for (int i = 0, size = columns.size(); i < size; i++) {
Column column = columns.get(i); Column column = columns.get(i);
Function f = Function.getFunction(database, "ARRAY_GET"); Function f = Function.getFunction(database, "ARRAY_GET");
f.setParameter(0, expression); f.setParameter(0, expression);
......
...@@ -105,7 +105,7 @@ public class Call extends Prepared { ...@@ -105,7 +105,7 @@ public class Call extends Prepared {
} }
public boolean isReadOnly() { public boolean isReadOnly() {
return expression.isEverything(ExpressionVisitor.READONLY); return expression.isEverything(ExpressionVisitor.READONLY_VISITOR);
} }
......
...@@ -200,7 +200,7 @@ public abstract class Query extends Prepared { ...@@ -200,7 +200,7 @@ public abstract class Query extends Prepared {
return false; return false;
} }
} }
if (!isEverything(ExpressionVisitor.DETERMINISTIC) || !isEverything(ExpressionVisitor.INDEPENDENT)) { if (!isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR) || !isEverything(ExpressionVisitor.INDEPENDENT_VISITOR)) {
return false; return false;
} }
if (db.getModificationDataId() > lastEval && getMaxDataModificationId() > lastEval) { if (db.getModificationDataId() > lastEval && getMaxDataModificationId() > lastEval) {
...@@ -241,7 +241,7 @@ public abstract class Query extends Prepared { ...@@ -241,7 +241,7 @@ public abstract class Query extends Prepared {
} }
Value[] params = getParameterValues(); Value[] params = getParameterValues();
long now = session.getDatabase().getModificationDataId(); long now = session.getDatabase().getModificationDataId();
if (isEverything(ExpressionVisitor.DETERMINISTIC)) { if (isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR)) {
if (lastResult != null && !lastResult.isClosed() && limit == lastLimit) { if (lastResult != null && !lastResult.isClosed() && limit == lastLimit) {
if (sameResultAsLast(session, params, lastParameters, lastEvaluated)) { if (sameResultAsLast(session, params, lastParameters, lastEvaluated)) {
lastResult = lastResult.createShallowCopy(session); lastResult = lastResult.createShallowCopy(session);
......
...@@ -874,7 +874,8 @@ public class Select extends Query { ...@@ -874,7 +874,8 @@ public class Select extends Query {
} }
public void fireBeforeSelectTriggers() { public void fireBeforeSelectTriggers() {
for (TableFilter filter : filters) { for (int i = 0, size = filters.size(); i < size; i++) {
TableFilter filter = filters.get(i);
filter.getTable().fire(session, Trigger.SELECT, true); filter.getTable().fire(session, Trigger.SELECT, true);
} }
} }
...@@ -908,7 +909,7 @@ public class Select extends Query { ...@@ -908,7 +909,7 @@ public class Select extends Query {
} }
Expression on = f.getJoinCondition(); Expression on = f.getJoinCondition();
if (on != null) { if (on != null) {
if (!on.isEverything(ExpressionVisitor.EVALUATABLE)) { if (!on.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
if (SysProperties.NESTED_JOINS) { if (SysProperties.NESTED_JOINS) {
f.removeJoinCondition(); f.removeJoinCondition();
// need to check that all added are bound to a table // need to check that all added are bound to a table
...@@ -932,7 +933,7 @@ public class Select extends Query { ...@@ -932,7 +933,7 @@ public class Select extends Query {
} }
on = f.getFilterCondition(); on = f.getFilterCondition();
if (on != null) { if (on != null) {
if (!on.isEverything(ExpressionVisitor.EVALUATABLE)) { if (!on.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
f.removeFilterCondition(); f.removeFilterCondition();
addCondition(on); addCondition(on);
} }
...@@ -1105,7 +1106,7 @@ public class Select extends Query { ...@@ -1105,7 +1106,7 @@ public class Select extends Query {
Expression comp; Expression comp;
Expression col = expressions.get(columnId); Expression col = expressions.get(columnId);
col = col.getNonAliasExpression(); col = col.getNonAliasExpression();
if (col.isEverything(ExpressionVisitor.QUERY_COMPARABLE)) { if (col.isEverything(ExpressionVisitor.QUERY_COMPARABLE_VISITOR)) {
comp = new Comparison(session, comparisonType, col, param); comp = new Comparison(session, comparisonType, col, param);
} else { } else {
// add the parameters, so they can be set later // add the parameters, so they can be set later
...@@ -1159,7 +1160,8 @@ public class Select extends Query { ...@@ -1159,7 +1160,8 @@ public class Select extends Query {
if (isForUpdate) { if (isForUpdate) {
return false; return false;
} }
for (TableFilter f : filters) { for (int i = 0, size = filters.size(); i < size; i++) {
TableFilter f = filters.get(i);
if (!f.getTable().isDeterministic()) { if (!f.getTable().isDeterministic()) {
return false; return false;
} }
...@@ -1167,7 +1169,8 @@ public class Select extends Query { ...@@ -1167,7 +1169,8 @@ public class Select extends Query {
break; break;
} }
case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID: { case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID: {
for (TableFilter f : filters) { for (int i = 0, size = filters.size(); i < size; i++) {
TableFilter f = filters.get(i);
long m = f.getTable().getMaxDataModificationId(); long m = f.getTable().getMaxDataModificationId();
visitor.addDataModificationId(m); visitor.addDataModificationId(m);
} }
...@@ -1180,8 +1183,9 @@ public class Select extends Query { ...@@ -1180,8 +1183,9 @@ public class Select extends Query {
break; break;
} }
case ExpressionVisitor.GET_DEPENDENCIES: { case ExpressionVisitor.GET_DEPENDENCIES: {
for (TableFilter filter : filters) { for (int i = 0, size = filters.size(); i < size; i++) {
Table table = filter.getTable(); TableFilter f = filters.get(i);
Table table = f.getTable();
visitor.addDependency(table); visitor.addDependency(table);
table.addDependencies(visitor.getDependencies()); table.addDependencies(visitor.getDependencies());
} }
...@@ -1191,7 +1195,8 @@ public class Select extends Query { ...@@ -1191,7 +1195,8 @@ public class Select extends Query {
} }
visitor.incrementQueryLevel(1); visitor.incrementQueryLevel(1);
boolean result = true; boolean result = true;
for (Expression e : expressions) { for (int i = 0, size = expressions.size(); i < size; i++) {
Expression e = expressions.get(i);
if (!e.isEverything(visitor)) { if (!e.isEverything(visitor)) {
result = false; result = false;
break; break;
...@@ -1208,7 +1213,7 @@ public class Select extends Query { ...@@ -1208,7 +1213,7 @@ public class Select extends Query {
} }
public boolean isReadOnly() { public boolean isReadOnly() {
return isEverything(ExpressionVisitor.READONLY); return isEverything(ExpressionVisitor.READONLY_VISITOR);
} }
......
...@@ -148,7 +148,7 @@ public class Database implements DataHandler { ...@@ -148,7 +148,7 @@ public class Database implements DataHandler {
private Mode mode = Mode.getInstance(Mode.REGULAR); private Mode mode = Mode.getInstance(Mode.REGULAR);
private boolean multiThreaded; private boolean multiThreaded;
private int maxOperationMemory = SysProperties.DEFAULT_MAX_OPERATION_MEMORY; private int maxOperationMemory = SysProperties.DEFAULT_MAX_OPERATION_MEMORY;
private SmallLRUCache<String, String[]> lobFileListCache = SmallLRUCache.newInstance(128); private SmallLRUCache<String, String[]> lobFileListCache;
private boolean autoServerMode; private boolean autoServerMode;
private Server server; private Server server;
private HashMap<TableLinkConnection, TableLinkConnection> linkConnections; private HashMap<TableLinkConnection, TableLinkConnection> linkConnections;
...@@ -672,7 +672,7 @@ public class Database implements DataHandler { ...@@ -672,7 +672,7 @@ public class Database implements DataHandler {
} }
synchronized (infoSchema) { synchronized (infoSchema) {
if (!metaTablesInitialized) { if (!metaTablesInitialized) {
for (int type = 0; type < MetaTable.getMetaTableTypeCount(); type++) { for (int type = 0, count = MetaTable.getMetaTableTypeCount(); type < count; type++) {
MetaTable m = new MetaTable(infoSchema, -1 - type, type); MetaTable m = new MetaTable(infoSchema, -1 - type, type);
infoSchema.add(m); infoSchema.add(m);
} }
...@@ -1962,6 +1962,9 @@ public class Database implements DataHandler { ...@@ -1962,6 +1962,9 @@ public class Database implements DataHandler {
} }
public SmallLRUCache<String, String[]> getLobFileListCache() { public SmallLRUCache<String, String[]> getLobFileListCache() {
if (lobFileListCache == null) {
lobFileListCache = SmallLRUCache.newInstance(128);
}
return lobFileListCache; return lobFileListCache;
} }
......
...@@ -469,7 +469,8 @@ public class Session extends SessionWithState { ...@@ -469,7 +469,8 @@ public class Session extends SessionWithState {
rows.add(entry.getRow()); rows.add(entry.getRow());
undoLog.removeLast(false); undoLog.removeLast(false);
} }
for (Row r : rows) { for (int i = 0, size = rows.size(); i < size; i++) {
Row r = rows.get(i);
r.commit(); r.commit();
} }
} }
...@@ -615,7 +616,9 @@ public class Session extends SessionWithState { ...@@ -615,7 +616,9 @@ public class Session extends SessionWithState {
undoLog.add(log); undoLog.add(log);
} else { } else {
// see also UndoLogRecord.commit // see also UndoLogRecord.commit
for (Index index : table.getIndexes()) { ArrayList<Index> indexes = table.getIndexes();
for (int i = 0, size = indexes.size(); i < size; i++) {
Index index = indexes.get(i);
index.commit(operation, row); index.commit(operation, row);
} }
row.commit(); row.commit();
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.engine; package org.h2.engine;
import java.util.ArrayList;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.index.Index; import org.h2.index.Index;
...@@ -149,8 +150,9 @@ public class UndoLogRecord { ...@@ -149,8 +150,9 @@ public class UndoLogRecord {
} }
buff.writeLong(row.getKey()); buff.writeLong(row.getKey());
buff.writeInt(row.getSessionId()); buff.writeInt(row.getSessionId());
buff.writeInt(row.getColumnCount()); int count = row.getColumnCount();
for (int i = 0; i < row.getColumnCount(); i++) { buff.writeInt(count);
for (int i = 0; i < count; i++) {
Value v = row.getValue(i); Value v = row.getValue(i);
buff.checkCapacity(buff.getValueLen(v)); buff.checkCapacity(buff.getValueLen(v));
buff.writeValue(v); buff.writeValue(v);
...@@ -260,7 +262,9 @@ public class UndoLogRecord { ...@@ -260,7 +262,9 @@ public class UndoLogRecord {
* It commits the change to the indexes. * It commits the change to the indexes.
*/ */
public void commit() { public void commit() {
for (Index index : table.getIndexes()) { ArrayList<Index> indexes = table.getIndexes();
for (int i = 0, size = indexes.size(); i < size; i++) {
Index index = indexes.get(i);
index.commit(operation, row); index.commit(operation, row);
} }
} }
......
...@@ -201,9 +201,10 @@ public class Aggregate extends Expression { ...@@ -201,9 +201,10 @@ public class Aggregate extends Expression {
} }
private SortOrder initOrder(Session session) { private SortOrder initOrder(Session session) {
int[] index = new int[orderList.size()]; int size = orderList.size();
int[] sortType = new int[orderList.size()]; int[] index = new int[size];
for (int i = 0; i < orderList.size(); i++) { int[] sortType = new int[size];
for (int i = 0; i < size; i++) {
SelectOrderBy o = orderList.get(i); SelectOrderBy o = orderList.get(i);
index[i] = i + 1; index[i] = i + 1;
int order = o.descending ? SortOrder.DESCENDING : SortOrder.ASCENDING; int order = o.descending ? SortOrder.DESCENDING : SortOrder.ASCENDING;
...@@ -240,9 +241,10 @@ public class Aggregate extends Expression { ...@@ -240,9 +241,10 @@ public class Aggregate extends Expression {
if (v != ValueNull.INSTANCE) { if (v != ValueNull.INSTANCE) {
v = v.convertTo(Value.STRING); v = v.convertTo(Value.STRING);
if (orderList != null) { if (orderList != null) {
Value[] array = new Value[1 + orderList.size()]; int size = orderList.size();
Value[] array = new Value[1 + size];
array[0] = v; array[0] = v;
for (int i = 0; i < orderList.size(); i++) { for (int i = 0; i < size; i++) {
SelectOrderBy o = orderList.get(i); SelectOrderBy o = orderList.get(i);
array[i + 1] = o.expression.getValue(session); array[i + 1] = o.expression.getValue(session);
} }
......
...@@ -284,7 +284,7 @@ public abstract class Expression { ...@@ -284,7 +284,7 @@ public abstract class Expression {
* @param outerJoin if the expression is part of an outer join * @param outerJoin if the expression is part of an outer join
*/ */
public void addFilterConditions(TableFilter filter, boolean outerJoin) { public void addFilterConditions(TableFilter filter, boolean outerJoin) {
if (!addedToFilter && !outerJoin && isEverything(ExpressionVisitor.EVALUATABLE)) { if (!addedToFilter && !outerJoin && isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
filter.addFilterCondition(this, false); filter.addFilterCondition(this, false);
addedToFilter = true; addedToFilter = true;
} }
......
...@@ -8,7 +8,9 @@ package org.h2.expression; ...@@ -8,7 +8,9 @@ package org.h2.expression;
import java.util.HashSet; import java.util.HashSet;
import org.h2.constant.SysProperties;
import org.h2.engine.DbObject; import org.h2.engine.DbObject;
import org.h2.message.DbException;
import org.h2.table.ColumnResolver; import org.h2.table.ColumnResolver;
import org.h2.table.Table; import org.h2.table.Table;
...@@ -25,6 +27,11 @@ public class ExpressionVisitor { ...@@ -25,6 +27,11 @@ public class ExpressionVisitor {
*/ */
public static final int INDEPENDENT = 0; public static final int INDEPENDENT = 0;
/**
* The visitor singleton for the type INDEPENDENT.
*/
public static final ExpressionVisitor INDEPENDENT_VISITOR = new ExpressionVisitor(INDEPENDENT);
/** /**
* Are all aggregates MIN(column), MAX(column), or COUNT(*) for the given * Are all aggregates MIN(column), MAX(column), or COUNT(*) for the given
* table (getTable)? * table (getTable)?
...@@ -36,6 +43,11 @@ public class ExpressionVisitor { ...@@ -36,6 +43,11 @@ public class ExpressionVisitor {
*/ */
public static final int DETERMINISTIC = 2; public static final int DETERMINISTIC = 2;
/**
* The visitor singleton for the type DETERMINISTIC.
*/
public static final ExpressionVisitor DETERMINISTIC_VISITOR = new ExpressionVisitor(DETERMINISTIC);
/** /**
* Can the expression be evaluated, that means are all columns set to * Can the expression be evaluated, that means are all columns set to
* 'evaluatable'? * 'evaluatable'?
...@@ -43,7 +55,12 @@ public class ExpressionVisitor { ...@@ -43,7 +55,12 @@ public class ExpressionVisitor {
public static final int EVALUATABLE = 3; public static final int EVALUATABLE = 3;
/** /**
* Request to set the latest modification id. * The visitor singleton for the type EVALUATABLE.
*/
public static final ExpressionVisitor EVALUATABLE_VISITOR = new ExpressionVisitor(EVALUATABLE);
/**
* Request to set the latest modification id (addDataModificationId).
*/ */
public static final int SET_MAX_DATA_MODIFICATION_ID = 4; public static final int SET_MAX_DATA_MODIFICATION_ID = 4;
...@@ -52,6 +69,11 @@ public class ExpressionVisitor { ...@@ -52,6 +69,11 @@ public class ExpressionVisitor {
*/ */
public static final int READONLY = 5; public static final int READONLY = 5;
/**
* The visitor singleton for the type EVALUATABLE.
*/
public static final ExpressionVisitor READONLY_VISITOR = new ExpressionVisitor(READONLY);
/** /**
* Does an expression have no relation to the given table filter * Does an expression have no relation to the given table filter
* (getResolver)? * (getResolver)?
...@@ -59,7 +81,7 @@ public class ExpressionVisitor { ...@@ -59,7 +81,7 @@ public class ExpressionVisitor {
public static final int NOT_FROM_RESOLVER = 6; public static final int NOT_FROM_RESOLVER = 6;
/** /**
* Request to get the set of dependencies. * Request to get the set of dependencies (addDependency).
*/ */
public static final int GET_DEPENDENCIES = 7; public static final int GET_DEPENDENCIES = 7;
...@@ -71,6 +93,11 @@ public class ExpressionVisitor { ...@@ -71,6 +93,11 @@ public class ExpressionVisitor {
*/ */
public static final int QUERY_COMPARABLE = 8; public static final int QUERY_COMPARABLE = 8;
/**
* The visitor singleton for the type QUERY_COMPARABLE.
*/
public static final ExpressionVisitor QUERY_COMPARABLE_VISITOR = new ExpressionVisitor(QUERY_COMPARABLE);
private int queryLevel; private int queryLevel;
private Table table; private Table table;
private int type; private int type;
...@@ -89,6 +116,16 @@ public class ExpressionVisitor { ...@@ -89,6 +116,16 @@ public class ExpressionVisitor {
* @return the new visitor * @return the new visitor
*/ */
public static ExpressionVisitor get(int type) { public static ExpressionVisitor get(int type) {
if (SysProperties.CHECK) {
switch (type) {
case INDEPENDENT:
case DETERMINISTIC:
case EVALUATABLE:
case READONLY:
case QUERY_COMPARABLE:
throw DbException.throwInternalError("Singleton not used");
}
}
return new ExpressionVisitor(type); return new ExpressionVisitor(type);
} }
......
...@@ -1356,8 +1356,9 @@ public class Function extends Expression implements FunctionCall { ...@@ -1356,8 +1356,9 @@ public class Function extends Expression implements FunctionCall {
} }
private static String rawToHex(String s) { private static String rawToHex(String s) {
StringBuilder buff = new StringBuilder(4 * s.length()); int length = s.length();
for (int i = 0; i < s.length(); i++) { StringBuilder buff = new StringBuilder(4 * length);
for (int i = 0; i < length; i++) {
String hex = Integer.toHexString(s.charAt(i) & 0xffff); String hex = Integer.toHexString(s.charAt(i) & 0xffff);
for (int j = hex.length(); j < 4; j++) { for (int j = hex.length(); j < 4; j++) {
buff.append('0'); buff.append('0');
......
...@@ -505,10 +505,11 @@ public class FullText { ...@@ -505,10 +505,11 @@ public class FullText {
* @param columns the column list * @param columns the column list
*/ */
protected static void setColumns(int[] index, ArrayList<String> keys, ArrayList<String> columns) throws SQLException { protected static void setColumns(int[] index, ArrayList<String> keys, ArrayList<String> columns) throws SQLException {
for (int i = 0; i < keys.size(); i++) { for (int i = 0, keySize = keys.size(); i < keySize; i++) {
String key = keys.get(i); String key = keys.get(i);
int found = -1; int found = -1;
for (int j = 0; found == -1 && j < columns.size(); j++) { int columnsSize = columns.size();
for (int j = 0; found == -1 && j < columnsSize; j++) {
String column = columns.get(j); String column = columns.get(j);
if (column.equals(key)) { if (column.equals(key)) {
found = j; found = j;
...@@ -733,9 +734,10 @@ public class FullText { ...@@ -733,9 +734,10 @@ public class FullText {
if (data.indexOf('\'') < 0) { if (data.indexOf('\'') < 0) {
return "'" + data + "'"; return "'" + data + "'";
} }
StringBuilder buff = new StringBuilder(data.length() + 2); int len = data.length();
StringBuilder buff = new StringBuilder(len + 2);
buff.append('\''); buff.append('\'');
for (int i = 0; i < data.length(); i++) { for (int i = 0; i < len; i++) {
char ch = data.charAt(i); char ch = data.charAt(i);
if (ch == '\'') { if (ch == '\'') {
buff.append(ch); buff.append(ch);
......
...@@ -313,17 +313,17 @@ public class IndexCondition { ...@@ -313,17 +313,17 @@ public class IndexCondition {
*/ */
public boolean isEvaluatable() { public boolean isEvaluatable() {
if (expression != null) { if (expression != null) {
return expression.isEverything(ExpressionVisitor.EVALUATABLE); return expression.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR);
} }
if (expressionList != null) { if (expressionList != null) {
for (Expression e : expressionList) { for (Expression e : expressionList) {
if (!e.isEverything(ExpressionVisitor.EVALUATABLE)) { if (!e.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
return false; return false;
} }
} }
return true; return true;
} }
return expressionQuery.isEverything(ExpressionVisitor.EVALUATABLE); return expressionQuery.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR);
} }
} }
...@@ -217,7 +217,7 @@ public class PageBtreeIndex extends PageIndex { ...@@ -217,7 +217,7 @@ public class PageBtreeIndex extends PageIndex {
trace.debug(getName() + " remove " + row); trace.debug(getName() + " remove " + row);
} }
if (tableData.getContainsLargeObject()) { if (tableData.getContainsLargeObject()) {
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0, len = row.getColumnCount(); i < len; i++) {
Value v = row.getValue(i); Value v = row.getValue(i);
if (v.isLinked()) { if (v.isLinked()) {
session.unlinkAtCommit(v); session.unlinkAtCommit(v);
......
...@@ -104,7 +104,7 @@ public class PageDataIndex extends PageIndex { ...@@ -104,7 +104,7 @@ public class PageDataIndex extends PageIndex {
} }
} }
if (tableData.getContainsLargeObject()) { if (tableData.getContainsLargeObject()) {
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0, len = row.getColumnCount(); i < len; i++) {
Value v = row.getValue(i); Value v = row.getValue(i);
Value v2 = v.link(database, getId()); Value v2 = v.link(database, getId());
if (v2.isLinked()) { if (v2.isLinked()) {
...@@ -304,7 +304,7 @@ public class PageDataIndex extends PageIndex { ...@@ -304,7 +304,7 @@ public class PageDataIndex extends PageIndex {
public void remove(Session session, Row row) { public void remove(Session session, Row row) {
if (tableData.getContainsLargeObject()) { if (tableData.getContainsLargeObject()) {
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0, len = row.getColumnCount(); i < len; i++) {
Value v = row.getValue(i); Value v = row.getValue(i);
if (v.isLinked()) { if (v.isLinked()) {
session.unlinkAtCommit(v); session.unlinkAtCommit(v);
......
...@@ -45,7 +45,7 @@ public class ViewCursor implements Cursor { ...@@ -45,7 +45,7 @@ public class ViewCursor implements Cursor {
} }
current = table.getTemplateRow(); current = table.getTemplateRow();
Value[] values = result.currentRow(); Value[] values = result.currentRow();
for (int i = 0; i < current.getColumnCount(); i++) { for (int i = 0, len = current.getColumnCount(); i < len; i++) {
Value v = i < values.length ? values[i] : ValueNull.INSTANCE; Value v = i < values.length ? values[i] : ValueNull.INSTANCE;
current.setValue(i, v); current.setValue(i, v);
} }
......
...@@ -203,7 +203,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -203,7 +203,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
debugCodeCall("clearParameters"); debugCodeCall("clearParameters");
checkClosed(); checkClosed();
ArrayList< ? extends ParameterInterface> parameters = command.getParameters(); ArrayList< ? extends ParameterInterface> parameters = command.getParameters();
for (ParameterInterface param : parameters) { for (int i = 0, size = parameters.size(); i < size; i++) {
ParameterInterface param = parameters.get(i);
// can only delete old temp files if they are not in the batch // can only delete old temp files if they are not in the batch
param.setValue(null, batchParameters == null); param.setValue(null, batchParameters == null);
} }
...@@ -1068,12 +1069,13 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -1068,12 +1069,13 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
// TODO batch: check what other database do if no parameters are set // TODO batch: check what other database do if no parameters are set
batchParameters = New.arrayList(); batchParameters = New.arrayList();
} }
int[] result = new int[batchParameters.size()]; int size = batchParameters.size();
int[] result = new int[size];
boolean error = false; boolean error = false;
SQLException next = null; SQLException next = null;
checkClosedForWrite(); checkClosedForWrite();
try { try {
for (int i = 0; i < batchParameters.size(); i++) { for (int i = 0; i < size; i++) {
Value[] set = batchParameters.get(i); Value[] set = batchParameters.get(i);
ArrayList< ? extends ParameterInterface> parameters = command.getParameters(); ArrayList< ? extends ParameterInterface> parameters = command.getParameters();
for (int j = 0; j < set.length; j++) { for (int j = 0; j < set.length; j++) {
...@@ -1122,8 +1124,9 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -1122,8 +1124,9 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
checkClosedForWrite(); checkClosedForWrite();
try { try {
ArrayList< ? extends ParameterInterface> parameters = command.getParameters(); ArrayList< ? extends ParameterInterface> parameters = command.getParameters();
Value[] set = new Value[parameters.size()]; int size = parameters.size();
for (int i = 0; i < parameters.size(); i++) { Value[] set = new Value[size];
for (int i = 0; i < size; i++) {
ParameterInterface param = parameters.get(i); ParameterInterface param = parameters.get(i);
Value value = param.getParamValue(); Value value = param.getParamValue();
set[i] = value; set[i] = value;
...@@ -1494,7 +1497,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat ...@@ -1494,7 +1497,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStat
ArrayList< ? extends ParameterInterface> oldParams = command.getParameters(); ArrayList< ? extends ParameterInterface> oldParams = command.getParameters();
command = conn.prepareCommand(sqlStatement, fetchSize); command = conn.prepareCommand(sqlStatement, fetchSize);
ArrayList< ? extends ParameterInterface> newParams = command.getParameters(); ArrayList< ? extends ParameterInterface> newParams = command.getParameters();
for (int i = 0; i < oldParams.size(); i++) { for (int i = 0, size = oldParams.size(); i < size; i++) {
ParameterInterface old = oldParams.get(i); ParameterInterface old = oldParams.get(i);
Value value = old.getParamValue(); Value value = old.getParamValue();
if (value != null) { if (value != null) {
......
...@@ -622,9 +622,10 @@ public class JdbcStatement extends TraceObject implements Statement { ...@@ -622,9 +622,10 @@ public class JdbcStatement extends TraceObject implements Statement {
// TODO batch: check what other database do if no commands are set // TODO batch: check what other database do if no commands are set
batchCommands = New.arrayList(); batchCommands = New.arrayList();
} }
int[] result = new int[batchCommands.size()]; int size = batchCommands.size();
int[] result = new int[size];
boolean error = false; boolean error = false;
for (int i = 0; i < batchCommands.size(); i++) { for (int i = 0; i < size; i++) {
String sql = batchCommands.get(i); String sql = batchCommands.get(i);
try { try {
result[i] = executeUpdateInternal(sql); result[i] = executeUpdateInternal(sql);
......
...@@ -100,7 +100,6 @@ public class TraceSystem implements TraceWriter { ...@@ -100,7 +100,6 @@ public class TraceSystem implements TraceWriter {
public TraceSystem(String fileName) { public TraceSystem(String fileName) {
this.fileName = fileName; this.fileName = fileName;
updateLevel(); updateLevel();
traces = SmallLRUCache.newInstance(100);
} }
private void updateLevel() { private void updateLevel() {
...@@ -126,6 +125,9 @@ public class TraceSystem implements TraceWriter { ...@@ -126,6 +125,9 @@ public class TraceSystem implements TraceWriter {
* @return the trace object * @return the trace object
*/ */
public synchronized Trace getTrace(String module) { public synchronized Trace getTrace(String module) {
if (traces == null) {
traces = SmallLRUCache.newInstance(16);
}
Trace t = traces.get(module); Trace t = traces.get(module);
if (t == null) { if (t == null) {
t = new Trace(writer, module); t = new Trace(writer, module);
......
...@@ -176,7 +176,7 @@ class ResultDiskBuffer implements ResultExternal { ...@@ -176,7 +176,7 @@ class ResultDiskBuffer implements ResultExternal {
private Value[] nextSorted() { private Value[] nextSorted() {
int next = -1; int next = -1;
for (int i = 0; i < tapes.size(); i++) { for (int i = 0, size = tapes.size(); i < size; i++) {
ResultDiskTape tape = tapes.get(i); ResultDiskTape tape = tapes.get(i);
if (tape.buffer.size() == 0 && tape.pos < tape.end) { if (tape.buffer.size() == 0 && tape.pos < tape.end) {
file.seek(tape.pos); file.seek(tape.pos);
......
...@@ -49,12 +49,13 @@ public class RowList { ...@@ -49,12 +49,13 @@ public class RowList {
buff.checkCapacity(1 + Data.LENGTH_INT * 8); buff.checkCapacity(1 + Data.LENGTH_INT * 8);
buff.writeByte((byte) 1); buff.writeByte((byte) 1);
buff.writeInt(r.getMemory()); buff.writeInt(r.getMemory());
buff.writeInt(r.getColumnCount()); int columnCount = r.getColumnCount();
buff.writeInt(columnCount);
buff.writeLong(r.getKey()); buff.writeLong(r.getKey());
buff.writeInt(r.getVersion()); buff.writeInt(r.getVersion());
buff.writeInt(r.isDeleted() ? 1 : 0); buff.writeInt(r.isDeleted() ? 1 : 0);
buff.writeInt(r.getSessionId()); buff.writeInt(r.getSessionId());
for (int i = 0; i < r.getColumnCount(); i++) { for (int i = 0; i < columnCount; i++) {
Value v = r.getValue(i); Value v = r.getValue(i);
buff.checkCapacity(1); buff.checkCapacity(1);
if (v == null) { if (v == null) {
......
...@@ -136,7 +136,7 @@ public class UpdatableRow { ...@@ -136,7 +136,7 @@ public class UpdatableRow {
} }
private void setKey(PreparedStatement prep, int start, Value[] current) throws SQLException { private void setKey(PreparedStatement prep, int start, Value[] current) throws SQLException {
for (int i = 0; i < key.size(); i++) { for (int i = 0, size = key.size(); i < size; i++) {
String col = key.get(i); String col = key.get(i);
int idx = getColumnIndex(col); int idx = getColumnIndex(col);
Value v = current[idx]; Value v = current[idx];
......
...@@ -331,7 +331,7 @@ public class Schema extends DbObjectBase { ...@@ -331,7 +331,7 @@ public class Schema extends DbObjectBase {
String hash = Integer.toHexString(obj.getName().hashCode()).toUpperCase(); String hash = Integer.toHexString(obj.getName().hashCode()).toUpperCase();
String name = null; String name = null;
synchronized (temporaryUniqueNames) { synchronized (temporaryUniqueNames) {
for (int i = 1; i < hash.length(); i++) { for (int i = 1, len = hash.length(); i < len; i++) {
name = prefix + hash.substring(0, i); name = prefix + hash.substring(0, i);
if (!map.containsKey(name) && !temporaryUniqueNames.contains(name)) { if (!map.containsKey(name) && !temporaryUniqueNames.contains(name)) {
break; break;
......
...@@ -22,7 +22,7 @@ public class PageFreeList extends Page { ...@@ -22,7 +22,7 @@ public class PageFreeList extends Page {
private static final int DATA_START = 3; private static final int DATA_START = 3;
private final PageStore store; private final PageStore store;
private final BitField used = new BitField(); private final BitField used;
private final int pageCount; private final int pageCount;
private boolean full; private boolean full;
private Data data; private Data data;
...@@ -31,6 +31,7 @@ public class PageFreeList extends Page { ...@@ -31,6 +31,7 @@ public class PageFreeList extends Page {
setPos(pageId); setPos(pageId);
this.store = store; this.store = store;
pageCount = (store.getPageSize() - DATA_START) * 8; pageCount = (store.getPageSize() - DATA_START) * 8;
used = new BitField(pageCount);
used.set(0); used.set(0);
} }
......
...@@ -382,8 +382,8 @@ public class PageStore implements CacheWriter { ...@@ -382,8 +382,8 @@ public class PageStore implements CacheWriter {
private void writeBack() { private void writeBack() {
ArrayList<CacheObject> list = cache.getAllChanged(); ArrayList<CacheObject> list = cache.getAllChanged();
Collections.sort(list); Collections.sort(list);
for (CacheObject rec : list) { for (int i = 0, size = list.size(); i < size; i++) {
writeBack(rec); writeBack(list.get(i));
} }
} }
...@@ -1095,6 +1095,7 @@ public class PageStore implements CacheWriter { ...@@ -1095,6 +1095,7 @@ public class PageStore implements CacheWriter {
* @param after all allocated pages are higher than this page * @param after all allocated pages are higher than this page
*/ */
void allocatePages(IntArray list, int pagesToAllocate, BitField exclude, int after) { void allocatePages(IntArray list, int pagesToAllocate, BitField exclude, int after) {
list.ensureCapacity(list.size() + pagesToAllocate);
for (int i = 0; i < pagesToAllocate; i++) { for (int i = 0; i < pagesToAllocate; i++) {
int page = allocatePage(exclude, after); int page = allocatePage(exclude, after);
after = page; after = page;
......
...@@ -112,7 +112,7 @@ public class Plan { ...@@ -112,7 +112,7 @@ public class Plan {
setEvaluatable(tableFilter, true); setEvaluatable(tableFilter, true);
Expression on = tableFilter.getJoinCondition(); Expression on = tableFilter.getJoinCondition();
if (on != null) { if (on != null) {
if (!on.isEverything(ExpressionVisitor.EVALUATABLE)) { if (!on.isEverything(ExpressionVisitor.EVALUATABLE_VISITOR)) {
invalidPlan = true; invalidPlan = true;
break; break;
} }
......
...@@ -224,7 +224,7 @@ public class RegularTable extends TableBase { ...@@ -224,7 +224,7 @@ public class RegularTable extends TableBase {
long total = remaining; long total = remaining;
Cursor cursor = scan.find(session, null, null); Cursor cursor = scan.find(session, null, null);
long i = 0; long i = 0;
int bufferSize = Constants.DEFAULT_MAX_MEMORY_ROWS; int bufferSize = (int) Math.min(rowCount, Constants.DEFAULT_MAX_MEMORY_ROWS);
ArrayList<Row> buffer = New.arrayList(bufferSize); ArrayList<Row> buffer = New.arrayList(bufferSize);
String n = getName() + ":" + index.getName(); String n = getName() + ":" + index.getName();
int t = MathUtils.convertLongToInt(total); int t = MathUtils.convertLongToInt(total);
...@@ -667,7 +667,8 @@ public class RegularTable extends TableBase { ...@@ -667,7 +667,8 @@ public class RegularTable extends TableBase {
public boolean canTruncate() { public boolean canTruncate() {
if (getCheckForeignKeyConstraints() && database.getReferentialIntegrity()) { if (getCheckForeignKeyConstraints() && database.getReferentialIntegrity()) {
ArrayList<Constraint> constraints = getConstraints(); ArrayList<Constraint> constraints = getConstraints();
for (int i = 0; constraints != null && i < constraints.size(); i++) { if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint c = constraints.get(i); Constraint c = constraints.get(i);
if (!(c.getConstraintType().equals(Constraint.REFERENTIAL))) { if (!(c.getConstraintType().equals(Constraint.REFERENTIAL))) {
continue; continue;
...@@ -678,6 +679,7 @@ public class RegularTable extends TableBase { ...@@ -678,6 +679,7 @@ public class RegularTable extends TableBase {
} }
} }
} }
}
return true; return true;
} }
......
...@@ -121,11 +121,13 @@ public abstract class Table extends SchemaObjectBase { ...@@ -121,11 +121,13 @@ public abstract class Table extends SchemaObjectBase {
public void rename(String newName) { public void rename(String newName) {
super.rename(newName); super.rename(newName);
for (int i = 0; constraints != null && i < constraints.size(); i++) { if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint constraint = constraints.get(i); Constraint constraint = constraints.get(i);
constraint.rebuild(); constraint.rebuild();
} }
} }
}
/** /**
* Lock the table for the given session. * Lock the table for the given session.
...@@ -470,14 +472,17 @@ public abstract class Table extends SchemaObjectBase { ...@@ -470,14 +472,17 @@ public abstract class Table extends SchemaObjectBase {
* @throws SQLException if the column is referenced * @throws SQLException if the column is referenced
*/ */
public void checkColumnIsNotReferenced(Column col) { public void checkColumnIsNotReferenced(Column col) {
for (int i = 0; constraints != null && i < constraints.size(); i++) { if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint constraint = constraints.get(i); Constraint constraint = constraints.get(i);
if (constraint.containsColumn(col)) { if (constraint.containsColumn(col)) {
throw DbException.get(ErrorCode.COLUMN_MAY_BE_REFERENCED_1, constraint.getSQL()); throw DbException.get(ErrorCode.COLUMN_MAY_BE_REFERENCED_1, constraint.getSQL());
} }
} }
}
ArrayList<Index> indexes = getIndexes(); ArrayList<Index> indexes = getIndexes();
for (int i = 0; indexes != null && i < indexes.size(); i++) { if (indexes != null) {
for (int i = 0, size = indexes.size(); i < size; i++) {
Index index = indexes.get(i); Index index = indexes.get(i);
if (index.getColumns().length == 1) { if (index.getColumns().length == 1) {
continue; continue;
...@@ -490,6 +495,7 @@ public abstract class Table extends SchemaObjectBase { ...@@ -490,6 +495,7 @@ public abstract class Table extends SchemaObjectBase {
} }
} }
} }
}
public Row getTemplateRow() { public Row getTemplateRow() {
return new Row(new Value[columns.length], Row.MEMORY_CALCULATE); return new Row(new Value[columns.length], Row.MEMORY_CALCULATE);
......
...@@ -87,7 +87,7 @@ public class TableView extends Table { ...@@ -87,7 +87,7 @@ public class TableView extends Table {
tables = New.arrayList(query.getTables()); tables = New.arrayList(query.getTables());
ArrayList<Expression> expressions = query.getExpressions(); ArrayList<Expression> expressions = query.getExpressions();
ArrayList<Column> list = New.arrayList(); ArrayList<Column> list = New.arrayList();
for (int i = 0; i < query.getColumnCount(); i++) { for (int i = 0, count = query.getColumnCount(); i < count; i++) {
Expression expr = expressions.get(i); Expression expr = expressions.get(i);
String name = null; String name = null;
if (columnNames != null && columnNames.length > i) { if (columnNames != null && columnNames.length > i) {
...@@ -389,7 +389,7 @@ public class TableView extends Table { ...@@ -389,7 +389,7 @@ public class TableView extends Table {
if (recursive) { if (recursive) {
return false; return false;
} }
return viewQuery.isEverything(ExpressionVisitor.DETERMINISTIC); return viewQuery.isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR);
} }
public void setRecursiveResult(ResultInterface recursiveResult) { public void setRecursiveResult(ResultInterface recursiveResult) {
......
...@@ -1217,6 +1217,7 @@ public class Recover extends Tool implements DataHandler { ...@@ -1217,6 +1217,7 @@ public class Recover extends Tool implements DataHandler {
sql = sql.substring("IF NOT EXISTS ".length()); sql = sql.substring("IF NOT EXISTS ".length());
} }
boolean ignore = false; boolean ignore = false;
// sql is modified in the loop
for (int i = 0; i < sql.length(); i++) { for (int i = 0; i < sql.length(); i++) {
char ch = sql.charAt(i); char ch = sql.charAt(i);
if (ch == '\"') { if (ch == '\"') {
......
...@@ -209,7 +209,7 @@ public class Shell extends Tool implements Runnable { ...@@ -209,7 +209,7 @@ public class Shell extends Tool implements Runnable {
listMode = !listMode; listMode = !listMode;
println("Result list mode is now " + (listMode ? "on" : "off")); println("Result list mode is now " + (listMode ? "on" : "off"));
} else if ("HISTORY".equals(upper)) { } else if ("HISTORY".equals(upper)) {
for (int i = 0; i < history.size(); i++) { for (int i = 0, size = history.size(); i < size; i++) {
String s = history.get(i); String s = history.get(i);
s = s.replace('\n', ' ').replace('\r', ' '); s = s.replace('\n', ' ').replace('\r', ' ');
println("#" + (1 + i) + ": " + s); println("#" + (1 + i) + ": " + s);
......
...@@ -511,11 +511,13 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData { ...@@ -511,11 +511,13 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData {
* closed * closed
*/ */
public int findColumn(String columnLabel) throws SQLException { public int findColumn(String columnLabel) throws SQLException {
for (int i = 0; columnLabel != null && columns != null && i < columns.size(); i++) { if (columnLabel != null && columns != null) {
for (int i = 0, size = columns.size(); i < size; i++) {
if (columnLabel.equalsIgnoreCase(getColumn(i).name)) { if (columnLabel.equalsIgnoreCase(getColumn(i).name)) {
return i + 1; return i + 1;
} }
} }
}
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, columnLabel).getSQLException(); throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, columnLabel).getSQLException();
} }
......
...@@ -14,9 +14,17 @@ public final class BitField { ...@@ -14,9 +14,17 @@ public final class BitField {
private static final int ADDRESS_BITS = 6; private static final int ADDRESS_BITS = 6;
private static final int BITS = 64; private static final int BITS = 64;
private static final int ADDRESS_MASK = BITS - 1; private static final int ADDRESS_MASK = BITS - 1;
private long[] data = new long[10]; private long[] data;
private int maxLength; private int maxLength;
public BitField() {
this(64);
}
public BitField(int capacity) {
data = new long[capacity >>> 3];
}
/** /**
* Get the index of the next bit that is not set. * Get the index of the next bit that is not set.
* *
......
...@@ -49,7 +49,9 @@ public class IntArray { ...@@ -49,7 +49,9 @@ public class IntArray {
* @param value the value to append * @param value the value to append
*/ */
public void add(int value) { public void add(int value) {
checkCapacity(); if (size >= data.length) {
ensureCapacity(size + size);
}
data[size++] = value; data[size++] = value;
} }
...@@ -83,9 +85,16 @@ public class IntArray { ...@@ -83,9 +85,16 @@ public class IntArray {
size--; size--;
} }
private void checkCapacity() { /**
if (size >= data.length) { * Ensure the the underlying array is large enough for the given number of
int[] d = new int[Math.max(4, data.length * 2)]; * entries.
*
* @param minCapacity the minimum capacity
*/
public void ensureCapacity(int minCapacity) {
minCapacity = Math.max(4, minCapacity);
if (minCapacity >= data.length) {
int[] d = new int[minCapacity];
System.arraycopy(data, 0, d, 0, data.length); System.arraycopy(data, 0, d, 0, data.length);
data = d; data = d;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论