Skip to content

Commit

Permalink
整理配置参数
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Jan 5, 2015
1 parent 3793c33 commit 6b30780
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 217 deletions.
27 changes: 16 additions & 11 deletions lealone-common/src/main/java/org/lealone/engine/ConnectionInfo.java
Expand Up @@ -67,6 +67,8 @@ private static boolean isKnownSetting(String s) {
private SessionFactory sessionFactory; private SessionFactory sessionFactory;
private SessionInterface session; private SessionInterface session;


private DbSettings dbSettings;

/** /**
* Create a server connection info object. * Create a server connection info object.
* *
Expand Down Expand Up @@ -217,7 +219,7 @@ public void readProperties(Properties info) {
prop.put(key, value); prop.put(key, value);
} else { } else {
if (s == null) { if (s == null) {
s = getDbSettings(); s = DbSettings.getDefaultSettings();
} }
if (s.containsKey(key)) { if (s.containsKey(key)) {
prop.put(key, value); prop.put(key, value);
Expand All @@ -227,7 +229,7 @@ public void readProperties(Properties info) {
} }


private void readSettingsFromURL() { private void readSettingsFromURL() {
DbSettings dbSettings = DbSettings.getInstance(); DbSettings dbSettings = DbSettings.getDefaultSettings();
//Lealone的JDBC URL语法: //Lealone的JDBC URL语法:
//jdbc:lealone:tcp://[host:port],[host:port].../[database][;propertyName1][=propertyValue1][;propertyName2][=propertyValue2] //jdbc:lealone:tcp://[host:port],[host:port].../[database][;propertyName1][=propertyValue1][;propertyName2][=propertyValue2]
//数据库名与参数之间用';'号分隔,不同参数之间也用';'号分隔 //数据库名与参数之间用';'号分隔,不同参数之间也用';'号分隔
Expand Down Expand Up @@ -586,18 +588,21 @@ public void setServerKey(String serverKey) {
} }


public DbSettings getDbSettings() { public DbSettings getDbSettings() {
DbSettings defaultSettings = DbSettings.getInstance(); if (dbSettings == null) {
HashMap<String, String> s = null; DbSettings defaultSettings = DbSettings.getDefaultSettings();
for (Object k : prop.keySet()) { HashMap<String, String> s = null;
String key = k.toString(); for (Object k : prop.keySet()) {
if (!isKnownSetting(key) && defaultSettings.containsKey(key)) { String key = k.toString();
if (s == null) { if (!isKnownSetting(key) && defaultSettings.containsKey(key)) {
s = New.hashMap(); if (s == null) {
s = New.hashMap();
}
s.put(key, prop.getProperty(key));
} }
s.put(key, prop.getProperty(key));
} }
dbSettings = DbSettings.getInstance(s);
} }
return DbSettings.getInstance(s); return dbSettings;
} }


private static String remapURL(String url) { private static String remapURL(String url) {
Expand Down
126 changes: 108 additions & 18 deletions lealone-common/src/main/java/org/lealone/engine/DbSettings.java
Expand Up @@ -8,6 +8,10 @@


import java.util.HashMap; import java.util.HashMap;


import org.lealone.api.ErrorCode;
import org.lealone.message.DbException;
import org.lealone.util.Utils;

/** /**
* This class contains various database-level settings. To override the * This class contains various database-level settings. To override the
* documented default value for a database, append the setting in the database * documented default value for a database, append the setting in the database
Expand All @@ -20,9 +24,43 @@
* backward compatible. * backward compatible.
* </p> * </p>
*/ */
//可以通过系统属性的方式来设定每个数据库的默认值,比如lealone.x.y.z = 10,
//还可以通过在URL中指定参数来覆盖默认值,在URL中的参数格式是x_y_z = 10(没有lealone前缀,用下划线替换点号)

class SettingsBase {
//这个字段需要放在这,在DbSettings的构造函数中可以提前初始化它,
//如果放在子类中,DbSettings类的其他字段会在执行构造函数中的代码之前执行,此时settings还是null。
protected final HashMap<String, String> settings;

protected SettingsBase(HashMap<String, String> s) {
this.settings = s;
}
}

public class DbSettings extends SettingsBase { public class DbSettings extends SettingsBase {
private static final DbSettings defaultSettings = new DbSettings(new HashMap<String, String>());


private static DbSettings defaultSettings; public static DbSettings getDefaultSettings() {
return defaultSettings;
}

/**
* INTERNAL.
* Get the settings for the given properties (may be null).
*
* @param s the settings
* @return the settings
*/
public static DbSettings getInstance(HashMap<String, String> s) {
if (s == null || s.isEmpty()) {
return defaultSettings;
}
return new DbSettings(s);
}

private DbSettings(HashMap<String, String> s) {
super(s);
}


/** /**
* Database setting <code>ALIAS_COLUMN_NAME</code> (default: false).<br /> * Database setting <code>ALIAS_COLUMN_NAME</code> (default: false).<br />
Expand Down Expand Up @@ -324,10 +362,6 @@ public class DbSettings extends SettingsBase {
*/ */
public final String defaultStorageEngine = get("DEFAULT_STORAGE_ENGINE", Constants.DEFAULT_STORAGE_ENGINE_NAME); public final String defaultStorageEngine = get("DEFAULT_STORAGE_ENGINE", Constants.DEFAULT_STORAGE_ENGINE_NAME);


private DbSettings(HashMap<String, String> s) {
super(s);
}

/** /**
* Database setting <code>COMPRESS</code> * Database setting <code>COMPRESS</code>
* (default: false).<br /> * (default: false).<br />
Expand All @@ -336,23 +370,79 @@ private DbSettings(HashMap<String, String> s) {
public final boolean compressData = get("COMPRESS", false); public final boolean compressData = get("COMPRESS", false);


/** /**
* INTERNAL. * Get the setting for the given key.
* Get the settings for the given properties (may be null).
* *
* @param s the settings * @param key the key
* @return the settings * @param defaultValue the default value
*/ * @return the setting
public static DbSettings getInstance(HashMap<String, String> s) { */
if (s == null || s.isEmpty()) { private boolean get(String key, boolean defaultValue) {
if (defaultSettings == null) { String s = get(key, defaultValue ? "true" : "false");
defaultSettings = new DbSettings(new HashMap<String, String>()); try {
return Boolean.parseBoolean(s);
} catch (NumberFormatException e) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, "key:" + key + " value:" + s);
}
}

/**
* Get the setting for the given key.
*
* @param key the key
* @param defaultValue the default value
* @return the setting
*/
private int get(String key, int defaultValue) {
String s = get(key, Integer.toString(defaultValue));
try {
return Integer.decode(s);
} catch (NumberFormatException e) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, e, "key:" + key + " value:" + s);
}
}

/**
* Get the setting for the given key.
*
* @param key the key
* @param defaultValue the default value
* @return the setting
*/
protected String get(String key, String defaultValue) {
StringBuilder buff = new StringBuilder(Constants.PROJECT_NAME_PREFIX);
for (char c : key.toCharArray()) {
if (c == '_') {
buff.append('.');
} else {
// Character.toUpperCase / toLowerCase ignores the locale
buff.append(Character.toLowerCase(c));
} }
return defaultSettings;
} }
return new DbSettings(s); String sysProperty = buff.toString();
String v = settings.get(key);
if (v == null) {
v = Utils.getProperty(sysProperty, defaultValue);
settings.put(key, v);
}
return v;
}

/**
* Check if the settings contains the given key.
*
* @param k the key
* @return true if they do
*/
public boolean containsKey(String k) {
return settings.containsKey(k);
} }


public static DbSettings getInstance() { /**
return getInstance(null); * Get all settings.
*
* @return the settings
*/
public HashMap<String, String> getSettings() {
return settings;
} }
} }
103 changes: 0 additions & 103 deletions lealone-common/src/main/java/org/lealone/engine/SettingsBase.java

This file was deleted.

0 comments on commit 6b30780

Please sign in to comment.