Search in sources :

Example 21 with HugeEdge

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

the class GraphTransaction method joinTxEdges.

private Iterator<?> joinTxEdges(Query query, Iterator<HugeEdge> edges, Map<Id, HugeVertex> removingVertices) {
    assert query.resultType().isEdge();
    BiFunction<Query, HugeEdge, HugeEdge> matchTxFunc = (q, e) -> {
        assert q.resultType() == HugeType.EDGE;
        if (e.expired() && !q.showExpired()) {
            // Filter expired edges with TTL
            return null;
        }
        // Filter edges matched conditions
        return q.test(e) ? e : q.test(e = e.switchOwner()) ? e : null;
    };
    edges = this.joinTxRecords(query, edges, matchTxFunc, this.addedEdges, this.removedEdges, this.updatedEdges);
    if (removingVertices.isEmpty()) {
        return edges;
    }
    // Filter edges that belong to deleted vertex
    return new FilterIterator<HugeEdge>(edges, edge -> {
        for (HugeVertex v : removingVertices.values()) {
            if (edge.belongToVertex(v)) {
                return false;
            }
        }
        return true;
    });
}
Also used : Arrays(java.util.Arrays) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) ListIterator(com.baidu.hugegraph.iterator.ListIterator) BiFunction(java.util.function.BiFunction) Graph(org.apache.tinkerpop.gremlin.structure.Graph) BackendException(com.baidu.hugegraph.backend.BackendException) HugeProperty(com.baidu.hugegraph.structure.HugeProperty) HugeElement(com.baidu.hugegraph.structure.HugeElement) ElementHelper(org.apache.tinkerpop.gremlin.structure.util.ElementHelper) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) SplicingIdGenerator(com.baidu.hugegraph.backend.id.SplicingIdGenerator) LimitExceedException(com.baidu.hugegraph.exception.LimitExceedException) DeleteExpiredJob(com.baidu.hugegraph.job.system.DeleteExpiredJob) LockUtil(com.baidu.hugegraph.util.LockUtil) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) BackendStore(com.baidu.hugegraph.backend.store.BackendStore) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Set(java.util.Set) QueryList(com.baidu.hugegraph.backend.page.QueryList) InsertionOrderUtil(com.baidu.hugegraph.util.InsertionOrderUtil) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) ForbiddenException(jakarta.ws.rs.ForbiddenException) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) CloseableIterator(org.apache.tinkerpop.gremlin.structure.util.CloseableIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) Id(com.baidu.hugegraph.backend.id.Id) AggregateFunc(com.baidu.hugegraph.backend.query.Aggregate.AggregateFunc) HugeVertexFeatures(com.baidu.hugegraph.structure.HugeFeatures.HugeVertexFeatures) HugeGraphParams(com.baidu.hugegraph.HugeGraphParams) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConditionQueryFlatten(com.baidu.hugegraph.backend.query.ConditionQueryFlatten) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) HugeGraph(com.baidu.hugegraph.HugeGraph) OptimizedType(com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType) E(com.baidu.hugegraph.util.E) CoreOptions(com.baidu.hugegraph.config.CoreOptions) Edge(org.apache.tinkerpop.gremlin.structure.Edge) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PageInfo(com.baidu.hugegraph.backend.page.PageInfo) Iterator(java.util.Iterator) HugeEdgeProperty(com.baidu.hugegraph.structure.HugeEdgeProperty) Condition(com.baidu.hugegraph.backend.query.Condition) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Action(com.baidu.hugegraph.type.define.Action) HugeException(com.baidu.hugegraph.HugeException) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Directions(com.baidu.hugegraph.type.define.Directions) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) HugeVertexProperty(com.baidu.hugegraph.structure.HugeVertexProperty) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) HugeConfig(com.baidu.hugegraph.config.HugeConfig) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) HugeType(com.baidu.hugegraph.type.HugeType) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 22 with HugeEdge

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

the class GraphTransaction method prepareDeletions.

