Search in sources :

Example 46 with HugeEdge

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

the class RamTableTest method testReloadAndQueryWithBigVertex.

@Test
public void testReloadAndQueryWithBigVertex() throws Exception {
    HugeGraph graph = this.graph();
    // only enable this test when ram > 20G
    boolean enableBigRamTest = false;
    long big1 = 2400000000L;
    long big2 = 4200000000L;
    if (!enableBigRamTest) {
        big1 = 100L;
        big2 = 1000L;
    }
    // insert vertices and edges
    for (int i = 0; i < 100; i++) {
        Vertex v1 = graph.addVertex(T.label, "vl1", T.id, i + big1);
        Vertex v2 = graph.addVertex(T.label, "vl1", T.id, i + big1 + 100);
        v1.addEdge("el1", v2);
    }
    graph.tx().commit();
    for (int i = 0; i < 100; i++) {
        Vertex v1 = graph.addVertex(T.label, "vl2", T.id, i + big2);
        Vertex v2 = graph.addVertex(T.label, "vl2", T.id, i + big2);
        v1.addEdge("el2", v2);
    }
    graph.tx().commit();
    // reload ramtable
    Whitebox.invoke(graph.getClass(), "reloadRamtable", graph);
    // query edges
    for (int i = 0; i < 100; i++) {
        long source = i + big1;
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(source), Directions.OUT, null);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge = (HugeEdge) edges.next();
        Assert.assertEquals(source, edge.id().ownerVertexId().asLong());
        Assert.assertEquals(i + big1 + 100, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el1", edge.label());
        Assert.assertFalse(edges.hasNext());
    }
    for (int i = 0; i < 100; i++) {
        long source = i + big2;
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(source), Directions.OUT, null);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge = (HugeEdge) edges.next();
        Assert.assertEquals(source, edge.id().ownerVertexId().asLong());
        Assert.assertEquals(i + big2, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el2", edge.label());
        Assert.assertFalse(edges.hasNext());
    }
}
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) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 47 with HugeEdge

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

the class RamTableTest method testReloadAndQueryWithMultiEdges.

@Test
public void testReloadAndQueryWithMultiEdges() throws Exception {
    HugeGraph graph = this.graph();
    // insert vertices and edges
    for (int i = 0; i < 100; i++) {
        Vertex v1 = graph.addVertex(T.label, "vl1", T.id, i);
        Vertex v2 = graph.addVertex(T.label, "vl1", T.id, i + 100);
        Vertex v3 = graph.addVertex(T.label, "vl1", T.id, i + 200);
        v1.addEdge("el1", v2);
        v1.addEdge("el1", v3);
        v3.addEdge("el1", v1);
    }
    graph.tx().commit();
    for (int i = 1000; i < 1100; i++) {
        Vertex v1 = graph.addVertex(T.label, "vl2", T.id, i);
        Vertex v2 = graph.addVertex(T.label, "vl2", T.id, i + 100);
        Vertex v3 = graph.addVertex(T.label, "vl2", T.id, i + 200);
        v1.addEdge("el2", v2);
        v1.addEdge("el2", v3);
        v2.addEdge("el2", v3);
    }
    graph.tx().commit();
    // reload ramtable
    Whitebox.invoke(graph.getClass(), "reloadRamtable", graph);
    // query edges by OUT
    for (int i = 0; i < 100; i++) {
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(i), Directions.OUT, null);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 100, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el1", edge.label());
        Assert.assertTrue(edges.hasNext());
        edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 200, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el1", edge.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query edges by BOTH
    for (int i = 0; i < 100; i++) {
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(i), Directions.BOTH, null);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 100, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el1", edge.label());
        Assert.assertTrue(edges.hasNext());
        edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 200, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el1", edge.label());
        Assert.assertTrue(edges.hasNext());
        edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 200, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge.direction());
        Assert.assertEquals("el1", edge.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query edges by IN
    for (int i = 0; i < 100; i++) {
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(i), Directions.IN, null);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 200, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge.direction());
        Assert.assertEquals("el1", edge.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query edges by OUT
    for (int i = 1000; i < 1100; i++) {
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(i), Directions.OUT, null);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 100, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el2", edge.label());
        Assert.assertTrue(edges.hasNext());
        edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 200, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el2", edge.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query edges by BOTH
    for (int i = 1000; i < 1100; i++) {
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(i), Directions.BOTH, null);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 100, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el2", edge.label());
        Assert.assertTrue(edges.hasNext());
        edge = (HugeEdge) edges.next();
        Assert.assertEquals(i + 200, edge.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge.direction());
        Assert.assertEquals("el2", edge.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query edges by IN
    for (int i = 1000; i < 1100; i++) {
        Iterator<Edge> edges = this.edgesOfVertex(IdGenerator.of(i), Directions.IN, null);
        Assert.assertFalse(edges.hasNext());
    }
}
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) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 48 with HugeEdge

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

the class RamTableTest method testAddInvalidVertexOrEdge.

