Search in sources :

Example 66 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class IndexLabelRebuildJob method rebuildIndex.

private void rebuildIndex(SchemaLabel label, Collection<Id> indexLabelIds) {
    SchemaTransaction schemaTx = this.params().schemaTransaction();
    GraphTransaction graphTx = this.params().graphTransaction();
    Consumer<?> indexUpdater = (elem) -> {
        for (Id id : indexLabelIds) {
            graphTx.updateIndex(id, (HugeElement) elem, false);
        }
    };
    LockUtil.Locks locks = new LockUtil.Locks(schemaTx.graphName());
    try {
        locks.lockWrites(LockUtil.INDEX_LABEL_REBUILD, indexLabelIds);
        Set<IndexLabel> ils = indexLabelIds.stream().map(this.graph()::indexLabel).collect(Collectors.toSet());
        for (IndexLabel il : ils) {
            if (il.status() == SchemaStatus.CREATING) {
                continue;
            }
            schemaTx.updateSchemaStatus(il, SchemaStatus.REBUILDING);
        }
        this.removeIndex(indexLabelIds);
        /*
             * Note: Here must commit index transaction firstly.
             * Because remove index convert to (id like <?>:personByCity):
             * `delete from index table where label = ?`,
             * But append index will convert to (id like Beijing:personByCity):
             * `update index element_ids += xxx where field_value = ?
             * and index_label_name = ?`,
             * They have different id lead to it can't compare and optimize
             */
        graphTx.commit();
        try {
            if (label.type() == HugeType.VERTEX_LABEL) {
                @SuppressWarnings("unchecked") Consumer<Vertex> consumer = (Consumer<Vertex>) indexUpdater;
                graphTx.traverseVerticesByLabel((VertexLabel) label, consumer, false);
            } else {
                assert label.type() == HugeType.EDGE_LABEL;
                @SuppressWarnings("unchecked") Consumer<Edge> consumer = (Consumer<Edge>) indexUpdater;
                graphTx.traverseEdgesByLabel((EdgeLabel) label, consumer, false);
            }
            graphTx.commit();
        } catch (Throwable e) {
            for (IndexLabel il : ils) {
                schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
            }
            throw e;
        }
        for (IndexLabel il : ils) {
            schemaTx.updateSchemaStatus(il, SchemaStatus.CREATED);
        }
    } finally {
        locks.unlock();
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) SchemaElement(com.baidu.hugegraph.schema.SchemaElement) ImmutableSet(com.google.common.collect.ImmutableSet) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) Set(java.util.Set) HugeElement(com.baidu.hugegraph.structure.HugeElement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus) LockUtil(com.baidu.hugegraph.util.LockUtil) Id(com.baidu.hugegraph.backend.id.Id) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeType(com.baidu.hugegraph.type.HugeType) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LockUtil(com.baidu.hugegraph.util.LockUtil) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeElement(com.baidu.hugegraph.structure.HugeElement) Consumer(java.util.function.Consumer) Id(com.baidu.hugegraph.backend.id.Id) Edge(org.apache.tinkerpop.gremlin.structure.Edge) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

Example 67 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class HugeVariables method createVariableQuery.

private ConditionQuery createVariableQuery(String name) {
    ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
    VertexLabel vl = this.variableVertexLabel();
    query.eq(HugeKeys.LABEL, vl.id());
    if (name != null) {
        PropertyKey pkey = this.params.graph().propertyKey(Hidden.hide(VARIABLE_KEY));
        query.query(Condition.eq(pkey.id(), name));
    }
    query.showHidden(true);
    return query;
}
Also used : ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 68 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class Example1 method testRemove.

