Search in sources :

Example 16 with InternalVertex

use of org.janusgraph.graphdb.internal.InternalVertex in project janusgraph by JanusGraph.

the class ModificationDeserializer method parseRelation.

public static InternalRelation parseRelation(TransactionLogHeader.Modification modification, StandardJanusGraphTx tx) {
    Change state = modification.state;
    assert state.isProper();
    long outVertexId = modification.outVertexId;
    Entry relEntry = modification.relationEntry;
    InternalVertex outVertex = tx.getInternalVertex(outVertexId);
    // Special relation parsing, compare to {@link RelationConstructor}
    RelationCache relCache = tx.getEdgeSerializer().readRelation(relEntry, false, tx);
    assert relCache.direction == Direction.OUT;
    InternalRelationType type = (InternalRelationType) tx.getExistingRelationType(relCache.typeId);
    assert type.getBaseType() == null;
    InternalRelation rel;
    if (type.isPropertyKey()) {
        if (state == Change.REMOVED) {
            rel = new StandardVertexProperty(relCache.relationId, (PropertyKey) type, outVertex, relCache.getValue(), ElementLifeCycle.Removed);
        } else {
            rel = new CacheVertexProperty(relCache.relationId, (PropertyKey) type, outVertex, relCache.getValue(), relEntry);
        }
    } else {
        assert type.isEdgeLabel();
        InternalVertex otherVertex = tx.getInternalVertex(relCache.getOtherVertexId());
        if (state == Change.REMOVED) {
            rel = new StandardEdge(relCache.relationId, (EdgeLabel) type, outVertex, otherVertex, ElementLifeCycle.Removed);
        } else {
            rel = new CacheEdge(relCache.relationId, (EdgeLabel) type, outVertex, otherVertex, relEntry);
        }
    }
    if (state == Change.REMOVED && relCache.hasProperties()) {
        // copy over properties
        for (LongObjectCursor<Object> entry : relCache) {
            rel.setPropertyDirect(tx.getExistingPropertyKey(entry.key), entry.value);
        }
    }
    return rel;
}
Also used : RelationCache(org.janusgraph.graphdb.relations.RelationCache) EdgeLabel(org.janusgraph.core.EdgeLabel) StandardVertexProperty(org.janusgraph.graphdb.relations.StandardVertexProperty) Change(org.janusgraph.core.log.Change) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) StandardEdge(org.janusgraph.graphdb.relations.StandardEdge) Entry(org.janusgraph.diskstorage.Entry) CacheVertexProperty(org.janusgraph.graphdb.relations.CacheVertexProperty) CacheEdge(org.janusgraph.graphdb.relations.CacheEdge) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) PropertyKey(org.janusgraph.core.PropertyKey)

Example 17 with InternalVertex

use of org.janusgraph.graphdb.internal.InternalVertex in project janusgraph by JanusGraph.

the class StandardJanusGraphTx method getVertex.

@Override
public JanusGraphVertex getVertex(long vertexId) {
    verifyOpen();
    if (null != config.getGroupName()) {
        MetricManager.INSTANCE.getCounter(config.getGroupName(), "db", "getVertexByID").inc();
    }
    if (!isValidVertexId(vertexId))
        return null;
    // Make canonical partitioned vertex id
    if (idInspector.isPartitionedVertex(vertexId))
        vertexId = idManager.getCanonicalVertexId(vertexId);
    final InternalVertex v = vertexCache.get(vertexId, externalVertexRetriever);
    return (null == v || v.isRemoved()) ? null : v;
}
Also used : InternalVertex(org.janusgraph.graphdb.internal.InternalVertex)

Example 18 with InternalVertex

use of org.janusgraph.graphdb.internal.InternalVertex in project janusgraph by JanusGraph.

the class BasicVertexCentricQueryBuilder method executeRelations.

protected Iterable<JanusGraphRelation> executeRelations(InternalVertex vertex, BaseVertexCentricQuery baseQuery) {
    if (isPartitionedVertex(vertex)) {
        if (!hasAllCanonicalTypes()) {
            InternalVertex[] representatives = tx.getAllRepresentatives(vertex, restrict2Partitions);
            Iterable<JanusGraphRelation> merge = null;
            for (InternalVertex rep : representatives) {
                Iterable<JanusGraphRelation> iterable = executeIndividualRelations(rep, baseQuery);
                if (merge == null) {
                    merge = iterable;
                } else {
                    merge = ResultMergeSortIterator.mergeSort(merge, iterable, (Comparator) orders, false);
                }
            }
            return ResultSetIterator.wrap(merge, baseQuery.getLimit());
        } else
            vertex = tx.getCanonicalVertex(vertex);
    }
    return executeIndividualRelations(vertex, baseQuery);
}
Also used : JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) Comparator(java.util.Comparator)

Example 19 with InternalVertex

use of org.janusgraph.graphdb.internal.InternalVertex in project janusgraph by JanusGraph.

the class BasicVertexCentricQueryBuilder method executeVertexIds.

