Skip to content

Commit

Permalink
REPLICATION STRATEGY相关的参数在DATABASE层配置,而不是SCHEMA层
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Feb 6, 2017
1 parent 884f28b commit 0572616
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 174 deletions.
40 changes: 23 additions & 17 deletions lealone-db/src/main/java/org/lealone/db/Database.java
Expand Up @@ -2603,30 +2603,33 @@ public String getSQL() {
}

public static String getCreateSQL(String quotedDbName, ConnectionInfo ci) {
return getCreateSQL(quotedDbName, ci.getDbSettings(), null);
return getCreateSQL(quotedDbName, ci.getDbSettings(), null, null);
}

private static String getCreateSQL(String quotedDbName, DbSettings dbSettings,
Map<String, String> replicationProperties) {
Map<String, String> replicationProperties, RunMode runMode) {
StatementBuilder sql = new StatementBuilder("CREATE DATABASE IF NOT EXISTS ");
sql.append(quotedDbName).append(" WITH ( ");
sql.append(quotedDbName);
if (runMode != null) {
sql.append(" RUN MODE ").append(runMode.toString());
}
if (replicationProperties != null && !replicationProperties.isEmpty()) {
sql.append(" WITH REPLICATION STRATEGY");
appendMap(sql, replicationProperties);
}
sql.append(" PARAMETERS");
appendMap(sql, dbSettings.getSettings());
return sql.toString();
}

Map<String, String> map = dbSettings.getSettings();
private static void appendMap(StatementBuilder sql, Map<String, String> map) {
sql.resetCount();
sql.append("(");
for (Entry<String, String> e : map.entrySet()) {
sql.appendExceptFirst(",");
sql.append(e.getKey()).append('=').append("'").append(e.getValue()).append("'");
}
sql.append(" )");

if (replicationProperties != null && !replicationProperties.isEmpty()) {
sql.append(" REPLICATION (");
for (Map.Entry<String, String> e : replicationProperties.entrySet()) {
sql.appendExceptFirst(",");
sql.append('\'').append(e.getKey()).append("':'").append(e.getValue()).append('\'');
}
sql.append(')');
}
return sql.toString();
sql.append(')');
}

