Skip to content

Commit b532dc3

Browse files
committed
Merge branch 'bulkReadWrite'
2 parents 0842bce + a02e8c2 commit b532dc3

File tree

4 files changed

+115
-43
lines changed

4 files changed

+115
-43
lines changed

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

+4-10
Original file line numberDiff line numberDiff line change
@@ -417,20 +417,14 @@ public void write(DocumentWriteSet writeSet, Transaction transaction) {
417417
}
418418

419419
public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction) {
420-
JacksonHandle jacksonHandle = new JacksonHandle();
421-
jacksonHandle = services.postBulkDocuments(
420+
Format defaultFormat = contentFormat;
421+
services.postBulkDocuments(
422422
requestLogger,
423423
writeSet,
424424
(transform != null) ? transform : getWriteTransform(),
425425
(transaction == null) ? null : transaction.getTransactionId(),
426-
jacksonHandle);
427-
JsonNode root = jacksonHandle.get();
428-
for (JsonNode item: root.get("documents")) {
429-
String uri = item.get("uri").asText();
430-
String mimetype = item.get("mime-type").asText();
431-
String category = item.get("category").get(0).asText();
432-
}
433-
426+
defaultFormat,
427+
null);
434428
}
435429

436430
// shortcut writers

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

+37-26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.net.ssl.SSLContext;
3434
import javax.net.ssl.SSLException;
3535
import javax.ws.rs.core.Cookie;
36+
import javax.ws.rs.core.HttpHeaders;
3637
import javax.ws.rs.core.MediaType;
3738
import javax.ws.rs.core.MultivaluedMap;
3839
import javax.ws.rs.core.StreamingOutput;
@@ -1722,9 +1723,9 @@ private void updateMimetype(ContentDescriptor descriptor, String mimetype) {
17221723
}
17231724
}
17241725

