Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit d072e59

Browse files
committed
#74 Added BatchHandler interface
1 parent fc9dc0a commit d072e59

File tree

4 files changed

+100
-37
lines changed

4 files changed

+100
-37
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.marklogic.client.ext.batch;
2+
3+
import com.marklogic.client.DatabaseClient;
4+
import com.marklogic.client.document.DocumentWriteOperation;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Allows for customizing what RestBatchWriter does with each batch of documents.
10+
*/
11+
public interface BatchHandler {
12+
13+
void handleBatch(DatabaseClient client, List<? extends DocumentWriteOperation> items);
14+
15+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.marklogic.client.ext.batch;
2+
3+
import com.marklogic.client.DatabaseClient;
4+
import com.marklogic.client.document.DocumentManager;
5+
import com.marklogic.client.document.DocumentWriteOperation;
6+
import com.marklogic.client.document.DocumentWriteSet;
7+
import com.marklogic.client.document.ServerTransform;
8+
import com.marklogic.client.ext.helper.LoggingObject;
9+
import com.marklogic.client.io.Format;
10+
11+
import java.util.List;
12+
13+
/**
14+
* Default implementation of BatchHandler that uses a DocumentManager to write documents, along with an optional
15+
* ServerTransform.
16+
*/
17+
public class DefaultBatchHandler extends LoggingObject implements BatchHandler {
18+
19+
private Format contentFormat;
20+
private ServerTransform serverTransform;
21+
22+
public DefaultBatchHandler() {
23+
}
24+
25+
public DefaultBatchHandler(Format contentFormat, ServerTransform serverTransform) {
26+
this.contentFormat = contentFormat;
27+
this.serverTransform = serverTransform;
28+
}
29+
30+
@Override
31+
public void handleBatch(DatabaseClient client, List<? extends DocumentWriteOperation> items) {
32+
DocumentManager<?, ?> mgr = buildDocumentManager(client);
33+
if (contentFormat != null) {
34+
mgr.setContentFormat(contentFormat);
35+
}
36+
37+
DocumentWriteSet set = mgr.newWriteSet();
38+
for (DocumentWriteOperation item : items) {
39+
set.add(item);
40+
}
41+
int count = set.size();
42+
if (logger.isDebugEnabled()) {
43+
logger.debug("Writing " + count + " documents to MarkLogic");
44+
}
45+
if (serverTransform != null) {
46+
mgr.write(set, serverTransform);
47+
} else {
48+
mgr.write(set);
49+
}
50+
if (logger.isInfoEnabled()) {
51+
logger.info("Wrote " + count + " documents to MarkLogic");
52+
}
53+
}
54+
55+
protected DocumentManager<?, ?> buildDocumentManager(DatabaseClient client) {
56+
return client.newDocumentManager();
57+
}
58+
59+
60+
public void setServerTransform(ServerTransform serverTransform) {
61+
this.serverTransform = serverTransform;
62+
}
63+
64+
public void setContentFormat(Format contentFormat) {
65+
this.contentFormat = contentFormat;
66+
}
67+
}

src/main/java/com/marklogic/client/ext/batch/RestBatchWriter.java

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.marklogic.client.ext.batch;
22

33
import com.marklogic.client.DatabaseClient;
4-
import com.marklogic.client.document.DocumentManager;
54
import com.marklogic.client.document.DocumentWriteOperation;
6-
import com.marklogic.client.document.DocumentWriteSet;
75
import com.marklogic.client.document.ServerTransform;
86
import com.marklogic.client.io.Format;
97

@@ -14,16 +12,19 @@
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
*/
1819
public 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();

src/main/java/com/marklogic/xcc/template/XccTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public XccTemplate(String uri) {
3838
public XccTemplate(ContentSource contentSource) {
3939
this.contentSource = contentSource;
4040
}
41-
41+
4242
public <T> T execute(XccCallback<T> callback) {
4343
Session session = contentSource.newSession();
4444
try {

0 commit comments

Comments
 (0)