@Override
Expand All @@ -2641,7 +2644,7 @@ public Database getDatabase() {

@Override
public String getCreateSQL() {
return getCreateSQL(quoteIdentifier(name), dbSettings, replicationProperties);
return getCreateSQL(quoteIdentifier(name), dbSettings, replicationProperties, runMode);
}

@Override
Expand Down Expand Up @@ -2804,7 +2807,10 @@ boolean isRootUser(String userName) {
}

synchronized User alterRootUserPassword(byte[] userPasswordHash) {
User rootUser = users.get(ROOT_USER);
String userName = ROOT_USER;
if (!getSettings().databaseToUpper)
userName = "root";
User rootUser = users.get(userName);
if (!rootUser.validateUserPasswordHash(userPasswordHash)) {
rootUser.setUserPasswordHash(userPasswordHash);
updateMeta(systemSession, rootUser);
Expand Down
31 changes: 0 additions & 31 deletions lealone-db/src/main/java/org/lealone/db/schema/Schema.java
Expand Up @@ -8,7 +8,6 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.lealone.api.ErrorCode;
import org.lealone.common.exceptions.DbException;
Expand Down Expand Up @@ -50,9 +49,6 @@ public class Schema extends DbObjectBase {
private final HashMap<String, Constant> constants;
private final HashMap<String, FunctionAlias> functions;

private Map<String, String> replicationProperties;
private ReplicationPropertiesChangeListener replicationPropertiesChangeListener;

/**
* The set of returned unique names that are not yet stored. It is used to
* avoid returning the same unique name twice when multiple threads
Expand Down Expand Up @@ -87,24 +83,6 @@ public DbObjectType getType() {
return DbObjectType.SCHEMA;
}

public Map<String, String> getReplicationProperties() {
return replicationProperties;
}

public void setReplicationProperties(Map<String, String> replicationProperties) {
this.replicationProperties = replicationProperties;
if (replicationPropertiesChangeListener != null)
replicationPropertiesChangeListener.replicationPropertiesChanged(this);
}

public void setReplicationPropertiesChangeListener(ReplicationPropertiesChangeListener listener) {
replicationPropertiesChangeListener = listener;
}

public static interface ReplicationPropertiesChangeListener {
void replicationPropertiesChanged(Schema schema);
}

/**
* Check if this schema can be dropped. System schemas can not be dropped.
*
Expand All @@ -119,17 +97,8 @@ public String getCreateSQL() {
if (system) {
return null;
}

StatementBuilder sql = new StatementBuilder();
sql.append("CREATE SCHEMA IF NOT EXISTS ").append(getSQL()).append(" AUTHORIZATION ").append(owner.getSQL());
if (replicationProperties != null && !replicationProperties.isEmpty()) {
sql.append(" WITH REPLICATION = (");
for (Map.Entry<String, String> e : replicationProperties.entrySet()) {
sql.appendExceptFirst(",");
sql.append('\'').append(e.getKey()).append("':'").append(e.getValue()).append('\'');
}
sql.append(')');
}
return sql.toString();
}

Expand Down
75 changes: 38 additions & 37 deletions lealone-sql/src/main/java/org/lealone/sql/Parser.java
Expand Up @@ -68,7 +68,6 @@
import org.lealone.sql.ddl.AlterDatabase;
import org.lealone.sql.ddl.AlterIndexRename;
import org.lealone.sql.ddl.AlterSchemaRename;
import org.lealone.sql.ddl.AlterSchemaWithReplication;
import org.lealone.sql.ddl.AlterSequence;
import org.lealone.sql.ddl.AlterTableAddConstraint;
import org.lealone.sql.ddl.AlterTableAlterColumn;
Expand Down Expand Up @@ -908,7 +907,8 @@ private StatementBase parseShow() {
if (readIf("FROM")) {
schema = readUniqueIdentifier();
}
buff.append("TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=? ORDER BY TABLE_NAME");
buff.append(
"TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=? ORDER BY TABLE_NAME");
paramValues.add(ValueString.get(schema));
} else if (readIf("COLUMNS")) {
// for MySQL compatibility
Expand Down Expand Up @@ -1254,6 +1254,16 @@ private StatementBase parseDrop() {
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
return command;
// } else if (readIf("DATABASE")) {
// boolean ifExists = readIfExists(false);
// String dbName = readUniqueIdentifier();
// DropDatabase command = new DropDatabase(session);
// command.setDropAllObjects(true);
// if (readIf("DELETE")) {
// read("FILES");
// command.setDeleteFiles(true);
// }
// return command;
} else if (readIf("ALL")) {
read("OBJECTS");
DropDatabase command = new DropDatabase(session);
Expand Down Expand Up @@ -4204,17 +4214,18 @@ private CreateDatabase parseCreateDatabase() {
// Map<String, String> resourceQuota = null;
Map<String, String> parameters = null;
if (runMode == RunMode.REPLICATION || runMode == RunMode.SHARDING) {
if (readIf("REPLICATION")) {
if (readIf("WITH")) {
read("REPLICATION");
read("STRATEGY");
replicationProperties = parseParameters();
replicationProperties = parseParameters(true);
checkReplicationProperties(replicationProperties);
}
}
// if (readIf("RESOURCE")) {
// read("QUOTA");
// resourceQuota = parseParameters();
// }
if (readIf("WITH"))
if (readIf("PARAMETERS"))
parameters = parseParameters();
// return new CreateDatabase(session, dbName, ifNotExists, runMode, replicationProperties, resourceQuota,
// parameters);
Expand All @@ -4237,6 +4248,11 @@ else if (readIf("REPLICATION"))
return null;
}

private void checkReplicationProperties(Map<String, String> replicationProperties) {
if (replicationProperties != null && !replicationProperties.containsKey("class"))
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, sqlCommand + ", missing replication strategy class");
}

private CreateSchema parseCreateSchema() {
CreateSchema command = new CreateSchema(session);
command.setIfNotExists(readIfNotExists());
Expand All @@ -4246,26 +4262,23 @@ private CreateSchema parseCreateSchema() {
} else {
command.setAuthorization(session.getUser().getName());
}

if (readIf("WITH")) {
read("REPLICATION");
read("=");
Map<String, String> replicationProperties = parseParameters();
command.setReplicationProperties(replicationProperties);
checkReplicationProperties(replicationProperties);
}

return command;
}

private Map<String, String> parseParameters() {
return parseParameters(false);
}

private Map<String, String> parseParameters(boolean toLowerCase) {
read("(");
HashMap<String, String> parameters = New.hashMap();
if (readIf(")"))
return parameters;
String k, v;
do {
k = readUniqueIdentifier();
if (toLowerCase)
k = k.toLowerCase();
if (readIf("=") || readIf(":"))
v = readString();
else
Expand Down Expand Up @@ -4622,16 +4635,19 @@ private void checkSchema(Schema old) {
private AlterDatabase parseAlterDatabase() {
String dbName = readUniqueIdentifier();
Database db = LealoneDatabase.getInstance().getDatabase(dbName);
Map<String, String> parameters = null;
RunMode runMode = parseRunMode();
Map<String, String> replicationProperties = null;
if (readIf("WITH"))
parameters = parseParameters();
if (readIf("REPLICATION")) {
replicationProperties = parseParameters();
checkReplicationProperties(replicationProperties);
Map<String, String> parameters = null;
if (runMode == RunMode.REPLICATION || runMode == RunMode.SHARDING) {
if (readIf("WITH")) {
read("REPLICATION");
read("STRATEGY");
replicationProperties = parseParameters(true);
checkReplicationProperties(replicationProperties);
}
}

RunMode runMode = parseRunMode();
if (readIf("PARAMETERS"))
parameters = parseParameters();
return new AlterDatabase(session, db, parameters, replicationProperties, runMode);
}

Expand Down Expand Up @@ -4663,24 +4679,9 @@ private AlterView parseAlterView() {

private DefineStatement parseAlterSchema() {
String schemaName = readIdentifierWithSchema();
if (readIf("WITH")) {
read("REPLICATION");
read("=");
AlterSchemaWithReplication command = new AlterSchemaWithReplication(session);
command.setSchema(getSchema(schemaName));
Map<String, String> replicationProperties = parseParameters();
command.setReplicationProperties(replicationProperties);
checkReplicationProperties(replicationProperties);
return command;
}
return parseAlterSchemaRename(schemaName);
}

private void checkReplicationProperties(Map<String, String> replicationProperties) {
if (replicationProperties != null && !replicationProperties.containsKey("class"))
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, sqlCommand + ", missing replication strategy class");
}

private AlterSchemaRename parseAlterSchemaRename(String schemaName) {
Schema old = getSchema();
AlterSchemaRename command = new AlterSchemaRename(session);
Expand Down

This file was deleted.

Expand Up @@ -6,8 +6,6 @@
*/
package org.lealone.sql.ddl;

import java.util.Map;

import org.lealone.api.ErrorCode;
import org.lealone.common.exceptions.DbException;
import org.lealone.db.Database;
Expand All @@ -27,8 +25,6 @@ public class CreateSchema extends DefineStatement {
private String authorization;
private boolean ifNotExists;

private Map<String, String> replicationProperties;

public CreateSchema(ServerSession session) {
super(session);
}
Expand Down Expand Up @@ -60,7 +56,6 @@ public int update() {
}
int id = getObjectId();
Schema schema = new Schema(db, id, schemaName, user, false);
schema.setReplicationProperties(replicationProperties);
db.addDatabaseObject(session, schema);
}
return 0;
Expand All @@ -74,8 +69,4 @@ public void setAuthorization(String userName) {
this.authorization = userName;
}

public void setReplicationProperties(Map<String, String> replicationProperties) {
this.replicationProperties = replicationProperties;
}

}

0 comments on commit 0572616

Please sign in to comment.