Class FileBinaryRequestExecutor
- java.lang.Object
-
- com.cognite.client.servicesV1.executor.FileBinaryRequestExecutor
-
public abstract class FileBinaryRequestExecutor extends Object
This request executor implements specific behavior to deal with very large request/response bodies when operating on file binaries. It will cap in-memory bodies at 200MiB and use temporary blob storage to host the binary. This "overflow to disk" behavior is supported for both downloads and uploads. This class will execute an okhttp3 request on a separate thread and publish the result via aCompletableFuture
. This allows the client code to spin off multiple concurrent request without blocking the main thread. This represents the "blocking IO on a separate thread" pattern, and will work fine for client workloads (limited number of concurrent requests).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileBinaryRequestExecutor.Builder
-
Field Summary
Fields Modifier and Type Field Description protected static org.slf4j.Logger
LOG
-
Constructor Summary
Constructors Constructor Description FileBinaryRequestExecutor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description com.cognite.client.dto.FileBinary
downloadBinary(okhttp3.Request request)
Executes a given request and returns the response body as a file binary.CompletableFuture<com.cognite.client.dto.FileBinary>
downloadBinaryAsync(okhttp3.Request request)
Executes a given request and returns the response body as a file binary.FileBinaryRequestExecutor
enableDeleteTempFile(boolean enable)
Configure how to treat a temp blob after an upload.FileBinaryRequestExecutor
enableForceTempStorage(boolean enable)
Forces the use of temp storage for all binaries--not just the >200MiB ones.static FileBinaryRequestExecutor
of(okhttp3.OkHttpClient client)
ResponseBinary
uploadBinary(com.cognite.client.dto.FileBinary fileBinary, URL targetURL)
Writes a file binary to the target URL.CompletableFuture<ResponseBinary>
uploadBinaryAsync(com.cognite.client.dto.FileBinary fileBinary, URL targetURL)
Executes a given request and returns the response body as a file binary.FileBinaryRequestExecutor
withExecutor(Executor executor)
Sets the executor to use for running the api requests.FileBinaryRequestExecutor
withMaxRetries(int retries)
Sets the maximum number of retries.FileBinaryRequestExecutor
withTempStoragePath(URI path)
Sets the temporary storage path for storing large file binaries.FileBinaryRequestExecutor
withValidResponseCodes(List<Integer> validResponseCodes)
Specifies a set of valid http response codes *in addition* to the 200-range.
-
-
-
Method Detail
-
of
public static FileBinaryRequestExecutor of(okhttp3.OkHttpClient client)
-
withExecutor
public FileBinaryRequestExecutor withExecutor(Executor executor)
Sets the executor to use for running the api requests. The default executor is aForkJoinPool
with a target parallelism of four threads per core.- Parameters:
executor
-- Returns:
-
withMaxRetries
public FileBinaryRequestExecutor withMaxRetries(int retries)
Sets the maximum number of retries. The default setting is 3.- Parameters:
retries
-- Returns:
-
withValidResponseCodes
public FileBinaryRequestExecutor withValidResponseCodes(List<Integer> validResponseCodes)
Specifies a set of valid http response codes *in addition* to the 200-range. By default, any 2xx response is considered a valid response. By specifying additional codes, this executor will return responses from outside the 200-range. This could be useful in case you want to handle non-200 responses with custom logic. For example, duplicate detection and constraint violations are reported as non-200 responses from the Cognite API.- Parameters:
validResponseCodes
- A list of valid response codes.- Returns:
-
enableForceTempStorage
public FileBinaryRequestExecutor enableForceTempStorage(boolean enable)
Forces the use of temp storage for all binaries--not just the >200MiB ones. The default isfalse
.- Parameters:
enable
-- Returns:
-
withTempStoragePath
public FileBinaryRequestExecutor withTempStoragePath(URI path)
Sets the temporary storage path for storing large file binaries. If the binary is >200 MiB it will be stored in temp storage instead of in memory. The following storage providers are supported: - Google Cloud Storage. Specify the temp path asgs://<my-storage-bucket>/<my-path>/
.- Parameters:
path
-- Returns:
-
enableDeleteTempFile
public FileBinaryRequestExecutor enableDeleteTempFile(boolean enable)
Configure how to treat a temp blob after an upload. This setting only affects behavior when uploading file binaries to the Cognite API--it has no effect on downloading file binaries. When set totrue
, the temp file (if present) will be removed after a successful upload. If the file binary is memory-based (which is the default for small and medium sized files), this setting has no effect. When set tofalse
, the temp file (if present) will not be deleted. The default setting istrue
.- Parameters:
enable
-- Returns:
-
downloadBinaryAsync
public CompletableFuture<com.cognite.client.dto.FileBinary> downloadBinaryAsync(okhttp3.Request request)
Executes a given request and returns the response body as a file binary. Checks for transient server errors and retires the request until a valid response is produced, or the max number of retries is reached. This method executes as a blocking I/O operation on a separate thread--the calling thread is not blocked and can continue working on its tasks. Each retry is performed with exponential back-off in case the api is overloaded. If no valid response can be produced, this method will throw an exception.- Parameters:
request
- The request to execute- Returns:
-
downloadBinary
public com.cognite.client.dto.FileBinary downloadBinary(okhttp3.Request request) throws Exception
Executes a given request and returns the response body as a file binary. Checks for transient server errors and retires the request until a valid response is produced, or the max number of retries is reached. This method blocks until aResponse
is produced. Each retry is performed with exponential back-off in case the api is overloaded. If no valid response can be produced, this method will throw an exception. The async version of this method isdownloadBinaryAsync
- Throws:
Exception
-
uploadBinaryAsync
public CompletableFuture<ResponseBinary> uploadBinaryAsync(com.cognite.client.dto.FileBinary fileBinary, URL targetURL)
Executes a given request and returns the response body as a file binary. Checks for transient server errors and retires the request until a valid response is produced, or the max number of retries is reached. This method executes as a blocking I/O operation on a separate thread--the calling thread is not blocked and can continue working on its tasks. Each retry is performed with exponential back-off in case the api is overloaded. If no valid response can be produced, this method will throw an exception.
-
uploadBinary
public ResponseBinary uploadBinary(com.cognite.client.dto.FileBinary fileBinary, URL targetURL) throws Exception
Writes a file binary to the target URL. Supports both in-memory bytes streams and temp storage blobs. Checks for transient server errors and retires the request until a valid response is produced, or the max number of retries is reached. This method blocks until aResponse
is produced. Each retry is performed with exponential back-off in case the api is overloaded. If no valid response can be produced, this method will throw an exception. The async version of this method isuploadBinaryAsync
- Throws:
Exception
-
-