Search in sources :

Example 6 with TinkerGraph

use of org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph in project janusgraph by JanusGraph.

the class JanusGraphVertexDeserializer 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();
    TinkerVertex tv = null;
    // Iterate over edgestore columns to find the vertex's label relation
    for (final Entry data : entries) {
        RelationReader relationReader = setup.getRelationReader();
        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 = getOrCreateVertex(vertexId, vl.name(), tg);
        } else if (systemTypes.isTypeSystemType(relation.typeId)) {
            log.trace("Vertex {} is a system vertex", vertexId);
            return null;
        }
    }
    // Added this following testing
    if (null == tv) {
        tv = getOrCreateVertex(vertexId, null, tg);
    }
    Preconditions.checkNotNull(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();
            final RelationCache relation = relationReader.parseRelation(data, false, typeManager);
            // 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());
                VertexProperty<Object> vp = tv.property(card, type.name(), value, T.id, relation.relationId);
                // Decode meta properties
                decodeProperties(relation, vp);
            } 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;
                // We don't know the label of the other vertex, but one must be provided
                TinkerVertex adjacentVertex = getOrCreateVertex(relation.getOtherVertexId(), null, tg);
                // skip self-loop edges that were already processed, but from a different direction
                if (tv.equals(adjacentVertex) && edgeExists(tv, type, relation)) {
                    continue;
                }
                if (relation.direction.equals(Direction.IN)) {
                    te = (TinkerEdge) adjacentVertex.addEdge(type.name(), tv, T.id, relation.relationId);
                } else if (relation.direction.equals(Direction.OUT)) {
                    te = (TinkerEdge) tv.addEdge(type.name(), adjacentVertex, T.id, relation.relationId);
                } else {
                    throw new RuntimeException("Direction.BOTH is not supported");
                }
                decodeProperties(relation, te);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return tv;
}
Also used : TinkerVertex(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) RelationCache(org.janusgraph.graphdb.relations.RelationCache) NoSuchElementException(java.util.NoSuchElementException) Entry(org.janusgraph.diskstorage.Entry) VertexLabel(org.janusgraph.core.VertexLabel) RelationReader(org.janusgraph.graphdb.database.RelationReader) RelationType(org.janusgraph.core.RelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) InternalRelationType(org.janusgraph.graphdb.internal.InternalRelationType) TinkerEdge(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty)

Example 7 with TinkerGraph

use of org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph in project janusgraph by JanusGraph.

the class TinkerGraphApp method createSchema.

@Override
public void createSchema() {
    LOGGER.info("creating schema");
    final TinkerGraph tinkerGraph = (TinkerGraph) graph;
    // naive check if the schema was previously created
    if (!tinkerGraph.getIndexedKeys(Vertex.class).iterator().hasNext()) {
        tinkerGraph.createIndex("name", Vertex.class);
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph)

Aggregations

TinkerGraph (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph)7 NoSuchElementException (java.util.NoSuchElementException)2 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)2 TinkerEdge (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge)2 TinkerVertex (org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex)2 Test (org.junit.Test)2 GraknTx (ai.grakn.GraknTx)1 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)1 GraknTxTinker (ai.grakn.kb.internal.GraknTxTinker)1 Entry (com.thinkaurelius.titan.diskstorage.Entry)1 RelationReader (com.thinkaurelius.titan.graphdb.database.RelationReader)1 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)1 RelationCache (com.thinkaurelius.titan.graphdb.relations.RelationCache)1 Map (java.util.Map)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1 RelationType (org.janusgraph.core.RelationType)1 VertexLabel (org.janusgraph.core.VertexLabel)1 Entry (org.janusgraph.diskstorage.Entry)1 RelationReader (org.janusgraph.graphdb.database.RelationReader)1