Java Code Examples for org.apache.cassandra.utils.ByteBufferUtil#readWithShortLength()

The following examples show how to use org.apache.cassandra.utils.ByteBufferUtil#readWithShortLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrepareResponse.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public PrepareResponse deserialize(DataInput in, int version) throws IOException
{
    boolean success = in.readBoolean();
    ByteBuffer key = ByteBufferUtil.readWithShortLength(in);
    return new PrepareResponse(success,
                               new Commit(key,
                                          UUIDSerializer.serializer.deserialize(in, version),
                                          ColumnFamily.serializer.deserialize(in,
                                                                              ArrayBackedSortedColumns.factory,
                                                                              ColumnSerializer.Flag.LOCAL, version)),
                               new Commit(key,
                                          UUIDSerializer.serializer.deserialize(in, version),
                                          ColumnFamily.serializer.deserialize(in,
                                                                              ArrayBackedSortedColumns.factory,
                                                                              ColumnSerializer.Flag.LOCAL, version)));
}
 
Example 2
Source File: PagingState.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public static PagingState deserialize(ByteBuffer bytes)
{
    if (bytes == null)
        return null;

    try
    {
        DataInputStream in = new DataInputStream(ByteBufferUtil.inputStream(bytes));
        ByteBuffer pk = ByteBufferUtil.readWithShortLength(in);
        ByteBuffer cn = ByteBufferUtil.readWithShortLength(in);
        int remaining = in.readInt();
        return new PagingState(pk, cn, remaining);
    }
    catch (IOException e)
    {
        throw new ProtocolException("Invalid value for the paging state");
    }
}
 
Example 3
Source File: SSTableRowRecordReader.java    From hadoop-sstable with Apache License 2.0 6 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {

    if (!hasMore()) {
        return false;
    }

    // Read the key and set it.
    final ByteBuffer keyBytes = ByteBufferUtil.readWithShortLength(getReader());
    setCurrentKey(keyBytes);

    // Read the data size.
    // TODO: may have to take into account the fact that files can support long or int depending on Cassandra version.
    final long dataSize = getReader().readLong();

    // Read the value and set it.
    final SSTableIdentityIterator ssTableIdentityIterator = getIdentityIterator(keyBytes, dataSize);
    setCurrentValue(ssTableIdentityIterator);

    return true;
}
 
Example 4
Source File: Commit.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public Commit deserialize(DataInput in, int version) throws IOException
{
    return new Commit(ByteBufferUtil.readWithShortLength(in),
                      UUIDSerializer.serializer.deserialize(in, version),
                      ColumnFamily.serializer.deserialize(in,
                                                          ArrayBackedSortedColumns.factory,
                                                          ColumnSerializer.Flag.LOCAL,
                                                          version));
}
 
Example 5
Source File: SuperColumns.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public OnDiskAtom next()
{
    try
    {
        if (subColumnsIterator != null && subColumnsIterator.hasNext())
        {
            Cell c = subColumnsIterator.next();
            return c.withUpdatedName(type.makeCellName(scName, c.name().toByteBuffer()));
        }

        // Read one more super column
        ++read;

        scName = ByteBufferUtil.readWithShortLength(in);
        DeletionInfo delInfo = new DeletionInfo(DeletionTime.serializer.deserialize(in));

        /* read the number of columns */
        int size = in.readInt();
        List<Cell> subCells = new ArrayList<>(size);

        ColumnSerializer colSer = subType(type).columnSerializer();
        for (int i = 0; i < size; ++i)
            subCells.add(colSer.deserialize(in, flag, expireBefore));

        subColumnsIterator = subCells.iterator();

        // If the SC was deleted, return that first, otherwise return the first subcolumn
        DeletionTime dtime = delInfo.getTopLevelDeletion();
        if (!dtime.equals(DeletionTime.LIVE))
            return new RangeTombstone(startOf(scName), endOf(scName), dtime);

        return next();
    }
    catch (IOException e)
    {
        throw new IOError(e);
    }
}
 
