Search in sources :

Example 1 with InternalVertex

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

the class LRUVertexCache method get.

@Override
public InternalVertex get(long id, final Retriever<Long, InternalVertex> retriever) {
    final Long vertexId = id;
    InternalVertex vertex = cache.get(vertexId);
    if (vertex == null) {
        InternalVertex newVertex = volatileVertices.get(vertexId);
        if (newVertex == null) {
            newVertex = retriever.get(vertexId);
        }
        vertex = cache.putIfAbsent(vertexId, newVertex);
        if (vertex == null)
            vertex = newVertex;
    }
    return vertex;
}
Also used : NonBlockingHashMapLong(org.cliffc.high_scale_lib.NonBlockingHashMapLong) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex)

Example 2 with InternalVertex

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

the class IndexRepairJob method process.

@Override
public void process(JanusGraphVertex vertex, ScanMetrics metrics) {
    try {
        BackendTransaction mutator = writeTx.getTxHandle();
        if (index instanceof RelationTypeIndex) {
            RelationTypeIndexWrapper wrapper = (RelationTypeIndexWrapper) index;
            InternalRelationType wrappedType = wrapper.getWrappedType();
            EdgeSerializer edgeSerializer = writeTx.getEdgeSerializer();
            List<Entry> outAdditions = new ArrayList<>();
            Map<StaticBuffer, List<Entry>> inAdditionsMap = new HashMap<>();
            for (Object relation : vertex.query().types(indexRelationTypeName).direction(Direction.OUT).relations()) {
                InternalRelation janusgraphRelation = (InternalRelation) relation;
                for (int pos = 0; pos < janusgraphRelation.getArity(); pos++) {
                    if (!wrappedType.isUnidirected(Direction.BOTH) && !wrappedType.isUnidirected(EdgeDirection.fromPosition(pos)))
                        // Directionality is not covered
                        continue;
                    Entry entry = edgeSerializer.writeRelation(janusgraphRelation, wrappedType, pos, writeTx);
                    if (pos == 0) {
                        outAdditions.add(entry);
                    } else {
                        assert pos == 1;
                        InternalVertex otherVertex = janusgraphRelation.getVertex(1);
                        StaticBuffer otherVertexKey = writeTx.getIdInspector().getKey(otherVertex.longId());
                        inAdditionsMap.computeIfAbsent(otherVertexKey, k -> new ArrayList<>()).add(entry);
                    }
                }
            }
            // Mutating all OUT relationships for the current vertex
            StaticBuffer vertexKey = writeTx.getIdInspector().getKey(vertex.longId());
            mutator.mutateEdges(vertexKey, outAdditions, KCVSCache.NO_DELETIONS);
            // Mutating all IN relationships for the current vertex
            int totalInAdditions = 0;
            for (Map.Entry<StaticBuffer, List<Entry>> entry : inAdditionsMap.entrySet()) {
                StaticBuffer otherVertexKey = entry.getKey();
                List<Entry> inAdditions = entry.getValue();
                totalInAdditions += inAdditions.size();
                mutator.mutateEdges(otherVertexKey, inAdditions, KCVSCache.NO_DELETIONS);
            }
            metrics.incrementCustom(ADDED_RECORDS_COUNT, outAdditions.size() + totalInAdditions);
        } else if (index instanceof JanusGraphIndex) {
            IndexType indexType = managementSystem.getSchemaVertex(index).asIndexType();
            assert indexType != null;
            IndexSerializer indexSerializer = graph.getIndexSerializer();
            // Gather elements to index
            List<JanusGraphElement> elements;
            switch(indexType.getElement()) {
                case VERTEX:
                    elements = Collections.singletonList(vertex);
                    break;
                case PROPERTY:
                    elements = new ArrayList<>();
                    addIndexSchemaConstraint(vertex.query(), indexType).properties().forEach(elements::add);
                    break;
                case EDGE:
                    elements = new ArrayList<>();
                    addIndexSchemaConstraint(vertex.query().direction(Direction.OUT), indexType).edges().forEach(elements::add);
                    break;
                default:
                    throw new AssertionError("Unexpected category: " + indexType.getElement());
            }
            if (indexType.isCompositeIndex()) {
                for (JanusGraphElement element : elements) {
                    Set<IndexSerializer.IndexUpdate<StaticBuffer, Entry>> updates = indexSerializer.reindexElement(element, (CompositeIndexType) indexType);
                    for (IndexSerializer.IndexUpdate<StaticBuffer, Entry> update : updates) {
                        log.debug("Mutating index {}: {}", indexType, update.getEntry());
                        mutator.mutateIndex(update.getKey(), new ArrayList<Entry>(1) {

                            {
                                add(update.getEntry());
                            }
                        }, KCVSCache.NO_DELETIONS);
                        metrics.incrementCustom(ADDED_RECORDS_COUNT);
                    }
                }
            } else {
                assert indexType.isMixedIndex();
                for (JanusGraphElement element : elements) {
                    if (indexSerializer.reindexElement(element, (MixedIndexType) indexType, documentsPerStore)) {
                        metrics.incrementCustom(DOCUMENT_UPDATES_COUNT);
                    }
                }
            }
        } else
            throw new UnsupportedOperationException("Unsupported index found: " + index);
    } catch (final Exception e) {
        managementSystem.rollback();
        writeTx.rollback();
        metrics.incrementCustom(FAILED_TX);
        throw new JanusGraphException(e.getMessage(), e);
    }
}
Also used : InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) StringUtils(org.janusgraph.util.StringUtils) BaseVertexQuery(org.janusgraph.core.BaseVertexQuery) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction) HashMap(java.util.HashMap) SchemaAction(org.janusgraph.core.schema.SchemaAction) IndexSerializer(org.janusgraph.graphdb.database.IndexSerializer) ArrayList(java.util.ArrayList) ScanMetrics(org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) SchemaStatus(org.janusgraph.core.schema.SchemaStatus) QueryContainer(org.janusgraph.graphdb.olap.QueryContainer) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) Map(java.util.Map) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphException(org.janusgraph.core.JanusGraphException) IndexType(org.janusgraph.graphdb.types.IndexType) JanusGraphElement(org.janusgraph.core.JanusGraphElement) JanusGraphSchemaType(org.janusgraph.core.schema.JanusGraphSchemaType) BackendException(org.janusgraph.diskstorage.BackendException) RelationType(org.janusgraph.core.RelationType) VertexScanJob(org.janusgraph.graphdb.olap.VertexScanJob) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) PropertyKey(org.janusgraph.core.PropertyKey) KCVSCache(org.janusgraph.diskstorage.keycolumnvalue.cache.KCVSCache) BaseLabel(org.janusgraph.graphdb.types.system.BaseLabel) Set(java.util.Set) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) EdgeSerializer(org.janusgraph.graphdb.database.EdgeSerializer) EdgeDirection(org.janusgraph.graphdb.relations.EdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Entry(org.janusgraph.diskstorage.Entry) Preconditions(com.google.common.base.Preconditions) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) RelationTypeIndexWrapper(org.janusgraph.graphdb.database.management.RelationTypeIndexWrapper) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) Collections(java.util.Collections) Set(java.util.Set) HashMap(java.util.HashMap) JanusGraphException(org.janusgraph.core.JanusGraphException) ArrayList(java.util.ArrayList) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) Entry(org.janusgraph.diskstorage.Entry) JanusGraphElement(org.janusgraph.core.JanusGraphElement) EdgeSerializer(org.janusgraph.graphdb.database.EdgeSerializer) RelationTypeIndexWrapper(org.janusgraph.graphdb.database.management.RelationTypeIndexWrapper) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ArrayList(java.util.ArrayList) List(java.util.List) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) BackendTransaction(org.janusgraph.diskstorage.BackendTransaction) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) IndexSerializer(org.janusgraph.graphdb.database.IndexSerializer) JanusGraphException(org.janusgraph.core.JanusGraphException) BackendException(org.janusgraph.diskstorage.BackendException) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with InternalVertex

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

