// Path: titan-core/src/main/java/com/thinkaurelius/titan/graphdb/relations/RelationCache.java
// public class RelationCache implements Iterable<LongObjectCursor<Object>> {
// 
//     private static final LongObjectHashMap<Object> EMPTY = new LongObjectHashMap<>(0);
// 
//     public final Direction direction;
//     public final long typeId;
//     public final long relationId;
//     private final Object other;
//     private final LongObjectHashMap<Object> properties;
// 
//     public RelationCache(final Direction direction, final long typeId, final long relationId,
//                          final Object other, final LongObjectHashMap<Object> properties) {
//         this.direction = direction;
//         this.typeId = typeId;
//         this.relationId = relationId;
//         this.other = other;
//         this.properties = (properties == null || properties.size() > 0) ? properties : EMPTY;
//     }
// 
//     public RelationCache(final Direction direction, final long typeId, final long relationId,
//                          final Object other) {
//         this(direction,typeId,relationId,other,null);
//     }
// 
//     @SuppressWarnings("unchecked")
//     public <O> O get(long key) {
//         return (O) properties.get(key);
//     }
// 
//     public boolean hasProperties() {
//         return properties != null && !properties.isEmpty();
//     }
// 
//     public int numProperties() {
//         return properties.size();
//     }
// 
//     public Object getValue() {
//         return other;
//     }
// 
//     public Long getOtherVertexId() {
//         return (Long) other;
//     }
// 
//     public Iterator<LongObjectCursor<Object>> propertyIterator() {
//         return properties.iterator();
//     }
// 
//     @Override
//     public Iterator<LongObjectCursor<Object>> iterator() {
//         return propertyIterator();
//     }
// 
//     @Override
//     public String toString() {
//          return typeId + "-" + direction + "->" + other + ":" + relationId;
//     }
// 
// }


