pvary commented on code in PR #18198:
URL: https://github.com/apache/iceberg/pull/18198#discussion_r4063884209


##########
core/src/main/java/org/apache/iceberg/encryption/InputFilesDecryptor.java:
##########
@@ -18,51 +18,80 @@
  */
 package org.apache.iceberg.encryption;
 
-import java.nio.ByteBuffer;
-import java.util.Collections;
+import java.util.Collection;
 import java.util.Map;
-import java.util.stream.Stream;
 import org.apache.iceberg.CombinedScanTask;
+import org.apache.iceberg.ContentFile;
+import org.apache.iceberg.DeleteFile;
 import org.apache.iceberg.FileScanTask;
 import org.apache.iceberg.io.FileIO;
 import org.apache.iceberg.io.InputFile;
 import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
 import org.apache.iceberg.relocated.com.google.common.collect.Maps;
 
+/** Resolves the files referenced by scan tasks into readable, decrypted 
{@link InputFile}s. */
 public class InputFilesDecryptor {
 
-  private final Map<String, InputFile> decryptedInputFiles;
+  private final Iterable<? extends ContentFile<?>> referencedFiles;
+  private final EncryptingFileIO encryptingIO;
+  private Map<String, InputFile> lazyInputFiles = null;
 
+  /**
+   * @deprecated since 1.12.0, will be removed in 1.13.0; use {@link 
#fromTasks(Iterable,
+   *     EncryptingFileIO)} instead.
+   */
+  @Deprecated
   public InputFilesDecryptor(
       CombinedScanTask combinedTask, FileIO io, EncryptionManager encryption) {
-    Map<String, ByteBuffer> keyMetadata = Maps.newHashMap();
-    combinedTask.files().stream()
-        .flatMap(
-            fileScanTask ->
-                Stream.concat(Stream.of(fileScanTask.file()), 
fileScanTask.deletes().stream()))
-        .forEach(file -> keyMetadata.put(file.location(), file.keyMetadata()));
-    Stream<EncryptedInputFile> encrypted =
-        keyMetadata.entrySet().stream()
-            .map(
-                entry ->
-                    EncryptedFiles.encryptedInput(
-                        io.newInputFile(entry.getKey()), entry.getValue()));
+    this(
+        () -> referencedFiles(combinedTask.files()).iterator(),
+        EncryptingFileIO.combine(io, encryption));
+  }
+
+  public static InputFilesDecryptor fromTasks(
+      Iterable<FileScanTask> tasks, EncryptingFileIO encryptingIO) {
+    return new InputFilesDecryptor(() -> referencedFiles(tasks).iterator(), 
encryptingIO);
+  }
+
+  private InputFilesDecryptor(
+      Iterable<? extends ContentFile<?>> files, EncryptingFileIO encryptingIO) 
{
+    this.referencedFiles = files;
+    this.encryptingIO = encryptingIO;
+  }
+
+  private Map<String, InputFile> inputFiles() {
+    if (lazyInputFiles == null) {
+      this.lazyInputFiles = encryptingIO.bulkDecrypt(referencedFiles);
+    }
+
+    return lazyInputFiles;
+  }
 
-    // decrypt with the batch call to avoid multiple RPCs to a key server, if 
possible
-    @SuppressWarnings("StreamToIterable")
-    Iterable<InputFile> decryptedFiles = 
encryption.decrypt(encrypted::iterator);
+  private static Collection<ContentFile<?>> 
referencedFiles(Iterable<FileScanTask> tasks) {
+    Map<String, ContentFile<?>> files = Maps.newHashMap();
+    for (FileScanTask task : tasks) {
+      files.put(task.file().location(), task.file());
+      for (DeleteFile delete : task.deletes()) {
+        files.put(delete.location(), delete);
+      }
+    }
 
-    Map<String, InputFile> files = 
Maps.newHashMapWithExpectedSize(keyMetadata.size());
-    decryptedFiles.forEach(decrypted -> 
files.putIfAbsent(decrypted.location(), decrypted));
-    this.decryptedInputFiles = Collections.unmodifiableMap(files);
+    return files.values();
   }
 
+  /**
+   * @deprecated since 1.12.0, will be removed in 1.13.0; use {@link 
#getInputFile(String)} instead.
+   */
+  @Deprecated
   public InputFile getInputFile(FileScanTask task) {
     Preconditions.checkArgument(!task.isDataTask(), "Invalid task type");
-    return decryptedInputFiles.get(task.file().location());
+    return getInputFile(task.file().location());
   }
 
   public InputFile getInputFile(String location) {
-    return decryptedInputFiles.get(location);
+    InputFile inputFile = inputFiles().get(location);
+    Preconditions.checkArgument(

Review Comment:
   It is a behavioural change. Seems acceptable, but I would like to make sure 
that we are ok with this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to