Search in sources :

Example 26 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.

the class GraphTransaction method removeLeftIndexIfNeeded.

private void removeLeftIndexIfNeeded(Map<Id, HugeVertex> vertices) {
    Set<Id> ids = vertices.keySet();
    if (ids.isEmpty()) {
        return;
    }
    IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids);
    Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery);
    try {
        while (results.hasNext()) {
            HugeVertex existedVertex = results.next();
            this.indexTx.updateVertexIndex(existedVertex, true);
        }
    } finally {
        CloseableIterator.closeIterator(results);
    }
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 27 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.

the class GraphTransaction method queryEdgesFromBackend.

protected Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
    assert query.resultType().isEdge();
    QueryResults<BackendEntry> results = this.query(query);
    Iterator<BackendEntry> entries = results.iterator();
    Iterator<HugeEdge> edges = new FlatMapperIterator<>(entries, entry -> {
        // Edges are in a vertex
        HugeVertex vertex = this.parseEntry(entry);
        if (vertex == null) {
            return null;
        }
        if (query.idsSize() == 1) {
            assert vertex.getEdges().size() == 1;
        }
        /*
             * Copy to avoid ConcurrentModificationException when removing edge
             * because HugeEdge.remove() will update edges in owner vertex
             */
        return new ListIterator<>(ImmutableList.copyOf(vertex.getEdges()));
    });
    edges = this.filterExpiredResultFromFromBackend(query, edges);
    if (!this.store().features().supportsQuerySortByInputIds()) {
        // There is no id in BackendEntry, so sort after deserialization
        edges = results.keepInputOrderIfNeeded(edges);
    }
    return edges;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) ListIterator(com.baidu.hugegraph.iterator.ListIterator) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 28 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.

the class GraphIndexTransaction method updateLabelIndex.

@Watched(prefix = "index")
public void updateLabelIndex(HugeElement element, boolean removed) {
    if (element instanceof HugeVertex && ((HugeVertex) element).olap()) {
        return;
    }
    if (!this.needIndexForLabel()) {
        return;
    }
    // Don't update label index if it's not enabled
    SchemaLabel label = element.schemaLabel();
    if (!label.enableLabelIndex()) {
        return;
    }
    // Update label index if backend store not supports label-query
    HugeIndex index = new HugeIndex(this.graph(), IndexLabel.label(element.type()));
    index.fieldValues(element.schemaLabel().id());
    index.elementIds(element.id(), element.expiredTime());
    if (removed) {
        this.doEliminate(this.serializer.writeIndex(index));
    } else {
        this.doAppend(this.serializer.writeIndex(index));
    }
}
Also used : SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 29 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.

the class EdgeCoreTest method testQueryAdjacentVerticesOfEdgesWithoutVertex.