@Test
public void testAddInvalidVertexOrEdge() {
    HugeGraph graph = this.graph();
    VertexLabel vl3 = graph.vertexLabel("vl3");
    EdgeLabel el3 = graph.edgeLabel("el3");
    VertexLabel vl2 = graph.vertexLabel("vl2");
    EdgeLabel el2 = graph.edgeLabel("el2");
    RamTable table = new RamTable(graph, VERTEX_SIZE, EDGE_SIZE);
    HugeVertex ownerVertex = new HugeVertex(graph, IdGenerator.of(1), vl3);
    HugeEdge edge1 = HugeEdge.constructEdge(ownerVertex, true, el3, "marko", IdGenerator.of(2));
    Assert.assertThrows(HugeException.class, () -> {
        table.addEdge(true, edge1);
    }, e -> {
        Assert.assertContains("Only edge label without sortkey is " + "supported by ramtable, but got 'el3(id=3)'", e.getMessage());
    });
    HugeVertex v1 = new HugeVertex(graph, IdGenerator.of("s1"), vl2);
    HugeEdge edge2 = HugeEdge.constructEdge(v1, true, el2, "marko", IdGenerator.of("s2"));
    Assert.assertThrows(HugeException.class, () -> {
        table.addEdge(true, edge2);
    }, e -> {
        Assert.assertContains("Only number id is supported by ramtable, " + "but got string id 's1'", e.getMessage());
    });
    HugeVertex v2 = new HugeVertex(graph, IdGenerator.of(2), vl2);
    HugeEdge edge3 = HugeEdge.constructEdge(v2, true, el2, "marko", IdGenerator.of("s2"));
    Assert.assertThrows(HugeException.class, () -> {
        table.addEdge(true, edge3);
    }, e -> {
        Assert.assertContains("Only number id is supported by ramtable, " + "but got string id 's2'", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Test(org.junit.Test)

Example 49 with HugeEdge

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

the class CachedGraphTransactionTest method testEdgeCacheClearWhenUpdateVertex.

@Test
public void testEdgeCacheClearWhenUpdateVertex() {
    CachedGraphTransaction cache = this.cache();
    HugeVertex v1 = this.newVertex(IdGenerator.of(1));
    HugeVertex v2 = this.newVertex(IdGenerator.of(2));
    HugeVertex v3 = this.newVertex(IdGenerator.of(3));
    cache.addVertex(v1);
    cache.addVertex(v2);
    cache.commit();
    HugeEdge edge = this.newEdge(v1, v2);
    cache.addEdge(edge);
    cache.commit();
    Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext());
    Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext());
    Assert.assertEquals(2L, Whitebox.invoke(cache, "edgesCache", "size"));
    cache.addVertexProperty(new HugeVertexProperty<>(v3, cache.graph().schema().getPropertyKey("name"), "test-name"));
    cache.commit();
    Assert.assertEquals(0L, Whitebox.invoke(cache, "edgesCache", "size"));
    Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(1)).hasNext());
    Assert.assertTrue(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext());
    Assert.assertEquals(2L, Whitebox.invoke(cache, "edgesCache", "size"));
    cache.addVertexProperty(new HugeVertexProperty<>(v1, cache.graph().schema().getPropertyKey("name"), "test-name"));
    cache.commit();
    Assert.assertEquals(0L, Whitebox.invoke(cache, "edgesCache", "size"));
    String name = cache.queryEdgesByVertex(IdGenerator.of(1)).next().outVertex().value("name");
    Assert.assertEquals("test-name", name);
}
Also used : CachedGraphTransaction(com.baidu.hugegraph.backend.cache.CachedGraphTransaction) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 50 with HugeEdge

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

the class JsonUtilTest method testSerializeEdge.

@Test
public void testSerializeEdge() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
    PropertyKey date = fakeObject.newPropertyKey(IdGenerator.of(4), "date", DataType.DATE);
    PropertyKey weight = fakeObject.newPropertyKey(IdGenerator.of(5), "weight", DataType.DOUBLE);
    VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    EdgeLabel el = fakeObject.newEdgeLabel(IdGenerator.of(1), "knows", Frequency.SINGLE, vl.id(), vl.id(), date.id(), weight.id());
    HugeVertex source = new HugeVertex(fakeObject.graph(), IdGenerator.of(123456), vl);
    HugeVertex target = new HugeVertex(fakeObject.graph(), IdGenerator.of(987654), vl);
    Id id = EdgeId.parse("L123456>1>>L987654");
    HugeEdge edge = new HugeEdge(fakeObject.graph(), id, el);
    Whitebox.setInternalState(edge, "sourceVertex", source);
    Whitebox.setInternalState(edge, "targetVertex", target);
    Date dateValue = Utils.date("2019-03-12");
    MutableIntObjectMap<HugeProperty<?>> properties = CollectionFactory.newIntObjectMap(date.id(), new HugeEdgeProperty<>(edge, date, dateValue), weight.id(), new HugeEdgeProperty<>(edge, weight, 0.8));
    Whitebox.setInternalState(edge, "properties", properties);
    String json = JsonUtil.toJson(edge);
    Assert.assertEquals("{\"id\":\"L123456>1>>L987654\"," + "\"label\":\"knows\",\"type\":\"edge\"," + "\"outV\":123456,\"outVLabel\":\"person\"," + "\"inV\":987654,\"inVLabel\":\"person\"," + "\"properties\":{\"date\":" + "\"2019-03-12 00:00:00.000\"," + "\"weight\":0.8}}", json);
}
Also used : HugeProperty(com.baidu.hugegraph.structure.HugeProperty) FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Date(java.util.Date) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

HugeEdge (com.baidu.hugegraph.structure.HugeEdge)54 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)29 Id (com.baidu.hugegraph.backend.id.Id)26 Edge (org.apache.tinkerpop.gremlin.structure.Edge)22 Test (org.junit.Test)20 HugeGraph (com.baidu.hugegraph.HugeGraph)17 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)12 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)11 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)9 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)9 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)8 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)8 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)6 EdgeStep (com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep)6 Iterator (java.util.Iterator)6 List (java.util.List)6 Set (java.util.Set)6 HugeException (com.baidu.hugegraph.HugeException)5 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)5 HugeConfig (com.baidu.hugegraph.config.HugeConfig)5