Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PostgreSQL and CockroachDB backends #484

Merged
merged 10 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public static void registerBackends() {
String confFile = "/backend.properties";
InputStream input = RegisterUtil.class.getClass()
.getResourceAsStream(confFile);
E.checkState(input != null, "Can't read file '%s' as stream", confFile);
E.checkState(input != null,
"Can't read file '%s' as stream", confFile);

PropertiesConfiguration props = new PropertiesConfiguration();
props.setDelimiterParsingDisabled(true);
Expand Down Expand Up @@ -93,7 +94,8 @@ private static void registerBackend(String backend) {
registerPostgresql();
break;
default:
throw new HugeException("Unsupported backend type '%s'", backend);
throw new HugeException("Unsupported backend type '%s'",
backend);
}
}

Expand Down Expand Up @@ -171,13 +173,13 @@ public static void registerPalo() {
public static void registerPostgresql() {
// Register config
OptionSpace.register("postgresql",
"com.baidu.hugegraph.backend.store.postgresql.PostgresqlOptions");
"com.baidu.hugegraph.backend.store.postgresql.PostgresqlOptions");
// Register serializer
SerializerFactory.register("postgresql",
"com.baidu.hugegraph.backend.store.postgresql.PostgresqlSerializer");
"com.baidu.hugegraph.backend.store.postgresql.PostgresqlSerializer");
// Register backend
BackendProviderFactory.register("postgresql",
"com.baidu.hugegraph.backend.store.postgresql.PostgresqlStoreProvider");
"com.baidu.hugegraph.backend.store.postgresql.PostgresqlStoreProvider");
}

public static void registerServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected String engine() {

protected void dropTable(Session session) {
LOG.debug("Drop table: {}", this.table());
String sql = String.format(this.dropTableTemplate, this.table());
String sql = this.buildDropTemplate();
try {
session.execute(sql);
} catch (SQLException e) {
Expand All @@ -131,7 +131,7 @@ protected void dropTable(Session session) {

protected void truncateTable(Session session) {
LOG.debug("Truncate table: {}", this.table());
String sql = String.format(this.truncateTableTemplate, this.table());
String sql = this.buildTruncateTemplate();
try {
session.execute(sql);
} catch (SQLException e) {
Expand Down Expand Up @@ -199,6 +199,14 @@ protected String buildDeleteTemplate(List<HugeKeys> idNames) {
return this.deleteTemplate;
}

protected String buildDropTemplate() {
return String.format(this.dropTableTemplate, this.table());
}

protected String buildTruncateTemplate() {
return String.format(this.truncateTableTemplate, this.table());
}

/**
* Insert an entire row
*/
Expand All @@ -211,7 +219,7 @@ public void insert(Session session, MysqlBackendEntry.Row entry) {
// Create or get insert prepare statement
insertStmt = session.prepareStatement(template);
int i = 1;
for (Object object : this.insertTemplateObjects(entry)) {
for (Object object : this.buildInsertObjects(entry)) {
insertStmt.setObject(i++, object);
}
} catch (SQLException e) {
Expand Down Expand Up @@ -560,7 +568,7 @@ protected void appendPartition(StringBuilder delete) {
// pass
}

protected List<Object> insertTemplateObjects(MysqlBackendEntry.Row entry) {
protected List<Object> buildInsertObjects(MysqlBackendEntry.Row entry) {
List<Object> objects = new ArrayList<>();
for (Object key : entry.columns().keySet()) {
objects.add(entry.columns().get(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static synchronized PostgresqlOptions instance() {
"jdbc.url",
"The url of database in JDBC format.",
disallowEmpty(),
"jdbc:postgresql://127.0.0.1:5432"
"jdbc:postgresql://127.0.0.1:5432/"
);

public static final ConfigOption<String> JDBC_USERNAME =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public PostgresqlSessions(HugeConfig config, String database, String store) {

@Override
protected String buildCreateDatabase(String database) {
return String.format("CREATE DATABASE %s ENCODING='UTF-8' " +
"TEMPLATE=template0 LC_COLLATE='C' LC_CTYPE='C';", database);
return String.format("CREATE DATABASE %s ENCODING='UTF-8'", database);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ protected String engine() {
return Strings.EMPTY;
}


@Override
protected List<Object> insertTemplateObjects(MysqlBackendEntry.Row entry) {
protected List<Object> buildInsertObjects(MysqlBackendEntry.Row entry) {
List<Object> objects = new ArrayList<>();
objects.addAll(super.insertTemplateObjects(entry));
objects.addAll(super.insertTemplateObjects(entry));
objects.addAll(super.buildInsertObjects(entry));
objects.addAll(super.buildInsertObjects(entry));
return objects;
}

Expand Down