Search in sources :

Example 1 with RelationCache

use of com.thinkaurelius.titan.graphdb.relations.RelationCache in project titan by thinkaurelius.

the class EdgeSerializer method readRelation.

public RelationCache readRelation(Entry data, boolean parseHeaderOnly, TypeInspector tx) {
    RelationCache map = data.getCache();
    if (map == null || !(parseHeaderOnly || map.hasProperties())) {
        map = parseRelation(data, parseHeaderOnly, tx);
        data.setCache(map);
    }
    return map;
}
Also used : RelationCache(com.thinkaurelius.titan.graphdb.relations.RelationCache)

Example 2 with RelationCache

use of com.thinkaurelius.titan.graphdb.relations.RelationCache in project titan by thinkaurelius.

the class EdgeSerializer method parseRelation.

@Override
public RelationCache parseRelation(Entry data, boolean excludeProperties, TypeInspector tx) {
    ReadBuffer in = data.asReadBuffer();
    LongObjectHashMap properties = excludeProperties ? null : new LongObjectHashMap(4);
    RelationTypeParse typeAndDir = IDHandler.readRelationType(in);
    long typeId = typeAndDir.typeId;
    Direction dir = typeAndDir.dirID.getDirection();
    RelationCategory rtype = typeAndDir.dirID.getRelationCategory();
    RelationType relationType = tx.getExistingRelationType(typeId);
    InternalRelationType def = (InternalRelationType) relationType;
    Multiplicity multiplicity = def.multiplicity();
    long[] keysig = def.getSortKey();
    long relationId;
    Object other;
    int startKeyPos = in.getPosition();
    int endKeyPos = 0;
    if (relationType.isEdgeLabel()) {
        long otherVertexId;
        if (multiplicity.isConstrained()) {
            if (multiplicity.isUnique(dir)) {
                otherVertexId = VariableLong.readPositive(in);
            } else {
                in.movePositionTo(data.getValuePosition());
                otherVertexId = VariableLong.readPositiveBackward(in);
                in.movePositionTo(data.getValuePosition());
            }
            relationId = VariableLong.readPositive(in);
        } else {
            in.movePositionTo(data.getValuePosition());
            relationId = VariableLong.readPositiveBackward(in);
            otherVertexId = VariableLong.readPositiveBackward(in);
            endKeyPos = in.getPosition();
            in.movePositionTo(data.getValuePosition());
        }
        other = otherVertexId;
    } else {
        assert relationType.isPropertyKey();
        PropertyKey key = (PropertyKey) relationType;
        if (multiplicity.isConstrained()) {
            other = readPropertyValue(in, key);
            relationId = VariableLong.readPositive(in);
        } else {
            in.movePositionTo(data.getValuePosition());
            relationId = VariableLong.readPositiveBackward(in);
            endKeyPos = in.getPosition();
            in.movePositionTo(data.getValuePosition());
            other = readPropertyValue(in, key);
        }
        Preconditions.checkState(other != null, "Encountered error in deserializer [null value returned]. Check serializer compatibility.");
    }
    assert other != null;
    if (!excludeProperties && !multiplicity.isConstrained() && keysig.length > 0) {
        int currentPos = in.getPosition();
        //Read sort key which only exists if type is not unique in this direction
        assert endKeyPos > startKeyPos;
        //after reading the ids, we are on the last byte of the key
        int keyLength = endKeyPos - startKeyPos;
        in.movePositionTo(startKeyPos);
        ReadBuffer inkey = in;
        if (def.getSortOrder() == Order.DESC)
            inkey = in.subrange(keyLength, true);
        readInlineTypes(keysig, properties, inkey, tx, InlineType.KEY);
        in.movePositionTo(currentPos);
    }
    if (!excludeProperties) {
        //read value signature
        readInlineTypes(def.getSignature(), properties, in, tx, InlineType.SIGNATURE);
        //Third: read rest
        while (in.hasRemaining()) {
            PropertyKey type = tx.getExistingPropertyKey(IDHandler.readInlineRelationType(in));
            Object pvalue = readInline(in, type, InlineType.NORMAL);
            assert pvalue != null;
            properties.put(type.longId(), pvalue);
        }
        if (data.hasMetaData()) {
            for (Map.Entry<EntryMetaData, Object> metas : data.getMetaData().entrySet()) {
                ImplicitKey key = ImplicitKey.MetaData2ImplicitKey.get(metas.getKey());
                if (key != null) {
                    assert metas.getValue() != null;
                    properties.put(key.longId(), metas.getValue());
                }
            }
        }
    }
    return new RelationCache(dir, typeId, relationId, other, properties);
}
Also used : LongObjectHashMap(com.carrotsearch.hppc.LongObjectHashMap) RelationCache(com.thinkaurelius.titan.graphdb.relations.RelationCache) EdgeDirection(com.thinkaurelius.titan.graphdb.relations.EdgeDirection) Direction(org.apache.tinkerpop.gremlin.structure.Direction) EntryMetaData(com.thinkaurelius.titan.diskstorage.EntryMetaData) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) RelationTypeParse(com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.RelationTypeParse) Map(java.util.Map) LongObjectHashMap(com.carrotsearch.hppc.LongObjectHashMap) ImplicitKey(com.thinkaurelius.titan.graphdb.types.system.ImplicitKey)

