-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Introduce DeletionVectorIndexFileMaintainer (#3387)
- Loading branch information
Showing
6 changed files
with
286 additions
and
9 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
...re/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorIndexFileMaintainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.paimon.deletionvectors; | ||
|
||
import org.apache.paimon.fs.Path; | ||
import org.apache.paimon.index.IndexFileHandler; | ||
import org.apache.paimon.index.IndexFileMeta; | ||
import org.apache.paimon.manifest.FileKind; | ||
import org.apache.paimon.manifest.IndexManifestEntry; | ||
import org.apache.paimon.table.source.DeletionFile; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** DeletionVectorIndexFileMaintainer. */ | ||
public class DeletionVectorIndexFileMaintainer { | ||
|
||
private final IndexFileHandler indexFileHandler; | ||
|
||
private final Map<String, IndexManifestEntry> indexNameToEntry = new HashMap<>(); | ||
|
||
private final Map<String, Map<String, DeletionFile>> indexFileToDeletionFiles = new HashMap<>(); | ||
|
||
private final Set<String> touchedIndexFiles = new HashSet<>(); | ||
|
||
public DeletionVectorIndexFileMaintainer( | ||
IndexFileHandler indexFileHandler, Map<String, DeletionFile> dataFileToDeletionFiles) { | ||
this.indexFileHandler = indexFileHandler; | ||
List<String> touchedIndexFileNames = | ||
dataFileToDeletionFiles.values().stream() | ||
.map(deletionFile -> new Path(deletionFile.path()).getName()) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
indexFileHandler.scan().stream() | ||
.filter( | ||
indexManifestEntry -> | ||
touchedIndexFileNames.contains( | ||
indexManifestEntry.indexFile().fileName())) | ||
.forEach(entry -> indexNameToEntry.put(entry.indexFile().fileName(), entry)); | ||
|
||
for (String dataFile : dataFileToDeletionFiles.keySet()) { | ||
DeletionFile deletionFile = dataFileToDeletionFiles.get(dataFile); | ||
String indexFileName = new Path(deletionFile.path()).getName(); | ||
if (!indexFileToDeletionFiles.containsKey(indexFileName)) { | ||
indexFileToDeletionFiles.put(indexFileName, new HashMap<>()); | ||
} | ||
indexFileToDeletionFiles.get(indexFileName).put(dataFile, deletionFile); | ||
} | ||
} | ||
|
||
public void notifyDeletionFiles(Map<String, DeletionFile> dataFileToDeletionFiles) { | ||
for (String dataFile : dataFileToDeletionFiles.keySet()) { | ||
DeletionFile deletionFile = dataFileToDeletionFiles.get(dataFile); | ||
String indexFileName = new Path(deletionFile.path()).getName(); | ||
touchedIndexFiles.add(indexFileName); | ||
if (indexFileToDeletionFiles.containsKey(indexFileName)) { | ||
indexFileToDeletionFiles.get(indexFileName).remove(dataFile); | ||
if (indexFileToDeletionFiles.get(indexFileName).isEmpty()) { | ||
indexFileToDeletionFiles.remove(indexFileName); | ||
indexNameToEntry.remove(indexFileName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public List<IndexManifestEntry> writeUnchangedDeletionVector() { | ||
DeletionVectorsIndexFile deletionVectorsIndexFile = indexFileHandler.deletionVectorsIndex(); | ||
List<IndexManifestEntry> newIndexEntries = new ArrayList<>(); | ||
for (String indexFile : indexFileToDeletionFiles.keySet()) { | ||
if (touchedIndexFiles.contains(indexFile)) { | ||
IndexManifestEntry oldEntry = indexNameToEntry.get(indexFile); | ||
|
||
// write unchanged deletion vector. | ||
Map<String, DeletionFile> dataFileToDeletionFiles = | ||
indexFileToDeletionFiles.get(indexFile); | ||
if (!dataFileToDeletionFiles.isEmpty()) { | ||
IndexFileMeta newIndexFile = | ||
indexFileHandler.writeDeletionVectorsIndex( | ||
deletionVectorsIndexFile.readDeletionVector( | ||
dataFileToDeletionFiles)); | ||
newIndexEntries.add( | ||
new IndexManifestEntry( | ||
FileKind.ADD, | ||
oldEntry.partition(), | ||
oldEntry.bucket(), | ||
newIndexFile)); | ||
} | ||
|
||
// mark the touched index file as removed. | ||
newIndexEntries.add(oldEntry.toDeleteEntry()); | ||
} | ||
} | ||
return newIndexEntries; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...rc/test/java/org/apache/paimon/deletionvectors/DeletionVectorIndexFileMaintainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.paimon.deletionvectors; | ||
|
||
import org.apache.paimon.TestAppendFileStore; | ||
import org.apache.paimon.data.BinaryRow; | ||
import org.apache.paimon.index.IndexFileMeta; | ||
import org.apache.paimon.manifest.FileKind; | ||
import org.apache.paimon.manifest.IndexManifestEntry; | ||
import org.apache.paimon.table.sink.CommitMessageImpl; | ||
import org.apache.paimon.table.source.DeletionFile; | ||
import org.apache.paimon.utils.Pair; | ||
import org.apache.paimon.utils.PathFactory; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** Test for DeletionVectorIndexFileMaintainer. */ | ||
public class DeletionVectorIndexFileMaintainerTest { | ||
|
||
@TempDir java.nio.file.Path tempDir; | ||
|
||
@Test | ||
public void test() throws Exception { | ||
TestAppendFileStore store = TestAppendFileStore.createAppendStore(tempDir, new HashMap<>()); | ||
|
||
Map<String, List<Integer>> dvs = new HashMap<>(); | ||
dvs.put("f1", Arrays.asList(1, 3, 5)); | ||
dvs.put("f2", Arrays.asList(2, 4, 6)); | ||
CommitMessageImpl commitMessage1 = store.writeDVIndexFiles(BinaryRow.EMPTY_ROW, 0, dvs); | ||
CommitMessageImpl commitMessage2 = | ||
store.writeDVIndexFiles( | ||
BinaryRow.EMPTY_ROW, | ||
1, | ||
Collections.singletonMap("f3", Arrays.asList(1, 2, 3))); | ||
store.commit(commitMessage1, commitMessage2); | ||
|
||
PathFactory indexPathFactory = store.pathFactory().indexFileFactory(); | ||
Map<String, DeletionFile> dataFileToDeletionFiles = new HashMap<>(); | ||
dataFileToDeletionFiles.putAll( | ||
createDeletionFileMapFromIndexFileMetas( | ||
indexPathFactory, commitMessage1.indexIncrement().newIndexFiles())); | ||
dataFileToDeletionFiles.putAll( | ||
createDeletionFileMapFromIndexFileMetas( | ||
indexPathFactory, commitMessage2.indexIncrement().newIndexFiles())); | ||
|
||
DeletionVectorIndexFileMaintainer dvIFMaintainer = | ||
store.createDVIFMaintainer(dataFileToDeletionFiles); | ||
|
||
// no dv should be rewritten, because nothing is changed. | ||
List<IndexManifestEntry> res = dvIFMaintainer.writeUnchangedDeletionVector(); | ||
assertThat(res.size()).isEqualTo(0); | ||
|
||
// no dv should be rewritten, because all the deletion vectors have been updated in the | ||
// index file that contains the dv of f3. | ||
dvIFMaintainer.notifyDeletionFiles( | ||
Collections.singletonMap("f3", dataFileToDeletionFiles.get("f3"))); | ||
res = dvIFMaintainer.writeUnchangedDeletionVector(); | ||
assertThat(res.size()).isEqualTo(0); | ||
|
||
// the dv of f1 and f2 are in one index file, and the dv of f1 is updated. | ||
// the dv of f2 need to be rewritten, and this index file should be marked as REMOVE. | ||
dvIFMaintainer.notifyDeletionFiles( | ||
Collections.singletonMap("f1", dataFileToDeletionFiles.get("f1"))); | ||
res = dvIFMaintainer.writeUnchangedDeletionVector(); | ||
assertThat(res.size()).isEqualTo(2); | ||
IndexManifestEntry entry = | ||
res.stream().filter(file -> file.kind() == FileKind.ADD).findAny().get(); | ||
assertThat(entry.indexFile().deletionVectorsRanges().containsKey("f2")).isTrue(); | ||
entry = res.stream().filter(file -> file.kind() == FileKind.DELETE).findAny().get(); | ||
assertThat(entry.indexFile()) | ||
.isEqualTo(commitMessage1.indexIncrement().newIndexFiles().get(0)); | ||
} | ||
|
||
private Map<String, DeletionFile> createDeletionFileMapFromIndexFileMetas( | ||
PathFactory indexPathFactory, List<IndexFileMeta> fileMetas) { | ||
Map<String, DeletionFile> dataFileToDeletionFiles = new HashMap<>(); | ||
for (IndexFileMeta indexFileMeta : fileMetas) { | ||
for (Map.Entry<String, Pair<Integer, Integer>> range : | ||
indexFileMeta.deletionVectorsRanges().entrySet()) { | ||
dataFileToDeletionFiles.put( | ||
range.getKey(), | ||
new DeletionFile( | ||
indexPathFactory.toPath(indexFileMeta.fileName()).toString(), | ||
range.getValue().getLeft(), | ||
range.getValue().getRight())); | ||
} | ||
} | ||
return dataFileToDeletionFiles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters