Search in sources :

Example 16 with HugeVertex

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

the class EdgeCoreTest method testQueryAdjacentVerticesOfEdgesWithoutVertexAndNoLazyLoad.

@Test
public void testQueryAdjacentVerticesOfEdgesWithoutVertexAndNoLazyLoad() 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();
    Whitebox.setInternalState(params().graphTransaction(), "lazyLoadAdjacentVertex", false);
    try {
        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.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.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);
        // NOTE: if not load, adjacent.label() will return 'book'
        Assert.assertEquals("book", adjacent.label());
        adjacent.forceLoad();
        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());
    } finally {
        Whitebox.setInternalState(params().graphTransaction(), "lazyLoadAdjacentVertex", true);
    }
    Whitebox.setInternalState(params().graphTransaction(), "lazyLoadAdjacentVertex", 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().toList();
        }, e -> {
            Assert.assertContains("Vertex 'java' does not exist", e.getMessage());
        });
        Assert.assertThrows(HugeException.class, () -> {
            graph.traversal().V(james.id()).outE().otherV().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());
        });
    } finally {
        Whitebox.setInternalState(params().graphTransaction(), "lazyLoadAdjacentVertex", true);
        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 17 with HugeVertex

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

the class VertexAPI method update.

@PUT
@Timed(name = "single-update")
@Path("{id}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=vertex_write" })
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String idValue, @QueryParam("action") String action, JsonVertex jsonVertex) {
    LOG.debug("Graph [{}] update vertex: {}", graph, jsonVertex);
    checkUpdatingBody(jsonVertex);
    Id id = checkAndParseVertexId(idValue);
    // Parse action param
    boolean append = checkAndParseAction(action);
    HugeGraph g = graph(manager, graph);
    HugeVertex vertex = (HugeVertex) g.vertex(id);
    VertexLabel vertexLabel = vertex.schemaLabel();
    for (String key : jsonVertex.properties.keySet()) {
        PropertyKey pkey = g.propertyKey(key);
        E.checkArgument(vertexLabel.properties().contains(pkey.id()), "Can't update property for vertex '%s' because " + "there is no property key '%s' in its vertex label", id, key);
    }
    commit(g, () -> updateProperties(vertex, jsonVertex, append));
    return manager.serializer(g).writeVertex(vertex);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Path(jakarta.ws.rs.Path) RolesAllowed(jakarta.annotation.security.RolesAllowed) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) PUT(jakarta.ws.rs.PUT)

Example 18 with HugeVertex

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

the class HugeVariables method createVariableVertex.

private void createVariableVertex(String key, Object value) {
    VertexLabel vl = this.variableVertexLabel();
    GraphTransaction tx = this.params.graphTransaction();
    HugeVertex vertex = HugeVertex.create(tx, null, vl);
    try {
        this.setProperty(vertex, key, value);
    } catch (IllegalArgumentException e) {
        throw Graph.Variables.Exceptions.dataTypeOfVariableValueNotSupported(value, e);
    }
    // PrimaryKey id
    vertex.assignId(null);
    tx.addVertex(vertex);
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 19 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex 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 20 with HugeVertex

use of com.baidu.hugegraph.structure.HugeVertex 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

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