11package com .marklogic .client .ext .batch ;
22
33import com .marklogic .client .DatabaseClient ;
4- import com .marklogic .client .document .DocumentManager ;
54import com .marklogic .client .document .DocumentWriteOperation ;
6- import com .marklogic .client .document .DocumentWriteSet ;
75import com .marklogic .client .document .ServerTransform ;
86import com .marklogic .client .io .Format ;
97
1412 * REST API-based implementation, using the Java Client API. By default, this will call release() on each of the
1513 * DatabaseClient objects that are passed in. Be sure to disable this if you want to keep using those DatabaseClient
1614 * objects.
15+ * <p>
16+ * To customize what this does with every batch, you can set a new instance of BatchHandler. This class defaults to using
17+ * DefaultBatchHandler; it'll pass its instances of Format and ServerTransform to that class.
1718 */
1819public class RestBatchWriter extends BatchWriterSupport {
1920
2021 private List <DatabaseClient > databaseClients ;
2122 private int clientIndex = 0 ;
2223 private boolean releaseDatabaseClients = true ;
23- private ServerTransform serverTransform ;
2424
25- // Specific to Client API 4.0.+
2625 private Format contentFormat ;
26+ private ServerTransform serverTransform ;
27+ private BatchHandler batchHandler ;
2728
2829 public RestBatchWriter (DatabaseClient databaseClient ) {
2930 this (databaseClient , true );
@@ -46,6 +47,17 @@ public void write(List<? extends DocumentWriteOperation> items) {
4647 executeRunnable (runnable , items );
4748 }
4849
50+ @ Override
51+ public void initialize () {
52+ super .initialize ();
53+ if (batchHandler == null ) {
54+ DefaultBatchHandler dbh = new DefaultBatchHandler ();
55+ dbh .setContentFormat (contentFormat );
56+ dbh .setServerTransform (serverTransform );
57+ this .batchHandler = dbh ;
58+ }
59+ }
60+
4961 protected DatabaseClient determineDatabaseClientToUse () {
5062 if (clientIndex >= databaseClients .size ()) {
5163 clientIndex = 0 ;
@@ -59,42 +71,11 @@ protected Runnable buildRunnable(final DatabaseClient client, final List<? exten
5971 return new Runnable () {
6072 @ Override
6173 public void run () {
62- DocumentManager <?, ?> mgr = buildDocumentManager (client );
63- if (contentFormat != null ) {
64- mgr .setContentFormat (contentFormat );
65- }
66-
67- DocumentWriteSet set = mgr .newWriteSet ();
68- for (DocumentWriteOperation item : items ) {
69- set .add (item );
70- }
71- int count = set .size ();
72- if (logger .isDebugEnabled ()) {
73- logger .debug ("Writing " + count + " documents to MarkLogic" );
74- }
75- if (serverTransform != null ) {
76- mgr .write (set , serverTransform );
77- } else {
78- mgr .write (set );
79- }
80- if (logger .isInfoEnabled ()) {
81- logger .info ("Wrote " + count + " documents to MarkLogic" );
82- }
74+ batchHandler .handleBatch (client , items );
8375 }
8476 };
8577 }
8678
87- /**
88- * Factored out so it can be overridden for e.g. those already using MarkLogic 9, who may need to set the content
89- * format on the manager.
90- *
91- * @param client
92- * @return
93- */
94- protected DocumentManager <?, ?> buildDocumentManager (DatabaseClient client ) {
95- return client .newDocumentManager ();
96- }
97-
9879 @ Override
9980 public void waitForCompletion () {
10081 super .waitForCompletion ();
0 commit comments