public static void testRemove(final HugeGraph graph) {
    // remove vertex (and its edges)
    List<Vertex> vertices = graph.traversal().V().hasLabel("person").has("age", 19).toList();
    assert vertices.size() == 1;
    Vertex james = vertices.get(0);
    Vertex book6 = graph.addVertex(T.label, "book", "name", "java-6");
    james.addEdge("look", book6, "timestamp", "2017-5-2 12:00:08.0");
    james.addEdge("look", book6, "timestamp", "2017-5-3 12:00:08.0");
    graph.tx().commit();
    assert graph.traversal().V(book6.id()).bothE().hasNext();
    LOG.info(">>>> removing vertex: {}", james);
    james.remove();
    graph.tx().commit();
    assert !graph.traversal().V(james.id()).hasNext();
    assert !graph.traversal().V(book6.id()).bothE().hasNext();
    // remove edge
    VertexLabel author = graph.schema().getVertexLabel("author");
    String authorId = String.format("%s:%s", author.id().asString(), "11");
    EdgeLabel authored = graph.edgeLabel("authored");
    VertexLabel book = graph.schema().getVertexLabel("book");
    String book2Id = String.format("%s:%s", book.id().asString(), "java-2");
    String edgeId = String.format("S%s>%s>%s>S%s", authorId, authored.id(), "", book2Id);
    List<Edge> edges = graph.traversal().E(edgeId).toList();
    assert edges.size() == 1;
    Edge edge = edges.get(0);
    LOG.info(">>>> removing edge: {}", edge);
    edge.remove();
    graph.tx().commit();
    assert !graph.traversal().E(edgeId).hasNext();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 69 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class IndexLabelCoreTest method testRemoveIndexLabelOfVertex.

@Test
public void testRemoveIndexLabelOfVertex() {
    Assume.assumeTrue("Not support range condition query", storeFeatures().supportsQueryWithRangeCondition());
    super.initPropertyKeys();
    SchemaManager schema = graph().schema();
    schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").create();
    schema.indexLabel("personByCity").onV("person").secondary().by("city").create();
    schema.indexLabel("personByAge").onV("person").range().by("age").create();
    VertexLabel person = schema.getVertexLabel("person");
    Assert.assertEquals(2, person.indexLabels().size());
    assertContainsIl(person.indexLabels(), "personByCity", "personByAge");
    graph().addVertex(T.label, "person", "name", "Baby", "city", "Hongkong", "age", 3);
    graph().tx().commit();
    Vertex vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
    Assert.assertNotNull(vertex);
    vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
    Assert.assertNotNull(vertex);
    schema.indexLabel("personByCity").remove();
    Assert.assertThrows(NotFoundException.class, () -> {
        schema.getIndexLabel("personByCity");
    });
    person = schema.getVertexLabel("person");
    Assert.assertEquals(1, person.indexLabels().size());
    assertNotContainsIl(person.indexLabels(), "personByCity");
    assertContainsIl(person.indexLabels(), "personByAge");
    Assert.assertThrows(NoIndexException.class, () -> {
        graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
    });
    vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
    Assert.assertNotNull(vertex);
    schema.indexLabel("personByAge").remove();
    Assert.assertThrows(NotFoundException.class, () -> {
        schema.getIndexLabel("personByAge");
    });
    person = schema.getVertexLabel("person");
    Assert.assertEquals(0, person.indexLabels().size());
    Assert.assertThrows(NoIndexException.class, () -> {
        graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
    });
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Example 70 with VertexLabel

use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.

the class IndexLabelCoreTest method testRebuildIndexLabelOfVertexLabel.

@Test
public void testRebuildIndexLabelOfVertexLabel() {
    Assume.assumeTrue("Not support range condition query", storeFeatures().supportsQueryWithRangeCondition());
    super.initPropertyKeys();
    SchemaManager schema = graph().schema();
    schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").create();
    schema.indexLabel("personByCity").onV("person").secondary().by("city").create();
    schema.indexLabel("personByAge").onV("person").range().by("age").create();
    VertexLabel person = schema.getVertexLabel("person");
    Assert.assertEquals(2, person.indexLabels().size());
    assertContainsIl(person.indexLabels(), "personByCity", "personByAge");
    graph().addVertex(T.label, "person", "name", "Baby", "city", "Hongkong", "age", 3);
    graph().tx().commit();
    Vertex vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
    Assert.assertNotNull(vertex);
    vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
    Assert.assertNotNull(vertex);
    schema.vertexLabel("person").rebuildIndex();
    vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
    Assert.assertNotNull(vertex);
    vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
    Assert.assertNotNull(vertex);
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Aggregations

VertexLabel (com.baidu.hugegraph.schema.VertexLabel)86 Test (org.junit.Test)35 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)27 Id (com.baidu.hugegraph.backend.id.Id)23 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)20 HugeGraph (com.baidu.hugegraph.HugeGraph)19 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)19 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)12 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)12 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)11 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)7 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)6 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)5 Timed (com.codahale.metrics.annotation.Timed)5 RolesAllowed (jakarta.annotation.security.RolesAllowed)5 Produces (jakarta.ws.rs.Produces)5 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)4 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)4 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4 Date (java.util.Date)4