import com.google.common.base.Preconditions;
import com.thinkaurelius.titan.core.EdgeLabel;
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.TitanRelation;
import com.thinkaurelius.titan.diskstorage.Entry;
import com.thinkaurelius.titan.graphdb.database.EdgeSerializer;
import com.thinkaurelius.titan.graphdb.internal.InternalRelation;
import com.thinkaurelius.titan.graphdb.internal.InternalRelationType;
import com.thinkaurelius.titan.graphdb.internal.InternalVertex;
import com.thinkaurelius.titan.graphdb.relations.CacheEdge;
import com.thinkaurelius.titan.graphdb.relations.CacheVertexProperty;
import com.thinkaurelius.titan.graphdb.relations.RelationCache;
import com.thinkaurelius.titan.graphdb.types.TypeInspector;
import com.thinkaurelius.titan.graphdb.types.TypeUtil;
import org.apache.tinkerpop.gremlin.structure.Direction;
import java.util.Iterator;

                    @Override
                    public void remove() {
                        Preconditions.checkState(current!=null);
                        current.remove();
                    }
                };
            }
        };
    }

    public static InternalRelation readRelation(final InternalVertex vertex, final Entry data, final StandardTitanTx tx) {
        RelationCache relation = tx.getEdgeSerializer().readRelation(data, true, tx);
        return readRelation(vertex,relation,data,tx,tx);
    }

    public static InternalRelation readRelation(final InternalVertex vertex, final Entry data,
                                                final EdgeSerializer serializer, final TypeInspector types,
                                                final VertexFactory vertexFac) {
        RelationCache relation = serializer.readRelation(data, true, types);
        return readRelation(vertex,relation,data,types,vertexFac);
    }


    private static InternalRelation readRelation(final InternalVertex vertex, final RelationCache relation,
                                         final Entry data, final TypeInspector types, final VertexFactory vertexFac) {
        InternalRelationType type = TypeUtil.getBaseType((InternalRelationType) types.getExistingRelationType(relation.typeId));

        if (type.isPropertyKey()) {
            assert relation.direction == Direction.OUT;
// Path: titan-core/src/main/java/com/thinkaurelius/titan/graphdb/relations/RelationCache.java
// public class RelationCache implements Iterable<LongObjectCursor<Object>> {
// 
//     private static final LongObjectHashMap<Object> EMPTY = new LongObjectHashMap<>(0);
// 
//     public final Direction direction;
//     public final long typeId;
//     public final long relationId;
//     private final Object other;
//     private final LongObjectHashMap<Object> properties;
// 
//     public RelationCache(final Direction direction, final long typeId, final long relationId,
//                          final Object other, final LongObjectHashMap<Object> properties) {
//         this.direction = direction;
//         this.typeId = typeId;
//         this.relationId = relationId;
//         this.other = other;
//         this.properties = (properties == null || properties.size() > 0) ? properties : EMPTY;
//     }
// 
//     public RelationCache(final Direction direction, final long typeId, final long relationId,
//                          final Object other) {
//         this(direction,typeId,relationId,other,null);
//     }
// 
//     @SuppressWarnings("unchecked")
//     public <O> O get(long key) {
//         return (O) properties.get(key);
//     }
// 
//     public boolean hasProperties() {
//         return properties != null && !properties.isEmpty();
//     }
// 
//     public int numProperties() {
//         return properties.size();
//     }
// 
//     public Object getValue() {
//         return other;
//     }
// 
//     public Long getOtherVertexId() {
//         return (Long) other;
//     }
// 
//     public Iterator<LongObjectCursor<Object>> propertyIterator() {
//         return properties.iterator();
//     }
// 
//     @Override
//     public Iterator<LongObjectCursor<Object>> iterator() {
//         return propertyIterator();
//     }
// 
//     @Override
//     public String toString() {
//          return typeId + "-" + direction + "->" + other + ":" + relationId;
//     }
// 
// }


// Path: titan-core/src/main/java/com/thinkaurelius/titan/graphdb/transaction/RelationConstructor.java
import com.google.common.base.Preconditions;
import com.thinkaurelius.titan.core.EdgeLabel;
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.TitanRelation;
import com.thinkaurelius.titan.diskstorage.Entry;
import com.thinkaurelius.titan.graphdb.database.EdgeSerializer;
import com.thinkaurelius.titan.graphdb.internal.InternalRelation;
import com.thinkaurelius.titan.graphdb.internal.InternalRelationType;
import com.thinkaurelius.titan.graphdb.internal.InternalVertex;
import com.thinkaurelius.titan.graphdb.relations.CacheEdge;
import com.thinkaurelius.titan.graphdb.relations.CacheVertexProperty;
import com.thinkaurelius.titan.graphdb.relations.RelationCache;
import com.thinkaurelius.titan.graphdb.types.TypeInspector;
import com.thinkaurelius.titan.graphdb.types.TypeUtil;
import org.apache.tinkerpop.gremlin.structure.Direction;
import java.util.Iterator;


                    @Override
                    public void remove() {
                        Preconditions.checkState(current!=null);
                        current.remove();
                    }
                };
            }
        };
    }

    public static InternalRelation readRelation(final InternalVertex vertex, final Entry data, final StandardTitanTx tx) {
        RelationCache relation = tx.getEdgeSerializer().readRelation(data, true, tx);
        return readRelation(vertex,relation,data,tx,tx);
    }

    public static InternalRelation readRelation(final InternalVertex vertex, final Entry data,
                                                final EdgeSerializer serializer, final TypeInspector types,
                                                final VertexFactory vertexFac) {
        RelationCache relation = serializer.readRelation(data, true, types);
        return readRelation(vertex,relation,data,types,vertexFac);
    }


    private static InternalRelation readRelation(final InternalVertex vertex, final RelationCache relation,
                                         final Entry data, final TypeInspector types, final VertexFactory vertexFac) {
        InternalRelationType type = TypeUtil.getBaseType((InternalRelationType) types.getExistingRelationType(relation.typeId));

        if (type.isPropertyKey()) {
            assert relation.direction == Direction.OUT;
            return new CacheVertexProperty(relation.relationId, (PropertyKey) type, vertex, relation.getValue(), data);