Search in sources :

Example 36 with HugeVertex

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

the class TableSerializer method writeVertexProperty.

@Override
public BackendEntry writeVertexProperty(HugeVertexProperty<?> prop) {
    HugeVertex vertex = prop.element();
    TableBackendEntry entry = newBackendEntry(vertex);
    if (vertex.hasTtl()) {
        entry.ttl(vertex.ttl());
        entry.column(HugeKeys.EXPIRED_TIME, vertex.expiredTime());
    }
    entry.subId(IdGenerator.of(prop.key()));
    entry.column(HugeKeys.ID, this.writeId(vertex.id()));
    entry.column(HugeKeys.LABEL, vertex.schemaLabel().id().asLong());
    this.formatProperty(prop, entry.row());
    return entry;
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 37 with HugeVertex

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

the class BinaryScatterSerializer method readVertex.

@Override
public HugeVertex readVertex(HugeGraph graph, BackendEntry bytesEntry) {
    if (bytesEntry == null) {
        return null;
    }
    BinaryBackendEntry entry = this.convertEntry(bytesEntry);
    // Parse label
    final byte[] VL = this.formatSyspropName(entry.id(), HugeKeys.LABEL);
    BackendColumn vl = entry.column(VL);
    VertexLabel vertexLabel = VertexLabel.NONE;
    if (vl != null) {
        Id labelId = BytesBuffer.wrap(vl.value).readId();
        vertexLabel = graph.vertexLabelOrNone(labelId);
    }
    // Parse id
    Id id = entry.id().origin();
    HugeVertex vertex = new HugeVertex(graph, id, vertexLabel);
    // Parse all properties and edges of a Vertex
    for (BackendColumn col : entry.columns()) {
        this.parseColumn(col, vertex);
    }
    return vertex;
}
Also used : BackendColumn(com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 38 with HugeVertex

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

the class GraphTransaction method addVertexProperty.

@Watched(prefix = "graph")
public <V> void addVertexProperty(HugeVertexProperty<V> prop) {
    // NOTE: this method can also be used to update property
    HugeVertex vertex = prop.element();
    E.checkState(vertex != null, "No owner for updating property '%s'", prop.key());
    // Add property in memory for new created vertex
    if (vertex.fresh()) {
        // The owner will do property update
        vertex.setProperty(prop);
        return;
    }
    // Check is updating property of added/removed vertex
    E.checkArgument(!this.addedVertices.containsKey(vertex.id()) || this.updatedVertices.containsKey(vertex.id()), "Can't update property '%s' for adding-state vertex", prop.key());
    E.checkArgument(!vertex.removed() && !this.removedVertices.containsKey(vertex.id()), "Can't update property '%s' for removing-state vertex", prop.key());
    // Check is updating primary key
    List<Id> primaryKeyIds = vertex.schemaLabel().primaryKeys();
    E.checkArgument(!primaryKeyIds.contains(prop.propertyKey().id()), "Can't update primary key: '%s'", prop.key());
    // Do property update
    this.lockForUpdateProperty(vertex.schemaLabel(), prop, () -> {
        // Update old vertex to remove index (without new property)
        this.indexTx.updateVertexIndex(vertex, true);
        // Update(add) vertex property
        this.propertyUpdated(vertex, prop, vertex.setProperty(prop));
    });
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 39 with HugeVertex

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

the class GraphTransaction method prepareAdditions.

protected void prepareAdditions(Map<Id, HugeVertex> addedVertices, Map<Id, HugeEdge> addedEdges) {
    if (this.checkCustomVertexExist) {
        this.checkVertexExistIfCustomizedId(addedVertices);
    }
    if (this.removeLeftIndexOnOverwrite) {
        this.removeLeftIndexIfNeeded(addedVertices);
    }
    // Do vertex update
    for (HugeVertex v : addedVertices.values()) {
        assert !v.removed();
        v.committed();
        this.checkAggregateProperty(v);
        // Check whether passed all non-null properties
        if (!this.graphMode().loading()) {
            this.checkNonnullProperty(v);
        }
        // Add vertex entry
        this.doInsert(this.serializer.writeVertex(v));
        // Update index of vertex(only include props)
        this.indexTx.updateVertexIndex(v, false);
        this.indexTx.updateLabelIndex(v, false);
    }
    // Do edge update
    for (HugeEdge e : addedEdges.values()) {
        assert !e.removed();
        e.committed();
        // Skip edge if its owner has been removed
        if (this.removingEdgeOwner(e)) {
            continue;
        }
        this.checkAggregateProperty(e);
        // Add edge entry of OUT and IN
        this.doInsert(this.serializer.writeEdge(e));
        this.doInsert(this.serializer.writeEdge(e.switchOwner()));
        // Update index of edge
        this.indexTx.updateEdgeIndex(e, false);
        this.indexTx.updateLabelIndex(e, false);
    }
}
Also used : HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 40 with HugeVertex

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

the class GraphTransaction method queryVerticesByIds.

protected Iterator<Vertex> queryVerticesByIds(Object[] vertexIds, boolean adjacentVertex, boolean checkMustExist) {
    Query.checkForceCapacity(vertexIds.length);
    // NOTE: allowed duplicated vertices if query by duplicated ids
    List<Id> ids = InsertionOrderUtil.newList();
    Map<Id, HugeVertex> vertices = new HashMap<>(vertexIds.length);
    IdQuery query = new IdQuery(HugeType.VERTEX);
    for (Object vertexId : vertexIds) {
        HugeVertex vertex;
        Id id = HugeVertex.getIdValue(vertexId);
        if (id == null || this.removedVertices.containsKey(id)) {
            // The record has been deleted
            continue;
        } else if ((vertex = this.addedVertices.get(id)) != null || (vertex = this.updatedVertices.get(id)) != null) {
            if (vertex.expired()) {
                continue;
            }
            // Found from local tx
            vertices.put(vertex.id(), vertex);
        } else {
            // Prepare to query from backend store
            query.query(id);
        }
        ids.add(id);
    }
    if (!query.empty()) {
        // Query from backend store
        query.mustSortByInput(false);
        Iterator<HugeVertex> it = this.queryVerticesFromBackend(query);
        QueryResults.fillMap(it, vertices);
    }
    return new MapperIterator<>(ids.iterator(), id -> {
        HugeVertex vertex = vertices.get(id);
        if (vertex == null) {
            if (checkMustExist) {
                throw new NotFoundException("Vertex '%s' does not exist", id);
            } else if (adjacentVertex) {
                assert !checkMustExist;
                // Return undefined if adjacentVertex but !checkMustExist
                vertex = HugeVertex.undefined(this.graph(), id);
            } else {
                // Return null
                assert vertex == null;
            }
        }
        return vertex;
    });
}
Also used : MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) HashMap(java.util.HashMap) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) 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)

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