From 753a9433228cb4579f0602b9292dd16944339607 Mon Sep 17 00:00:00 2001 From: Robert Stupp Date: Wed, 16 Sep 2015 00:43:11 +0200 Subject: [PATCH] Rename DataOutputBuffer.getFilePointer() to position() patch by Robert Stupp; reviewed by Benedict for CASSANDRA-10349 --- .../org/apache/cassandra/db/ColumnIndex.java | 6 +++--- .../apache/cassandra/db/RowIndexEntry.java | 6 +++--- .../io/sstable/format/big/BigTableWriter.java | 12 +++++------ .../cassandra/io/util/DataOutputBuffer.java | 4 ++-- .../cassandra/io/util/DataOutputPlus.java | 21 +++++++++++++++---- .../cassandra/io/util/SequentialWriter.java | 6 +++--- .../cassandra/db/RowIndexEntryTest.java | 4 ++-- .../io/util/BufferedRandomAccessFileTest.java | 10 ++++----- .../CompressedInputStreamTest.java | 2 +- 9 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/java/org/apache/cassandra/db/ColumnIndex.java b/src/java/org/apache/cassandra/db/ColumnIndex.java index 6b2ef59b67..add5fa741a 100644 --- a/src/java/org/apache/cassandra/db/ColumnIndex.java +++ b/src/java/org/apache/cassandra/db/ColumnIndex.java @@ -91,7 +91,7 @@ public class ColumnIndex this.writer = writer; this.header = header; this.version = version; - this.initialPosition = writer.getFilePointer(); + this.initialPosition = writer.position(); } private void writePartitionHeader(UnfilteredRowIterator iterator) throws IOException @@ -105,7 +105,7 @@ public class ColumnIndex public ColumnIndex build() throws IOException { writePartitionHeader(iterator); - this.headerLength = writer.getFilePointer() - initialPosition; + this.headerLength = writer.position() - initialPosition; while (iterator.hasNext()) add(iterator.next()); @@ -115,7 +115,7 @@ public class ColumnIndex private long currentPosition() { - return writer.getFilePointer() - initialPosition; + return writer.position() - initialPosition; } private void addIndexBlock() diff --git a/src/java/org/apache/cassandra/db/RowIndexEntry.java b/src/java/org/apache/cassandra/db/RowIndexEntry.java index 6cd6542caf..43dc80c800 100644 --- a/src/java/org/apache/cassandra/db/RowIndexEntry.java +++ b/src/java/org/apache/cassandra/db/RowIndexEntry.java @@ -142,15 +142,15 @@ public class RowIndexEntry implements IMeasurableMemory int[] offsets = new int[rie.columnsIndex().size()]; - if (out.hasFilePointer()) + if (out.hasPosition()) { // Out is usually a SequentialWriter, so using the file-pointer is fine to generate the offsets. // A DataOutputBuffer also works. - long start = out.getFilePointer(); + long start = out.position(); int i = 0; for (IndexHelper.IndexInfo info : rie.columnsIndex()) { - offsets[i] = i == 0 ? 0 : (int)(out.getFilePointer() - start); + offsets[i] = i == 0 ? 0 : (int)(out.position() - start); i++; idxSerializer.serialize(info, out); } diff --git a/src/java/org/apache/cassandra/io/sstable/format/big/BigTableWriter.java b/src/java/org/apache/cassandra/io/sstable/format/big/BigTableWriter.java index 089ae6fcf6..3d68a74049 100644 --- a/src/java/org/apache/cassandra/io/sstable/format/big/BigTableWriter.java +++ b/src/java/org/apache/cassandra/io/sstable/format/big/BigTableWriter.java @@ -107,7 +107,7 @@ public class BigTableWriter extends SSTableWriter assert decoratedKey != null : "Keys must not be null"; // empty keys ARE allowed b/c of indexed column values //if (lastWrittenKey != null && lastWrittenKey.compareTo(decoratedKey) >= 0) // throw new RuntimeException("Last written key " + lastWrittenKey + " >= current key " + decoratedKey + " writing into " + getFilename()); - return (lastWrittenKey == null) ? 0 : dataFile.getFilePointer(); + return (lastWrittenKey == null) ? 0 : dataFile.position(); } private void afterAppend(DecoratedKey decoratedKey, long dataEnd, RowIndexEntry index) throws IOException @@ -153,7 +153,7 @@ public class BigTableWriter extends SSTableWriter RowIndexEntry entry = RowIndexEntry.create(startPosition, iterator.partitionLevelDeletion(), index); - long endPosition = dataFile.getFilePointer(); + long endPosition = dataFile.position(); long rowSize = endPosition - startPosition; maybeLogLargePartitionWarning(key, rowSize); metadataCollector.addPartitionSizeInBytes(rowSize); @@ -352,7 +352,7 @@ public class BigTableWriter extends SSTableWriter public long getFilePointer() { - return dataFile.getFilePointer(); + return dataFile.position(); } public long getOnDiskFilePointer() @@ -403,7 +403,7 @@ public class BigTableWriter extends SSTableWriter public void append(DecoratedKey key, RowIndexEntry indexEntry, long dataEnd) throws IOException { bf.add(key); - long indexStart = indexFile.getFilePointer(); + long indexStart = indexFile.position(); try { ByteBufferUtil.writeWithShortLength(key.getKey(), indexFile); @@ -413,7 +413,7 @@ public class BigTableWriter extends SSTableWriter { throw new FSWriteError(e, indexFile.getPath()); } - long indexEnd = indexFile.getFilePointer(); + long indexEnd = indexFile.position(); if (logger.isTraceEnabled()) logger.trace("wrote index entry: {} at {}", indexEntry, indexStart); @@ -462,7 +462,7 @@ public class BigTableWriter extends SSTableWriter flushBf(); // truncate index file - long position = iwriter.indexFile.getFilePointer(); + long position = iwriter.indexFile.position(); iwriter.indexFile.setDescriptor(descriptor).prepareToCommit(); FileUtils.truncate(iwriter.indexFile.getPath(), position); diff --git a/src/java/org/apache/cassandra/io/util/DataOutputBuffer.java b/src/java/org/apache/cassandra/io/util/DataOutputBuffer.java index c4d6f54eb8..4ba55464cd 100644 --- a/src/java/org/apache/cassandra/io/util/DataOutputBuffer.java +++ b/src/java/org/apache/cassandra/io/util/DataOutputBuffer.java @@ -114,12 +114,12 @@ public class DataOutputBuffer extends BufferedDataOutputStreamPlus return buffer.position(); } - public boolean hasFilePointer() + public boolean hasPosition() { return true; } - public long getFilePointer() + public long position() { return getLength(); } diff --git a/src/java/org/apache/cassandra/io/util/DataOutputPlus.java b/src/java/org/apache/cassandra/io/util/DataOutputPlus.java index 551d386862..60a5727b45 100644 --- a/src/java/org/apache/cassandra/io/util/DataOutputPlus.java +++ b/src/java/org/apache/cassandra/io/util/DataOutputPlus.java @@ -22,10 +22,10 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; -import org.apache.cassandra.utils.vint.VIntCoding; - import com.google.common.base.Function; +import org.apache.cassandra.utils.vint.VIntCoding; + /** * Extension to DataOutput that provides for writing ByteBuffer and Memory, potentially with an efficient * implementation that is zero copy or at least has reduced bounds checking overhead. @@ -60,12 +60,25 @@ public interface DataOutputPlus extends DataOutput VIntCoding.writeUnsignedVInt(i, this); } - default long getFilePointer() + /** + * Returns the current position of the underlying target like a file-pointer + * or the position withing a buffer. Not every implementation may support this + * functionality. Whether or not this functionality is supported can be checked + * via the {@link #hasPosition()}. + * + * @throws UnsupportedOperationException if the implementation does not support + * position + */ + default long position() { throw new UnsupportedOperationException(); } - default boolean hasFilePointer() + /** + * If the implementation supports providing a position, this method returns + * {@code true}, otherwise {@code false}. + */ + default boolean hasPosition() { return false; } diff --git a/src/java/org/apache/cassandra/io/util/SequentialWriter.java b/src/java/org/apache/cassandra/io/util/SequentialWriter.java index 1814acacde..6000f952fc 100644 --- a/src/java/org/apache/cassandra/io/util/SequentialWriter.java +++ b/src/java/org/apache/cassandra/io/util/SequentialWriter.java @@ -253,12 +253,12 @@ public class SequentialWriter extends BufferedDataOutputStreamPlus implements Tr runPostFlush.run(); } - public boolean hasFilePointer() + public boolean hasPosition() { return true; } - public long getFilePointer() + public long position() { return current(); } @@ -274,7 +274,7 @@ public class SequentialWriter extends BufferedDataOutputStreamPlus implements Tr */ public long getOnDiskFilePointer() { - return getFilePointer(); + return position(); } public long length() diff --git a/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java b/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java index d004d455c2..25baa4ea85 100644 --- a/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java +++ b/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java @@ -155,12 +155,12 @@ public class RowIndexEntryTest extends CQLTester // test with an output stream that doesn't support a file-pointer buffer = new DataOutputBuffer() { - public boolean hasFilePointer() + public boolean hasPosition() { return false; } - public long getFilePointer() + public long position() { throw new UnsupportedOperationException(); } diff --git a/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java b/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java index 364ea7129a..c3a4539c1c 100644 --- a/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java +++ b/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java @@ -46,7 +46,7 @@ public class BufferedRandomAccessFileTest byte[] data = "Hello".getBytes(); w.write(data); assertEquals(data.length, w.length()); - assertEquals(data.length, w.getFilePointer()); + assertEquals(data.length, w.position()); w.sync(); @@ -67,9 +67,9 @@ public class BufferedRandomAccessFileTest for (int i = 0; i < bigData.length; i++) bigData[i] = 'd'; - long initialPosition = w.getFilePointer(); + long initialPosition = w.position(); w.write(bigData); // writing data - assertEquals(w.getFilePointer(), initialPosition + bigData.length); + assertEquals(w.position(), initialPosition + bigData.length); assertEquals(w.length(), initialPosition + bigData.length); // file size should equals to last position w.sync(); @@ -285,10 +285,10 @@ public class BufferedRandomAccessFileTest { final SequentialWriter w = createTempFile("brafGetFilePointer"); - assertEquals(w.getFilePointer(), 0); // initial position should be 0 + assertEquals(w.position(), 0); // initial position should be 0 w.write(generateByteArray(20)); - assertEquals(w.getFilePointer(), 20); // position 20 after writing 20 bytes + assertEquals(w.position(), 20); // position 20 after writing 20 bytes w.sync(); diff --git a/test/unit/org/apache/cassandra/streaming/compression/CompressedInputStreamTest.java b/test/unit/org/apache/cassandra/streaming/compression/CompressedInputStreamTest.java index fea6d2be94..30670fbee5 100644 --- a/test/unit/org/apache/cassandra/streaming/compression/CompressedInputStreamTest.java +++ b/test/unit/org/apache/cassandra/streaming/compression/CompressedInputStreamTest.java @@ -70,7 +70,7 @@ public class CompressedInputStreamTest Map index = new HashMap(); for (long l = 0L; l < 1000; l++) { - index.put(l, writer.getFilePointer()); + index.put(l, writer.position()); writer.writeLong(l); } writer.finish();