Example 6
Source File: SliceFromReadCommand.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ReadCommand deserialize(DataInput in, int version) throws IOException
{
    boolean isDigest = in.readBoolean();
    String keyspaceName = in.readUTF();
    ByteBuffer key = ByteBufferUtil.readWithShortLength(in);
    String cfName = in.readUTF();
    long timestamp = in.readLong();
    CFMetaData metadata = Schema.instance.getCFMetaData(keyspaceName, cfName);
    SliceQueryFilter filter = metadata.comparator.sliceQueryFilterSerializer().deserialize(in, version);
    return new SliceFromReadCommand(keyspaceName, key, cfName, timestamp, filter).setIsDigestQuery(isDigest);
}
 
Example 7
Source File: RowPosition.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public RowPosition deserialize(DataInput in) throws IOException
{
    Kind kind = Kind.fromOrdinal(in.readByte());
    if (kind == Kind.ROW_KEY)
    {
        ByteBuffer k = ByteBufferUtil.readWithShortLength(in);
        return StorageService.getPartitioner().decorateKey(k);
    }
    else
    {
        Token t = Token.serializer.deserialize(in);
        return kind == Kind.MIN_BOUND ? t.minKeyBound() : t.maxKeyBound();
    }
}
 
Example 8
Source File: SliceByNamesReadCommand.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ReadCommand deserialize(DataInput in, int version) throws IOException
{
    boolean isDigest = in.readBoolean();
    String keyspaceName = in.readUTF();
    ByteBuffer key = ByteBufferUtil.readWithShortLength(in);
    String cfName = in.readUTF();
    long timestamp = in.readLong();
    CFMetaData metadata = Schema.instance.getCFMetaData(keyspaceName, cfName);
    NamesQueryFilter filter = metadata.comparator.namesQueryFilterSerializer().deserialize(in, version);
    return new SliceByNamesReadCommand(keyspaceName, key, cfName, timestamp, filter).setIsDigestQuery(isDigest);
}
 
Example 9
Source File: SSTableReader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Finds and returns the first key beyond a given token in this SSTable or null if no such key exists.
 */
public DecoratedKey firstKeyBeyond(RowPosition token)
{
    if (token.compareTo(first) < 0)
        return first;

    long sampledPosition = getIndexScanPosition(token);

    Iterator<FileDataInput> segments = ifile.iterator(sampledPosition);
    while (segments.hasNext())
    {
        FileDataInput in = segments.next();
        try
        {
            while (!in.isEOF())
            {
                ByteBuffer indexKey = ByteBufferUtil.readWithShortLength(in);
                DecoratedKey indexDecoratedKey = partitioner.decorateKey(indexKey);
                if (indexDecoratedKey.compareTo(token) > 0)
                    return indexDecoratedKey;

                RowIndexEntry.Serializer.skip(in);
            }
        }
        catch (IOException e)
        {
            markSuspect();
            throw new CorruptSSTableException(e, in.getPath());
        }
        finally
        {
            FileUtils.closeQuietly(in);
        }
    }

    return null;
}
 
Example 10
Source File: IndexOffsetScanner.java    From hadoop-sstable with Apache License 2.0 5 votes vote down vote up
/**
 * Get the next offset from the SSTable index.
 * @return SSTable offset.
 */
public long next() {
    try {
        ByteBufferUtil.readWithShortLength(input);

        final long offset = input.readLong();

        // TODO: Because this is version ic > ia promotedIndex is true and we need to handle it. See C* Descriptor
        skipPromotedIndex(input);

        return offset;
    } catch (IOException e) {
        throw new IOError(e);
    }
}
 
Example 11
Source File: SSTableExport.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Export specific rows from an SSTable and write the resulting JSON to a PrintStream.
 *
 * @param desc     the descriptor of the sstable to read from
 * @param outs     PrintStream to write the output to
 * @param toExport the keys corresponding to the rows to export
 * @param excludes keys to exclude from export
 * @param metadata Metadata to print keys in a proper format
 * @throws IOException on failure to read/write input/output
 */
