Search in sources :

Example 6 with InternalType

use of com.thinkaurelius.titan.graphdb.internal.InternalType in project titan by thinkaurelius.

the class EdgeSerializer method writeRelation.

public Entry writeRelation(InternalRelation relation, int position, StandardTitanTx tx) {
    Preconditions.checkArgument(position < relation.getLen());
    TitanType type = relation.getType();
    long typeid = type.getID();
    Direction dir = EdgeDirection.fromPosition(position);
    int dirID = getDirID(dir, relation.isProperty() ? RelationType.PROPERTY : RelationType.EDGE);
    DataOutput colOut = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY, true);
    IDHandler.writeEdgeType(colOut, typeid, dirID);
    InternalType definition = (InternalType) type;
    long[] sortKey = definition.getSortKey();
    int startPosition = colOut.getPosition();
    if (!type.isUnique(dir)) {
        writeInlineTypes(sortKey, relation, colOut, tx);
    }
    int endPosition = colOut.getPosition();
    DataOutput writer = colOut;
    long vertexIdDiff = 0;
    long relationIdDiff = relation.getID() - relation.getVertex(position).getID();
    if (relation.isEdge())
        vertexIdDiff = relation.getVertex((position + 1) % 2).getID() - relation.getVertex(position).getID();
    if (type.isUnique(dir)) {
        writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
        if (relation.isEdge())
            VariableLong.write(writer, vertexIdDiff);
        VariableLong.write(writer, relationIdDiff);
    } else {
        if (relation.isEdge())
            VariableLong.writeBackward(writer, vertexIdDiff);
        VariableLong.writeBackward(writer, relationIdDiff);
    }
    if (!type.isUnique(dir)) {
        writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
    }
    if (relation.isProperty()) {
        Preconditions.checkArgument(relation.isProperty());
        Object value = ((TitanProperty) relation).getValue();
        Preconditions.checkNotNull(value);
        TitanKey key = (TitanKey) type;
        assert key.getDataType().isInstance(value);
        if (hasGenericDataType(key)) {
            writer.writeClassAndObject(value);
        } else {
            writer.writeObjectNotNull(value);
        }
    }
    // Write signature & sort key if unique
    if (type.isUnique(dir)) {
        writeInlineTypes(sortKey, relation, writer, tx);
    }
    long[] signature = definition.getSignature();
    writeInlineTypes(signature, relation, writer, tx);
    // Write remaining properties
    LongSet writtenTypes = new LongOpenHashSet(sortKey.length + signature.length);
    if (sortKey.length > 0 || signature.length > 0) {
        for (long id : sortKey) writtenTypes.add(id);
        for (long id : signature) writtenTypes.add(id);
    }
    LongArrayList remainingTypes = new LongArrayList(8);
    for (TitanType t : relation.getPropertyKeysDirect()) {
        if (!writtenTypes.contains(t.getID())) {
            remainingTypes.add(t.getID());
        }
    }
    // Sort types before writing to ensure that value is always written the same way
    long[] remaining = remainingTypes.toArray();
    Arrays.sort(remaining);
    for (long tid : remaining) {
        TitanType t = tx.getExistingType(tid);
        writeInline(writer, t, relation.getProperty(t), true);
    }
    StaticBuffer column = ((InternalType) type).getSortOrder() == Order.DESC ? colOut.getStaticBufferFlipBytes(startPosition, endPosition) : colOut.getStaticBuffer();
    return new StaticBufferEntry(column, writer.getStaticBuffer());
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) LongArrayList(com.carrotsearch.hppc.LongArrayList) LongSet(com.carrotsearch.hppc.LongSet) EdgeDirection(com.thinkaurelius.titan.graphdb.relations.EdgeDirection) Direction(com.tinkerpop.blueprints.Direction) InternalType(com.thinkaurelius.titan.graphdb.internal.InternalType) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) LongOpenHashSet(com.carrotsearch.hppc.LongOpenHashSet) StaticBufferEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry)

Example 7 with InternalType

use of com.thinkaurelius.titan.graphdb.internal.InternalType in project titan by thinkaurelius.

the class IndexSerializer method processSingleCondition.

private List<Object> processSingleCondition(ElementType resultType, PredicateCondition pc, final int limit, BackendTransaction tx) {
    Preconditions.checkArgument(resultType == ElementType.EDGE || resultType == ElementType.VERTEX);
    Preconditions.checkArgument(pc.getPredicate() == Cmp.EQUAL, "Only equality index retrievals are supported on standard index");
    Preconditions.checkNotNull(pc.getValue());
    Preconditions.checkArgument(limit >= 0);
    TitanKey key = (TitanKey) pc.getKey();
    Preconditions.checkArgument(key.hasIndex(Titan.Token.STANDARD_INDEX, resultType.getElementType()), "Cannot retrieve for given property key - it does not have an index [%s]", key.getName());
    Object value = pc.getValue();
    StaticBuffer column = getUniqueIndexColumn(key);
    KeySliceQuery sq = new KeySliceQuery(getIndexKey(value), column, SliceQuery.pointRange(column), ((InternalType) key).isStatic(Direction.IN)).setLimit(limit);
    List<Entry> r;
    if (resultType == ElementType.VERTEX) {
        r = tx.vertexIndexQuery(sq);
    } else {
        r = tx.edgeIndexQuery(sq);
    }
    List<Object> results = new ArrayList<Object>(r.size());
    for (Entry entry : r) {
        ReadBuffer entryValue = entry.getReadValue();
        if (resultType == ElementType.VERTEX) {
            results.add(VariableLong.readPositive(entryValue));
        } else {
            results.add(bytebuffer2RelationId(entryValue));
        }
    }
    Preconditions.checkArgument(!(resultType == ElementType.VERTEX && key.isUnique(Direction.IN)) || results.size() <= 1);
    return results;
}
Also used : InternalType(com.thinkaurelius.titan.graphdb.internal.InternalType) StaticBufferEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry) Entry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry) KeySliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)

Aggregations

InternalType (com.thinkaurelius.titan.graphdb.internal.InternalType)7 Direction (com.tinkerpop.blueprints.Direction)5 EdgeDirection (com.thinkaurelius.titan.graphdb.relations.EdgeDirection)3 LongArrayList (com.carrotsearch.hppc.LongArrayList)2 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)2 StaticBufferEntry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry)2 EdgeSerializer (com.thinkaurelius.titan.graphdb.database.EdgeSerializer)2 Interval (com.thinkaurelius.titan.util.datastructures.Interval)2 PointInterval (com.thinkaurelius.titan.util.datastructures.PointInterval)2 ProperInterval (com.thinkaurelius.titan.util.datastructures.ProperInterval)2 LongObjectOpenHashMap (com.carrotsearch.hppc.LongObjectOpenHashMap)1 LongOpenHashSet (com.carrotsearch.hppc.LongOpenHashSet)1 LongSet (com.carrotsearch.hppc.LongSet)1 Cmp (com.thinkaurelius.titan.core.attribute.Cmp)1 BackendTransaction (com.thinkaurelius.titan.diskstorage.BackendTransaction)1 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)1 Entry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry)1 KeySliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)1 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)1 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)1