Interface WriteBatcher

All Superinterfaces:
Batcher

public interface WriteBatcherextends Batcher

To facilitate long-running write jobs, batches documents added by many external threads and coordinates internal threads to send the batches round-robin to all appropriate hosts in the cluster. Appropriate hosts are those containing a forest associated with the database for the DatabaseClient provided to DataMovementManager. Many external threads (threads not managed by WriteBatcher) can concurrently add documents by calling WriteBatcher add or addAs. Each time enough documents are added to make a batch, the batch is added to an internal queue where the first available internal thread will pick it up and write it to the server. Since batches are not written until they are full, you should always call flushAsync() or flushAndWait() when no more documents will be written to ensure that any partial batch is written.

Sample Usage:

     WriteBatcher whb = dataMovementManager.newWriteBatcher()
         .withBatchSize(100)
         .withThreadCount(20)
         .onBatchSuccess(batch -> {
             logger.debug("batch # {}, so far: {}", batch.getJobBatchNumber(), batch.getJobWritesSoFar());
         })
         .onBatchFailure((batch,throwable) -> throwable.printStackTrace() );
     JobTicket ticket = dataMovementManager.startJob(whb);
     whb.add  ("doc1.txt", new StringHandle("doc1 contents"));
     whb.addAs("doc2.txt", "doc2 contents");

     whb.flushAndWait(); // send the two docs even though they're not a full batch
     dataMovementManager.stopJob(ticket);

Note: All Closeable content or metadata handles passed to add methods will be closed as soon as possible (after the batch is written). This is to avoid IO resource leakage. This differs from the normal usage of the Java Client API because WriteBatcher is asynchronous so there's no easy way to know which handles have finished writing and can therefore be closed. So to save confusion we close all handles for you. If you have a resource that must be closed after a batch is written, but is not closed by your handle, override the close method of any Closeable handle and close your resource there.