public static void export(Descriptor desc, PrintStream outs, Collection<String> toExport, String[] excludes, CFMetaData metadata) throws IOException
{
    SSTableReader sstable = SSTableReader.open(desc);
    RandomAccessReader dfile = sstable.openDataReader();
    try
    {
        IPartitioner partitioner = sstable.partitioner;

        if (excludes != null)
            toExport.removeAll(Arrays.asList(excludes));

        outs.println("[");

        int i = 0;

        // last key to compare order
        DecoratedKey lastKey = null;

        for (String key : toExport)
        {
            DecoratedKey decoratedKey = partitioner.decorateKey(metadata.getKeyValidator().fromString(key));

            if (lastKey != null && lastKey.compareTo(decoratedKey) > 0)
                throw new IOException("Key out of order! " + lastKey + " > " + decoratedKey);

            lastKey = decoratedKey;

            RowIndexEntry entry = sstable.getPosition(decoratedKey, SSTableReader.Operator.EQ);
            if (entry == null)
                continue;

            dfile.seek(entry.position);
            ByteBufferUtil.readWithShortLength(dfile); // row key
            DeletionInfo deletionInfo = new DeletionInfo(DeletionTime.serializer.deserialize(dfile));

            Iterator<OnDiskAtom> atomIterator = sstable.metadata.getOnDiskIterator(dfile, sstable.descriptor.version);
            checkStream(outs);

            if (i != 0)
                outs.println(",");
            i++;
            serializeRow(deletionInfo, atomIterator, sstable.metadata, decoratedKey, outs);
        }

        outs.println("\n]");
        outs.flush();
    }
    finally
    {
        dfile.close();
    }
}
 
Example 12
Source File: SSTableReader.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Build index summary(and optionally bloom filter) by reading through Index.db file.
 *
 * @param recreateBloomFilter true if recreate bloom filter
 * @param ibuilder
 * @param dbuilder
 * @param summaryLoaded true if index summary is already loaded and not need to build again
 * @throws IOException
 */
private void buildSummary(boolean recreateBloomFilter, SegmentedFile.Builder ibuilder, SegmentedFile.Builder dbuilder, boolean summaryLoaded, int samplingLevel) throws IOException
{
    // we read the positions in a BRAF so we don't have to worry about an entry spanning a mmap boundary.
    RandomAccessReader primaryIndex = RandomAccessReader.open(new File(descriptor.filenameFor(Component.PRIMARY_INDEX)));

    try
    {
        long indexSize = primaryIndex.length();
        long histogramCount = sstableMetadata.estimatedRowSize.count();
        long estimatedKeys = histogramCount > 0 && !sstableMetadata.estimatedRowSize.isOverflowed()
                             ? histogramCount
                             : estimateRowsFromIndex(primaryIndex); // statistics is supposed to be optional

        try(IndexSummaryBuilder summaryBuilder = summaryLoaded ? null : new IndexSummaryBuilder(estimatedKeys, metadata.getMinIndexInterval(), samplingLevel))
        {

            if (recreateBloomFilter)
                bf = FilterFactory.getFilter(estimatedKeys, metadata.getBloomFilterFpChance(), true);

            long indexPosition;
            while ((indexPosition = primaryIndex.getFilePointer()) != indexSize)
            {
                ByteBuffer key = ByteBufferUtil.readWithShortLength(primaryIndex);
                RowIndexEntry indexEntry = metadata.comparator.rowIndexEntrySerializer().deserialize(primaryIndex, descriptor.version);
                DecoratedKey decoratedKey = partitioner.decorateKey(key);
                if (first == null)
                    first = decoratedKey;
                last = decoratedKey;

                if (recreateBloomFilter)
                    bf.add(decoratedKey.getKey());

                // if summary was already read from disk we don't want to re-populate it using primary index
                if (!summaryLoaded)
                {
                    summaryBuilder.maybeAddEntry(decoratedKey, indexPosition);
                    ibuilder.addPotentialBoundary(indexPosition);
                    dbuilder.addPotentialBoundary(indexEntry.position);
                }
            }

            if (!summaryLoaded)
                indexSummary = summaryBuilder.build(partitioner);
        }
    }
    finally
    {
        FileUtils.closeQuietly(primaryIndex);
    }

    first = getMinimalKey(first);
    last = getMinimalKey(last);
}
 
Example 13
Source File: SSTableReader.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Validates that an index summary has full sampling, as expected when the serialization format does not support
 * persisting the sampling level.
 * @return true if the summary has full sampling, false otherwise
 */