Example 3 with RelationCache

use of com.thinkaurelius.titan.graphdb.relations.RelationCache in project titan by thinkaurelius.

the class TitanVertexDeserializer method readHadoopVertex.

// Read a single row from the edgestore and create a TinkerVertex corresponding to the row
// The neighboring vertices are represented by DetachedVertex instances
public TinkerVertex readHadoopVertex(final StaticBuffer key, Iterable<Entry> entries) {
    // Convert key to a vertex ID
    final long vertexId = idManager.getKeyID(key);
    Preconditions.checkArgument(vertexId > 0);
    // Partitioned vertex handling
    if (idManager.isPartitionedVertex(vertexId)) {
        Preconditions.checkState(setup.getFilterPartitionedVertices(), "Read partitioned vertex (ID=%s), but partitioned vertex filtering is disabled.", vertexId);
        log.debug("Skipping partitioned vertex with ID {}", vertexId);
        return null;
    }
    // Create TinkerVertex
    TinkerGraph tg = TinkerGraph.open();
    boolean foundVertexState = !verifyVertexExistence;
    TinkerVertex tv = null;
    // Iterate over edgestore columns to find the vertex's label relation
    for (final Entry data : entries) {
        RelationReader relationReader = setup.getRelationReader(vertexId);
        final RelationCache relation = relationReader.parseRelation(data, false, typeManager);
        if (systemTypes.isVertexLabelSystemType(relation.typeId)) {
            // Found vertex Label
            long vertexLabelId = relation.getOtherVertexId();
            VertexLabel vl = typeManager.getExistingVertexLabel(vertexLabelId);
            // Create TinkerVertex with this label
            //tv = (TinkerVertex)tg.addVertex(T.label, vl.label(), T.id, vertexId);
            tv = getOrCreateVertex(vertexId, vl.name(), tg);
        }
    }
    // Added this following testing
    if (null == tv) {
        //tv = (TinkerVertex)tg.addVertex(T.id, vertexId);
        tv = getOrCreateVertex(vertexId, null, tg);
    }
    Preconditions.checkState(null != tv, "Unable to determine vertex label for vertex with ID %s", vertexId);
    // Iterate over and decode edgestore columns (relations) on this vertex
    for (final Entry data : entries) {
        try {
            RelationReader relationReader = setup.getRelationReader(vertexId);
            final RelationCache relation = relationReader.parseRelation(data, false, typeManager);
            if (systemTypes.isVertexExistsSystemType(relation.typeId)) {
                foundVertexState = true;
            }
            //Ignore system types
            if (systemTypes.isSystemType(relation.typeId))
                continue;
            final RelationType type = typeManager.getExistingRelationType(relation.typeId);
            //Ignore hidden types
            if (((InternalRelationType) type).isInvisibleType())
                continue;
            // Decode and create the relation (edge or property)
            if (type.isPropertyKey()) {
                // Decode property
                Object value = relation.getValue();
                Preconditions.checkNotNull(value);
                VertexProperty.Cardinality card = getPropertyKeyCardinality(type.name());
                tv.property(card, type.name(), value, T.id, relation.relationId);
            } else {
                assert type.isEdgeLabel();
                // Partitioned vertex handling
                if (idManager.isPartitionedVertex(relation.getOtherVertexId())) {
                    Preconditions.checkState(setup.getFilterPartitionedVertices(), "Read edge incident on a partitioned vertex, but partitioned vertex filtering is disabled.  " + "Relation ID: %s.  This vertex ID: %s.  Other vertex ID: %s.  Edge label: %s.", relation.relationId, vertexId, relation.getOtherVertexId(), type.name());
                    log.debug("Skipping edge with ID {} incident on partitioned vertex with ID {} (and nonpartitioned vertex with ID {})", relation.relationId, relation.getOtherVertexId(), vertexId);
                    continue;
                }
                // Decode edge
                TinkerEdge te;
                if (relation.direction.equals(Direction.IN)) {
                    // We don't know the label of the other vertex, but one must be provided
                    TinkerVertex outV = getOrCreateVertex(relation.getOtherVertexId(), null, tg);
                    te = (TinkerEdge) outV.addEdge(type.name(), tv, T.id, relation.relationId);
                } else if (relation.direction.equals(Direction.OUT)) {
                    // We don't know the label of the other vertex, but one must be provided
                    TinkerVertex inV = getOrCreateVertex(relation.getOtherVertexId(), null, tg);
                    te = (TinkerEdge) tv.addEdge(type.name(), inV, T.id, relation.relationId);
                } else {
                    throw new RuntimeException("Direction.BOTH is not supported");
                }
                if (relation.hasProperties()) {
                    // Load relation properties
                    for (final LongObjectCursor<Object> next : relation) {
                        assert next.value != null;
                        RelationType rt = typeManager.getExistingRelationType(next.key);
                        if (rt.isPropertyKey()) {
                            //                                PropertyKey pkey = (PropertyKey)vertex.getTypeManager().getPropertyKey(rt.name());
                            //                                log.debug("Retrieved key {} for name \"{}\"", pkey, rt.name());
                            //                                frel.property(pkey.label(), next.value);
                            te.property(rt.name(), next.value);
                        } else {
                            throw new RuntimeException("Metaedges are not supported");
                        //                                assert next.value instanceof Long;
                        //                                EdgeLabel el = (EdgeLabel)vertex.getTypeManager().getEdgeLabel(rt.name());
                        //                                log.debug("Retrieved ege label {} for name \"{}\"", el, rt.name());
                        //                                frel.setProperty(el, new FaunusVertex(configuration,(Long)next.value));
                        }
                    }
                }
            }
        //                // Iterate over and copy the relation's metaproperties
        //                if (relation.hasProperties()) {
        //                    // Load relation properties
        //                    for (final LongObjectCursor<Object> next : relation) {
        //                        assert next.value != null;
        //                        RelationType rt = typeManager.getExistingRelationType(next.key);
        //                        if (rt.isPropertyKey()) {
        //                            PropertyKey pkey = (PropertyKey)vertex.getTypeManager().getPropertyKey(rt.name());
        //                            log.debug("Retrieved key {} for name \"{}\"", pkey, rt.name());
        //                            frel.property(pkey.label(), next.value);
        //                        } else {
        //                            assert next.value instanceof Long;
        //                            EdgeLabel el = (EdgeLabel)vertex.getTypeManager().getEdgeLabel(rt.name());
        //                            log.debug("Retrieved ege label {} for name \"{}\"", el, rt.name());
        //                            frel.setProperty(el, new FaunusVertex(configuration,(Long)next.value));
        //                        }
        //                    }
        //                    for (TitanRelation rel : frel.query().queryAll().relations())
        //                        ((FaunusRelation)rel).setLifeCycle(ElementLifeCycle.Loaded);
        //                }
        //                frel.setLifeCycle(ElementLifeCycle.Loaded);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /*Since we are filtering out system relation types, we might end up with vertices that have no incident relations.
         This is especially true for schema vertices. Those are filtered out.     */
    if (!foundVertexState) {
        log.trace("Vertex {} has unknown lifecycle state", vertexId);
        return null;
    } else if (!tv.edges(Direction.BOTH).hasNext() && !tv.properties().hasNext()) {
        log.trace("Vertex {} has no relations", vertexId);
        return null;
    }
    return tv;
}
Also used : TinkerVertex(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) RelationCache(com.thinkaurelius.titan.graphdb.relations.RelationCache) NoSuchElementException(java.util.NoSuchElementException) Entry(com.thinkaurelius.titan.diskstorage.Entry) RelationReader(com.thinkaurelius.titan.graphdb.database.RelationReader) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) TinkerEdge(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty)

Aggregations

RelationCache (com.thinkaurelius.titan.graphdb.relations.RelationCache)3 LongObjectHashMap (com.carrotsearch.hppc.LongObjectHashMap)1 Entry (com.thinkaurelius.titan.diskstorage.Entry)1 EntryMetaData (com.thinkaurelius.titan.diskstorage.EntryMetaData)1 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)1 RelationReader (com.thinkaurelius.titan.graphdb.database.RelationReader)1 RelationTypeParse (com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.RelationTypeParse)1 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)1 EdgeDirection (com.thinkaurelius.titan.graphdb.relations.EdgeDirection)1 ImplicitKey (com.thinkaurelius.titan.graphdb.types.system.ImplicitKey)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Direction (org.apache.tinkerpop.gremlin.structure.Direction)1 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)1 TinkerEdge (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge)1 TinkerGraph (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph)1 TinkerVertex (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex)1