Skip to content

Commit

Permalink
adjust table define
Browse files Browse the repository at this point in the history
Change-Id: I155d9f121dc47309c0aab3181cb82916bf62d91f
  • Loading branch information
zhoney committed Apr 30, 2019
1 parent 9578c87 commit b63f052
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 562 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ jdk:

sudo: required

addons:
postgresql: "9.5"

cache:
directories:
- $HOME/.m2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,35 @@
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.InsertionOrderUtil;

import jersey.repackaged.com.google.common.collect.ImmutableMap;

public class TableDefine {

private final Map<HugeKeys, String> columns;
private final List<HugeKeys> keys;
private final Map<String, String> typesMapping;

public TableDefine() {
this.columns = InsertionOrderUtil.newMap();
this.keys = InsertionOrderUtil.newList();
this.typesMapping = ImmutableMap.of();
}

public TableDefine(Map<String, String> typesMapping) {
this.columns = InsertionOrderUtil.newMap();
this.keys = InsertionOrderUtil.newList();
this.typesMapping = typesMapping;
}

public TableDefine column(HugeKeys key, String... desc) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < desc.length; i++) {
sb.append(desc[i]);
String type = desc[i];
if (i == 0 && this.typesMapping.containsKey(type)) {
type = this.typesMapping.get(type);
}
assert type != null;
sb.append(type);
if (i != desc.length - 1) {
sb.append(" ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cassandra.password=
#jdbc.password=
#jdbc.reconnect_max_times=3
#jdbc.reconnect_interval=3

#jdbc.sslmode=disable

# palo backend config
#palo.host=127.0.0.1
Expand Down
5 changes: 3 additions & 2 deletions hugegraph-dist/src/assembly/travis/install-postgresql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -ev

TRAVIS_DIR=`dirname $0`
CONF=$TRAVIS_DIR/../../../../hugegraph-test/src/main/resources/hugegraph.properties
CONF=hugegraph-test/src/main/resources/hugegraph.properties

POSTGRESQL_DRIVER=org.postgresql.Driver
POSTGRESQL_URL=jdbc:postgresql://localhost:5432/
Expand All @@ -14,4 +14,5 @@ sed -i "s/jdbc.driver=.*/jdbc.driver=$POSTGRESQL_DRIVER/" $CONF
sed -i "s?jdbc.url=.*?jdbc.url=$POSTGRESQL_URL?" $CONF
sed -i "s/jdbc.username=.*/jdbc.username=$POSTGRESQL_USERNAME/" $CONF

sudo service postgresql restart
sudo service postgresql stop 9.2
sudo service postgresql start 9.5
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public class MysqlOptions extends OptionHolder {

private MysqlOptions() {
protected MysqlOptions() {
super();
}

Expand Down Expand Up @@ -89,4 +89,12 @@ public static synchronized MysqlOptions instance() {
rangeInt(1, 10),
3
);

public static final ConfigOption<String> SSL_MODE =
new ConfigOption<>(
"jdbc.ssl_mode",
"The url of database in JDBC format.",
disallowEmpty(),
"disable"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;

import com.baidu.hugegraph.backend.BackendException;
Expand All @@ -50,6 +51,8 @@ public abstract class MysqlTable

private static final Logger LOG = Log.logger(MysqlStore.class);

protected String dropTableTemplate = "DROP TABLE IF EXISTS %s;";
protected String truncateTableTemplate = "TRUNCATE TABLE %s;";
// The template for insert and delete statements
private String insertTemplate;
private String deleteTemplate;
Expand Down Expand Up @@ -91,14 +94,16 @@ protected void createTable(Session session, TableDefine tableDefine) {
// Specified primary keys
sql.append(" PRIMARY KEY (");
int i = 0;
int size = tableDefine.keys().size();
for (HugeKeys key : tableDefine.keys()) {
sql.append(key);
if (++i != tableDefine.keys().size()) {
sql.append(formatKey(key));
if (++i != size) {
sql.append(", ");
}
}

sql.append(")) ENGINE=InnoDB;");
sql.append("))");
sql.append(this.engine());
sql.append(";");

LOG.debug("Create table: {}", sql);
try {
Expand All @@ -109,9 +114,13 @@ protected void createTable(Session session, TableDefine tableDefine) {
}
}

protected String engine() {
return " ENGINE=InnoDB";
}

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

protected void truncateTable(Session session) {
LOG.debug("Truncate table: {}", this.table());
String sql = String.format("TRUNCATE TABLE %s;", this.table());
String sql = String.format(this.truncateTableTemplate, this.table());
try {
session.execute(sql);
} catch (SQLException e) {
Expand Down Expand Up @@ -202,7 +211,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 : entry.columns().values()) {
for (Object object : this.insertTemplateObjects(entry)) {
insertStmt.setObject(i++, object);
}
} catch (SQLException e) {
Expand Down Expand Up @@ -413,7 +422,7 @@ protected StringBuilder relation2Sql(Condition.Relation relation) {
Object value = relation.serialValue();

// Serialize value (TODO: should move to Serializer)
value = serializeValue(value);
value = this.serializeValue(value);

StringBuilder sql = new StringBuilder(32);
sql.append(key);
Expand Down Expand Up @@ -503,13 +512,19 @@ protected void wrapPage(StringBuilder select, Query query) {
select.append(where.build());
}

select.append(this.orderByKeys());

assert query.limit() != Query.NO_LIMIT;
// Fetch `limit + 1` records for judging whether reached the last page
select.append(" limit ");
select.append(query.limit() + 1);
select.append(";");
}

protected String orderByKeys() {
return Strings.EMPTY;
}

protected void wrapOffset(StringBuilder select, Query query) {
assert query.limit() >= 0;
assert query.offset() >= 0;
Expand All @@ -521,7 +536,7 @@ protected void wrapOffset(StringBuilder select, Query query) {
select.append(";");
}

protected static Object serializeValue(Object value) {
protected Object serializeValue(Object value) {
if (value instanceof Id) {
value = ((Id) value).asObject();
}
Expand All @@ -545,6 +560,14 @@ protected void appendPartition(StringBuilder delete) {
// pass
}

protected List<Object> insertTemplateObjects(MysqlBackendEntry.Row entry) {
List<Object> objects = new ArrayList<>();
for (Object key : entry.columns().keySet()) {
objects.add(entry.columns().get(key));
}
return objects;
}

public static String formatKey(HugeKeys key) {
return key.name();
}
Expand Down
Loading

0 comments on commit b63f052

Please sign in to comment.