Search in sources :

Example 6 with HugeEdge

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

the class FakeObjects method newEdge.

public HugeEdge newEdge(long sourceVertexId, long targetVertexId) {
    PropertyKey name = this.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = this.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = this.newPropertyKey(IdGenerator.of(3), "city");
    PropertyKey date = this.newPropertyKey(IdGenerator.of(4), "date", DataType.DATE);
    PropertyKey weight = this.newPropertyKey(IdGenerator.of(5), "weight", DataType.DOUBLE);
    VertexLabel vl = this.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    EdgeLabel el = this.newEdgeLabel(IdGenerator.of(1), "knows", Frequency.SINGLE, vl.id(), vl.id(), date.id(), weight.id());
    HugeVertex source = new HugeVertex(this.graph(), IdGenerator.of(sourceVertexId), vl);
    source.addProperty(name, "tom");
    source.addProperty(age, 18);
    source.addProperty(city, "Beijing");
    HugeVertex target = new HugeVertex(this.graph(), IdGenerator.of(targetVertexId), vl);
    target.addProperty(name, "cat");
    target.addProperty(age, 20);
    target.addProperty(city, "Shanghai");
    Id id = EdgeId.parse("L123456>1>>L987654");
    HugeEdge edge = new HugeEdge(this.graph(), id, el);
    Whitebox.setInternalState(edge, "sourceVertex", source);
    Whitebox.setInternalState(edge, "targetVertex", target);
    edge.assignId();
    edge.addProperty(date, new Date());
    edge.addProperty(weight, 0.75);
    return edge;
}
Also used : 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)

Example 7 with HugeEdge

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

the class FakeObjects method newEdge.

public HugeEdge newEdge(String sourceVertexId, String targetVertexId) {
    PropertyKey name = this.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = this.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = this.newPropertyKey(IdGenerator.of(3), "city");
    PropertyKey date = this.newPropertyKey(IdGenerator.of(4), "date", DataType.DATE);
    PropertyKey weight = this.newPropertyKey(IdGenerator.of(5), "weight", DataType.DOUBLE);
    VertexLabel vl = this.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    EdgeLabel el = this.newEdgeLabel(IdGenerator.of(1), "knows", Frequency.SINGLE, vl.id(), vl.id(), date.id(), weight.id());
    HugeVertex source = new HugeVertex(this.graph(), IdGenerator.of(sourceVertexId), vl);
    source.addProperty(name, "tom");
    source.addProperty(age, 18);
    source.addProperty(city, "Beijing");
    HugeVertex target = new HugeVertex(this.graph(), IdGenerator.of(targetVertexId), vl);
    target.addProperty(name, "cat");
    target.addProperty(age, 20);
    target.addProperty(city, "Shanghai");
    Id id = EdgeId.parse("L123456>1>>L987654");
    HugeEdge edge = new HugeEdge(this.graph(), id, el);
    Whitebox.setInternalState(edge, "sourceVertex", source);
    Whitebox.setInternalState(edge, "targetVertex", target);
    edge.assignId();
    edge.addProperty(date, new Date());
    edge.addProperty(weight, 0.75);
    return edge;
}
Also used : 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)

Example 8 with HugeEdge

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

the class CachedGraphTransactionTest method testEdgeCacheClearWhenDeleteVertex.

@Test
public void testEdgeCacheClearWhenDeleteVertex() {
    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.removeVertex(v3);
    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.removeVertex(v1);
    cache.commit();
    Assert.assertEquals(0L, Whitebox.invoke(cache, "edgesCache", "size"));
    Assert.assertFalse(cache.queryEdgesByVertex(IdGenerator.of(2)).hasNext());
}
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 9 with HugeEdge

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

the class RamTableTest method testAddAndQuery.

@Test
public void testAddAndQuery() throws Exception {
    HugeGraph graph = this.graph();
    int el1 = (int) graph.edgeLabel("el1").id().asLong();
    int el2 = (int) graph.edgeLabel("el2").id().asLong();
    RamTable table = new RamTable(graph, VERTEX_SIZE, EDGE_SIZE);
    long oldSize = table.edgesSize();
    // insert edges
    for (int i = 0; i < VERTEX_SIZE; i++) {
        table.addEdge(true, i, i, Directions.OUT, el1);
        Assert.assertEquals(oldSize + 2 * i + 1, table.edgesSize());
        table.addEdge(false, i, i + 1, Directions.IN, el2);
        Assert.assertEquals(oldSize + 2 * i + 2, table.edgesSize());
    }
    // query by BOTH
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, 0);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge2 = edges.next();
        Assert.assertEquals(i, edge2.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge2.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge2.direction());
        Assert.assertEquals("el2", edge2.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by OUT
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.OUT, el1);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by IN
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.IN, el2);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge1.direction());
        Assert.assertEquals("el2", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by BOTH & label 1
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, el1);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by BOTH & label 2
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, el2);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge1.direction());
        Assert.assertEquals("el2", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query non-exist vertex
    Iterator<HugeEdge> edges = table.query(VERTEX_SIZE, Directions.BOTH, 0);
    Assert.assertFalse(edges.hasNext());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) Test(org.junit.Test)

Example 10 with HugeEdge

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

the class RamTableTest method testAddAndQueryWithoutAdjEdges.

@Test
public void testAddAndQueryWithoutAdjEdges() throws Exception {
    HugeGraph graph = this.graph();
    int el1 = (int) graph.edgeLabel("el1").id().asLong();
    int el2 = (int) graph.edgeLabel("el2").id().asLong();
    RamTable table = new RamTable(graph, VERTEX_SIZE, EDGE_SIZE);
    long oldSize = table.edgesSize();
    // insert edges
    for (int i = 0; i < VERTEX_SIZE; i++) {
        if (i % 3 != 0) {
            // don't insert edges for 2/3 vertices
            continue;
        }
        table.addEdge(true, i, i, Directions.OUT, el1);
        Assert.assertEquals(oldSize + i + 1, table.edgesSize());
        table.addEdge(false, i, i, Directions.OUT, el2);
        Assert.assertEquals(oldSize + i + 2, table.edgesSize());
        table.addEdge(false, i, i + 1, Directions.IN, el2);
        Assert.assertEquals(oldSize + i + 3, table.edgesSize());
    }
    // query by BOTH
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, 0);
        if (i % 3 != 0) {
            Assert.assertFalse(edges.hasNext());
            continue;
        }
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge2 = edges.next();
        Assert.assertEquals(i, edge2.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge2.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge2.direction());
        Assert.assertEquals("el2", edge2.label());
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge3 = edges.next();
        Assert.assertEquals(i, edge3.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge3.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge3.direction());
        Assert.assertEquals("el2", edge3.label());
        Assert.assertFalse(edges.hasNext());
    }
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) 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