From e5c58b7765996db94764257a2f6e0f63ba608d10 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Mon, 3 Jun 2019 22:09:46 +0800 Subject: [PATCH] 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 a20cf43fc2..f36a36c125 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); @@ -145,15 +149,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 } @@ -166,17 +170,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()); } } } @@ -190,7 +194,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();