protected void prepareDeletions(Map<Id, HugeVertex> removedVertices, Map<Id, HugeEdge> removedEdges) {
    // Remove related edges of each vertex
    for (HugeVertex v : removedVertices.values()) {
        if (!v.schemaLabel().existsLinkLabel()) {
            continue;
        }
        // Query all edges of the vertex and remove them
        Query query = constructEdgesQuery(v.id(), Directions.BOTH);
        Iterator<HugeEdge> vedges = this.queryEdgesFromBackend(query);
        try {
            while (vedges.hasNext()) {
                this.checkTxEdgesCapacity();
                HugeEdge edge = vedges.next();
                // NOTE: will change the input parameter
                removedEdges.put(edge.id(), edge);
                // Commit first if enabled commit-part mode
                if (this.commitPartOfAdjacentEdges > 0 && removedEdges.size() >= this.commitPartOfAdjacentEdges) {
                    this.commitPartOfEdgeDeletions(removedEdges);
                }
            }
        } finally {
            CloseableIterator.closeIterator(vedges);
        }
    }
    // Remove vertices
    for (HugeVertex v : removedVertices.values()) {
        this.checkAggregateProperty(v);
        /*
             * If the backend stores vertex together with edges, it's edges
             * would be removed after removing vertex. Otherwise, if the
             * backend stores vertex which is separated from edges, it's
             * edges should be removed manually when removing vertex.
             */
        this.doRemove(this.serializer.writeVertex(v.prepareRemoved()));
        this.indexTx.updateVertexIndex(v, true);
        this.indexTx.updateLabelIndex(v, true);
    }
    // Remove edges
    this.prepareDeletions(removedEdges);
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 23 with HugeEdge

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

the class GraphTransaction method queryEdgesFromBackend.

protected Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
    assert query.resultType().isEdge();
    QueryResults<BackendEntry> results = this.query(query);
    Iterator<BackendEntry> entries = results.iterator();
    Iterator<HugeEdge> edges = new FlatMapperIterator<>(entries, entry -> {
        // Edges are in a vertex
        HugeVertex vertex = this.parseEntry(entry);
        if (vertex == null) {
            return null;
        }
        if (query.idsSize() == 1) {
            assert vertex.getEdges().size() == 1;
        }
        /*
             * Copy to avoid ConcurrentModificationException when removing edge
             * because HugeEdge.remove() will update edges in owner vertex
             */
        return new ListIterator<>(ImmutableList.copyOf(vertex.getEdges()));
    });
    edges = this.filterExpiredResultFromFromBackend(query, edges);
    if (!this.store().features().supportsQuerySortByInputIds()) {
        // There is no id in BackendEntry, so sort after deserialization
        edges = results.keepInputOrderIfNeeded(edges);
    }
    return edges;
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) ListIterator(com.baidu.hugegraph.iterator.ListIterator) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 24 with HugeEdge

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

the class EdgeCoreTest method testQueryAdjacentVerticesOfEdges.

@Test
public void testQueryAdjacentVerticesOfEdges() {
    HugeGraph graph = graph();
    init18Edges();
    Vertex jeff = vertex("person", "name", "Jeff");
    Vertex java3 = vertex("book", "name", "java-3");
    // BOTH
    List<Vertex> vertices = graph.traversal().V(jeff.id()).bothE("friend").as("e").otherV().toList();
    Assert.assertEquals(2, vertices.size());
    // OUT
    List<Edge> edges = graph.traversal().V(jeff.id()).outE("look").toList();
    Assert.assertEquals(1, edges.size());
    HugeEdge edge = (HugeEdge) edges.get(0);
    Assert.assertEquals(jeff, edge.ownerVertex());
    Assert.assertEquals(jeff, edge.sourceVertex());
    Assert.assertEquals(java3, edge.otherVertex());
    Assert.assertEquals(java3, edge.targetVertex());
    Assert.assertEquals(jeff, edge.vertices(Direction.OUT).next());
    Assert.assertEquals(java3, edge.vertices(Direction.IN).next());
    // Fill edge properties
    Assert.assertEquals(2, edge.getProperties().size());
    Whitebox.setInternalState(edge, "propLoaded", false);
    Whitebox.setInternalState(edge, "properties", CollectionFactory.newIntObjectMap());
    Assert.assertEquals(0, edge.getProperties().size());
    Assert.assertEquals(2, edge.getFilledProperties().size());
    Assert.assertEquals(2, edge.getProperties().size());
    // Fill vertex properties
    Assert.assertTrue(edge.otherVertex().getProperties().isEmpty());
    Assert.assertEquals(1, edge.otherVertex().getFilledProperties().size());
    Assert.assertEquals(1, edge.otherVertex().getProperties().size());
}
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) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 25 with HugeEdge

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

the class EdgeAPI method update.

@PUT
@Timed(name = "single-update")
@Path("{id}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=edge_write" })
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String id, @QueryParam("action") String action, JsonEdge jsonEdge) {
    LOG.debug("Graph [{}] update edge: {}", graph, jsonEdge);
    checkUpdatingBody(jsonEdge);
    if (jsonEdge.id != null) {
        E.checkArgument(id.equals(jsonEdge.id), "The ids are different between url and " + "request body ('%s' != '%s')", id, jsonEdge.id);
    }
    // Parse action param
    boolean append = checkAndParseAction(action);
    HugeGraph g = graph(manager, graph);
    HugeEdge edge = (HugeEdge) g.edge(id);
    EdgeLabel edgeLabel = edge.schemaLabel();
    for (String key : jsonEdge.properties.keySet()) {
        PropertyKey pkey = g.propertyKey(key);
        E.checkArgument(edgeLabel.properties().contains(pkey.id()), "Can't update property for edge '%s' because " + "there is no property key '%s' in its edge label", id, key);
    }
    commit(g, () -> updateProperties(edge, jsonEdge, append));
    return manager.serializer(g).writeEdge(edge);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) 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)

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