Search in sources :

Example 16 with HugeEdge

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

the class KneighborTraverser method customizedKneighbor.

public KneighborRecords customizedKneighbor(Id source, EdgeStep step, int maxDepth, long limit) {
    E.checkNotNull(source, "source vertex id");
    this.checkVertexExist(source, "source vertex");
    checkPositive(maxDepth, "k-neighbor max_depth");
    checkLimit(limit);
    boolean concurrent = maxDepth >= this.concurrentDepth();
    KneighborRecords records = new KneighborRecords(concurrent, source, true);
    Consumer<Id> consumer = v -> {
        if (this.reachLimit(limit, records.size())) {
            return;
        }
        Iterator<Edge> edges = edgesOfVertex(v, step);
        while (!this.reachLimit(limit, records.size()) && edges.hasNext()) {
            Id target = ((HugeEdge) edges.next()).id().otherVertexId();
            records.addPath(v, target);
        }
    };
    while (maxDepth-- > 0) {
        records.startOneLayer(true);
        traverseIds(records.keys(), consumer, concurrent);
        records.finishOneLayer();
    }
    return records;
}
Also used : Consumer(java.util.function.Consumer) Iterator(java.util.Iterator) KneighborRecords(com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords) Directions(com.baidu.hugegraph.type.define.Directions) HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) Set(java.util.Set) E(com.baidu.hugegraph.util.E) Edge(org.apache.tinkerpop.gremlin.structure.Edge) KneighborRecords(com.baidu.hugegraph.traversal.algorithm.records.KneighborRecords) Iterator(java.util.Iterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id)

Example 17 with HugeEdge

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

the class RamTableTest method testReloadAndQuery.

@Test
public void testReloadAndQuery() 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);
        v1.addEdge("el1", v2);
    }
    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);
        v1.addEdge("el2", v2);
    }
    graph.tx().commit();
    // reload ramtable
    Whitebox.invoke(graph.getClass(), "reloadRamtable", graph);
    // query edges
    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.assertFalse(edges.hasNext());
    }
    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.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 18 with HugeEdge

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

the class BinaryScatterSerializerTest method testVertex.

@Test
public void testVertex() {
    HugeConfig config = FakeObjects.newConfig();
    BinaryScatterSerializer ser = new BinaryScatterSerializer(config);
    HugeEdge edge = new FakeObjects().newEdge(123, 456);
    BackendEntry entry1 = ser.writeVertex(edge.sourceVertex());
    HugeVertex vertex1 = ser.readVertex(edge.graph(), entry1);
    Assert.assertEquals(edge.sourceVertex(), vertex1);
    assertCollectionEquals(edge.sourceVertex().getProperties(), vertex1.getProperties());
    BackendEntry entry2 = ser.writeVertex(edge.targetVertex());
    HugeVertex vertex2 = ser.readVertex(edge.graph(), entry2);
    Assert.assertEquals(edge.targetVertex(), vertex2);
    assertCollectionEquals(edge.targetVertex().getProperties(), vertex2.getProperties());
    Whitebox.setInternalState(vertex2, "removed", true);
    Assert.assertTrue(vertex2.removed());
    BackendEntry entry3 = ser.writeVertex(vertex2);
    Assert.assertEquals(0, entry3.columnsSize());
    Assert.assertNull(ser.readVertex(edge.graph(), null));
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) BinaryBackendEntry(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry) BinaryScatterSerializer(com.baidu.hugegraph.backend.serializer.BinaryScatterSerializer) FakeObjects(com.baidu.hugegraph.unit.FakeObjects) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeConfig(com.baidu.hugegraph.config.HugeConfig) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 19 with HugeEdge

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

the class RolePermissionTest method testHugeResourceFilterVertexOrEdge.

