Search in sources :

Example 21 with HugeVertex

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

the class JsonUtilTest method testSerializeVertexWithNumberId.

@Test
public void testSerializeVertexWithNumberId() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
    VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    Id id = IdGenerator.of(123456L);
    HugeVertex vertex = new HugeVertex(fakeObject.graph(), id, vl);
    MutableIntObjectMap<HugeProperty<?>> properties = CollectionFactory.newIntObjectMap(name.id(), new HugeVertexProperty<>(vertex, name, "marko"), age.id(), new HugeVertexProperty<>(vertex, age, 29), city.id(), new HugeVertexProperty<>(vertex, city, "Beijing"));
    Whitebox.setInternalState(vertex, "properties", properties);
    String json = JsonUtil.toJson(vertex);
    Assert.assertEquals("{\"id\":123456,\"label\":\"person\"," + "\"type\":\"vertex\",\"properties\":{\"" + "name\":\"marko\",\"age\":29," + "\"city\":\"Beijing\"}}", json);
}
Also used : HugeProperty(com.baidu.hugegraph.structure.HugeProperty) FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 22 with HugeVertex

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

the class SplicingIdGeneratorTest method testGenerate.

@Test
public void testGenerate() {
    FakeObjects fakeObjects = new FakeObjects();
    PropertyKey name = fakeObjects.newPropertyKey(IdGenerator.of(1), "name");
    VertexLabel vertexLabel = fakeObjects.newVertexLabel(IdGenerator.of(1L), "fake", IdStrategy.PRIMARY_KEY, name.id());
    HugeVertex vertex = Mockito.mock(HugeVertex.class);
    Mockito.when(vertex.schemaLabel()).thenReturn(vertexLabel);
    Mockito.when(vertex.name()).thenReturn("marko");
    Id vid = SplicingIdGenerator.instance().generate(vertex);
    Assert.assertEquals(IdGenerator.of("1:marko"), vid);
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) 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) Test(org.junit.Test)

Example 23 with HugeVertex

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

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

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

the class GraphTransaction method checkVertexExistIfCustomizedId.

private void checkVertexExistIfCustomizedId(Map<Id, HugeVertex> vertices) {
    Set<Id> ids = new HashSet<>();
    for (HugeVertex vertex : vertices.values()) {
        VertexLabel vl = vertex.schemaLabel();
        if (!vl.hidden() && vl.idStrategy().isCustomized()) {
            ids.add(vertex.id());
        }
    }
    if (ids.isEmpty()) {
        return;
    }
    IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids);
    Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery);
    try {
        if (!results.hasNext()) {
            return;
        }
        HugeVertex existedVertex = results.next();
        HugeVertex newVertex = vertices.get(existedVertex.id());
        if (!existedVertex.label().equals(newVertex.label())) {
            throw new HugeException("The newly added vertex with id:'%s' label:'%s' " + "is not allowed to insert, because already exist " + "a vertex with same id and different label:'%s'", newVertex.id(), newVertex.label(), existedVertex.label());
        }
    } finally {
        CloseableIterator.closeIterator(results);
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) HugeException(com.baidu.hugegraph.HugeException) HashSet(java.util.HashSet)

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