private void trim(LogFilePath srcPath, long startOffset) throws Exception {
if (startOffset == srcPath.getOffset()) {
return;
}
FileReaderWriter reader = null;
FileReaderWriter writer = null;
LogFilePath dstPath = null;
int copiedMessages = 0;
// Deleting the writer closes its stream flushing all pending data to the disk.
mFileRegistry.deleteWriter(srcPath);
try {
CompressionCodec codec = null;
String extension = "";
if (mConfig.getCompressionCodec() != null && !mConfig.getCompressionCodec().isEmpty()) {
codec = CompressionUtil.createCompressionCodec(mConfig.getCompressionCodec());
extension = codec.getDefaultExtension();
}
reader = createReader(srcPath, codec);
KeyValue keyVal;
while ((keyVal = reader.next()) != null) {
if (keyVal.getKey() >= startOffset) {
if (writer == null) {
String localPrefix = mConfig.getLocalPath() + '/' +
IdUtil.getLocalMessageDir();
dstPath = new LogFilePath(localPrefix, srcPath.getTopic(),
srcPath.getPartitions(), srcPath.getGeneration(),
srcPath.getKafkaPartition(), startOffset,
extension);
writer = mFileRegistry.getOrCreateWriter(dstPath,
codec);
}
writer.write(keyVal);
copiedMessages++;
}
}
} finally {
if (reader != null) {