Search in sources :

Example 36 with StaticBuffer

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

the class EdgeSerializer method getQuery.

public SliceQuery getQuery(InternalRelationType type, Direction dir, TypedInterval[] sortKey) {
    Preconditions.checkNotNull(type);
    Preconditions.checkNotNull(dir);
    Preconditions.checkArgument(type.isUnidirected(Direction.BOTH) || type.isUnidirected(dir));
    StaticBuffer sliceStart = null, sliceEnd = null;
    RelationCategory rt = type.isPropertyKey() ? RelationCategory.PROPERTY : RelationCategory.EDGE;
    if (dir == Direction.BOTH) {
        assert type.isEdgeLabel();
        sliceStart = IDHandler.getRelationType(type.longId(), getDirID(Direction.OUT, rt), type.isInvisibleType());
        sliceEnd = IDHandler.getRelationType(type.longId(), getDirID(Direction.IN, rt), type.isInvisibleType());
        assert sliceStart.compareTo(sliceEnd) < 0;
        sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd);
    } else {
        DirectionID dirID = getDirID(dir, rt);
        DataOutput colStart = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY);
        DataOutput colEnd = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY);
        IDHandler.writeRelationType(colStart, type.longId(), dirID, type.isInvisibleType());
        IDHandler.writeRelationType(colEnd, type.longId(), dirID, type.isInvisibleType());
        long[] sortKeyIDs = type.getSortKey();
        Preconditions.checkArgument(sortKey.length >= sortKeyIDs.length);
        assert colStart.getPosition() == colEnd.getPosition();
        int keyStartPos = colStart.getPosition();
        int keyEndPos = -1;
        for (int i = 0; i < sortKey.length && sortKey[i] != null; i++) {
            PropertyKey skey = sortKey[i].key;
            Interval interval = sortKey[i].interval;
            if (i >= sortKeyIDs.length) {
                assert !type.multiplicity().isUnique(dir);
                assert (skey instanceof ImplicitKey) && (skey == ImplicitKey.TITANID || skey == ImplicitKey.ADJACENT_ID);
                assert skey != ImplicitKey.ADJACENT_ID || (i == sortKeyIDs.length);
                assert skey != ImplicitKey.TITANID || (!type.multiplicity().isConstrained() && (i == sortKeyIDs.length && skey.isPropertyKey() || i == sortKeyIDs.length + 1 && skey.isEdgeLabel()));
                assert colStart.getPosition() == colEnd.getPosition();
                assert interval == null || interval.isPoints();
                keyEndPos = colStart.getPosition();
            } else {
                assert !type.multiplicity().isConstrained();
                assert skey.longId() == sortKeyIDs[i];
            }
            if (interval == null || interval.isEmpty()) {
                break;
            }
            if (interval.isPoints()) {
                if (skey == ImplicitKey.TITANID || skey == ImplicitKey.ADJACENT_ID) {
                    assert !type.multiplicity().isUnique(dir);
                    VariableLong.writePositiveBackward(colStart, (Long) interval.getStart());
                    VariableLong.writePositiveBackward(colEnd, (Long) interval.getEnd());
                } else {
                    writeInline(colStart, skey, interval.getStart(), InlineType.KEY);
                    writeInline(colEnd, skey, interval.getEnd(), InlineType.KEY);
                }
            } else {
                if (interval.getStart() != null)
                    writeInline(colStart, skey, interval.getStart(), InlineType.KEY);
                if (interval.getEnd() != null)
                    writeInline(colEnd, skey, interval.getEnd(), InlineType.KEY);
                switch(type.getSortOrder()) {
                    case ASC:
                        sliceStart = colStart.getStaticBuffer();
                        sliceEnd = colEnd.getStaticBuffer();
                        if (!interval.startInclusive())
                            sliceStart = BufferUtil.nextBiggerBuffer(sliceStart);
                        if (interval.endInclusive())
                            sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd);
                        break;
                    case DESC:
                        sliceEnd = colStart.getStaticBufferFlipBytes(keyStartPos, colStart.getPosition());
                        sliceStart = colEnd.getStaticBufferFlipBytes(keyStartPos, colEnd.getPosition());
                        if (interval.startInclusive())
                            sliceEnd = BufferUtil.nextBiggerBuffer(sliceEnd);
                        if (!interval.endInclusive())
                            sliceStart = BufferUtil.nextBiggerBuffer(sliceStart);
                        break;
                    default:
                        throw new AssertionError(type.getSortOrder().toString());
                }
                assert sliceStart.compareTo(sliceEnd) <= 0;
                break;
            }
        }
        if (sliceStart == null) {
            assert sliceEnd == null && colStart.getPosition() == colEnd.getPosition();
            if (keyEndPos < 0)
                keyEndPos = colStart.getPosition();
            switch(type.getSortOrder()) {
                case ASC:
                    sliceStart = colStart.getStaticBuffer();
                    break;
                case DESC:
                    sliceStart = colStart.getStaticBufferFlipBytes(keyStartPos, keyEndPos);
                    break;
                default:
                    throw new AssertionError(type.getSortOrder().toString());
            }
            sliceEnd = BufferUtil.nextBiggerBuffer(sliceStart);
        }
    }
    return new SliceQuery(sliceStart, sliceEnd);
}
Also used : DataOutput(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) ImplicitKey(com.thinkaurelius.titan.graphdb.types.system.ImplicitKey) SliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery) DirectionID(com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler.DirectionID) Interval(com.thinkaurelius.titan.util.datastructures.Interval)

