Class AbstractGoogleAsyncWriteChannel<T>
- java.lang.Object
-
- com.google.cloud.hadoop.util.AbstractGoogleAsyncWriteChannel<T>
-
- Type Parameters:
T- The type of the result of the completed upload operation.
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,WritableByteChannel
public abstract class AbstractGoogleAsyncWriteChannel<T> extends Object implements WritableByteChannel
Skeletal implementation of a WritableByteChannel that executes an asynchronous upload operation and optionally handles the result.
-
-
Field Summary
Fields Modifier and Type Field Description protected AsyncWriteChannelOptionschannelOptionsprotected com.google.common.hash.HashercumulativeCrc32cHasherprotected static com.google.common.flogger.GoogleLoggerloggerprotected ExecutorServicethreadPoolprotected Future<T>uploadOperation
-
Constructor Summary
Constructors Constructor Description AbstractGoogleAsyncWriteChannel(ExecutorService threadPool, AsyncWriteChannelOptions channelOptions)Construct a new channel using the given ExecutorService to run background uploads.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this channel.protected abstract StringgetResourceString()voidhandleResponse(T response)Handle the API response.voidinitialize()Initialize this channel object for writing.booleanisDirectUploadEnabled()Returns true if direct media uploads are enabled.booleanisOpen()Tells whether this channel is open.abstract voidstartUpload(InputStream pipeSource)Create a new thread which handles the upload.intwrite(ByteBuffer buffer)Writes contents of the given buffer to this channel.
-
-
-
Field Detail
-
logger
protected static final com.google.common.flogger.GoogleLogger logger
-
threadPool
protected final ExecutorService threadPool
-
channelOptions
protected final AsyncWriteChannelOptions channelOptions
-
cumulativeCrc32cHasher
protected final com.google.common.hash.Hasher cumulativeCrc32cHasher
-
-
Constructor Detail
-
AbstractGoogleAsyncWriteChannel
public AbstractGoogleAsyncWriteChannel(ExecutorService threadPool, AsyncWriteChannelOptions channelOptions)
Construct a new channel using the given ExecutorService to run background uploads.
-
-
Method Detail
-
handleResponse
public void handleResponse(T response) throws IOException
Handle the API response.This method is invoked after the upload has completed on the same thread that invokes close(). It can throw IOException if checksum matching is enabled and mismatched.
- Parameters:
response- The API response object.- Throws:
IOException
-
isDirectUploadEnabled
public boolean isDirectUploadEnabled()
Returns true if direct media uploads are enabled.
-
write
public int write(ByteBuffer buffer) throws IOException
Writes contents of the given buffer to this channel.Note: The data that one writes gets written to a pipe which must not block if the pipe has sufficient buffer space. A success code returned from this method does not mean that the specific data was successfully written to the underlying storage. It simply means that there is no error at present. The data upload may encounter an error on a separate thread. Such error is not ignored; it shows up as an exception during a subsequent call to write() or close(). The only way to be sure of successful upload is when the close() method returns successfully.
- Specified by:
writein interfaceWritableByteChannel- Parameters:
buffer- buffer to write- Throws:
IOException- on IO error
-
isOpen
public boolean isOpen()
Tells whether this channel is open.
-
close
public void close() throws IOExceptionCloses this channel.Note: The method returns only after all data has been successfully written to GCS or if there is a non-retry-able error.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Throws:
IOException- on IO error
-
initialize
public void initialize() throws IOExceptionInitialize this channel object for writing.- Throws:
IOException
-
startUpload
public abstract void startUpload(InputStream pipeSource) throws IOException
Create a new thread which handles the upload.- Throws:
IOException
-
getResourceString
protected abstract String getResourceString()
-
-