the class VertexIDAssigner method assignIDs.

public void assignIDs(Iterable<InternalRelation> addedRelations) {
    if (!placementStrategy.supportsBulkPlacement()) {
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    assignID(vertex, getVertexIDType(vertex));
                }
            }
            assignID(relation);
        }
    } else {
        // 2) only assign ids to (user) vertices
        Map<InternalVertex, PartitionAssignment> assignments = new HashMap<>();
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    // Those are assigned ids immediately in the transaction
                    assert !(vertex instanceof JanusGraphSchemaVertex);
                    if (vertex.vertexLabel().isPartitioned())
                        // Assign partitioned vertex ids immediately
                        assignID(vertex, getVertexIDType(vertex));
                    else
                        assignments.put(vertex, PartitionAssignment.EMPTY);
                }
            }
        }
        log.trace("Bulk id assignment for {} vertices", assignments.size());
        for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS && (assignments != null && !assignments.isEmpty()); attempt++) {
            placementStrategy.getPartitions(assignments);
            Map<InternalVertex, PartitionAssignment> leftOvers = null;
            Iterator<Map.Entry<InternalVertex, PartitionAssignment>> iterator = assignments.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<InternalVertex, PartitionAssignment> entry = iterator.next();
                try {
                    assignID(entry.getKey(), entry.getValue().getPartitionID(), getVertexIDType(entry.getKey()));
                    Preconditions.checkArgument(entry.getKey().hasId());
                } catch (IDPoolExhaustedException e) {
                    if (leftOvers == null)
                        leftOvers = new HashMap<>();
                    leftOvers.put(entry.getKey(), PartitionAssignment.EMPTY);
                    break;
                }
            }
            if (leftOvers != null) {
                while (iterator.hasNext()) leftOvers.put(iterator.next().getKey(), PartitionAssignment.EMPTY);
                log.debug("Exhausted ID Pool in bulk assignment. Left-over vertices {}", leftOvers.size());
            }
            assignments = leftOvers;
        }
        if (assignments != null && !assignments.isEmpty())
            throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
        // 3) assign ids to relations
        for (InternalRelation relation : addedRelations) {
            assignID(relation);
        }
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation) PartitionAssignment(org.janusgraph.graphdb.database.idassigner.placement.PartitionAssignment) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) EnumMap(java.util.EnumMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with InternalVertex

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

the class VertexIDAssigner method assignID.

private void assignID(InternalElement element, IDManager.VertexIDType vertexIDType) {
    for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS; attempt++) {
        long partitionID = -1;
        if (element instanceof JanusGraphSchemaVertex) {
            partitionID = IDManager.SCHEMA_PARTITION;
        } else if (element instanceof JanusGraphVertex) {
            if (vertexIDType == IDManager.VertexIDType.PartitionedVertex)
                partitionID = IDManager.PARTITIONED_VERTEX_PARTITION;
            else
                partitionID = placementStrategy.getPartition(element);
        } else if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (attempt < relation.getLen()) {
                // On the first attempts, try to use partition of incident vertices
                InternalVertex incident = relation.getVertex(attempt);
                Preconditions.checkArgument(incident.hasId());
                if (!IDManager.VertexIDType.PartitionedVertex.is(incident.longId()) || relation.isProperty()) {
                    partitionID = getPartitionID(incident);
                } else {
                    continue;
                }
            } else {
                partitionID = placementStrategy.getPartition(element);
            }
        }
        try {
            assignID(element, partitionID, vertexIDType);
        } catch (IDPoolExhaustedException e) {
            // try again on a different partition
            continue;
        }
        assert element.hasId();
        // Check if we should assign a different representative of a potential partitioned vertex
        if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (relation.isProperty() && isPartitionedAt(relation, 0)) {
                // Always assign properties to the canonical representative of a partitioned vertex
                InternalVertex vertex = relation.getVertex(0);
                ((ReassignableRelation) relation).setVertexAt(0, vertex.tx().getInternalVertex(idManager.getCanonicalVertexId(vertex.longId())));
            } else if (relation.isEdge()) {
                for (int pos = 0; pos < relation.getArity(); pos++) {
                    if (isPartitionedAt(relation, pos)) {
                        InternalVertex incident = relation.getVertex(pos);
                        long newPartition;
                        int otherPosition = (pos + 1) % 2;
                        if (((InternalRelationType) relation.getType()).multiplicity().isUnique(EdgeDirection.fromPosition(pos))) {
                            // If the relation is unique in the direction, we assign it to the canonical vertex...
                            newPartition = idManager.getPartitionId(idManager.getCanonicalVertexId(incident.longId()));
                        } else if (!isPartitionedAt(relation, otherPosition)) {
                            // ...else, we assign it to the partition of the non-partitioned vertex...
                            newPartition = getPartitionID(relation.getVertex(otherPosition));
                        } else {
                            // ...and if such does not exists (i.e. both end vertices are partitioned) we use the hash of the relation id
                            newPartition = idManager.getPartitionHashForId(relation.longId());
                        }
                        if (idManager.getPartitionId(incident.longId()) != newPartition) {
                            ((ReassignableRelation) relation).setVertexAt(pos, incident.tx().getOtherPartitionVertex(incident, newPartition));
                        }
                    }
                }
            }
        }
        return;
    }
    throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
}
Also used : ReassignableRelation(org.janusgraph.graphdb.relations.ReassignableRelation) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) InternalRelation(org.janusgraph.graphdb.internal.InternalRelation)

