ADLSInputStream.java org::apache::iceberg::azure::adlsv2::ADLSInputStream org::apache::iceberg::azure::adlsv2 /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg.azure.adlsv2; importcom.azure.storage.file.datalake.DataLakeFileClient; importcom.azure.storage.file.datalake.models.DataLakeFileOpenInputStreamResult; importcom.azure.storage.file.datalake.models.FileRange; importcom.azure.storage.file.datalake.options.DataLakeFileInputStreamOptions; importjava.io.IOException; importjava.io.InputStream; importjava.util.Arrays; importorg.apache.iceberg.azure.AzureProperties; importorg.apache.iceberg.io.FileIOMetricsContext; importorg.apache.iceberg.io.IOUtil; importorg.apache.iceberg.io.RangeReadable; importorg.apache.iceberg.io.SeekableInputStream; importorg.apache.iceberg.metrics.Counter; importorg.apache.iceberg.metrics.MetricsContext; importorg.apache.iceberg.metrics.MetricsContext.Unit; importorg.apache.iceberg.relocated.com.google.common.base.Joiner; importorg.apache.iceberg.relocated.com.google.common.base.Preconditions; importorg.apache.iceberg.relocated.com.google.common.io.ByteStreams; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; classADLSInputStreamextendsSeekableInputStreamimplementsRangeReadable{ privatestaticfinalLoggerLOG=LoggerFactory.getLogger(ADLSInputStream.class); //maxskipsize,ifskipislarger,thenthestreamwillbereopenedatnewposition privatestaticfinalintSKIP_SIZE=1024*1024; privatefinalStackTraceElement[]createStack; privatefinalDataLakeFileClientfileClient; privateLongfileSize; privatefinalAzurePropertiesazureProperties; privateInputStreamstream; privatelongpos; privatelongnext; privatebooleanclosed; privatefinalCounterreadBytes; privatefinalCounterreadOperations; ADLSInputStream( DataLakeFileClientfileClient, LongfileSize, AzurePropertiesazureProperties, MetricsContextmetrics){ this.fileClient=fileClient; this.fileSize=fileSize; this.azureProperties=azureProperties; this.readBytes=metrics.counter(FileIOMetricsContext.READ_BYTES,Unit.BYTES); this.readOperations=metrics.counter(FileIOMetricsContext.READ_OPERATIONS); this.createStack=Thread.currentThread().getStackTrace(); openStream(); } privatevoidopenStream(){ DataLakeFileOpenInputStreamResultresult= fileClient.openInputStream(getInputOptions(newFileRange(pos))); this.fileSize=result.getProperties().getFileSize(); this.stream=result.getInputStream(); } privateDataLakeFileInputStreamOptionsgetInputOptions(FileRangerange){ DataLakeFileInputStreamOptionsoptions=newDataLakeFileInputStreamOptions(); azureProperties.adlsReadBlockSize().ifPresent(options::setBlockSize); options.setRange(range); returnoptions; } @Override publiclonggetPos(){ returnnext; } @Override publicvoidseek(longnewPos){ Preconditions.checkState(!closed,"Cannotseek:alreadyclosed"); Preconditions.checkArgument(newPos>=0,"Cannotseek:position%sisnegative",newPos); //thisallowsaseekbeyondtheendofthestreambutthenextreadwillfail this.next=newPos; } @Override publicintread()throwsIOException{ Preconditions.checkState(!closed,"Cannotread:alreadyclosed"); positionStream(); pos+=1; next+=1; readBytes.increment(); readOperations.increment(); returnstream.read(); } @Override publicintread(byte[]b,intoff,intlen)throwsIOException{ Preconditions.checkState(!closed,"Cannotread:alreadyclosed"); positionStream(); intbytesRead=stream.read(b,off,len); pos+=bytesRead; next+=bytesRead; readBytes.increment(bytesRead); readOperations.increment(); returnbytesRead; } privatevoidpositionStream()throwsIOException{ if((stream!=null)&&(next==pos)){ //alreadyatspecifiedposition return; } if((stream!=null)&&(next>pos)){ //seekingforwards longskip=next-pos; if(skip<=Math.max(stream.available(),SKIP_SIZE)){ //alreadybufferedorseekissmallenough try{ ByteStreams.skipFully(stream,skip); this.pos=next; return; }catch(IOExceptionignored){ //willretrybyre-openingthestream } } } //closethestreamandopenatdesiredposition this.pos=next; openStream(); } @Override publicvoidreadFully(longposition,byte[]buffer,intoffset,intlength)throwsIOException{ Preconditions.checkPositionIndexes(offset,offset+length,buffer.length); FileRangerange=newFileRange(position,position+length); IOUtil.readFully(openRange(range),buffer,offset,length); } @Override publicintreadTail(byte[]buffer,intoffset,intlength)throwsIOException{ Preconditions.checkPositionIndexes(offset,offset+length,buffer.length); if(this.fileSize==null){ this.fileSize=fileClient.getProperties().getFileSize(); } longreadStart=fileSize-length; returnIOUtil.readRemaining(openRange(newFileRange(readStart)),buffer,offset,length); } privateInputStreamopenRange(FileRangerange){ returnfileClient.openInputStream(getInputOptions(range)).getInputStream(); } @Override publicvoidclose()throwsIOException{ super.close(); this.closed=true; if(stream!=null){ stream.close(); } } @SuppressWarnings("checkstyle:NoFinalizer") @Override protectedvoidfinalize()throwsThrowable{ super.finalize(); if(!closed){ close();//releasingresourcesismoreimportantthanprintingthewarning Stringtrace=Joiner.on("\n\t").join(Arrays.copyOfRange(createStack,1,createStack.length)); LOG.warn("Unclosedinputstreamcreatedby:\n\t{}",trace); } } }