Skip to content

Commit

Permalink
代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Oct 12, 2015
1 parent 6e67e63 commit 99588d6
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 51 deletions.
Expand Up @@ -10,9 +10,6 @@
import org.lealone.db.Session;
import org.lealone.db.result.Row;

/**
* An index that stores the data in an MVStore.
*/
public interface StandardIndex extends Index {

/**
Expand Down
Expand Up @@ -28,13 +28,9 @@
import org.lealone.db.table.StandardTable;
import org.lealone.db.table.TableFilter;
import org.lealone.storage.Storage;
import org.lealone.storage.StorageEngine;
import org.lealone.transaction.TransactionEngine;
import org.lealone.transaction.TransactionMap;

/**
* A primary index stored in a storage.
*/
public class StandardPrimaryIndex extends IndexBase {

/**
Expand All @@ -58,10 +54,9 @@ public class StandardPrimaryIndex extends IndexBase {
private long lastKey;
private int mainIndexColumn = -1;

public StandardPrimaryIndex(StorageEngine storageEngine, Session session, StandardTable table, int id,
IndexColumn[] columns, IndexType indexType) {

initIndexBase(table, id, table.getName() + "_DATA", columns, indexType);
public StandardPrimaryIndex(Session session, StandardTable table) {
IndexColumn[] columns = IndexColumn.wrap(table.getColumns());
initIndexBase(table, table.getId(), table.getName() + "_DATA", columns, IndexType.createScan(true));

this.table = table;
int[] sortTypes = new int[columns.length];
Expand All @@ -72,7 +67,7 @@ public StandardPrimaryIndex(StorageEngine storageEngine, Session session, Standa
ValueDataType valueType = new ValueDataType(database.getCompareMode(), database, sortTypes);
mapName = "table." + table.getName() + "." + getId();

Storage storage = database.getStorage(storageEngine);
Storage storage = database.getStorage(table.getStorageEngine());
TransactionEngine transactionEngine = database.getTransactionEngine();
dataMap = transactionEngine.beginTransaction(false).openMap(mapName, table.getMapType(), keyType, valueType,
storage);
Expand Down
Expand Up @@ -29,24 +29,18 @@
import org.lealone.db.table.StandardTable;
import org.lealone.db.table.TableFilter;
import org.lealone.storage.Storage;
import org.lealone.storage.StorageEngine;
import org.lealone.transaction.TransactionEngine;
import org.lealone.transaction.TransactionMap;

/**
* A secondary index stored in a storage.
*/
public class StandardSecondaryIndex extends IndexBase implements StandardIndex {

private final StandardTable table;
private final int keyColumns;
private final String mapName;
private final TransactionMap<Value, Value> dataMap;
private final StorageEngine storageEngine;

public StandardSecondaryIndex(StorageEngine storageEngine, Session session, StandardTable table, int id,
String indexName, IndexColumn[] columns, IndexType indexType) {
this.storageEngine = storageEngine;
public StandardSecondaryIndex(Session session, StandardTable table, int id, String indexName,
IndexColumn[] columns, IndexType indexType) {
Database db = session.getDatabase();
this.table = table;
initIndexBase(table, id, indexName, columns, indexType);
Expand All @@ -65,7 +59,7 @@ public StandardSecondaryIndex(StorageEngine storageEngine, Session session, Stan
ValueDataType keyType = new ValueDataType(db.getCompareMode(), db, sortTypes);
ValueDataType valueType = new ValueDataType(null, null, null);

Storage storage = database.getStorage(storageEngine);
Storage storage = database.getStorage(table.getStorageEngine());
TransactionEngine transactionEngine = database.getTransactionEngine();
dataMap = transactionEngine.beginTransaction(false).openMap(mapName, table.getMapType(), keyType, valueType,
storage);
Expand Down Expand Up @@ -168,7 +162,7 @@ private TransactionMap<Value, Value> openMap(Session session, String mapName) {
ValueDataType keyType = new ValueDataType(database.getCompareMode(), database, sortTypes);
ValueDataType valueType = new ValueDataType(null, null, null);

Storage storage = database.getStorage(storageEngine);
Storage storage = database.getStorage(table.getStorageEngine());
TransactionEngine transactionEngine = database.getTransactionEngine();
TransactionMap<Value, Value> map = transactionEngine.beginTransaction(false).openMap(mapName,
table.getMapType(), keyType, valueType, storage);
Expand Down
14 changes: 7 additions & 7 deletions lealone-db/src/main/java/org/lealone/db/schema/Schema.java
Expand Up @@ -606,18 +606,18 @@ public Table createTable(CreateTableData data) {
data.schema = this;

// 先看看是否在连接参数中指定了
if (data.storageEngine == null && data.session.getConnectionInfo() != null) {
data.storageEngine = data.session.getConnectionInfo().getDbSettings().defaultStorageEngine;
if (data.storageEngineName == null && data.session.getConnectionInfo() != null) {
data.storageEngineName = data.session.getConnectionInfo().getDbSettings().defaultStorageEngine;
}
// 再用默认的数据库参数
if (data.storageEngine == null) {
data.storageEngine = database.getStorageEngineName();
if (data.storageEngineName == null) {
data.storageEngineName = database.getStorageEngineName();
}
if (data.storageEngine != null) {
StorageEngine engine = StorageEngineManager.getInstance().getEngine(data.storageEngine);
if (data.storageEngineName != null) {
StorageEngine engine = StorageEngineManager.getInstance().getEngine(data.storageEngineName);
if (engine == null) {
try {
engine = (StorageEngine) Utils.loadUserClass(data.storageEngine).newInstance();
engine = (StorageEngine) Utils.loadUserClass(data.storageEngineName).newInstance();
StorageEngineManager.getInstance().registerEngine(engine);
} catch (Exception e) {
throw DbException.convert(e);
Expand Down
Expand Up @@ -71,7 +71,7 @@ public class CreateTableData {
/**
* The storage engine to use for creating the table.
*/
public String storageEngine;
public String storageEngineName;

/**
* The storage engine parameters to use for creating the table.
Expand Down
18 changes: 9 additions & 9 deletions lealone-db/src/main/java/org/lealone/db/table/StandardTable.java
Expand Up @@ -43,9 +43,6 @@
import org.lealone.storage.StorageEngine;
import org.lealone.transaction.Transaction;

/**
* A standard table stored in a storage.
*/
public class StandardTable extends TableBase {

private final StandardPrimaryIndex primaryIndex;
Expand Down Expand Up @@ -98,8 +95,7 @@ public StandardTable(CreateTableData data, StorageEngine storageEngine) {
} else
mapType = null;

primaryIndex = new StandardPrimaryIndex(storageEngine, data.session, this, getId(),
IndexColumn.wrap(getColumns()), IndexType.createScan(true));
primaryIndex = new StandardPrimaryIndex(data.session, this);
indexes.add(primaryIndex);
}

Expand All @@ -111,6 +107,10 @@ public String getMapType() {
return mapType;
}

public StorageEngine getStorageEngine() {
return storageEngine;
}

@Override
public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
int lockMode = database.getLockMode();
Expand Down Expand Up @@ -420,7 +420,7 @@ public Index addIndex(Session session, String indexName, int indexId, IndexColum
Index index;
int mainIndexColumn = getMainIndexColumn(indexType, cols);
if (indexType.isDelegate()) {
index = createMVDelegateIndex(indexId, indexName, indexType, mainIndexColumn);
index = createDelegateIndex(indexId, indexName, indexType, mainIndexColumn);
} else {
if (database.isStarting()) {
if (database.getStorage(storageEngine).hasMap("index." + indexId)) {
Expand All @@ -430,7 +430,7 @@ public Index addIndex(Session session, String indexName, int indexId, IndexColum
mainIndexColumn = -1;
}
if (mainIndexColumn != -1) {
index = createMVDelegateIndex(indexId, indexName, indexType, mainIndexColumn);
index = createDelegateIndex(indexId, indexName, indexType, mainIndexColumn);
} else if (isGlobalUniqueIndex(session, indexType)) {
index = new GlobalUniqueIndex(session, this, indexId, indexName, cols, indexType);
containsGlobalUniqueIndex = true;
Expand All @@ -441,7 +441,7 @@ public Index addIndex(Session session, String indexName, int indexId, IndexColum
index = new NonUniqueHashIndex(this, indexId, indexName, cols, indexType);
}
} else {
index = new StandardSecondaryIndex(storageEngine, session, this, indexId, indexName, cols, indexType);
index = new StandardSecondaryIndex(session, this, indexId, indexName, cols, indexType);
}
if (index instanceof StandardIndex && index.needRebuild()) {
rebuildIndex(session, (StandardIndex) index, indexName);
Expand All @@ -467,7 +467,7 @@ private boolean isGlobalUniqueIndex(Session session, IndexType indexType) {
// !session.isLocal();
}

private StandardDelegateIndex createMVDelegateIndex(int indexId, String indexName, IndexType indexType,
private StandardDelegateIndex createDelegateIndex(int indexId, String indexName, IndexType indexType,
int mainIndexColumn) {
primaryIndex.setMainIndexColumn(mainIndexColumn);
return new StandardDelegateIndex(this, indexId, indexName, primaryIndex, indexType);
Expand Down
6 changes: 3 additions & 3 deletions lealone-db/src/main/java/org/lealone/db/table/Table.java
Expand Up @@ -100,7 +100,7 @@ public abstract class Table extends SchemaObjectBase {
/**
* The table engine used (null for regular tables).
*/
protected String storageEngine;
protected String storageEngineName;

private final HashMap<String, Column> columnMap;
private final boolean persistIndexes;
Expand Down Expand Up @@ -1162,8 +1162,8 @@ public Column getColumn(String columnFamilyName, String columnName, boolean isIn
return getColumn(columnName);
}

public String getStorageEngine() {
return storageEngine;
public String getStorageEngineName() {
return storageEngineName;
}

public void addColumn(Column column) {
Expand Down
8 changes: 4 additions & 4 deletions lealone-db/src/main/java/org/lealone/db/table/TableBase.java
Expand Up @@ -42,7 +42,7 @@ public abstract class TableBase extends Table {

public TableBase(CreateTableData data) {
super(data.schema, data.id, data.tableName, data.persistIndexes, data.persistData);
this.storageEngine = data.storageEngine;
this.storageEngineName = data.storageEngineName;
this.globalTemporary = data.globalTemporary;

setTemporary(data.temporary);
Expand Down Expand Up @@ -110,11 +110,11 @@ public String getCreateSQL() {
buff.append(column.getCreateSQL());
}
buff.append("\n)");
if (storageEngine != null) {
if (storageEngineName != null) {
String d = getDatabase().getSettings().defaultStorageEngine;
if (d == null || !storageEngine.endsWith(d)) {
if (d == null || !storageEngineName.endsWith(d)) {
buff.append("\nENGINE \"");
buff.append(storageEngine);
buff.append(storageEngineName);
buff.append('\"');
}
}
Expand Down
4 changes: 2 additions & 2 deletions lealone-sql/src/main/java/org/lealone/sql/Parser.java
Expand Up @@ -5520,9 +5520,9 @@ private CreateTable parseCreateTable(boolean temp, boolean globalTemp, boolean p
// throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, storageEngine);
// }
// }
command.setStorageEngine(readUniqueIdentifier());
command.setStorageEngineName(readUniqueIdentifier());
} else if (database.getSettings().defaultStorageEngine != null) {
command.setStorageEngine(database.getSettings().defaultStorageEngine);
command.setStorageEngineName(database.getSettings().defaultStorageEngine);
}
if (readIf("WITH")) {
read("(");
Expand Down
Expand Up @@ -313,7 +313,7 @@ private Table cloneTableStructure(Column[] columns, Database db, String tempName
data.isHidden = table.isHidden();
data.create = true;
data.session = session;
data.storageEngine = table.getStorageEngine();
data.storageEngineName = table.getStorageEngineName();
Table newTable = getSchema().createTable(data);
newTable.setComment(table.getComment());
StringBuilder buff = new StringBuilder();
Expand Down
Expand Up @@ -285,8 +285,8 @@ public void setSortedInsertMode(boolean sortedInsertMode) {
this.sortedInsertMode = sortedInsertMode;
}

public void setStorageEngine(String storageEngine) {
data.storageEngine = storageEngine;
public void setStorageEngineName(String storageEngineName) {
data.storageEngineName = storageEngineName;
}

public void setStorageEngineParams(Map<String, String> storageEngineParams) {
Expand Down

0 comments on commit 99588d6

Please sign in to comment.