Example 37 with StaticBuffer

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

the class IndexRemoveJob method process.

@Override
public void process(StaticBuffer key, Map<SliceQuery, EntryList> entries, ScanMetrics metrics) {
    //The queries are already tailored enough => everything should be removed
    try {
        BackendTransaction mutator = writeTx.getTxHandle();
        final List<Entry> deletions;
        if (entries.size() == 1)
            deletions = Iterables.getOnlyElement(entries.values());
        else {
            int size = IteratorUtils.stream(entries.values().iterator()).map(e -> e.size()).reduce(0, (x, y) -> x + y);
            deletions = new ArrayList<>(size);
            entries.values().forEach(e -> deletions.addAll(e));
        }
        metrics.incrementCustom(DELETED_RECORDS_COUNT, deletions.size());
        if (isRelationTypeIndex()) {
            mutator.mutateEdges(key, KCVSCache.NO_ADDITIONS, deletions);
        } else {
            mutator.mutateIndex(key, KCVSCache.NO_ADDITIONS, deletions);
        }
    } catch (final Exception e) {
        mgmt.rollback();
        writeTx.rollback();
        metrics.incrementCustom(FAILED_TX);
        throw new TitanException(e.getMessage(), e);
    }
}
Also used : VertexJobConverter(com.thinkaurelius.titan.graphdb.olap.VertexJobConverter) Configuration(com.thinkaurelius.titan.diskstorage.configuration.Configuration) Iterables(com.google.common.collect.Iterables) StandardTitanTx(com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx) Entry(com.thinkaurelius.titan.diskstorage.Entry) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) IndexSerializer(com.thinkaurelius.titan.graphdb.database.IndexSerializer) ScanJob(com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.ScanJob) SchemaStatus(com.thinkaurelius.titan.core.schema.SchemaStatus) QueryContainer(com.thinkaurelius.titan.graphdb.olap.QueryContainer) TitanException(com.thinkaurelius.titan.core.TitanException) ManagementSystem(com.thinkaurelius.titan.graphdb.database.management.ManagementSystem) ArrayList(java.util.ArrayList) GraphDatabaseConfiguration(com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration) ImmutableList(com.google.common.collect.ImmutableList) EntryList(com.thinkaurelius.titan.diskstorage.EntryList) RelationTypeIndex(com.thinkaurelius.titan.core.schema.RelationTypeIndex) IDManager(com.thinkaurelius.titan.graphdb.idmanagement.IDManager) Map(java.util.Map) KCVSCache(com.thinkaurelius.titan.diskstorage.keycolumnvalue.cache.KCVSCache) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) RelationTypeIndexWrapper(com.thinkaurelius.titan.graphdb.database.management.RelationTypeIndexWrapper) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) BackendTransaction(com.thinkaurelius.titan.diskstorage.BackendTransaction) ConfigOption(com.thinkaurelius.titan.diskstorage.configuration.ConfigOption) Predicate(java.util.function.Predicate) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) CompositeIndexType(com.thinkaurelius.titan.graphdb.types.CompositeIndexType) SliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) IndexType(com.thinkaurelius.titan.graphdb.types.IndexType) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) ConfigElement(com.thinkaurelius.titan.diskstorage.configuration.ConfigElement) ScanMetrics(com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.ScanMetrics) BufferUtil(com.thinkaurelius.titan.diskstorage.util.BufferUtil) Preconditions(com.google.common.base.Preconditions) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) TitanGraph(com.thinkaurelius.titan.core.TitanGraph) Entry(com.thinkaurelius.titan.diskstorage.Entry) TitanException(com.thinkaurelius.titan.core.TitanException) BackendTransaction(com.thinkaurelius.titan.diskstorage.BackendTransaction) TitanException(com.thinkaurelius.titan.core.TitanException)