1725-
private String getHeaderMimetype(MultivaluedMap<String, String> headers) {
1726-
if (headers.containsKey("Content-Type")) {
1727-
List<String> values = headers.get("Content-Type");
1726+
private String getHeaderMimetype(Map<String, List<String>> headers) {
1727+
if (headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
1728+
List<String> values = headers.get(HttpHeaders.CONTENT_TYPE);
17281729
if (values != null) {
17291730
String contentType = values.get(0);
17301731
String mimetype = contentType.contains(";") ? contentType
@@ -1748,8 +1749,8 @@ private void updateLength(ContentDescriptor descriptor, long length) {
17481749
}
17491750

17501751
private long getHeaderLength(MultivaluedMap<String, String> headers) {
1751-
if (headers.containsKey("Content-Length")) {
1752-
List<String> values = headers.get("Content-Length");
1752+
if (headers.containsKey(HttpHeaders.CONTENT_LENGTH)) {
1753+
List<String> values = headers.get(HttpHeaders.CONTENT_LENGTH);
17531754
if (values != null) {
17541755
return Long.valueOf(values.get(0));
17551756
}
@@ -3297,14 +3298,13 @@ public <R extends AbstractReadHandle, W extends AbstractWriteHandle> R postResou
32973298
@Override
32983299
public <R extends AbstractReadHandle, W extends AbstractWriteHandle> R postResource(
32993300
RequestLogger reqlog, String path, RequestParameters params,
3300-
W[] input, Map<String, List>[] headers, R output) throws ResourceNotFoundException,
3301+
W[] input, Map<String, List<String>>[] headers, R output) throws ResourceNotFoundException,
33013302
ResourceNotResendableException, ForbiddenUserException,
33023303
FailedRequestException {
3303-
HandleImplementation outputBase = HandleAccessor.checkHandle(output,
3304-
"read");
3304+
HandleImplementation outputBase = HandleAccessor.checkHandle(output, "read");
33053305

3306-
String outputMimetype = outputBase.getMimetype();
3307-
Class as = outputBase.receiveAs();
3306+
String outputMimetype = outputBase != null ? outputBase.getMimetype() : null;
3307+
Class as = outputBase != null ? outputBase.receiveAs() : null;
33083308

33093309
ClientResponse response = null;
33103310
ClientResponse.Status status = null;
@@ -3371,45 +3371,49 @@ public <R extends AbstractReadHandle, W extends AbstractWriteHandle> R postResou
33713371
@Override
33723372
public void postBulkDocuments(
33733373
RequestLogger reqlog, DocumentWriteSet writeSet,
3374-
ServerTransform transform, String transactionId)
3374+
ServerTransform transform, Format defaultFormat, String transactionId)
33753375
throws ForbiddenUserException, FailedRequestException
33763376
{
3377-
postBulkDocuments(reqlog, writeSet, transform, transactionId, null);
3377+
postBulkDocuments(reqlog, writeSet, transform, transactionId, defaultFormat, null);
33783378
}
33793379

33803380
@Override
33813381
public <R extends AbstractReadHandle> R postBulkDocuments(
33823382
RequestLogger reqlog, DocumentWriteSet writeSet,
3383-
ServerTransform transform, String transactionId, R output)
3383+
ServerTransform transform, String transactionId, Format defaultFormat, R output)
33843384
throws ForbiddenUserException, FailedRequestException
33853385
{
33863386
ArrayList<AbstractWriteHandle> writeHandles = new ArrayList<AbstractWriteHandle>();
3387-
ArrayList<Map<String, List>> headerList = new ArrayList<Map<String, List>>();
3387+
ArrayList<Map<String, List<String>>> headerList = new ArrayList<Map<String, List<String>>>();
33883388
for ( DocumentWriteOperation write: writeSet ) {
33893389
HandleImplementation metadata =
33903390
HandleAccessor.checkHandle(write.getMetadata(), "write");
33913391
HandleImplementation content =
33923392
HandleAccessor.checkHandle(write.getContent(), "write");
3393-
MultivaluedMap headers = new MultivaluedMapImpl();
33943393
if ( metadata != null ) {
3395-
headers.add("Content-Type", metadata.getMimetype());
3394+
MultivaluedMap headers = new MultivaluedMapImpl();
3395+
headers.add(HttpHeaders.CONTENT_TYPE, metadata.getMimetype());
33963396
if ( write.getOperationType() == DocumentWriteOperation.OperationType.METADATA_DEFAULT ) {
3397-
headers.add("Content-Disposition",
3398-
ContentDisposition.type("inline").build().toString()
3399-
);
3397+
headers.add("Content-Disposition", "inline; category=metadata");
34003398
} else {
34013399
headers.add("Content-Disposition",
34023400
ContentDisposition
34033401
.type("attachment")
34043402
.fileName(write.getUri())
3405-
.build().toString()
3403+
.build().toString() +
3404+
"; category=metadata"
34063405
);
34073406
}
34083407
headerList.add(headers);
34093408
writeHandles.add(write.getMetadata());
34103409
}
34113410
if ( content != null ) {
3412-
headers.add("Content-Type", content.getMimetype());
3411+
MultivaluedMap headers = new MultivaluedMapImpl();
3412+
String mimeType = content.getMimetype();
3413+
if ( mimeType == null && defaultFormat != null ) {
3414+
mimeType = defaultFormat.getDefaultMimetype();
3415+
}
3416+
headers.add(HttpHeaders.CONTENT_TYPE, mimeType);
34133417
headers.add("Content-Disposition",
34143418
ContentDisposition
34153419
.type("attachment")
@@ -3427,7 +3431,7 @@ public <R extends AbstractReadHandle> R postBulkDocuments(
34273431
"documents",
34283432
params,
34293433
(AbstractWriteHandle[]) writeHandles.toArray(new AbstractWriteHandle[0]),
3430-
(Map<String, List>[]) headerList.toArray(new HashMap[0]),
3434+
(Map<String, List<String>>[]) headerList.toArray(new HashMap[0]),
34313435
output);
34323436
}
34333437

@@ -3929,7 +3933,7 @@ private <W extends AbstractWriteHandle> boolean addParts(
39293933

39303934
private <W extends AbstractWriteHandle> boolean addParts(
39313935
MultiPart multiPart, RequestLogger reqlog, String[] mimetypes,
3932-
W[] input, Map<String, List>[] headers) {
3936+
W[] input, Map<String, List<String>>[] headers) {
39333937
if (mimetypes != null && mimetypes.length != input.length)
39343938
throw new IllegalArgumentException(
39353939
"Mismatch between count of mimetypes and input");
@@ -3944,13 +3948,19 @@ private <W extends AbstractWriteHandle> boolean addParts(
39443948
AbstractWriteHandle handle = input[i];
39453949
HandleImplementation handleBase = HandleAccessor.checkHandle(
39463950
handle, "write");
3947-
Object value = handleBase.sendContent();
3948-
String inputMimetype = (mimetypes != null) ? mimetypes[i]
3949-
: handleBase.getMimetype();
39503951

39513952
if (!hasStreamingPart)
39523953
hasStreamingPart = !handleBase.isResendable();
39533954

3955+
Object value = handleBase.sendContent();
3956+
3957+
String inputMimetype = null;
3958+
if ( mimetypes != null ) inputMimetype = mimetypes[i];
3959+
if ( inputMimetype == null && headers != null ) {
3960+
inputMimetype = getHeaderMimetype(headers[i]);
3961+
}
3962+
if ( inputMimetype == null ) inputMimetype = handleBase.getMimetype();
3963+
39543964
String[] typeParts = (inputMimetype != null && inputMimetype
39553965
.contains("/")) ? inputMimetype.split("/", 2) : null;
39563966

@@ -4737,4 +4747,5 @@ public InputStream match(String[] docIds, String[] candidateRules, ServerTransfo
47374747

47384748
return entity;
47394749
}
4750+
47404751
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public DocumentPage getBulkDocuments(RequestLogger logger, QueryDefinition query
8282
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
8383

8484
public void postBulkDocuments(RequestLogger logger, DocumentWriteSet writeSet,
85-
ServerTransform transform, String transactionId)
85+
ServerTransform transform, Format defaultFormat, String transactionId)
8686
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
8787
public <T extends AbstractReadHandle> T postBulkDocuments(RequestLogger logger, DocumentWriteSet writeSet,
88-
ServerTransform transform, String transactionId, T output)
88+
ServerTransform transform, String transactionId, Format defaultFormat, T output)
8989
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
9090

9191
public void putDocument(RequestLogger logger, DocumentDescriptor desc, String transactionId,
@@ -186,7 +186,7 @@ public <R extends AbstractReadHandle, W extends AbstractWriteHandle> R postResou
186186
ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
187187
public <R extends AbstractReadHandle, W extends AbstractWriteHandle> R postResource(
188188
RequestLogger reqlog, String path, RequestParameters params,
189-
W[] input, Map<String, List>[] headers, R output)
189+
W[] input, Map<String, List<String>>[] headers, R output)
190190
throws ResourceNotFoundException, ResourceNotResendableException,
191191
ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
192192
public ServiceResultIterator postIteratedResource(

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

+71-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import java.io.IOException;
2020
import java.util.HashMap;
2121
import java.util.Map;
22+
import java.util.logging.FileHandler;
23+
import java.util.logging.Handler;
24+
import java.util.logging.Logger;
25+
import java.util.logging.Level;
2226

2327
import javax.xml.bind.JAXBContext;
2428
import javax.xml.bind.JAXBException;
@@ -39,10 +43,15 @@
3943
import com.marklogic.client.document.DocumentPage;
4044
import com.marklogic.client.document.DocumentRecord;
4145
import com.marklogic.client.document.DocumentWriteSet;
46+
import com.marklogic.client.document.JSONDocumentManager;
47+
import com.marklogic.client.document.TextDocumentManager;
4248
import com.marklogic.client.document.XMLDocumentManager;
49+
import com.marklogic.client.io.DocumentMetadataHandle;
4350
import com.marklogic.client.io.Format;
51+
import com.marklogic.client.io.JacksonHandle;
4452
import com.marklogic.client.io.JAXBHandle;
4553
import com.marklogic.client.io.SearchHandle;
54+
import com.marklogic.client.io.StringHandle;
4655
import com.marklogic.client.query.DeleteQueryDefinition;
4756
import com.marklogic.client.query.QueryManager;
4857
import com.marklogic.client.query.StructuredQueryBuilder;
@@ -63,9 +72,12 @@ public class BulkReadWriteTest {
6372
public static void beforeClass() throws JAXBException {
6473
Common.connect();
6574
context = JAXBContext.newInstance(City.class);
75+
//System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
76+
//System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
6677
}
6778
@AfterClass
6879
public static void afterClass() {
80+
cleanUp();
6981
Common.release();
7082
}
7183

@@ -306,7 +318,7 @@ public void testBulkLoad() throws IOException, JAXBException {
306318
}
307319

308320
@Test
309-
public void testBulkRead() throws IOException, JAXBException {
321+
public void testBulkRead() {
310322
XMLDocumentManager docMgr = Common.client.newXMLDocumentManager();
311323

312324
DocumentPage page = docMgr.read(DIRECTORY + "1016670.xml", DIRECTORY + "108410.xml", DIRECTORY + "1205733.xml");
@@ -320,7 +332,7 @@ public void testBulkRead() throws IOException, JAXBException {
320332
}
321333

322334
@Test
323-
public void testBulkSearch() throws IOException, JAXBException {
335+
public void testBulkSearch() {
324336
XMLDocumentManager docMgr = Common.client.newXMLDocumentManager();
325337

326338
SearchHandle searchHandle = new SearchHandle();
@@ -335,7 +347,37 @@ public void testBulkSearch() throws IOException, JAXBException {
335347
assertEquals("Failed to find number of records expected", RECORDS_EXPECTED, page.getTotalSize());
336348
assertEquals("SearchHandle failed to report number of records expected", RECORDS_EXPECTED, searchHandle.getTotalResults());
337349
assertEquals("SearchHandle failed to report pageLength expected", pageLength, searchHandle.getPageLength());
338-
cleanUp();
350+
}
351+
352+
//public void testMixedLoad() {
353+
@Test
354+
public void testJsonLoad() {
355+
JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();
356+
357+
StringHandle doc1 =
358+
new StringHandle("{\"animal\": \"dog\", \"says\": \"woof\"}").withFormat(Format.JSON);
359+
360+
StringHandle doc2 =
361+
new StringHandle("{\"animal\": \"cat\", \"says\": \"meow\"}").withFormat(Format.JSON);
362+
363+
StringHandle doc2Metadata =
364+
new StringHandle("{\"quality\" : 2.0}").withFormat(Format.JSON);
365+
366+
DocumentWriteSet writeSet = docMgr.newWriteSet();
367+
writeSet.add("doc1.json", doc1);
368+
writeSet.add("doc2.json", doc2Metadata, doc2);
369+
370+
docMgr.write(writeSet);
371+
372+
JacksonHandle content1 = new JacksonHandle();
373+
docMgr.read("doc1.json", content1);
374+
JacksonHandle content2 = new JacksonHandle();
375+
DocumentMetadataHandle metadata2 = new DocumentMetadataHandle();
376+
docMgr.read("doc2.json", metadata2, content2);
377+
378+
assertEquals("Failed to read document 1", "dog", content1.get().get("animal").textValue());
379+
assertEquals("Failed to read expected quality", 2, metadata2.getQuality());
380+
assertEquals("Failed to read document 2", "cat", content2.get().get("animal").textValue());
339381
}
340382

341383
public void validateRecord(DocumentRecord record) {
@@ -360,6 +402,28 @@ public void validateRecord(DocumentRecord record) {
360402
}
361403
}
362404

405+
@Test
406+
public void testTextLoad() {
407+
String docId[] = {"/foo/test/myFoo1.xml","/foo/test/myFoo2.xml","/foo/test/myFoo3.xml"};
408+
TextDocumentManager docMgr = Common.client.newTextDocumentManager();
409+
DocumentWriteSet writeset =docMgr.newWriteSet();
410+
411+
writeset.add(docId[0], new StringHandle().with("This is so foo1"));
412+
writeset.add(docId[1], new StringHandle().with("This is so foo2"));
413+
writeset.add(docId[2], new StringHandle().with("This is so foo3"));
414+
docMgr.write(writeset);
415+
416+
assertEquals("Text document write difference", "This is so foo1", docMgr.read(docId[0], new StringHandle()).get());
417+
assertEquals("Text document write difference", "This is so foo2", docMgr.read(docId[1], new StringHandle()).get());
418+
assertEquals("Text document write difference", "This is so foo3", docMgr.read(docId[2], new StringHandle()).get());
419+
420+
docMgr.delete(docId[0]);
421+
docMgr.delete(docId[1]);
422+
docMgr.delete(docId[2]);
423+
}
424+
425+
426+
363427
private static void addCountry(String line, Map<String, Country> countries) {
364428
// skip comment lines
365429
if ( line.startsWith("#") ) return;
@@ -408,10 +472,13 @@ private static City newCity(String line, Map<String, Country> countries) {
408472
}
409473
}
410474

411-
private void cleanUp() {
475+
private static void cleanUp() {
412476
QueryManager queryMgr = Common.client.newQueryManager();
413477
DeleteQueryDefinition deleteQuery = queryMgr.newDeleteDefinition();
414478
deleteQuery.setDirectory("/cities/");
415479
queryMgr.delete(deleteQuery);
480+
JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();
481+
docMgr.delete("doc1.json");
482+
docMgr.delete("doc2.json");
416483
}
417484
}

0 commit comments

Comments
 (0)