private boolean validateSummarySamplingLevel()
{
    // We need to check index summary entries against the index to verify that none of them were dropped due to
    // downsampling.  Downsampling can drop any of the first BASE_SAMPLING_LEVEL entries (repeating that drop pattern
    // for the remainder of the summary).  Unfortunately, the first entry to be dropped is the entry at
    // index (BASE_SAMPLING_LEVEL - 1), so we need to check a full set of BASE_SAMPLING_LEVEL entries.
    Iterator<FileDataInput> segments = ifile.iterator(0);
    int i = 0;
    int summaryEntriesChecked = 0;
    int expectedIndexInterval = getMinIndexInterval();
    while (segments.hasNext())
    {
        FileDataInput in = segments.next();
        try
        {
            while (!in.isEOF())
            {
                ByteBuffer indexKey = ByteBufferUtil.readWithShortLength(in);
                if (i % expectedIndexInterval == 0)
                {
                    ByteBuffer summaryKey = ByteBuffer.wrap(indexSummary.getKey(i / expectedIndexInterval));
                    if (!summaryKey.equals(indexKey))
                        return false;
                    summaryEntriesChecked++;

                    if (summaryEntriesChecked == Downsampling.BASE_SAMPLING_LEVEL)
                        return true;
                }
                RowIndexEntry.Serializer.skip(in);
                i++;
            }
        }
        catch (IOException e)
        {
            markSuspect();
            throw new CorruptSSTableException(e, in.getPath());
        }
        finally
        {
            FileUtils.closeQuietly(in);
        }
    }

    return true;
}
 
Example 14
Source File: SSTableScanner.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
protected OnDiskAtomIterator computeNext()
{
    try
    {
        if (nextEntry == null)
        {
            do
            {
                // we're starting the first range or we just passed the end of the previous range
                if (!rangeIterator.hasNext())
                    return endOfData();

                currentRange = rangeIterator.next();
                seekToCurrentRangeStart();

                if (ifile.isEOF())
                    return endOfData();

                currentKey = sstable.partitioner.decorateKey(ByteBufferUtil.readWithShortLength(ifile));
                currentEntry = sstable.metadata.comparator.rowIndexEntrySerializer().deserialize(ifile, sstable.descriptor.version);
            } while (!currentRange.contains(currentKey));
        }
        else
        {
            // we're in the middle of a range
            currentKey = nextKey;
            currentEntry = nextEntry;
        }

        long readEnd;
        if (ifile.isEOF())
        {
            nextEntry = null;
            nextKey = null;
            readEnd = dfile.length();
        }
        else
        {
            // we need the position of the start of the next key, regardless of whether it falls in the current range
            nextKey = sstable.partitioner.decorateKey(ByteBufferUtil.readWithShortLength(ifile));
            nextEntry = sstable.metadata.comparator.rowIndexEntrySerializer().deserialize(ifile, sstable.descriptor.version);
            readEnd = nextEntry.position;

            if (!currentRange.contains(nextKey))
            {
                nextKey = null;
                nextEntry = null;
            }
        }

        if (dataRange == null || dataRange.selectsFullRowFor(currentKey.getKey()))
        {
            dfile.seek(currentEntry.position);
            ByteBufferUtil.readWithShortLength(dfile); // key
            long dataSize = readEnd - dfile.getFilePointer();
            return new SSTableIdentityIterator(sstable, dfile, currentKey, dataSize);
        }

        return new LazyColumnIterator(currentKey, new IColumnIteratorFactory()
        {
            public OnDiskAtomIterator create()
            {
                return dataRange.columnFilter(currentKey.getKey()).getSSTableColumnIterator(sstable, dfile, currentKey, currentEntry);
            }
        });

    }
    catch (IOException e)
    {
        sstable.markSuspect();
        throw new CorruptSSTableException(e, sstable.getFilename());
    }
}
 
Example 15
Source File: IndexExpression.java    From stratio-cassandra with Apache License 2.0 3 votes vote down vote up
/**
 * Deserializes an <code>IndexExpression</code> instance from the specified input. 
 *
 * @param input the input to read from 
 * @return the <code>IndexExpression</code> instance deserialized
 * @throws IOException if a problem occurs while deserializing the <code>IndexExpression</code> instance.
 */
public static IndexExpression readFrom(DataInput input) throws IOException
{
    return new IndexExpression(ByteBufferUtil.readWithShortLength(input),
                               Operator.readFrom(input),
                               ByteBufferUtil.readWithShortLength(input));
}