use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class PostgresqlSessions method createDatabase.
@Override
public void createDatabase() {
// Create database with non-database-session
LOG.debug("Create database: {}", this.database());
String sql = this.buildCreateDatabase(this.database());
try (Connection conn = this.openWithoutDB(0)) {
try {
conn.createStatement().execute(sql);
} catch (PSQLException e) {
// CockroachDB 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());
conn.createStatement().execute(sql);
}
}
} catch (SQLException e) {
if (!e.getMessage().endsWith("already exists")) {
throw new BackendException("Failed to create database '%s'", e, this.database());
}
// Ignore exception if database already exists
}
}
use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class RocksDBIteratorPool method allocIterator.
private RocksIterator allocIterator() {
/*
* NOTE: Seems there is a bug if share RocksIterator between threads
* RocksIterator iter = this.pool.poll();
*/
RocksIterator iter = this.pool.poll();
if (iter != null) {
if (this.refreshIterator(iter)) {
// Must refresh when an iterator is reused
return iter;
} else {
// Close it if can't fresh, and create a new one later
this.closeIterator(iter);
}
}
/*
* Create a new iterator if:
* - the pool is empty,
* - or the iterator obtained from the pool is closed,
* - or the iterator can't refresh.
*/
iter = this.createIterator();
try {
iter.status();
return iter;
} catch (RocksDBException e) {
this.closeIterator(iter);
throw new BackendException(e);
}
}
use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class EdgeCoreTest method testAddEdgePropertyWithSpecialValueForSecondaryIndex.
@Test
public void testAddEdgePropertyWithSpecialValueForSecondaryIndex() {
HugeGraph graph = graph();
initStrikeIndex();
Vertex louise = graph.addVertex(T.label, "person", "name", "Louise", "city", "Beijing", "age", 21);
Vertex sean = graph.addVertex(T.label, "person", "name", "Sean", "city", "Beijing", "age", 23);
graph.tx().commit();
long current = System.currentTimeMillis();
louise.addEdge("strike", sean, "id", 1, "timestamp", current, "place", "park", "tool", "b\u0001", "reason", "jeer", "arrested", false);
louise.addEdge("strike", sean, "id", 2, "timestamp", current, "place", "park", "tool", "c\u0002", "reason", "jeer", "arrested", false);
louise.addEdge("strike", sean, "id", 3, "timestamp", current, "place", "park", "tool", "d\u0003", "reason", "jeer", "arrested", false);
graph.tx().commit();
List<Edge> edges;
edges = graph.traversal().E().has("tool", "b\u0001").toList();
Assert.assertEquals(1, edges.size());
Assert.assertEquals(1, edges.get(0).value("id"));
edges = graph.traversal().E().has("tool", "c\u0002").toList();
Assert.assertEquals(1, edges.size());
Assert.assertEquals(2, edges.get(0).value("id"));
edges = graph.traversal().E().has("tool", "d\u0003").toList();
Assert.assertEquals(1, edges.size());
Assert.assertEquals(3, edges.get(0).value("id"));
String backend = graph.backend();
Set<String> nonZeroBackends = ImmutableSet.of("postgresql", "rocksdb", "hbase");
if (nonZeroBackends.contains(backend)) {
Assert.assertThrows(Exception.class, () -> {
louise.addEdge("strike", sean, "id", 4, "timestamp", current, "place", "park", "tool", "a\u0000", "reason", "jeer", "arrested", false);
graph.tx().commit();
}, e -> {
if (e instanceof BackendException) {
Assert.assertContains("0x00", e.getCause().getMessage());
} else {
Assert.assertContains("0x00", e.getMessage());
}
});
} else {
louise.addEdge("strike", sean, "id", 0, "timestamp", current, "place", "park", "tool", "a\u0000", "reason", "jeer", "arrested", false);
graph.tx().commit();
edges = graph.traversal().E().has("tool", "a\u0000").toList();
Assert.assertEquals(1, edges.size());
Assert.assertEquals(0, edges.get(0).value("id"));
}
}
use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.
the class VertexCoreTest method testAddVertexPropertyWithSpecialValueForSecondaryIndex.
@Test
public void testAddVertexPropertyWithSpecialValueForSecondaryIndex() {
HugeGraph graph = graph();
initPersonIndex(true);
graph.addVertex(T.label, "person", "name", "1", "city", "b\u0001", "age", 1);
graph.addVertex(T.label, "person", "name", "2", "city", "c\u0002", "age", 2);
graph.addVertex(T.label, "person", "name", "3", "city", "d\u0003", "age", 3);
this.mayCommitTx();
List<Vertex> vertices;
vertices = graph.traversal().V().has("city", "b\u0001").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals("1", vertices.get(0).value("name"));
vertices = graph.traversal().V().has("city", "c\u0002").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals("2", vertices.get(0).value("name"));
vertices = graph.traversal().V().has("city", "d\u0003").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals("3", vertices.get(0).value("name"));
String backend = graph.backend();
Set<String> nonZeroBackends = ImmutableSet.of("postgresql", "rocksdb", "hbase");
if (nonZeroBackends.contains(backend)) {
Assert.assertThrows(Exception.class, () -> {
graph.addVertex(T.label, "person", "name", "0", "city", "a\u0000", "age", 0);
this.commitTx();
}, e -> {
if (e instanceof BackendException) {
Assert.assertContains("0x00", e.getCause().getMessage());
} else {
Assert.assertContains("0x00", e.getMessage());
}
});
} else {
graph.addVertex(T.label, "person", "name", "0", "city", "a\u0000", "age", 0);
this.commitTx();
vertices = graph.traversal().V().has("city", "a\u0000").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals("0", vertices.get(0).value("name"));
}
}
Aggregations