Search in sources :

Example 56 with StaticBuffer

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

the class HadoopScanMapper method finishSetup.

protected void finishSetup(ModifiableHadoopConfiguration scanConf, Configuration graphConf) {
    jobConf = getJobConfiguration(scanConf);
    Preconditions.checkNotNull(metrics);
    // Allowed to be null for jobs that specify no configuration and no configuration root
    //Preconditions.checkNotNull(jobConf);
    Preconditions.checkNotNull(job);
    job.workerIterationStart(jobConf, graphConf, metrics);
    keyFilter = job.getKeyFilter();
    List<SliceQuery> sliceQueries = job.getQueries();
    Preconditions.checkArgument(null != sliceQueries, "Job cannot specify null query list");
    Preconditions.checkArgument(0 < sliceQueries.size(), "Job must specify at least one query");
    // Assign head of getQueries() to "initialQuery"
    initialQuery = sliceQueries.get(0);
    // Assign tail of getQueries() to "subsequentQueries"
    subsequentQueries = new ArrayList<>(sliceQueries.subList(1, sliceQueries.size()));
    Preconditions.checkState(sliceQueries.size() == subsequentQueries.size() + 1);
    Preconditions.checkNotNull(initialQuery);
    if (0 < subsequentQueries.size()) {
        //It is assumed that the first query is the grounding query if multiple queries exist
        StaticBuffer start = initialQuery.getSliceStart();
        Preconditions.checkArgument(start.equals(BufferUtil.zeroBuffer(1)), "Expected start of first query to be all 0s: %s", start);
        StaticBuffer end = initialQuery.getSliceEnd();
        Preconditions.checkArgument(end.equals(BufferUtil.oneBuffer(end.length())), "Expected end of first query to be all 1s: %s", end);
    }
}
Also used : StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) SliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery)

Example 57 with StaticBuffer

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

the class HadoopScanMapper method findEntriesMatchingQuery.

private EntryList findEntriesMatchingQuery(SliceQuery query, EntryList sortedEntries) {
    // Inclusive
    int lowestStartMatch = sortedEntries.size();
    // Inclusive
    int highestEndMatch = -1;
    final StaticBuffer queryStart = query.getSliceStart();
    final StaticBuffer queryEnd = query.getSliceEnd();
    // Find the lowest matchStart s.t. query.getSliceStart <= sortedEntries.get(matchStart)
    int low = 0;
    int high = sortedEntries.size() - 1;
    while (low <= high) {
        int mid = (low + high) >>> 1;
        Entry midVal = sortedEntries.get(mid);
        int cmpStart = queryStart.compareTo(midVal.getColumn());
        if (0 < cmpStart) {
            // query lower bound exceeds entry (no match)
            if (lowestStartMatch == mid + 1) {
                // lowestStartMatch located
                break;
            }
            // Move to higher list index
            low = mid + 1;
        } else /* (0 >= cmpStart) */
        {
            // entry equals or exceeds query lower bound (match, but not necessarily lowest match)
            if (mid < lowestStartMatch) {
                lowestStartMatch = mid;
            }
            // Move to a lower list index
            high = mid - 1;
        }
    }
    // so we can bypass the highestEndMatch search and just return an empty result.
    if (sortedEntries.size() == lowestStartMatch) {
        return EntryList.EMPTY_LIST;
    }
    // Find the highest matchEnd s.t. sortedEntries.get(matchEnd) < query.getSliceEnd
    low = 0;
    high = sortedEntries.size() - 1;
    while (low <= high) {
        int mid = (low + high) >>> 1;
        Entry midVal = sortedEntries.get(mid);
        int cmpEnd = queryEnd.compareTo(midVal.getColumn());
        if (0 < cmpEnd) {
            // query upper bound exceeds entry (match, not necessarily highest)
            if (mid > highestEndMatch) {
                highestEndMatch = mid;
            }
            // Move to higher list index
            low = mid + 1;
        } else /* (0 >= cmpEnd) */
        {
            // entry equals or exceeds query upper bound (no match)
            if (highestEndMatch == mid - 1) {
                // highestEndMatch located
                break;
            }
            // Move to a lower list index
            high = mid - 1;
        }
    }
    if (0 <= highestEndMatch - lowestStartMatch) {
        // Return sublist between indices (inclusive at both indices)
        // This will be passed into subList, which interprets it exclusively
        int endIndex = highestEndMatch + 1;
        if (query.hasLimit()) {
            endIndex = Math.min(endIndex, query.getLimit() + lowestStartMatch);
        }
        // TODO avoid unnecessary copy here
        return EntryArrayList.of(sortedEntries.subList(lowestStartMatch, endIndex));
    } else {
        return EntryList.EMPTY_LIST;
    }
}
Also used : Entry(com.thinkaurelius.titan.diskstorage.Entry) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Example 58 with StaticBuffer

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

the class LogTest method testFuzzMessagesSerial.

@Test
public void testFuzzMessagesSerial() throws Exception {
    final int maxLen = 1024 * 4;
    final int rounds = 32;
    StoringReader reader = new StoringReader(rounds);
    List<StaticBuffer> expected = new ArrayList<StaticBuffer>(rounds);
    Log l = manager.openLog("fuzz");
    l.registerReader(ReadMarker.fromNow(), reader);
    Random rand = new Random();
    for (int i = 0; i < rounds; i++) {
        //int len = rand.nextInt(maxLen + 1);
        int len = maxLen;
        if (0 == len)
            // 0 would throw IllegalArgumentException
            len = 1;
        byte[] raw = new byte[len];
        rand.nextBytes(raw);
        StaticBuffer sb = StaticArrayBuffer.of(raw);
        l.add(sb);
        expected.add(sb);
        Thread.sleep(50L);
    }
    reader.await(TIMEOUT_MS);
    assertEquals(rounds, reader.msgCount);
    assertEquals(expected, reader.msgs);
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer)

Example 59 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 60 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)

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