This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 1510c0a8d57 Enhanced standalone mode and make it persist metadata
(#21092) (#21176)
1510c0a8d57 is described below
commit 1510c0a8d572170d4ca841ad63d8138ea3d13a5f
Author: 小马哥 <[email protected]>
AuthorDate: Mon Sep 26 10:46:15 2022 +0800
Enhanced standalone mode and make it persist metadata (#21092) (#21176)
* Enhanced standalone mode and make it persist metadata (#21092)
* Optimize variable naming
---
.../repository/standalone/jdbc/JDBCRepository.java | 29 +++++++++++++---------
1 file changed, 17 insertions(+), 12 deletions(-)
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/
[...]
index 89b90a08c75..f7cc5ec7ebb 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-repository/shardingsphere-standalone-mode-repository-provider/shardingsphere-standalone-mode-repository-jdbc/shardingsphere-standalone-mode-repository-jdbc-core/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
@@ -48,6 +48,8 @@ public final class JDBCRepository implements
StandalonePersistRepository {
private static final String SEPARATOR = "/";
+ private static final String H2_FILE_MODE_KEY = "file:";
+
private JDBCRepositoryProvider provider;
private HikariDataSource hikariDataSource;
@@ -57,14 +59,17 @@ public final class JDBCRepository implements
StandalonePersistRepository {
public void init(final Properties props) {
JDBCRepositoryProperties jdbcRepositoryProps = new
JDBCRepositoryProperties(props);
hikariDataSource = new HikariDataSource();
-
hikariDataSource.setJdbcUrl(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.JDBC_URL));
+ String jdbcUrl =
jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.JDBC_URL);
+ hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.USERNAME));
hikariDataSource.setPassword(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PASSWORD));
try (
Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement()) {
provider =
JDBCRepositoryProviderFactory.getInstance(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PROVIDER));
- statement.execute(provider.dropTableSQL());
+ if (!jdbcUrl.contains(H2_FILE_MODE_KEY)) {
+ statement.execute(provider.dropTableSQL());
+ }
statement.execute(provider.createTableSQL());
}
}
@@ -112,16 +117,21 @@ public final class JDBCRepository implements
StandalonePersistRepository {
@Override
public void persist(final String key, final String value) {
- String[] paths = Arrays.stream(key.split(SEPARATOR)).filter(each ->
!Strings.isNullOrEmpty(each)).toArray(String[]::new);
- String tempPrefix = "";
- String parent = SEPARATOR;
try {
+ String oldValue = get(key);
+ if (!Strings.isNullOrEmpty(oldValue)) {
+ update(key, value);
+ return;
+ }
+ String tempPrefix = "";
+ String parent = SEPARATOR;
+ String[] paths = Arrays.stream(key.split(SEPARATOR)).filter(each
-> !Strings.isNullOrEmpty(each)).toArray(String[]::new);
// Create key level directory recursively.
for (int i = 0; i < paths.length - 1; i++) {
String tempKey = tempPrefix + SEPARATOR + paths[i];
String tempKeyVal = get(tempKey);
if (Strings.isNullOrEmpty(tempKeyVal)) {
- if (0 != i) {
+ if (i != 0) {
parent = tempPrefix;
}
insert(tempKey, "", parent);
@@ -129,12 +139,7 @@ public final class JDBCRepository implements
StandalonePersistRepository {
tempPrefix = tempKey;
parent = tempKey;
}
- String keyValue = get(key);
- if (Strings.isNullOrEmpty(keyValue)) {
- insert(key, value, parent);
- } else {
- update(key, value);
- }
+ insert(key, value, parent);
} catch (final SQLException ex) {
log.error("Persist {} data to key: {} failed", getType(), key, ex);
}