Search in sources :

Example 6 with VertexLabel

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

the class VertexLabelBuilder method checkNullableKeys.

@SuppressWarnings("unchecked")
private void checkNullableKeys(Action action) {
    // Not using switch-case to avoid indent too much
    if (action == Action.ELIMINATE) {
        if (!this.nullableKeys.isEmpty()) {
            throw new NotAllowException("Not support to eliminate nullableKeys " + "for vertex label currently");
        }
        return;
    }
    VertexLabel vertexLabel = this.vertexLabelOrNull(this.name);
    // The originProps is empty when firstly create vertex label
    List<String> originProps = vertexLabel == null ? ImmutableList.of() : this.graph().mapPkId2Name(vertexLabel.properties());
    Set<String> appendProps = this.properties;
    E.checkArgument(CollectionUtil.union(originProps, appendProps).containsAll(this.nullableKeys), "The nullableKeys: %s to be created or appended " + "must belong to the origin/new properties: %s/%s", this.nullableKeys, originProps, appendProps);
    List<String> primaryKeys = vertexLabel == null ? this.primaryKeys : this.graph().mapPkId2Name(vertexLabel.primaryKeys());
    E.checkArgument(!CollectionUtil.hasIntersection(primaryKeys, this.nullableKeys), "The nullableKeys: %s are not allowed to " + "belong to primaryKeys: %s of vertex label '%s'", this.nullableKeys, primaryKeys, this.name);
    if (action == Action.APPEND) {
        Collection<String> newAddedProps = CollectionUtils.subtract(appendProps, originProps);
        E.checkArgument(this.nullableKeys.containsAll(newAddedProps), "The new added properties: %s must be nullable", newAddedProps);
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) NotAllowException(com.baidu.hugegraph.exception.NotAllowException)

Example 7 with VertexLabel

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

the class VertexLabelBuilder method build.

@Override
public VertexLabel build() {
    Id id = this.validOrGenerateId(HugeType.VERTEX_LABEL, this.id, this.name);
    VertexLabel vertexLabel = new VertexLabel(this.graph(), id, this.name);
    vertexLabel.idStrategy(this.idStrategy);
    vertexLabel.enableLabelIndex(this.enableLabelIndex == null || this.enableLabelIndex);
    vertexLabel.ttl(this.ttl);
    if (this.ttlStartTime != null) {
        vertexLabel.ttlStartTime(this.graph().propertyKey(this.ttlStartTime).id());
    }
    // Assign properties
    for (String key : this.properties) {
        PropertyKey propertyKey = this.graph().propertyKey(key);
        vertexLabel.property(propertyKey.id());
    }
    for (String key : this.primaryKeys) {
        PropertyKey propertyKey = this.graph().propertyKey(key);
        vertexLabel.primaryKey(propertyKey.id());
    }
    for (String key : this.nullableKeys) {
        PropertyKey propertyKey = this.graph().propertyKey(key);
        vertexLabel.nullableKey(propertyKey.id());
    }
    vertexLabel.userdata(this.userdata);
    return vertexLabel;
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 8 with VertexLabel

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

the class VertexLabelRemoveJob method removeVertexLabel.

private static void removeVertexLabel(HugeGraphParams graph, Id id) {
    GraphTransaction graphTx = graph.graphTransaction();
    SchemaTransaction schemaTx = graph.schemaTransaction();
    VertexLabel vertexLabel = schemaTx.getVertexLabel(id);
    // If the vertex label does not exist, return directly
    if (vertexLabel == null) {
        return;
    }
    if (vertexLabel.status().deleting()) {
        LOG.info("The vertex label '{}' has been in {} status, " + "please check if it's expected to delete it again", vertexLabel, vertexLabel.status());
    }
    // Check no edge label use the vertex label
    List<EdgeLabel> edgeLabels = schemaTx.getEdgeLabels();
    for (EdgeLabel edgeLabel : edgeLabels) {
        if (edgeLabel.linkWithLabel(id)) {
            throw new HugeException("Not allowed to remove vertex label '%s' " + "because the edge label '%s' still link with it", vertexLabel.name(), edgeLabel.name());
        }
    }
    /*
         * Copy index label ids because removeIndexLabel will mutate
         * vertexLabel.indexLabels()
         */
    Set<Id> indexLabelIds = ImmutableSet.copyOf(vertexLabel.indexLabels());
    LockUtil.Locks locks = new LockUtil.Locks(graph.name());
    try {
        locks.lockWrites(LockUtil.VERTEX_LABEL_DELETE, id);
        schemaTx.updateSchemaStatus(vertexLabel, SchemaStatus.DELETING);
        try {
            for (Id ilId : indexLabelIds) {
                IndexLabelRemoveJob.removeIndexLabel(graph, ilId);
            }
            // TODO: use event to replace direct call
            // Deleting a vertex will automatically deletes the held edge
            graphTx.removeVertices(vertexLabel);
            /*
                 * Should commit changes to backend store before release
                 * delete lock
                 */
            graph.graph().tx().commit();
            // Remove vertex label
            removeSchema(schemaTx, vertexLabel);
        } catch (Throwable e) {
            schemaTx.updateSchemaStatus(vertexLabel, SchemaStatus.UNDELETED);
            throw e;
        }
    } finally {
        locks.unlock();
    }
}
Also used : LockUtil(com.baidu.hugegraph.util.LockUtil) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Id(com.baidu.hugegraph.backend.id.Id) HugeException(com.baidu.hugegraph.HugeException) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

Example 9 with VertexLabel

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

the class VertexCoreTest method testQueryFilterByPropName.

@Test
public void testQueryFilterByPropName() {
    HugeGraph graph = graph();
    Assume.assumeTrue("Not support CONTAINS_KEY query", storeFeatures().supportsQueryWithContainsKey());
    init10Vertices();
    VertexLabel language = graph.vertexLabel("language");
    PropertyKey dynamic = graph.propertyKey("dynamic");
    // Query vertex by condition (does contain the property name?)
    ConditionQuery q = new ConditionQuery(HugeType.VERTEX);
    q.eq(HugeKeys.LABEL, language.id());
    q.key(HugeKeys.PROPERTIES, dynamic.id());
    List<Vertex> vertices = ImmutableList.copyOf(graph.vertices(q));
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "language", "name", "python", "dynamic", true);
    // Query vertex by condition (does contain the property name?)
    q = new ConditionQuery(HugeType.VERTEX);
    q.key(HugeKeys.PROPERTIES, dynamic.id());
    vertices = ImmutableList.copyOf(graph.vertices(q));
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "language", "name", "python", "dynamic", true);
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Test(org.junit.Test)

Example 10 with VertexLabel

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

the class VertexLabelCoreTest method testAddVertexLabelWith2PrimaryKey.

@Test
public void testAddVertexLabelWith2PrimaryKey() {
    super.initPropertyKeys();
    SchemaManager schema = graph().schema();
    VertexLabel person = schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name", "age").create();
    Assert.assertNotNull(person);
    Assert.assertEquals("person", person.name());
    Assert.assertEquals(3, person.properties().size());
    assertContainsPk(person.properties(), "name", "age", "city");
    Assert.assertEquals(2, person.primaryKeys().size());
    assertContainsPk(person.primaryKeys(), "name", "age");
}
Also used : 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