@Test
public void testQueryAdjacentVerticesOfEdgesWithoutVertex() throws InterruptedException, ExecutionException {
    HugeGraph graph = graph();
    Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian");
    Vertex java = new HugeVertex(graph, IdGenerator.of("java"), graph.vertexLabel("book"));
    james.addEdge("authored", java, "score", 3);
    graph.tx().commit();
    List<Edge> edges = graph.traversal().V(james.id()).outE().toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(0, graph.traversal().V(java).toList().size());
    List<Vertex> vertices = graph.traversal().V(james.id()).out().toList();
    Assert.assertEquals(1, vertices.size());
    HugeVertex adjacent = (HugeVertex) vertices.get(0);
    Assert.assertFalse(adjacent.schemaLabel().undefined());
    // force load
    adjacent.forceLoad();
    Assert.assertTrue("label: " + adjacent.schemaLabel(), adjacent.schemaLabel().undefined());
    Assert.assertEquals("~undefined", adjacent.label());
    vertices = graph.traversal().V(james.id()).outE().otherV().toList();
    Assert.assertEquals(1, vertices.size());
    adjacent = (HugeVertex) vertices.get(0);
    Assert.assertTrue(adjacent.isPropLoaded());
    Assert.assertTrue(adjacent.schemaLabel().undefined());
    adjacent.forceLoad();
    Assert.assertTrue(adjacent.schemaLabel().undefined());
    Assert.assertEquals("~undefined", adjacent.label());
    params().graphEventHub().notify(Events.CACHE, "clear", null).get();
    vertices = graph.traversal().V(james.id()).outE().otherV().toList();
    Assert.assertEquals(1, vertices.size());
    adjacent = (HugeVertex) vertices.get(0);
    Assert.assertFalse(adjacent.isPropLoaded());
    Assert.assertFalse(adjacent.schemaLabel().undefined());
    adjacent.forceLoad();
    Assert.assertTrue(adjacent.schemaLabel().undefined());
    Assert.assertEquals("~undefined", adjacent.label());
    vertices = graph.traversal().V(james.id()).outE().has("score", 3).otherV().toList();
    Assert.assertEquals(1, vertices.size());
    adjacent = (HugeVertex) vertices.get(0);
    adjacent.forceLoad();
    // NOTE: if not commit, adjacent.label() will return 'book'
    Assert.assertTrue(adjacent.schemaLabel().undefined());
    Assert.assertEquals("~undefined", adjacent.label());
    Assert.assertFalse(adjacent.properties().hasNext());
    vertices = graph.traversal().V(james.id()).outE().has("score", 3).otherV().toList();
    Assert.assertEquals(1, vertices.size());
    adjacent = (HugeVertex) vertices.get(0);
    Assert.assertTrue(adjacent.schemaLabel().undefined());
    Assert.assertEquals("~undefined", adjacent.label());
    Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", true);
    try {
        Assert.assertThrows(HugeException.class, () -> {
            // read from cache
            graph.traversal().V(james.id()).outE().has("score", 3).otherV().values().toList();
        }, e -> {
            Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
        });
    } finally {
        Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", false);
    }
    Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", true);
    params().graphEventHub().notify(Events.CACHE, "clear", null).get();
    try {
        Assert.assertEquals(0, graph.traversal().V(java).toList().size());
        Assert.assertThrows(HugeException.class, () -> {
            graph.traversal().V(james.id()).out().values().toList();
        }, e -> {
            Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
        });
        Assert.assertThrows(HugeException.class, () -> {
            graph.traversal().V(james.id()).outE().otherV().values().toList();
        }, e -> {
            Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
        });
        Assert.assertThrows(HugeException.class, () -> {
            Vertex v = graph.traversal().V(james.id()).outE().has("score", 3).otherV().next();
            // throw
            v.properties();
        }, e -> {
            Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
        });
        Assert.assertThrows(HugeException.class, () -> {
            Vertex v = graph.traversal().V(james.id()).outE().has("score", 3).otherV().next();
            // throw
            v.values();
        }, e -> {
            Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
        });
        Assert.assertThrows(HugeException.class, () -> {
            Vertex v = graph.traversal().V(james.id()).outE().has("score", 3).otherV().next();
            // throw
            ((HugeVertex) v).forceLoad();
        }, e -> {
            Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
        });
    } finally {
        Whitebox.setInternalState(params().graphTransaction(), "checkAdjacentVertexExist", false);
    }
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 30 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.

the class EdgeCoreTest method testQueryEdgesOfVertexWithoutCommit.

@Test
public void testQueryEdgesOfVertexWithoutCommit() {
    HugeGraph graph = graph();
    init18Edges(false);
    HugeVertex james = (HugeVertex) vertex("author", "id", 1);
    List<Vertex> vertices = ImmutableList.copyOf(james.getVertices(Directions.BOTH));
    Assert.assertEquals(6, vertices.size());
    vertices = ImmutableList.copyOf(james.getVertices(Directions.OUT));
    Assert.assertEquals(4, vertices.size());
    vertices = ImmutableList.copyOf(james.getVertices(Directions.IN));
    Assert.assertEquals(2, vertices.size());
    vertices = ImmutableList.copyOf(james.getVertices(Directions.OUT, "authored"));
    Assert.assertEquals(3, vertices.size());
    vertices = ImmutableList.copyOf(james.getVertices(Directions.OUT, "authored", "created"));
    Assert.assertEquals(4, vertices.size());
    vertices = ImmutableList.copyOf(james.getVertices(Directions.IN, "know"));
    Assert.assertEquals(1, vertices.size());
    // Query BOTH edges of a vertex
    List<Edge> edges = graph.traversal().V(james.id()).bothE().toList();
    Assert.assertEquals(6, edges.size());
    edges = ImmutableList.copyOf(james.edges(Direction.BOTH));
    Assert.assertEquals(6, edges.size());
    // Query OUT edges of a vertex
    edges = graph.traversal().V(james.id()).outE().toList();
    Assert.assertEquals(4, edges.size());
    edges = ImmutableList.copyOf(james.edges(Direction.OUT));
    Assert.assertEquals(4, edges.size());
    // Query IN edges of a vertex
    edges = graph.traversal().V(james.id()).inE().toList();
    Assert.assertEquals(2, edges.size());
    edges = ImmutableList.copyOf(james.edges(Direction.IN));
    Assert.assertEquals(2, edges.size());
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Aggregations

HugeVertex (com.baidu.hugegraph.structure.HugeVertex)58 Id (com.baidu.hugegraph.backend.id.Id)27 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)26 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)18 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)16 Test (org.junit.Test)16 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)11 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)10 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)9 HugeGraph (com.baidu.hugegraph.HugeGraph)8 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)8 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)8 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)7 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)7 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)6 HugeException (com.baidu.hugegraph.HugeException)5 HugeConfig (com.baidu.hugegraph.config.HugeConfig)5 HugeProperty (com.baidu.hugegraph.structure.HugeProperty)4 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3