Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
Change-Id: Ib67261388516884dd2f376eaa95d6437d8d1d2c1
  • Loading branch information
zhoney committed Jun 3, 2019
1 parent f88d047 commit e5c58b7
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
}
Expand All @@ -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());
}
}
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -65,15 +65,16 @@ 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);
} catch (PSQLException e) {
// 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
EdgeCoreTest.class,
VertexPropertyCoreTest.class,
EdgePropertyCoreTest.class,
RestoreCoreTest.class
RestoreCoreTest.class,
MultiGraphsTest.class
})
public class CoreTestSuite {

Expand Down
Original file line number Diff line number Diff line change
@@ -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<HugeGraph> openGraphs(String... graphNames) {
List<HugeGraph> graphs = new ArrayList<>(graphNames.length);
PropertiesConfiguration conf = Utils.getConf();
Configuration config = new BaseConfiguration();
for (Iterator<String> 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<HugeGraph> graphs) {
for (HugeGraph graph : graphs) {
graph.close();
}
}

@Test
public void testCreateMultiGraphs() {
List<HugeGraph> graphs = openGraphs("g1", "g2", "g3", "123",
" g", "g 1", " .", ". .",
"@$%^&*()_+`-={}|[]\"<?;'~,./\\",
"azAZ0123456789", " ~", "g~", "g'");
destoryGraphs(graphs);
}

@Test
public void testCreateGraphsWithInvalidNames() {
Assert.assertThrows(RuntimeException.class,
() -> openGraphs(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -80,4 +85,22 @@ public static boolean contains(List<Edge> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<String, Object> getBaseConfiguration(
String graphName,
Expand All @@ -195,7 +177,7 @@ public Map<String, Object> getBaseConfiguration(
LOG.debug("Full name of test is: {}", testFullName);
LOG.debug("Prefix of test is: {}", testFullName.substring(0, index));
HashMap<String, Object> confMap = new HashMap<>();
PropertiesConfiguration config = getConf();
PropertiesConfiguration config = Utils.getConf();
Iterator<String> keys = config.getKeys();
while (keys.hasNext()) {
String key = keys.next();
Expand Down

0 comments on commit e5c58b7

Please sign in to comment.