Skip to content

Commit 391b562

Browse files
committed
fix #392 - add and test DocumentWriteSet.addAs methods
1 parent c7ba011 commit 391b562

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

src/main/java/com/marklogic/client/document/DocumentWriteSet.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public interface DocumentWriteSet extends Set<DocumentWriteOperation> {
4848
*/
4949
public DocumentWriteSet add(String docId, AbstractWriteHandle contentHandle);
5050

51+
/**
52+
* Adds to this write set a document with the given docId (server uri)
53+
* and contents.
54+
*
55+
* The IO class must have been registered before creating the database client.
56+
* By default, standard Java IO classes for document content are registered.
57+
*
58+
* @param docId the URI identifier for the document
59+
* @param content an IO representation of the document content
60+
* @return this instance (for method chaining)
61+
*/
62+
public DocumentWriteSet addAs(String docId, Object content);
63+
5164
/**
5265
* Adds to this write set a document with the given docId (server uri),
5366
* metadata, and contents provided by the handle
@@ -58,6 +71,20 @@ public interface DocumentWriteSet extends Set<DocumentWriteOperation> {
5871
*/
5972
public DocumentWriteSet add(String docId, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle);
6073

74+
/**
75+
* Adds to this write set a document with the given docId (server uri),
76+
* metadata, and contents.
77+
*
78+
* The IO class must have been registered before creating the database client.
79+
* By default, standard Java IO classes for document content are registered.
80+
*
81+
* @param docId the URI identifier for the document
82+
* @param metadataHandle a handle for writing the metadata of the document
83+
* @param content an IO representation of the document content
84+
* @return this instance (for method chaining)
85+
*/
86+
public DocumentWriteSet addAs(String docId, DocumentMetadataWriteHandle metadataHandle, Object content);
87+
6188
/**
6289
* Adds to this write set a document with the given uri template, and
6390
* contents provided by the handle

src/main/java/com/marklogic/client/impl/DocumentWriteSetImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.marklogic.client.impl;
1717

18+
import com.marklogic.client.DatabaseClientFactory;
19+
import com.marklogic.client.impl.Utilities;
1820
import com.marklogic.client.document.DocumentDescriptor;
1921
import com.marklogic.client.document.DocumentWriteSet;
2022
import com.marklogic.client.document.DocumentWriteOperation;
@@ -23,6 +25,7 @@
2325
import com.marklogic.client.io.Format;
2426
import com.marklogic.client.io.StringHandle;
2527
import com.marklogic.client.io.marker.AbstractWriteHandle;
28+
import com.marklogic.client.io.marker.ContentHandle;
2629
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
2730

2831
import java.util.LinkedHashSet;
@@ -46,12 +49,26 @@ public DocumentWriteSet add(String docId, AbstractWriteHandle contentHandle) {
4649
return this;
4750
}
4851

52+
public DocumentWriteSet addAs(String docId, Object content) {
53+
return addAs(docId, null, content);
54+
}
55+
4956
public DocumentWriteSet add(String docId, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle) {
5057
add(new DocumentWriteOperationImpl(OperationType.DOCUMENT_WRITE,
5158
docId, metadataHandle, contentHandle));
5259
return this;
5360
}
5461

62+
public DocumentWriteSet addAs(String docId, DocumentMetadataWriteHandle metadataHandle, Object content) {
63+
if (content == null) throw new IllegalArgumentException("content must not be null");
64+
65+
Class<?> as = content.getClass();
66+
ContentHandle<?> handle = DatabaseClientFactory.getHandleRegistry().makeHandle(as);
67+
Utilities.setHandleContent(handle, content);
68+
69+
return add(docId, metadataHandle, handle);
70+
}
71+
5572
public DocumentWriteSet add(DocumentDescriptor desc, AbstractWriteHandle contentHandle) {
5673
add(new DocumentWriteOperationImpl(OperationType.DOCUMENT_WRITE,
5774
desc.getUri(), null, contentHandle));

src/test/java/com/marklogic/client/test/BulkReadWriteTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ public class BulkReadWriteTest {
7979
private static final String COUNTRIES_FILE = "countryInfo.txt";
8080
private static final String CITIES_FILE = "cities_above_300K.txt";
8181
static final int RECORDS_EXPECTED = 1363;
82-
private static JAXBContext context = null;
8382

8483
@BeforeClass
8584
public static void beforeClass() throws JAXBException {
8685
Common.connect();
87-
context = JAXBContext.newInstance(City.class);
86+
DatabaseClientFactory.getHandleRegistry().register(
87+
JAXBHandle.newFactory(City.class)
88+
);
8889
//System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
8990
}
9091
@AfterClass
@@ -101,18 +102,13 @@ interface CityWriter {
101102

102103
private class BulkCityWriter implements CityWriter {
103104
private XMLDocumentManager docMgr = Common.client.newXMLDocumentManager();
104-
private JAXBContext context;
105105
private DocumentWriteSet writeSet = docMgr.newWriteSet();
106106

107107
BulkCityWriter() throws JAXBException {
108-
context = JAXBContext.newInstance(City.class);
109108
}
110109

111110
public void addCity(City city) {
112-
JAXBHandle<City> handle = new JAXBHandle<City>(context);
113-
// set the handle to the POJO instance
114-
handle.set(city);
115-
writeSet.add( DIRECTORY + city.getGeoNameId() + ".xml", handle );
111+
writeSet.addAs( DIRECTORY + city.getGeoNameId() + ".xml", city );
116112
}
117113

118114
public void finishBatch() {
@@ -295,7 +291,6 @@ public void testD_JsonLoad() {
295291
}
296292

297293
private void validateRecord(DocumentRecord record) {
298-
JAXBHandle<City> handle = new JAXBHandle<City>(context);
299294
assertNotNull("DocumentRecord should never be null", record);
300295
assertNotNull("Document uri should never be null", record.getUri());
301296
assertTrue("Document uri should start with " + DIRECTORY, record.getUri().startsWith(DIRECTORY));
@@ -305,7 +300,7 @@ private void validateRecord(DocumentRecord record) {
305300
record.getMimetype());
306301
*/
307302
if ( record.getUri().equals(DIRECTORY + "1205733.xml") ) {
308-
City chittagong = record.getContent(handle).get();
303+
City chittagong = record.getContentAs(City.class);
309304
validateChittagong(chittagong);
310305
}
311306
}

0 commit comments

Comments
 (0)