From 03fe1b8332aab87eaeea161d4d6a00712c84adee Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 28 May 2019 16:26:27 +0800 Subject: [PATCH 01/11] fix existDatabase(), clearBackend() and rollback() for postgresql fixed: #530 Change-Id: I5061eb95abf0e0ef3a6a83a74ced291647d9308d --- .../baidu/hugegraph/config/CoreOptions.java | 2 +- .../backend/store/mysql/MysqlSessions.java | 12 ++++----- .../store/postgresql/PostgresqlSessions.java | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java index 8623df8bb6..0009f9918e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/config/CoreOptions.java @@ -125,7 +125,7 @@ public static synchronized CoreOptions instance() { public static final ConfigOption VERTEX_CHECK_CUSTOMIZED_ID_EXIST = new ConfigOption<>( - "vertex.check_customzied_id_exist", + "vertex.check_customized_id_exist", "Whether to check the vertices exist for those using " + "customized id strategy", disallowEmpty(), diff --git a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java index 9411ed5f0e..1d24651ba2 100644 --- a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java +++ b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java @@ -169,8 +169,7 @@ protected String buildCreateDatabase(String database) { public void dropDatabase() { LOG.debug("Drop database: {}", this.database); - String sql = String.format("DROP DATABASE IF EXISTS %s;", - this.database); + String sql = this.buildDropDatabase(this.database); try (Connection conn = this.openWithoutDB(DROP_DB_TIMEOUT)) { conn.createStatement().execute(sql); } catch (SQLException e) { @@ -183,6 +182,10 @@ public void dropDatabase() { } } + protected String buildDropDatabase(String database) { + return String.format("DROP DATABASE IF EXISTS %s;", database); + } + public boolean existsDatabase() { try (Connection conn = this.openWithoutDB(0); ResultSet result = conn.getMetaData().getCatalogs()) { @@ -317,12 +320,9 @@ public Integer commit() { } this.conn.commit(); this.clear(); + this.end(); } catch (SQLException e) { throw new BackendException("Failed to commit", e); - } finally { - try { - this.end(); - } catch (SQLException ignored) {} } return updated; } diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java index fb882b7db2..229b1eb25d 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java @@ -20,6 +20,7 @@ package com.baidu.hugegraph.backend.store.postgresql; import java.sql.Connection; +import java.sql.ResultSet; import java.sql.SQLException; import org.apache.http.client.utils.URIBuilder; @@ -45,6 +46,20 @@ public PostgresqlSessions(HugeConfig config, String database, String store) { super(config, database, store); } + @Override + public boolean existsDatabase() { + String statement = String.format( + "SELECT datname FROM pg_catalog.pg_database " + + "WHERE datname = '%s';", this.database()); + try (Connection conn = this.openWithoutDB(0)) { + ResultSet result = conn.createStatement().executeQuery(statement); + return result.next(); + } catch (Exception e) { + throw new BackendException("Failed to obtain MySQL metadata, " + + "please ensure it is ok", e); + } + } + @Override public void createDatabase() { // Create database with non-database-session @@ -76,6 +91,17 @@ protected String buildCreateDatabase(String database) { return String.format(POSTGRESQL_DB_CREATE, database); } + @Override + protected String buildDropDatabase(String database) { + return String.format( + "REVOKE CONNECT ON DATABASE %s FROM public;" + + "SELECT pg_terminate_backend(pg_stat_activity.pid) " + + " FROM pg_stat_activity " + + " WHERE pg_stat_activity.datname = '%s';" + + "DROP DATABASE IF EXISTS %s;", + database, database, database); + } + @Override protected URIBuilder newConnectionURIBuilder() { // Suppress error log when database does not exist From f5f3f35dd97e6df2ac9768b10b8f64573281af68 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Mon, 3 Jun 2019 22:09:46 +0800 Subject: [PATCH 02/11] improve Change-Id: Ib67261388516884dd2f376eaa95d6437d8d1d2c1 --- .../backend/store/mysql/MysqlSessions.java | 24 +++--- .../store/postgresql/PostgresqlSessions.java | 11 +-- .../baidu/hugegraph/core/CoreTestSuite.java | 3 +- .../baidu/hugegraph/core/MultiGraphsTest.java | 82 +++++++++++++++++++ .../com/baidu/hugegraph/testutil/Utils.java | 23 ++++++ .../tinkerpop/TestGraphProvider.java | 24 +----- 6 files changed, 130 insertions(+), 37 deletions(-) create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java diff --git a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java index 1d24651ba2..b6d64bac8e 100644 --- a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java +++ b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java @@ -64,6 +64,10 @@ public String database() { return this.database; } + public String escapedDatabase() { + return MysqlUtil.escapeString(this.database()); + } + /** * Try connect with specified database, will not reconnect if failed * @throws SQLException if a database access error occurs @@ -86,9 +90,9 @@ protected boolean opened() { private Connection open(boolean autoReconnect) throws SQLException { String url = this.config.get(MysqlOptions.JDBC_URL); if (url.endsWith("/")) { - url = String.format("%s%s", url, this.database); + url = String.format("%s%s", url, this.database()); } else { - url = String.format("%s/%s", url, this.database); + url = String.format("%s/%s", url, this.database()); } int maxTimes = this.config.get(MysqlOptions.JDBC_RECONNECT_MAX_TIMES); @@ -146,15 +150,15 @@ public void checkSessionConnected() { public void createDatabase() { // Create database with non-database-session - LOG.debug("Create database: {}", this.database); + LOG.debug("Create database: {}", this.database()); - String sql = this.buildCreateDatabase(this.database); + String sql = this.buildCreateDatabase(this.escapedDatabase()); try (Connection conn = this.openWithoutDB(0)) { conn.createStatement().execute(sql); } catch (SQLException e) { if (!e.getMessage().endsWith("already exists")) { throw new BackendException("Failed to create database '%s'", e, - this.database); + this.database()); } // Ignore exception if database already exists } @@ -167,17 +171,17 @@ protected String buildCreateDatabase(String database) { } public void dropDatabase() { - LOG.debug("Drop database: {}", this.database); + LOG.debug("Drop database: {}", this.database()); - String sql = this.buildDropDatabase(this.database); + String sql = this.buildDropDatabase(this.escapedDatabase()); try (Connection conn = this.openWithoutDB(DROP_DB_TIMEOUT)) { conn.createStatement().execute(sql); } catch (SQLException e) { if (e.getCause() instanceof SocketTimeoutException) { - LOG.warn("Drop database '{}' timeout", this.database); + LOG.warn("Drop database '{}' timeout", this.database()); } else { throw new BackendException("Failed to drop database '%s'", - this.database); + this.database()); } } } @@ -191,7 +195,7 @@ public boolean existsDatabase() { ResultSet result = conn.getMetaData().getCatalogs()) { while (result.next()) { String dbName = result.getString(1); - if (dbName.equals(this.database)) { + if (dbName.equals(this.database())) { return true; } } diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java index 229b1eb25d..bc51b71838 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java @@ -50,13 +50,13 @@ public PostgresqlSessions(HugeConfig config, String database, String store) { public boolean existsDatabase() { String statement = String.format( "SELECT datname FROM pg_catalog.pg_database " + - "WHERE datname = '%s';", this.database()); + "WHERE datname = '%s';", this.escapedDatabase()); try (Connection conn = this.openWithoutDB(0)) { ResultSet result = conn.createStatement().executeQuery(statement); return result.next(); } catch (Exception e) { - throw new BackendException("Failed to obtain MySQL metadata, " + - "please ensure it is ok", e); + throw new BackendException("Failed to obtain PostgreSQL metadata," + + " please ensure it is ok", e); } } @@ -65,7 +65,7 @@ public void createDatabase() { // Create database with non-database-session LOG.debug("Create database: {}", this.database()); - String sql = this.buildCreateDatabase(this.database()); + String sql = this.buildCreateDatabase(this.escapedDatabase()); try (Connection conn = this.openWithoutDB(0)) { try { conn.createStatement().execute(sql); @@ -73,7 +73,8 @@ public void createDatabase() { // CockroackDB not support 'template' arg of CREATE DATABASE if (e.getMessage().contains("syntax error at or near " + "\"template\"")) { - sql = String.format(COCKROACH_DB_CREATE, this.database()); + sql = String.format(COCKROACH_DB_CREATE, + this.escapedDatabase()); conn.createStatement().execute(sql); } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/CoreTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/CoreTestSuite.java index 0f1b713720..cc4ed05aef 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/CoreTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/CoreTestSuite.java @@ -43,7 +43,8 @@ EdgeCoreTest.class, VertexPropertyCoreTest.class, EdgePropertyCoreTest.class, - RestoreCoreTest.class + RestoreCoreTest.class, + MultiGraphsTest.class }) public class CoreTestSuite { diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java new file mode 100644 index 0000000000..369c31ee4d --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -0,0 +1,82 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.core; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.commons.configuration.BaseConfiguration; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.config.CoreOptions; +import com.baidu.hugegraph.dist.RegisterUtil; +import com.baidu.hugegraph.testutil.Assert; +import com.baidu.hugegraph.testutil.Utils; + +public class MultiGraphsTest { + + @BeforeClass + public static void initEnv() { + RegisterUtil.registerBackends(); + } + + public static List openGraphs(String... graphNames) { + List graphs = new ArrayList<>(graphNames.length); + PropertiesConfiguration conf = Utils.getConf(); + Configuration config = new BaseConfiguration(); + for (Iterator keys = conf.getKeys(); keys.hasNext();) { + String key = keys.next(); + config.setProperty(key, conf.getProperty(key)); + } + ((BaseConfiguration) config).setDelimiterParsingDisabled(true); + for (String graphName : graphNames) { + config.setProperty(CoreOptions.STORE.name(), graphName); + graphs.add((HugeGraph) GraphFactory.open(config)); + } + return graphs; + } + + public static void destoryGraphs(List graphs) { + for (HugeGraph graph : graphs) { + graph.close(); + } + } + + @Test + public void testCreateMultiGraphs() { + List graphs = openGraphs("g1", "g2", "g3", "123", + " g", "g 1", " .", ". .", + "@$%^&*()_+`-={}|[]\" openGraphs("")); + } +} diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/testutil/Utils.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/testutil/Utils.java index c185d1d557..3883495870 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/testutil/Utils.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/testutil/Utils.java @@ -19,18 +19,23 @@ package com.baidu.hugegraph.testutil; +import java.io.File; import java.util.Date; import java.util.List; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; +import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeFactory; import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.backend.id.Id; import com.baidu.hugegraph.testutil.FakeObjects.FakeEdge; import com.baidu.hugegraph.testutil.FakeObjects.FakeVertex; import com.baidu.hugegraph.util.DateUtil; +import com.baidu.hugegraph.util.E; public class Utils { @@ -80,4 +85,22 @@ public static boolean contains(List edges, FakeEdge fakeEdge) { public static Date date(String rawDate) { return DateUtil.parse(rawDate); } + + public static PropertiesConfiguration getConf() { + String confFile = Utils.class.getClassLoader() + .getResource(CONF_PATH).getPath(); + File file = new File(confFile); + E.checkArgument(file.exists() && file.isFile() && file.canRead(), + "Need to specify a readable config file rather than:" + + " %s", file.toString()); + + PropertiesConfiguration config; + try { + config = new PropertiesConfiguration(file); + } catch (ConfigurationException e) { + throw new HugeException("Unable to load config file: %s", + e, confFile); + } + return config; + } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/tinkerpop/TestGraphProvider.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/tinkerpop/TestGraphProvider.java index 9cb8332059..db5e5543cf 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/tinkerpop/TestGraphProvider.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/tinkerpop/TestGraphProvider.java @@ -57,6 +57,7 @@ import com.baidu.hugegraph.structure.HugeProperty; import com.baidu.hugegraph.structure.HugeVertex; import com.baidu.hugegraph.structure.HugeVertexProperty; +import com.baidu.hugegraph.testutil.Utils; import com.baidu.hugegraph.type.define.IdStrategy; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; @@ -119,7 +120,7 @@ public TestGraphProvider(String suite) throws IOException { } private void initBlackList() throws IOException { - String filter = getConf().getString(FILTER); + String filter = Utils.getConf().getString(FILTER); if (filter == null || filter.isEmpty()) { filter = DEFAULT_FILTER; } @@ -157,25 +158,6 @@ private void initBlackList() throws IOException { } } - private static PropertiesConfiguration getConf() { - String confFile = TestGraphProvider.class.getClassLoader() - .getResource(CONF_PATH).getPath(); - File file = new File(confFile); - E.checkArgument( - file.exists() && file.isFile() && file.canRead(), - "Need to specify a readable config file rather than: %s", - file.toString()); - - PropertiesConfiguration config; - try { - config = new PropertiesConfiguration(file); - } catch (ConfigurationException e) { - throw new HugeException("Unable to load config file: %s", - e, confFile); - } - return config; - } - @Override public Map getBaseConfiguration( String graphName, @@ -195,7 +177,7 @@ public Map getBaseConfiguration( LOG.debug("Full name of test is: {}", testFullName); LOG.debug("Prefix of test is: {}", testFullName.substring(0, index)); HashMap confMap = new HashMap<>(); - PropertiesConfiguration config = getConf(); + PropertiesConfiguration config = Utils.getConf(); Iterator keys = config.getKeys(); while (keys.hasNext()) { String key = keys.next(); From c1d6fc6d1b4370ad3cbc8a150dbe5d6f55aac146 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 5 Jun 2019 17:40:33 +0800 Subject: [PATCH 03/11] improve Change-Id: I3e9d7b416360d190faf4e09e0aa06382baff05ba --- .../backend/store/mysql/MysqlSessions.java | 4 +- .../store/postgresql/PostgresqlSessions.java | 2 +- .../baidu/hugegraph/core/MultiGraphsTest.java | 90 +++++++++++++++---- 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java index b6d64bac8e..3534ca58be 100644 --- a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java +++ b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java @@ -324,10 +324,12 @@ public Integer commit() { } this.conn.commit(); this.clear(); - this.end(); } catch (SQLException e) { throw new BackendException("Failed to commit", e); } + try { + this.end(); + } catch (SQLException ignored) {} return updated; } diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java index bc51b71838..966bd74ccc 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java @@ -50,7 +50,7 @@ public PostgresqlSessions(HugeConfig config, String database, String store) { public boolean existsDatabase() { String statement = String.format( "SELECT datname FROM pg_catalog.pg_database " + - "WHERE datname = '%s';", this.escapedDatabase()); + "WHERE datname = %s;", this.escapedDatabase()); try (Connection conn = this.openWithoutDB(0)) { ResultSet result = conn.createStatement().executeQuery(statement); return result.next(); diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index 369c31ee4d..c41a4942cd 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -22,25 +22,79 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.tinkerpop.gremlin.structure.T; +import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.util.GraphFactory; -import org.junit.BeforeClass; import org.junit.Test; import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.backend.id.IdGenerator; import com.baidu.hugegraph.config.CoreOptions; -import com.baidu.hugegraph.dist.RegisterUtil; import com.baidu.hugegraph.testutil.Assert; import com.baidu.hugegraph.testutil.Utils; +import jersey.repackaged.com.google.common.collect.ImmutableList; +import jersey.repackaged.com.google.common.collect.ImmutableMap; + public class MultiGraphsTest { - @BeforeClass - public static void initEnv() { - RegisterUtil.registerBackends(); + private static final Map BACKENDS = + ImmutableMap.builder() + .put("memory", "text") + .put("rocksdb", "binary") + .put("cassandra", "cassandra") + .put("hbase", "hbase") + .put("mysql", "mysql") + .put("postgresql", "postgresql") + .build(); + + @Test + public void testCreateMultiGraphs() { + List graphs = openGraphs("g1", "g2", "g3", "123", + " g", "g 1", " .", ". .", + "@$%^&*()_+`-={}|[]\" openGraphs("")); + } + + @Test + public void testCreateGraphsWithSameName() { + HugeGraph g1 = openGraphs("graph").get(0); + HugeGraph g2 = openGraphs("graph").get(0); + + Assert.assertThrows(IllegalArgumentException.class, + () -> g2.vertexLabel("node")); + g1.schema().vertexLabel("node").useCustomizeNumberId() + .ifNotExist().create(); + g2.vertexLabel("node"); + + g1.addVertex(T.label, "node", T.id, 1); + Iterator vertices = g2.vertices(1); + Assert.assertTrue(vertices.hasNext()); + Vertex vertex = vertices.next(); + Assert.assertFalse(vertices.hasNext()); + Assert.assertEquals(IdGenerator.of(1), vertex.id()); + + destoryGraphs(ImmutableList.of(g1)); + } + + @Test + public void testCreateGraphWithDifferentBackends() { + HugeGraph g1 = openGraphWithBackend("g1", "memory"); + HugeGraph g2 = openGraphWithBackend("g2", "rocksdb"); + HugeGraph graph = openGraphs("graph").get(0); + destoryGraphs(ImmutableList.of(g1, g2, graph)); } public static List openGraphs(String... graphNames) { @@ -65,18 +119,18 @@ public static void destoryGraphs(List graphs) { } } - @Test - public void testCreateMultiGraphs() { - List graphs = openGraphs("g1", "g2", "g3", "123", - " g", "g 1", " .", ". .", - "@$%^&*()_+`-={}|[]\" openGraphs("")); + public static HugeGraph openGraphWithBackend(String graph, String backend) { + PropertiesConfiguration conf = Utils.getConf(); + Configuration config = new BaseConfiguration(); + for (Iterator keys = conf.getKeys(); keys.hasNext();) { + String key = keys.next(); + config.setProperty(key, conf.getProperty(key)); + } + ((BaseConfiguration) config).setDelimiterParsingDisabled(true); + config.setProperty(CoreOptions.STORE.name(), graph); + config.setProperty(CoreOptions.BACKEND.name(), backend); + config.setProperty(CoreOptions.SERIALIZER.name(), + BACKENDS.get(backend)); + return ((HugeGraph) GraphFactory.open(config)); } } From a9b2916e47394faadfc6cb76b6a1ad2cbec47a34 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 5 Jun 2019 20:34:33 +0800 Subject: [PATCH 04/11] improve Change-Id: I25da367b644a8c20d4f07e023de66cc136eb0192 --- .../backend/store/mysql/MysqlSessions.java | 4 ++-- .../store/postgresql/PostgresqlSessions.java | 5 ++--- .../baidu/hugegraph/core/MultiGraphsTest.java | 17 ++++++++++++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java index 3534ca58be..00fedd952b 100644 --- a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java +++ b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java @@ -152,7 +152,7 @@ public void createDatabase() { // Create database with non-database-session LOG.debug("Create database: {}", this.database()); - String sql = this.buildCreateDatabase(this.escapedDatabase()); + String sql = this.buildCreateDatabase(this.database()); try (Connection conn = this.openWithoutDB(0)) { conn.createStatement().execute(sql); } catch (SQLException e) { @@ -173,7 +173,7 @@ protected String buildCreateDatabase(String database) { public void dropDatabase() { LOG.debug("Drop database: {}", this.database()); - String sql = this.buildDropDatabase(this.escapedDatabase()); + String sql = this.buildDropDatabase(this.database()); try (Connection conn = this.openWithoutDB(DROP_DB_TIMEOUT)) { conn.createStatement().execute(sql); } catch (SQLException e) { diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java index 966bd74ccc..56b855d55c 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java @@ -65,7 +65,7 @@ public void createDatabase() { // Create database with non-database-session LOG.debug("Create database: {}", this.database()); - String sql = this.buildCreateDatabase(this.escapedDatabase()); + String sql = this.buildCreateDatabase(this.database()); try (Connection conn = this.openWithoutDB(0)) { try { conn.createStatement().execute(sql); @@ -73,8 +73,7 @@ public void createDatabase() { // CockroackDB not support 'template' arg of CREATE DATABASE if (e.getMessage().contains("syntax error at or near " + "\"template\"")) { - sql = String.format(COCKROACH_DB_CREATE, - this.escapedDatabase()); + sql = String.format(COCKROACH_DB_CREATE, this.database()); conn.createStatement().execute(sql); } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index c41a4942cd..1fee7bb6db 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -70,8 +70,12 @@ public void testCreateGraphsWithInvalidNames() { @Test public void testCreateGraphsWithSameName() { - HugeGraph g1 = openGraphs("graph").get(0); - HugeGraph g2 = openGraphs("graph").get(0); + List graphs = openGraphs("graph", "graph"); + HugeGraph g1 = graphs.get(0); + HugeGraph g2 = graphs.get(1); + + g1.initBackend(); + g2.initBackend(); Assert.assertThrows(IllegalArgumentException.class, () -> g2.vertexLabel("node")); @@ -90,7 +94,14 @@ public void testCreateGraphsWithSameName() { } @Test - public void testCreateGraphWithDifferentBackends() { + public void testCreateGraphWithSameNameDifferentBackends() { + openGraphWithBackend("graph", "memory"); + Assert.assertThrows(RuntimeException.class, + () -> openGraphWithBackend("graph", "rocksdb")); + } + + @Test + public void testCreateGraphsWithDifferentNameDifferentBackends() { HugeGraph g1 = openGraphWithBackend("g1", "memory"); HugeGraph g2 = openGraphWithBackend("g2", "rocksdb"); HugeGraph graph = openGraphs("graph").get(0); From 12991d9e72cbcabdda213d4a86e688d50c132872 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Thu, 6 Jun 2019 15:53:04 +0800 Subject: [PATCH 05/11] add graph name validate Change-Id: I8107e4ffef6b1c64696266cbd8309329d666c62c --- .../java/com/baidu/hugegraph/HugeFactory.java | 7 ++ .../baidu/hugegraph/core/MultiGraphsTest.java | 72 ++++++++++++------- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java index 7eca2139c7..4376ef0177 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java @@ -34,11 +34,18 @@ public class HugeFactory { + private static final String NAME_REGEX = "^[A-Za-z][A-Za-z0-9_]{0,31}$"; + private static final Map graphs = new HashMap<>(); public static synchronized HugeGraph open(Configuration config) { HugeConfig conf = new HugeConfig(config); String name = conf.get(CoreOptions.STORE); + E.checkArgument(name.matches(NAME_REGEX), + "Invalid graph name '%s', valid graph name is up to " + + "32 alpha-numeric characters and underscores " + + "and only letters are supported as first letter. " + + "Note: letter is case insensitive"); HugeGraph graph = graphs.get(name); if (graph == null || graph.closed()) { graph = new HugeGraph(conf); diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index 1fee7bb6db..f483ac3afa 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.Map; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; @@ -39,26 +38,16 @@ import com.baidu.hugegraph.testutil.Utils; import jersey.repackaged.com.google.common.collect.ImmutableList; -import jersey.repackaged.com.google.common.collect.ImmutableMap; public class MultiGraphsTest { - private static final Map BACKENDS = - ImmutableMap.builder() - .put("memory", "text") - .put("rocksdb", "binary") - .put("cassandra", "cassandra") - .put("hbase", "hbase") - .put("mysql", "mysql") - .put("postgresql", "postgresql") - .build(); - @Test public void testCreateMultiGraphs() { - List graphs = openGraphs("g1", "g2", "g3", "123", - " g", "g 1", " .", ". .", - "@$%^&*()_+`-={}|[]\" graphs = openGraphs("g1", "g_2", "azAZ0123456789"); + for (HugeGraph graph : graphs) { + graph.initBackend(); + graph.clearBackend(); + } destoryGraphs(graphs); } @@ -66,45 +55,78 @@ public void testCreateMultiGraphs() { public void testCreateGraphsWithInvalidNames() { Assert.assertThrows(RuntimeException.class, () -> openGraphs("")); + List invalidNames = ImmutableList.of( + "123", " g", "g 1", " .", ". .", + "@$%^&*()_+`-={}|[]\" openGraphs(name)); + } } @Test public void testCreateGraphsWithSameName() { - List graphs = openGraphs("graph", "graph"); + List graphs = openGraphs("g", "g", "G"); HugeGraph g1 = graphs.get(0); HugeGraph g2 = graphs.get(1); + HugeGraph g3 = graphs.get(2); g1.initBackend(); g2.initBackend(); + g3.initBackend(); Assert.assertThrows(IllegalArgumentException.class, () -> g2.vertexLabel("node")); + Assert.assertThrows(IllegalArgumentException.class, + () -> g3.vertexLabel("node")); g1.schema().vertexLabel("node").useCustomizeNumberId() .ifNotExist().create(); g2.vertexLabel("node"); + g3.vertexLabel("node"); g1.addVertex(T.label, "node", T.id, 1); + g1.tx().commit(); Iterator vertices = g2.vertices(1); Assert.assertTrue(vertices.hasNext()); Vertex vertex = vertices.next(); Assert.assertFalse(vertices.hasNext()); Assert.assertEquals(IdGenerator.of(1), vertex.id()); - destoryGraphs(ImmutableList.of(g1)); + vertices = g3.vertices(1); + Assert.assertTrue(vertices.hasNext()); + vertex = vertices.next(); + Assert.assertFalse(vertices.hasNext()); + Assert.assertEquals(IdGenerator.of(1), vertex.id()); + + g1.clearBackend(); + g2.clearBackend(); + g3.clearBackend(); + destoryGraphs(ImmutableList.of(g1, g3)); } @Test public void testCreateGraphWithSameNameDifferentBackends() { - openGraphWithBackend("graph", "memory"); + HugeGraph g1 = openGraphWithBackend("graph", "memory", "text"); + g1.initBackend(); Assert.assertThrows(RuntimeException.class, - () -> openGraphWithBackend("graph", "rocksdb")); + () -> openGraphWithBackend("graph", "rocksdb", + "binary")); + g1.clearBackend(); + g1.close(); } @Test public void testCreateGraphsWithDifferentNameDifferentBackends() { - HugeGraph g1 = openGraphWithBackend("g1", "memory"); - HugeGraph g2 = openGraphWithBackend("g2", "rocksdb"); + HugeGraph g1 = openGraphWithBackend("g1", "memory", "text"); + HugeGraph g2 = openGraphWithBackend("g2", "rocksdb", "binary"); HugeGraph graph = openGraphs("graph").get(0); + g1.initBackend(); + g2.initBackend(); + graph.initBackend(); + g1.clearBackend(); + g2.clearBackend(); + graph.clearBackend(); destoryGraphs(ImmutableList.of(g1, g2, graph)); } @@ -130,7 +152,8 @@ public static void destoryGraphs(List graphs) { } } - public static HugeGraph openGraphWithBackend(String graph, String backend) { + public static HugeGraph openGraphWithBackend(String graph, String backend, + String serializer) { PropertiesConfiguration conf = Utils.getConf(); Configuration config = new BaseConfiguration(); for (Iterator keys = conf.getKeys(); keys.hasNext();) { @@ -140,8 +163,7 @@ public static HugeGraph openGraphWithBackend(String graph, String backend) { ((BaseConfiguration) config).setDelimiterParsingDisabled(true); config.setProperty(CoreOptions.STORE.name(), graph); config.setProperty(CoreOptions.BACKEND.name(), backend); - config.setProperty(CoreOptions.SERIALIZER.name(), - BACKENDS.get(backend)); + config.setProperty(CoreOptions.SERIALIZER.name(), serializer); return ((HugeGraph) GraphFactory.open(config)); } } From 1f32428c2b5a272c04d4b3f831c7c9434532ea99 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Thu, 6 Jun 2019 16:30:33 +0800 Subject: [PATCH 06/11] graph name case insensitive Change-Id: I915a5cfea40fca960365eff5d2e83b2aa1f8a0f0 --- .../src/main/java/com/baidu/hugegraph/HugeFactory.java | 1 + .../src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java index 4376ef0177..3e19fa3997 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java @@ -46,6 +46,7 @@ public static synchronized HugeGraph open(Configuration config) { "32 alpha-numeric characters and underscores " + "and only letters are supported as first letter. " + "Note: letter is case insensitive"); + name = name.toLowerCase(); HugeGraph graph = graphs.get(name); if (graph == null || graph.closed()) { graph = new HugeGraph(conf); diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index f483ac3afa..662922f6c7 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -102,7 +102,7 @@ public void testCreateGraphsWithSameName() { g1.clearBackend(); g2.clearBackend(); g3.clearBackend(); - destoryGraphs(ImmutableList.of(g1, g3)); + destoryGraphs(ImmutableList.of(g1)); } @Test From e911fa227a0b618df8ba4ef56e7f044e70274d37 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Thu, 6 Jun 2019 17:05:53 +0800 Subject: [PATCH 07/11] change graph name length to 48 alpha-numeric Change-Id: Ic4b7b5cc5315f8417712307431c86fa73ec6ab6c --- .../src/main/java/com/baidu/hugegraph/HugeFactory.java | 4 ++-- .../main/java/com/baidu/hugegraph/core/MultiGraphsTest.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java index 3e19fa3997..1f7530ae9e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java @@ -34,7 +34,7 @@ public class HugeFactory { - private static final String NAME_REGEX = "^[A-Za-z][A-Za-z0-9_]{0,31}$"; + private static final String NAME_REGEX = "^[A-Za-z][A-Za-z0-9_]{0,47}$"; private static final Map graphs = new HashMap<>(); @@ -43,7 +43,7 @@ public static synchronized HugeGraph open(Configuration config) { String name = conf.get(CoreOptions.STORE); E.checkArgument(name.matches(NAME_REGEX), "Invalid graph name '%s', valid graph name is up to " + - "32 alpha-numeric characters and underscores " + + "48 alpha-numeric characters and underscores " + "and only letters are supported as first letter. " + "Note: letter is case insensitive"); name = name.toLowerCase(); diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index 662922f6c7..dc6d9caee0 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -41,9 +41,11 @@ public class MultiGraphsTest { + private static final String NAME48 = + "g12345678901234567890123456789012345678901234567"; @Test public void testCreateMultiGraphs() { - List graphs = openGraphs("g1", "g_2", "azAZ0123456789"); + List graphs = openGraphs("g1", "g_2", NAME48); for (HugeGraph graph : graphs) { graph.initBackend(); graph.clearBackend(); @@ -59,7 +61,7 @@ public void testCreateGraphsWithInvalidNames() { "123", " g", "g 1", " .", ". .", "@$%^&*()_+`-={}|[]\" openGraphs(name)); } From 6fccfa5a443a2fe399eff863f51b72a21126dbff Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 11 Jun 2019 15:27:16 +0800 Subject: [PATCH 08/11] tiny improve Change-Id: Ifdb8e9b4d5703259da14cfc933b2c87e36c9f1fa --- .../backend/store/postgresql/PostgresqlSerializer.java | 4 ++++ .../backend/store/postgresql/PostgresqlSessions.java | 8 ++++---- .../java/com/baidu/hugegraph/core/MultiGraphsTest.java | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java index 4a787e35c9..bdbfb3578c 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java @@ -62,6 +62,10 @@ protected String escapeString(String value) { if (value.equals("\u0000")) { return "\'\'"; } + return escapeStringForPg(value); + } + + public static String escapeStringForPg(String value) { StringBuilder builder = new StringBuilder(8 + value.length()); builder.append('\''); try { diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java index 56b855d55c..07bfe20580 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java @@ -94,12 +94,12 @@ protected String buildCreateDatabase(String database) { @Override protected String buildDropDatabase(String database) { return String.format( - "REVOKE CONNECT ON DATABASE %s FROM public;" + + "REVOKE CONNECT ON DATABASE %s FROM public;" + "SELECT pg_terminate_backend(pg_stat_activity.pid) " + " FROM pg_stat_activity " + - " WHERE pg_stat_activity.datname = '%s';" + - "DROP DATABASE IF EXISTS %s;", - database, database, database); + " WHERE pg_stat_activity.datname = %s;" + + "DROP DATABASE IF EXISTS %s;", database, + PostgresqlSerializer.escapeStringForPg(database), database); } @Override diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index dc6d9caee0..17b08bb94c 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -43,6 +43,7 @@ public class MultiGraphsTest { private static final String NAME48 = "g12345678901234567890123456789012345678901234567"; + @Test public void testCreateMultiGraphs() { List graphs = openGraphs("g1", "g_2", NAME48); @@ -123,12 +124,15 @@ public void testCreateGraphsWithDifferentNameDifferentBackends() { HugeGraph g1 = openGraphWithBackend("g1", "memory", "text"); HugeGraph g2 = openGraphWithBackend("g2", "rocksdb", "binary"); HugeGraph graph = openGraphs("graph").get(0); + g1.initBackend(); g2.initBackend(); graph.initBackend(); + g1.clearBackend(); g2.clearBackend(); graph.clearBackend(); + destoryGraphs(ImmutableList.of(g1, g2, graph)); } From db2caf8313cdc6b08c2253d2c174404c245da53e Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 11 Jun 2019 15:51:39 +0800 Subject: [PATCH 09/11] tiny improve Change-Id: Ifb3caf3124932aef9bb1beb1e4ec2d58e40ac7c6 --- .../postgresql/PostgresqlSerializer.java | 18 +---------------- .../store/postgresql/PostgresqlSessions.java | 20 ++++++++++++++++--- .../baidu/hugegraph/core/MultiGraphsTest.java | 3 +-- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java index bdbfb3578c..d133ee7a31 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSerializer.java @@ -19,12 +19,8 @@ package com.baidu.hugegraph.backend.store.postgresql; -import java.sql.SQLException; - import org.apache.logging.log4j.util.Strings; -import org.postgresql.core.Utils; -import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.backend.id.IdUtil; import com.baidu.hugegraph.backend.serializer.TableBackendEntry; import com.baidu.hugegraph.backend.store.BackendEntry; @@ -62,18 +58,6 @@ protected String escapeString(String value) { if (value.equals("\u0000")) { return "\'\'"; } - return escapeStringForPg(value); - } - - public static String escapeStringForPg(String value) { - StringBuilder builder = new StringBuilder(8 + value.length()); - builder.append('\''); - try { - Utils.escapeLiteral(builder, value, false); - } catch (SQLException e) { - throw new HugeException("Failed to escape '%s'", e, value); - } - builder.append('\''); - return builder.toString(); + return PostgresqlSessions.escapeString(value); } } diff --git a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java index 07bfe20580..0927db8569 100644 --- a/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java +++ b/hugegraph-postgresql/src/main/java/com/baidu/hugegraph/backend/store/postgresql/PostgresqlSessions.java @@ -24,9 +24,11 @@ import java.sql.SQLException; import org.apache.http.client.utils.URIBuilder; +import org.postgresql.core.Utils; import org.postgresql.util.PSQLException; import org.slf4j.Logger; +import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.backend.BackendException; import com.baidu.hugegraph.backend.store.mysql.MysqlSessions; import com.baidu.hugegraph.backend.store.mysql.MysqlStore; @@ -94,12 +96,12 @@ protected String buildCreateDatabase(String database) { @Override protected String buildDropDatabase(String database) { return String.format( - "REVOKE CONNECT ON DATABASE %s FROM public;" + + "REVOKE CONNECT ON DATABASE %s FROM public;" + "SELECT pg_terminate_backend(pg_stat_activity.pid) " + " FROM pg_stat_activity " + " WHERE pg_stat_activity.datname = %s;" + - "DROP DATABASE IF EXISTS %s;", database, - PostgresqlSerializer.escapeStringForPg(database), database); + "DROP DATABASE IF EXISTS %s;", + database, escapeString(database), database); } @Override @@ -107,4 +109,16 @@ protected URIBuilder newConnectionURIBuilder() { // Suppress error log when database does not exist return new URIBuilder().addParameter("loggerLevel", "OFF"); } + + public static String escapeString(String value) { + StringBuilder builder = new StringBuilder(8 + value.length()); + builder.append('\''); + try { + Utils.escapeLiteral(builder, value, false); + } catch (SQLException e) { + throw new HugeException("Failed to escape '%s'", e, value); + } + builder.append('\''); + return builder.toString(); + } } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index 17b08bb94c..fdce89e151 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -46,7 +46,7 @@ public class MultiGraphsTest { @Test public void testCreateMultiGraphs() { - List graphs = openGraphs("g1", "g_2", NAME48); + List graphs = openGraphs("g_1", NAME48); for (HugeGraph graph : graphs) { graph.initBackend(); graph.clearBackend(); @@ -124,7 +124,6 @@ public void testCreateGraphsWithDifferentNameDifferentBackends() { HugeGraph g1 = openGraphWithBackend("g1", "memory", "text"); HugeGraph g2 = openGraphWithBackend("g2", "rocksdb", "binary"); HugeGraph graph = openGraphs("graph").get(0); - g1.initBackend(); g2.initBackend(); graph.initBackend(); From 9ab709f982238358661c6d0ab3ecdef3ccc29514 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 11 Jun 2019 16:11:44 +0800 Subject: [PATCH 10/11] tiny improve Change-Id: I248bc00db991a6fa4779134410e307240f13d465 --- .../com/baidu/hugegraph/core/MultiGraphsTest.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java index fdce89e151..c8f8a975cc 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/MultiGraphsTest.java @@ -56,13 +56,16 @@ public void testCreateMultiGraphs() { @Test public void testCreateGraphsWithInvalidNames() { - Assert.assertThrows(RuntimeException.class, - () -> openGraphs("")); List invalidNames = ImmutableList.of( - "123", " g", "g 1", " .", ". .", - "@$%^&*()_+`-={}|[]\" openGraphs(name)); } From 5e67064fd5304d2908b909baecb89584b0c7efe8 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Tue, 11 Jun 2019 17:48:16 +0800 Subject: [PATCH 11/11] warn if failed to close transaction Also check if auto-commit when calling execute() for mysql Change-Id: Ia6cc5163fa0c01b3c44b0afed2d51799c47f6f97 --- .../backend/store/mysql/MysqlSessions.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java index 00fedd952b..0b9962b0e3 100644 --- a/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java +++ b/hugegraph-mysql/src/main/java/com/baidu/hugegraph/backend/store/mysql/MysqlSessions.java @@ -315,6 +315,14 @@ public void end() throws SQLException { this.conn.setAutoCommit(true); } + public void endAndLog() { + try { + this.conn.setAutoCommit(true); + } catch (SQLException e) { + LOG.warn("Failed to set connection to auto-commit status", e); + } + } + @Override public Integer commit() { int updated = 0; @@ -327,9 +335,7 @@ public Integer commit() { } catch (SQLException e) { throw new BackendException("Failed to commit", e); } - try { - this.end(); - } catch (SQLException ignored) {} + this.endAndLog(); return updated; } @@ -341,9 +347,7 @@ public void rollback() { } catch (SQLException e) { throw new BackendException("Failed to rollback", e); } finally { - try { - this.end(); - } catch (SQLException ignored) {} + this.endAndLog(); } } @@ -358,6 +362,13 @@ public ResultSet select(String sql) throws SQLException { } public boolean execute(String sql) throws SQLException { + /* + * commit() or rollback() failed to set connection to auto-commit + * status in prior transaction. Manually set to auto-commit here. + */ + if (this.conn.getAutoCommit()) { + this.end(); + } return this.conn.createStatement().execute(sql); }