public VertexList executeVertexIds(InternalVertex vertex, BaseVertexCentricQuery baseQuery) {
    if (isPartitionedVertex(vertex)) {
        // If there is a sort order, we need to first merge the relations (and sort) and then compute vertices
        if (!orders.isEmpty())
            return edges2VertexIds((Iterable) executeRelations(vertex, baseQuery), vertex);
        if (!hasAllCanonicalTypes()) {
            InternalVertex[] representatives = tx.getAllRepresentatives(vertex, restrict2Partitions);
            VertexListInternal merge = null;
            for (InternalVertex rep : representatives) {
                if (merge != null && merge.size() >= baseQuery.getLimit())
                    break;
                VertexList vertexList = executeIndividualVertexIds(rep, baseQuery);
                if (merge == null)
                    merge = (VertexListInternal) vertexList;
                else
                    merge.addAll(vertexList);
            }
            if (merge != null && merge.size() > baseQuery.getLimit()) {
                merge = (VertexListInternal) merge.subList(0, baseQuery.getLimit());
            }
            return merge;
        } else
            vertex = tx.getCanonicalVertex(vertex);
    }
    return executeIndividualVertexIds(vertex, baseQuery);
}
Also used : InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) VertexList(org.janusgraph.core.VertexList)

Example 20 with InternalVertex

use of org.janusgraph.graphdb.internal.InternalVertex in project janusgraph by JanusGraph.

the class MultiVertexCentricQueryBuilder method execute.

/* ---------------------------------------------------------------
     * Query Execution
	 * ---------------------------------------------------------------
	 */
/**
 * Constructs the BaseVertexCentricQuery through {@link BasicVertexCentricQueryBuilder#constructQuery(org.janusgraph.graphdb.internal.RelationCategory)}.
 * If the query asks for an implicit key, the resulting map is computed and returned directly.
 * If the query is empty, a map that maps each vertex to an empty list is returned.
 * Otherwise, the query is executed for all vertices through the transaction which will effectively
 * pre-load the return result sets into the associated {@link org.janusgraph.graphdb.vertices.CacheVertex} or
 * don't do anything at all if the vertex is new (and hence no edges in the storage backend).
 * After that, a map is constructed that maps each vertex to the corresponding VertexCentricQuery and wrapped
 * into a QueryProcessor. Hence, upon iteration the query will be executed like any other VertexCentricQuery
 * with the performance difference that the SliceQueries will have already been preloaded and not further
 * calls to the storage backend are needed.
 *
 * @param returnType
 * @return
 */
protected <Q> Map<JanusGraphVertex, Q> execute(RelationCategory returnType, ResultConstructor<Q> resultConstructor) {
    Preconditions.checkArgument(!vertices.isEmpty(), "Need to add at least one vertex to query");
    final Map<JanusGraphVertex, Q> result = new HashMap<>(vertices.size());
    BaseVertexCentricQuery bq = super.constructQuery(returnType);
    profiler.setAnnotation(QueryProfiler.MULTIQUERY_ANNOTATION, true);
    profiler.setAnnotation(QueryProfiler.NUMVERTICES_ANNOTATION, vertices.size());
    if (!bq.isEmpty()) {
        for (BackendQueryHolder<SliceQuery> sq : bq.getQueries()) {
            Set<InternalVertex> adjVertices = new HashSet<>(vertices);
            for (InternalVertex v : vertices) {
                if (isPartitionedVertex(v)) {
                    profiler.setAnnotation(QueryProfiler.PARTITIONED_VERTEX_ANNOTATION, true);
                    adjVertices.remove(v);
                    adjVertices.addAll(allRequiredRepresentatives(v));
                }
            }
            // Overwrite with more accurate size accounting for partitioned vertices
            profiler.setAnnotation(QueryProfiler.NUMVERTICES_ANNOTATION, adjVertices.size());
            tx.executeMultiQuery(adjVertices, sq.getBackendQuery(), sq.getProfiler());
        }
        for (InternalVertex v : vertices) {
            result.put(v, resultConstructor.getResult(v, bq));
        }
    } else {
        for (JanusGraphVertex v : vertices) result.put(v, resultConstructor.emptyResult());
    }
    return result;
}
Also used : HashMap(java.util.HashMap) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) SliceQuery(org.janusgraph.diskstorage.keycolumnvalue.SliceQuery) HashSet(java.util.HashSet)

Aggregations

InternalVertex (org.janusgraph.graphdb.internal.InternalVertex)20 InternalRelation (org.janusgraph.graphdb.internal.InternalRelation)10 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)6 HashMap (java.util.HashMap)5 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)5 JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)5 CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)4 LongArrayList (com.carrotsearch.hppc.LongArrayList)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 PropertyKey (org.janusgraph.core.PropertyKey)3 IndexTransaction (org.janusgraph.diskstorage.indexing.IndexTransaction)3 IndexType (org.janusgraph.graphdb.types.IndexType)3 Preconditions (com.google.common.base.Preconditions)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 List (java.util.List)2 NoSuchElementException (java.util.NoSuchElementException)2