Search in sources :

Example 76 with BackendException

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
    }
}
Also used : PSQLException(org.postgresql.util.PSQLException) SQLException(java.sql.SQLException) PSQLException(org.postgresql.util.PSQLException) Connection(java.sql.Connection) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 77 with BackendException

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);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) RocksIterator(org.rocksdb.RocksIterator) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 78 with BackendException

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"));
    }
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) BackendException(com.baidu.hugegraph.backend.BackendException) Test(org.junit.Test)

Example 79 with BackendException

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"));
    }
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) BackendException(com.baidu.hugegraph.backend.BackendException) Test(org.junit.Test)

Aggregations

BackendException (com.baidu.hugegraph.backend.BackendException)79 SQLException (java.sql.SQLException)15 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)6 Status (com.alipay.sofa.jraft.Status)5 PeerId (com.alipay.sofa.jraft.entity.PeerId)5 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)5 BytesBuffer (com.baidu.hugegraph.backend.serializer.BytesBuffer)4 Path (java.nio.file.Path)4 Random (java.util.Random)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 RocksDBException (org.rocksdb.RocksDBException)4 HugeGraph (com.baidu.hugegraph.HugeGraph)3 Id (com.baidu.hugegraph.backend.id.Id)3 Query (com.baidu.hugegraph.backend.query.Query)3 HugeConfig (com.baidu.hugegraph.config.HugeConfig)3 Map (java.util.Map)3 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)2 Condition (com.baidu.hugegraph.backend.query.Condition)2 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)2