|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, NVIDIA CORPORATION. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.nvidia.spark.rapids.fileio.hadoop |
| 18 | + |
| 19 | +import java.io.IOException |
| 20 | +import java.net.URI |
| 21 | +import java.util.OptionalLong |
| 22 | + |
| 23 | +import scala.collection.JavaConverters._ |
| 24 | + |
| 25 | +import ai.rapids.cudf.HostMemoryBuffer |
| 26 | +import com.nvidia.spark.rapids.{IntRangeWithOffset, PerfIO, RangeWithOffset, SuffixRangeWithOffset} |
| 27 | +import com.nvidia.spark.rapids.jni.fileio.{RapidsInputFile, SeekableInputStream} |
| 28 | +import org.apache.hadoop.conf.Configuration |
| 29 | +import org.apache.hadoop.fs.Path |
| 30 | + |
| 31 | +/** |
| 32 | + * GCS-backed {@link RapidsInputFile} for Hadoop-conf-driven (non-iceberg) reads. |
| 33 | + * {@code readVectored} issues batched byte-range reads through the optimized |
| 34 | + * PerfIO path; the other operations delegate to the standard {@link HadoopInputFile}. |
| 35 | + */ |
| 36 | +class GCSInputFile private ( |
| 37 | + delegate: HadoopInputFile, |
| 38 | + fileUri: URI, |
| 39 | + hadoopConf: Configuration) |
| 40 | + extends RapidsInputFile { |
| 41 | + |
| 42 | + override def path(): String = delegate.path() |
| 43 | + |
| 44 | + @throws[IOException] |
| 45 | + override def getLength(): Long = delegate.getLength() |
| 46 | + |
| 47 | + @throws[IOException] |
| 48 | + override def getLastModificationTime(): OptionalLong = delegate.getLastModificationTime() |
| 49 | + |
| 50 | + @throws[IOException] |
| 51 | + override def open(): SeekableInputStream = delegate.open() |
| 52 | + |
| 53 | + @throws[IOException] |
| 54 | + override def readVectored( |
| 55 | + output: HostMemoryBuffer, |
| 56 | + copyRanges: java.util.List[RapidsInputFile.CopyRange]): Unit = { |
| 57 | + val ranges = copyRanges.asScala.map { r => |
| 58 | + IntRangeWithOffset(r.getInputOffset, r.getLength, r.getOutputOffset) |
| 59 | + }.toSeq |
| 60 | + require( |
| 61 | + PerfIO.readToHostMemory(hadoopConf, output, fileUri, ranges).isDefined, |
| 62 | + "expected to use PerfIO to read") |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Issue a single suffix-range read for the last {@code length} bytes. Avoids |
| 67 | + * the {@code getLength()} round-trip the default {@link RapidsInputFile#readTail} |
| 68 | + * would make. PerfIO resolves the GCS suffix range internally. |
| 69 | + */ |
| 70 | + @throws[IOException] |
| 71 | + override def readTail(length: Long, output: HostMemoryBuffer): Unit = { |
| 72 | + if (length == 0) { |
| 73 | + return |
| 74 | + } |
| 75 | + if (length < 0) { |
| 76 | + throw new IllegalArgumentException("length must be non-negative") |
| 77 | + } |
| 78 | + val ranges = Seq[RangeWithOffset](SuffixRangeWithOffset(length, /*destOffset*/ 0L)) |
| 79 | + require( |
| 80 | + PerfIO.readToHostMemory(hadoopConf, output, fileUri, ranges).isDefined, |
| 81 | + "expected to use PerfIO to read") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +object GCSInputFile { |
| 86 | + @throws[IOException] |
| 87 | + def create(filePath: Path, conf: Configuration): GCSInputFile = { |
| 88 | + new GCSInputFile(HadoopInputFile.create(filePath, conf), filePath.toUri, conf) |
| 89 | + } |
| 90 | +} |
0 commit comments