Example 38 with StaticBuffer

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

the class IDManagementTest method testDirectionPrefix.

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

Example 39 with StaticBuffer

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

the class IDManagementTest method testEdgeTypeWriting.

public void testEdgeTypeWriting(long etid) {
    IDHandler.DirectionID[] dir = IDManager.VertexIDType.EdgeLabel.is(etid) ? new IDHandler.DirectionID[] { IDHandler.DirectionID.EDGE_IN_DIR, IDHandler.DirectionID.EDGE_OUT_DIR } : new IDHandler.DirectionID[] { IDHandler.DirectionID.PROPERTY_DIR };
    RelationCategory relCat = IDManager.VertexIDType.EdgeLabel.is(etid) ? RelationCategory.EDGE : RelationCategory.PROPERTY;
    boolean invisible = IDManager.isSystemRelationTypeId(etid);
    for (IDHandler.DirectionID d : dir) {
        StaticBuffer b = IDHandler.getRelationType(etid, d, invisible);
        IDHandler.RelationTypeParse parse = IDHandler.readRelationType(b.asReadBuffer());
        assertEquals(d, parse.dirID);
        assertEquals(etid, parse.typeId);
    }
}
Also used : RelationCategory(com.thinkaurelius.titan.graphdb.internal.RelationCategory) IDHandler(com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Example 40 with StaticBuffer

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

the class PartitionIDRangeTest method convert.

public static List<KeyRange> convert(long lower, long upper, int bitwidth) {
    StaticBuffer lowerBuffer = BufferUtil.getLongBuffer(convert(lower, bitwidth));
    StaticBuffer upperBuffer = BufferUtil.getLongBuffer(convert(upper, bitwidth));
    //        Preconditions.checkArgument(lowerBuffer.compareTo(upperBuffer) < 0, "%s vs %s",lowerBuffer,upperBuffer);
    return Lists.newArrayList(new KeyRange(lowerBuffer, upperBuffer));
}
Also used : KeyRange(com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Aggregations

StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)44 Entry (com.thinkaurelius.titan.diskstorage.Entry)13 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)9 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)8 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)7 Map (java.util.Map)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 Instant (java.time.Instant)6 HashMap (java.util.HashMap)6 KeyRange (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange)5 BiMap (com.google.common.collect.BiMap)4 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)4 EntryList (com.thinkaurelius.titan.diskstorage.EntryList)4 KeySliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeySliceQuery)4 SliceQuery (com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)4 Delete (org.apache.hadoop.hbase.client.Delete)4 Put (org.apache.hadoop.hbase.client.Put)4 Pair (org.apache.hadoop.hbase.util.Pair)4 ImmutableList (com.google.common.collect.ImmutableList)3