Example 5 with InternalVertex

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

the class VertexIterable method iterator.

@Override
public Iterator<InternalVertex> iterator() {
    return new Iterator<InternalVertex>() {

        final RecordIterator<Long> iterator = graph.getVertexIDs(tx.getTxHandle());

        InternalVertex nextVertex = nextVertex();

        private InternalVertex nextVertex() {
            InternalVertex v = null;
            while (v == null && iterator.hasNext()) {
                final long nextId = iterator.next();
                // Filter out invisible vertices
                if (IDManager.VertexIDType.Invisible.is(nextId))
                    continue;
                v = tx.getInternalVertex(nextId);
                // Filter out deleted vertices and types
                if (v.isRemoved())
                    v = null;
            }
            return v;
        }

        @Override
        public boolean hasNext() {
            return nextVertex != null;
        }

        @Override
        public InternalVertex next() {
            if (!hasNext())
                throw new NoSuchElementException();
            final InternalVertex returnVertex = nextVertex;
            nextVertex = nextVertex();
            return returnVertex;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : RecordIterator(org.janusgraph.diskstorage.util.RecordIterator) Iterator(java.util.Iterator) RecordIterator(org.janusgraph.diskstorage.util.RecordIterator) InternalVertex(org.janusgraph.graphdb.internal.InternalVertex) NoSuchElementException(java.util.NoSuchElementException)

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