@Test
public void testHugeResourceFilterVertexOrEdge() {
    HugeResource all = HugeResource.ALL;
    // vertex & edge
    FakeObjects fo = new FakeObjects();
    HugeEdge edge = fo.newEdge(1, 2);
    ResourceObject<?> r1 = ResourceObject.of("g1", edge.sourceVertex());
    ResourceObject<?> r2 = ResourceObject.of("g1", edge.targetVertex());
    ResourceObject<?> r3 = ResourceObject.of("g1", edge);
    Assert.assertTrue(all.filter(r1));
    Assert.assertTrue(all.filter(r2));
    Assert.assertTrue(all.filter(r3));
    HugeResource vr = new HugeResource(ResourceType.VERTEX, HugeResource.ANY, null);
    Assert.assertTrue(vr.filter(r1));
    Assert.assertTrue(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    vr = new HugeResource(ResourceType.VERTEX, "person", null);
    Assert.assertTrue(vr.filter(r1));
    Assert.assertTrue(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    vr = new HugeResource(ResourceType.VERTEX, "person", ImmutableMap.of("city", "Beijing"));
    Assert.assertTrue(vr.filter(r1));
    Assert.assertFalse(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    vr = new HugeResource(ResourceType.VERTEX, "person", ImmutableMap.of("city", "Shanghai"));
    Assert.assertFalse(vr.filter(r1));
    Assert.assertTrue(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    vr = new HugeResource(ResourceType.VERTEX, "person", ImmutableMap.of("city", "P.within(\"Beijing\", \"Shanghai\")"));
    Assert.assertTrue(vr.filter(r1));
    Assert.assertTrue(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    vr = new HugeResource(ResourceType.VERTEX, "person", ImmutableMap.of("age", "P.gt(18)"));
    Assert.assertFalse(vr.filter(r1));
    Assert.assertTrue(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    vr = new HugeResource(ResourceType.VERTEX, "person", ImmutableMap.of("age", "P.between(20, 21)"));
    Assert.assertFalse(vr.filter(r1));
    Assert.assertTrue(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    vr = new HugeResource(ResourceType.VERTEX, "person", ImmutableMap.of("age", "P.between(18, 21)"));
    Assert.assertTrue(vr.filter(r1));
    Assert.assertTrue(vr.filter(r2));
    Assert.assertFalse(vr.filter(r3));
    HugeResource er = new HugeResource(ResourceType.EDGE, "knows", null);
    Assert.assertFalse(er.filter(r1));
    Assert.assertFalse(er.filter(r2));
    Assert.assertTrue(er.filter(r3));
    er = new HugeResource(ResourceType.EDGE, "knows", ImmutableMap.of("weight", "P.gt(0.7)"));
    Assert.assertFalse(er.filter(r1));
    Assert.assertFalse(er.filter(r2));
    Assert.assertTrue(er.filter(r3));
    er = new HugeResource(ResourceType.EDGE, "knows", ImmutableMap.of("weight", "P.gt(0.8)"));
    Assert.assertFalse(er.filter(r1));
    Assert.assertFalse(er.filter(r2));
    Assert.assertFalse(er.filter(r3));
    er = new HugeResource(ResourceType.EDGE, "knows", ImmutableMap.of("weight", "P.lt(0.8)"));
    Assert.assertFalse(er.filter(r1));
    Assert.assertFalse(er.filter(r2));
    Assert.assertTrue(er.filter(r3));
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) HugeResource(com.baidu.hugegraph.auth.HugeResource) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Test(org.junit.Test)

Example 20 with HugeEdge

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

the class BinarySerializerTest method testVertexForPartition.

@Test
public void testVertexForPartition() {
    BinarySerializer ser = new BinarySerializer(true, true, true);
    HugeEdge edge = new FakeObjects().newEdge("123", "456");
    BackendEntry entry1 = ser.writeVertex(edge.sourceVertex());
    HugeVertex vertex1 = ser.readVertex(edge.graph(), entry1);
    Assert.assertEquals(edge.sourceVertex(), vertex1);
    assertCollectionEquals(edge.sourceVertex().getProperties(), vertex1.getProperties());
    BackendEntry entry2 = ser.writeVertex(edge.targetVertex());
    HugeVertex vertex2 = ser.readVertex(edge.graph(), entry2);
    Assert.assertEquals(edge.targetVertex(), vertex2);
    assertCollectionEquals(edge.targetVertex().getProperties(), vertex2.getProperties());
    Whitebox.setInternalState(vertex2, "removed", true);
    Assert.assertTrue(vertex2.removed());
    BackendEntry entry3 = ser.writeVertex(vertex2);
    Assert.assertEquals(0, entry3.columnsSize());
    Assert.assertNull(ser.readVertex(edge.graph(), null));
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) FakeObjects(com.baidu.hugegraph.unit.FakeObjects) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) BinarySerializer(com.baidu.hugegraph.backend.serializer.BinarySerializer) 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