Search in sources :

Example 66 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class TransactionLogHeader method parse.

public static Entry parse(StaticBuffer buffer, Serializer serializer, TimestampProvider times) {
    ReadBuffer read = buffer.asReadBuffer();
    Instant txTimestamp = times.getTime(read.getLong());
    TransactionLogHeader header = new TransactionLogHeader(VariableLong.readPositive(read), txTimestamp, times);
    LogTxStatus status = serializer.readObjectNotNull(read, LogTxStatus.class);
    EnumMap<LogTxMeta, Object> metadata = new EnumMap<LogTxMeta, Object>(LogTxMeta.class);
    int metaSize = VariableLong.unsignedByte(read.getByte());
    for (int i = 0; i < metaSize; i++) {
        LogTxMeta meta = LogTxMeta.values()[VariableLong.unsignedByte(read.getByte())];
        metadata.put(meta, serializer.readObjectNotNull(read, meta.dataType()));
    }
    if (read.hasRemaining()) {
        StaticBuffer content = read.subrange(read.getPosition(), read.length() - read.getPosition());
        return new Entry(header, content, status, metadata);
    } else {
        return new Entry(header, null, status, metadata);
    }
}
Also used : ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) Instant(java.time.Instant) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Example 67 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class IDManagementTest method testDirectionPrefix.

@Test
public void testDirectionPrefix() {
    for (RelationType type : RelationType.values()) {
        StaticBuffer[] bounds = IDHandler.getBounds(type);
        assertEquals(1, bounds[0].length());
        assertEquals(1, bounds[1].length());
        assertTrue(bounds[0].compareTo(bounds[1]) < 0);
    }
}
Also used : RelationType(com.thinkaurelius.titan.graphdb.internal.RelationType) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Test(org.junit.Test)

Example 68 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer 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 69 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class KCVSConfiguration method getConfigurationProperty.

/**
 * Reads the configuration property for this StoreManager
 *
 * @param key Key identifying the configuration property
 * @return Value stored for the key or null if the configuration property has not (yet) been defined.
 * @throws StorageException
 */
public String getConfigurationProperty(final String key) throws StorageException {
    StaticBuffer column = string2StaticBuffer(key);
    final KeySliceQuery query = new KeySliceQuery(rowKey, column, ByteBufferUtil.nextBiggerBuffer(column), false);
    StaticBuffer result = BackendOperation.execute(new Callable<StaticBuffer>() {

        @Override
        public StaticBuffer call() throws Exception {
            StoreTransaction txh = null;
            try {
                txh = manager.beginTransaction(new StoreTxConfig(ConsistencyLevel.KEY_CONSISTENT));
                List<Entry> entries = store.getSlice(query, txh);
                if (entries.isEmpty())
                    return null;
                return entries.get(0).getValue();
            } finally {
                if (txh != null)
                    txh.commit();
            }
        }

        @Override
        public String toString() {
            return "getConfiguration";
        }
    }, maxOperationWaitTime);
    if (result == null)
        return null;
    return staticBuffer2String(result);
}
Also used : StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) StorageException(com.thinkaurelius.titan.diskstorage.StorageException)

Example 70 with StaticBuffer

use of com.thinkaurelius.titan.diskstorage.StaticBuffer in project titan by thinkaurelius.

the class StandardTitanGraph method persist.

private <V extends InternalVertex> List<StaticBuffer> persist(ListMultimap<V, InternalRelation> mutatedEdges, StandardTitanTx tx) throws StorageException {
    assert mutatedEdges != null && !mutatedEdges.isEmpty();
    Collection<V> vertices = mutatedEdges.keySet();
    BackendTransaction mutator = tx.getTxHandle();
    List<StaticBuffer> mutatedKeys = new ArrayList<StaticBuffer>(vertices.size());
    for (V vertex : vertices) {
        Preconditions.checkArgument(vertex.getID() > 0, "Vertex has no id: %s", vertex.getID());
        List<InternalRelation> edges = mutatedEdges.get(vertex);
        List<Entry> additions = new ArrayList<Entry>(edges.size());
        List<StaticBuffer> deletions = new ArrayList<StaticBuffer>(Math.max(10, edges.size() / 10));
        for (InternalRelation edge : edges) {
            for (int pos = 0; pos < edge.getLen(); pos++) {
                if (edge.getVertex(pos).equals(vertex)) {
                    if (edge.isRemoved()) {
                        deletions.add(edgeSerializer.writeRelation(edge, pos, tx).getColumn());
                    } else {
                        Preconditions.checkArgument(edge.isNew());
                        additions.add(edgeSerializer.writeRelation(edge, pos, tx));
                    }
                }
            }
        }
        StaticBuffer vertexKey = IDHandler.getKey(vertex.getID());
        mutator.mutateEdges(vertexKey, additions, deletions);
        if (!vertex.isNew())
            mutatedKeys.add(vertexKey);
        // Index Updates
        for (InternalRelation relation : edges) {
            if (relation.getVertex(0).equals(vertex)) {
                if (relation.isRemoved()) {
                    if (relation.isProperty()) {
                        indexSerializer.removeProperty((TitanProperty) relation, mutator);
                    } else if (relation.isEdge()) {
                        indexSerializer.removeEdge(relation, mutator);
                    }
                } else {
                    Preconditions.checkArgument(relation.isNew());
                    if (relation.isProperty()) {
                        indexSerializer.addProperty((TitanProperty) relation, mutator);
                    } else {
                        indexSerializer.addEdge(relation, mutator);
                    }
                }
            }
        }
    }
    return mutatedKeys;
}
Also used : LongArrayList(com.carrotsearch.hppc.LongArrayList) ArrayList(java.util.ArrayList) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) BackendTransaction(com.thinkaurelius.titan.diskstorage.BackendTransaction)

Aggregations

StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)92 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)16 StaticBufferEntry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.StaticBufferEntry)15 Entry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.Entry)14 TemporaryStorageException (com.thinkaurelius.titan.diskstorage.TemporaryStorageException)13 ConsistentKeyLockStatus (com.thinkaurelius.titan.diskstorage.locking.consistentkey.ConsistentKeyLockStatus)13 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)12 HashMap (java.util.HashMap)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 Map (java.util.Map)11 Entry (com.thinkaurelius.titan.diskstorage.Entry)10 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)10 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)9 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)8 KeySliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)7 KCVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KCVMutation)6 KeyRange (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange)6 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)6 Instant (java.time.Instant)6