Search in sources :

Example 41 with EdgeLabel

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

the class FusiformSimilarityTraverser method matchMinNeighborCount.

private boolean matchMinNeighborCount(HugeVertex vertex, Directions direction, String label, int minNeighbors, long degree) {
    Iterator<Edge> edges;
    long neighborCount;
    EdgeLabel edgeLabel = null;
    Id labelId = null;
    if (label != null) {
        edgeLabel = this.graph().edgeLabel(label);
        labelId = edgeLabel.id();
    }
    if (edgeLabel != null && edgeLabel.frequency() == Frequency.SINGLE) {
        edges = this.edgesOfVertex(vertex.id(), direction, labelId, minNeighbors);
        neighborCount = IteratorUtils.count(edges);
    } else {
        edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree);
        Set<Id> neighbors = newIdSet();
        while (edges.hasNext()) {
            Id target = ((HugeEdge) edges.next()).id().otherVertexId();
            neighbors.add(target);
            if (neighbors.size() >= minNeighbors) {
                break;
            }
        }
        neighborCount = neighbors.size();
    }
    return neighborCount >= minNeighbors;
}
Also used : EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 42 with EdgeLabel

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

the class HugeEdgeProperty method remove.

@Override
public void remove() {
    assert this.owner instanceof HugeEdge;
    EdgeLabel edgeLabel = ((HugeEdge) this.owner).schemaLabel();
    E.checkArgument(edgeLabel.nullableKeys().contains(this.propertyKey().id()), "Can't remove non-null edge property '%s'", this);
    this.owner.graph().removeEdgeProperty(this);
}
Also used : EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel)

Example 43 with EdgeLabel

use of com.baidu.hugegraph.schema.EdgeLabel 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 44 with EdgeLabel

use of com.baidu.hugegraph.schema.EdgeLabel 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 45 with EdgeLabel

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

the class SchemaCoreTest method assertELEqual.

protected void assertELEqual(String label, Id id) {
    EdgeLabel edgeLabel = graph().edgeLabel(label);
    Assert.assertEquals(id, edgeLabel.id());
}
Also used : EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel)

Aggregations

EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)58 Test (org.junit.Test)22 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)19 Id (com.baidu.hugegraph.backend.id.Id)16 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)12 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)11 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)11 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Edge (org.apache.tinkerpop.gremlin.structure.Edge)10 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)8 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)7 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)6 Timed (com.codahale.metrics.annotation.Timed)5 RolesAllowed (jakarta.annotation.security.RolesAllowed)5 Produces (jakarta.ws.rs.Produces)5 Date (java.util.Date)4 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)3 SchemaTransaction (com.baidu.hugegraph.backend.tx.SchemaTransaction)3 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)3 SchemaStatus (com.baidu.hugegraph.type.define.SchemaStatus)3