use of org.apache.cassandra.io.util.Memory in project cassandra by apache.
the class IndexSummaryBuilder method downsample.
/**
* Downsamples an existing index summary to a new sampling level.
* @param existing an existing IndexSummary
* @param newSamplingLevel the target level for the new IndexSummary. This must be less than the current sampling
* level for `existing`.
* @param partitioner the partitioner used for the index summary
* @return a new IndexSummary
*/
@SuppressWarnings("resource")
public static IndexSummary downsample(IndexSummary existing, int newSamplingLevel, int minIndexInterval, IPartitioner partitioner) {
// To downsample the old index summary, we'll go through (potentially) several rounds of downsampling.
// Conceptually, each round starts at position X and then removes every Nth item. The value of X follows
// a particular pattern to evenly space out the items that we remove. The value of N decreases by one each
// round.
int currentSamplingLevel = existing.getSamplingLevel();
assert currentSamplingLevel > newSamplingLevel;
assert minIndexInterval == existing.getMinIndexInterval();
// calculate starting indexes for downsampling rounds
int[] startPoints = Downsampling.getStartPoints(currentSamplingLevel, newSamplingLevel);
// calculate new off-heap size
int newKeyCount = existing.size();
long newEntriesLength = existing.getEntriesLength();
for (int start : startPoints) {
for (int j = start; j < existing.size(); j += currentSamplingLevel) {
newKeyCount--;
long length = existing.getEndInSummary(j) - existing.getPositionInSummary(j);
newEntriesLength -= length;
}
}
Memory oldEntries = existing.getEntries();
Memory newOffsets = Memory.allocate(newKeyCount * 4);
Memory newEntries = Memory.allocate(newEntriesLength);
// Copy old entries to our new Memory.
int i = 0;
int newEntriesOffset = 0;
outer: for (int oldSummaryIndex = 0; oldSummaryIndex < existing.size(); oldSummaryIndex++) {
// and see if the entry's index is covered by that round
for (int start : startPoints) {
if ((oldSummaryIndex - start) % currentSamplingLevel == 0)
continue outer;
}
// write the position of the actual entry in the index summary (4 bytes)
newOffsets.setInt(i * 4, newEntriesOffset);
i++;
long start = existing.getPositionInSummary(oldSummaryIndex);
long length = existing.getEndInSummary(oldSummaryIndex) - start;
newEntries.put(newEntriesOffset, oldEntries, start, length);
newEntriesOffset += length;
}
assert newEntriesOffset == newEntriesLength;
return new IndexSummary(partitioner, newOffsets, newKeyCount, newEntries, newEntriesLength, existing.getMaxNumberOfEntries(), minIndexInterval, newSamplingLevel);
}
use of org.apache.cassandra.io.util.Memory in project cassandra by apache.
the class CompressionMetadata method readChunkOffsets.
/**
* Read offsets of the individual chunks from the given input.
*
* @param input Source of the data.
*
* @return collection of the chunk offsets.
*/
private Memory readChunkOffsets(DataInput input) {
final int chunkCount;
try {
chunkCount = input.readInt();
if (chunkCount <= 0)
throw new IOException("Compressed file with 0 chunks encountered: " + input);
} catch (IOException e) {
throw new FSReadError(e, indexFilePath);
}
@SuppressWarnings("resource") Memory offsets = Memory.allocate(chunkCount * 8L);
int i = 0;
try {
for (i = 0; i < chunkCount; i++) {
offsets.setLong(i * 8L, input.readLong());
}
return offsets;
} catch (IOException e) {
if (offsets != null)
offsets.close();
if (e instanceof EOFException) {
String msg = String.format("Corrupted Index File %s: read %d but expected %d chunks.", indexFilePath, i, chunkCount);
throw new CorruptSSTableException(new IOException(msg, e), indexFilePath);
}
throw new FSReadError(e, indexFilePath);
}
}
use of org.apache.cassandra.io.util.Memory in project cassandra by apache.
the class OffHeapBitSet method deserialize.
@SuppressWarnings("resource")
public static OffHeapBitSet deserialize(DataInput in) throws IOException {
long byteCount = in.readInt() * 8L;
Memory memory = Memory.allocate(byteCount);
for (long i = 0; i < byteCount; ) {
long v = in.readLong();
memory.setByte(i++, (byte) (v >>> 0));
memory.setByte(i++, (byte) (v >>> 8));
memory.setByte(i++, (byte) (v >>> 16));
memory.setByte(i++, (byte) (v >>> 24));
memory.setByte(i++, (byte) (v >>> 32));
memory.setByte(i++, (byte) (v >>> 40));
memory.setByte(i++, (byte) (v >>> 48));
memory.setByte(i++, (byte) (v >>> 56));
}
return new OffHeapBitSet(memory);
}
Aggregations