diff --git a/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorIndexFileWriter.java b/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorIndexFileWriter.java index 4dddd6ba162e..a1d85b5ba350 100644 --- a/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorIndexFileWriter.java +++ b/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorIndexFileWriter.java @@ -72,9 +72,8 @@ private IndexFileMeta tryWriter(Iterator> iter try { while (iterator.hasNext()) { Map.Entry entry = iterator.next(); - long currentSize = writer.write(entry.getKey(), entry.getValue()); - - if (writer.writtenSizeInBytes() + currentSize > targetSizeInBytes) { + writer.write(entry.getKey(), entry.getValue()); + if (writer.writtenSizeInBytes() > targetSizeInBytes) { break; } } @@ -92,50 +91,43 @@ private IndexFileMeta tryWriter(Iterator> iter *

TODO: We can consider sending a message to delete the deletion file in the future. */ private List emptyIndexFile() throws IOException { - try (SingleIndexFileWriter writer = new SingleIndexFileWriter()) { - return Collections.singletonList(writer.writtenIndexFile()); - } + SingleIndexFileWriter writer = new SingleIndexFileWriter(); + writer.close(); + return Collections.singletonList(writer.writtenIndexFile()); } private class SingleIndexFileWriter implements Closeable { private final Path path; - private final DataOutputStream dataOutputStream; - private final LinkedHashMap> dvRanges; - private long writtenSizeInBytes = 0L; - - public SingleIndexFileWriter() throws IOException { + private SingleIndexFileWriter() throws IOException { this.path = indexPathFactory.newPath(); this.dataOutputStream = new DataOutputStream(fileIO.newOutputStream(path, true)); dataOutputStream.writeByte(VERSION_ID_V1); this.dvRanges = new LinkedHashMap<>(); } - public long writtenSizeInBytes() { - return this.writtenSizeInBytes; + private long writtenSizeInBytes() { + return dataOutputStream.size(); } - public long write(String key, DeletionVector deletionVector) throws IOException { + private void write(String key, DeletionVector deletionVector) throws IOException { Preconditions.checkNotNull(dataOutputStream); byte[] data = deletionVector.serializeToBytes(); int size = data.length; - dvRanges.put(key, Pair.of(dataOutputStream.size(), size)); dataOutputStream.writeInt(size); dataOutputStream.write(data); dataOutputStream.writeInt(calculateChecksum(data)); - writtenSizeInBytes += size; - return size; } - public IndexFileMeta writtenIndexFile() throws IOException { + public IndexFileMeta writtenIndexFile() { return new IndexFileMeta( DELETION_VECTORS_INDEX, path.getName(), - fileIO.getFileSize(path), + writtenSizeInBytes(), dvRanges.size(), dvRanges); } diff --git a/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java b/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java index 2b1c9855093c..a4dd8f92c11f 100644 --- a/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java @@ -160,7 +160,7 @@ public void testWriteDVIndexWithLimitedTargetSizePerIndexFile() { List indexFiles = deletionVectorsIndexFile.write(fileToDV); // assert 1 - assertThat(indexFiles.size()).isEqualTo(5); + assertThat(indexFiles.size()).isEqualTo(3); Map dvs = deletionVectorsIndexFile.readAllDeletionVectors(indexFiles); for (String file : dvs.keySet()) {