exceptionfactory commented on code in PR #11052: URL: https://github.com/apache/nifi/pull/11052#discussion_r3010001832
########## nifi-extension-bundles/nifi-iceberg-bundle/nifi-iceberg-gcs/src/main/java/org/apache/nifi/services/iceberg/gcs/GoogleCloudStorageInputFile.java: ########## @@ -0,0 +1,157 @@ +/* + * 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.nifi.services.iceberg.gcs; + +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.SeekableInputStream; + +import java.io.IOException; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static java.net.HttpURLConnection.HTTP_OK; + +/** + * Google Cloud Storage implementation of Apache Iceberg InputFile using HttpClient for REST operations + */ +class GoogleCloudStorageInputFile implements InputFile { + + private static final String QUESTION_MARK = "?"; + private static final char AMPERSAND = '&'; + private static final String FIELDS_SIZE_QUERY = "fields=size"; + + private static final Pattern SIZE_PATTERN = Pattern.compile("\"size\"\\s*:\\s*\"(\\d+)\""); + private static final int SIZE_GROUP = 1; + + private final HttpClientProvider httpClientProvider; + + private final GoogleCloudStorageProperties storageProperties; + + private final GoogleCloudStorageLocation location; + + private final String path; + + private volatile Long cachedLength; + + GoogleCloudStorageInputFile( + final HttpClientProvider httpClientProvider, + final GoogleCloudStorageProperties storageProperties, + final GoogleCloudStorageLocation location, + final String path, + final Long cachedLength + ) { + this.httpClientProvider = httpClientProvider; + this.storageProperties = storageProperties; + this.location = location; + this.path = path; + this.cachedLength = cachedLength; + } + + /** + * Get Input File length in bytes and fetch remote object size when length is not cached + * + * @return Input File length in bytes + */ + @Override + public long getLength() { + if (cachedLength == null) { + cachedLength = getObjectSize(); + } + return cachedLength; + } + + /** + * Create Seekable InputStream for InputFile reading + * + * @return Seekable InputStream + */ + @Override + public SeekableInputStream newStream() { + return new GoogleCloudStorageSeekableInputStream(httpClientProvider, storageProperties, location, cachedLength); + } + + /** + * Get InputFile Location + * + * @return InputFile Location + */ + @Override + public String location() { + return path; + } + + /** + * Get status of InputFile existence based on Metadata URI + * + * @return InputFile existence status + */ + @Override + public boolean exists() { + final String uri = getMetadataUri(); + final HttpRequest request = httpClientProvider.newRequestBuilder(uri).GET().build(); + + try { + final HttpResponse<Void> response = httpClientProvider.send(request, HttpResponse.BodyHandlers.discarding()); + return response.statusCode() == HTTP_OK; Review Comment: That's a good point, I'll make an adjustment to throw an exception for status codes other than 200 and 404. -- 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]
