Skip to content

Commit ac5a76e

Browse files
committed
fix #28 and add related unit tests tests
1 parent ae74623 commit ac5a76e

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ private DocumentPage convertToDocumentPage(JerseyResultIterator iterator, boolea
726726
JerseyResult result = iterator.next();
727727
DocumentRecord record;
728728
if ( hasMetadata ) {
729-
if ( iterator.hasNext() ) {
729+
if ( ! iterator.hasNext() ) {
730730
throw new MarkLogicInternalException(
731731
"Metadata and content parts should always come in pairs!");
732732
}
@@ -750,6 +750,8 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog,
750750

751751
String path = "documents";
752752
RequestParameters params = new RequestParameters();
753+
boolean withContent = true;
754+
addCategoryParams(categories, params, withContent);
753755
if (format != null) params.add("format", format.toString().toLowerCase());
754756
for (String uri: uris) {
755757
params.add("uri", uri);
@@ -764,6 +766,8 @@ private JerseyResultIterator getBulkDocumentsImpl(RequestLogger reqlog,
764766
Set<Metadata> categories, Format format, RequestParameters extraParams)
765767
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
766768
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
769+
boolean withContent = true;
770+
addCategoryParams(categories, params, withContent);
767771
if (searchHandle != null && view != null) params.add("view", view.toString().toLowerCase());
768772
if (start > 1) params.add("start", Long.toString(start));
769773
if (pageLength > 0) params.add("pageLength", Long.toString(pageLength));
@@ -1605,6 +1609,39 @@ private void completeTransaction(String transactionId, String result)
16051609
response.close();
16061610
}
16071611

1612+
private void addCategoryParams(Set<Metadata> categories, MultivaluedMap<String, String> params,
1613+
boolean withContent)
1614+
{
1615+
if (withContent && categories == null || categories.size() == 0) {
1616+
params.add("category", "content");
1617+
} else {
1618+
if (withContent) params.add("category", "content");
1619+
if (categories.contains(Metadata.ALL)) {
1620+
params.add("category", "metadata");
1621+
} else {
1622+
for (Metadata category : categories) {
1623+
params.add("category", category.name().toLowerCase());
1624+
}
1625+
}
1626+
}
1627+
}
1628+
private void addCategoryParams(Set<Metadata> categories, RequestParameters params,
1629+
boolean withContent)
1630+
{
1631+
if (withContent && categories == null || categories.size() == 0) {
1632+
params.add("category", "content");
1633+
} else {
1634+
if (withContent) params.add("category", "content");
1635+
if (categories.contains(Metadata.ALL)) {
1636+
params.add("category", "metadata");
1637+
} else {
1638+
for (Metadata category : categories) {
1639+
params.add("category", category.name().toLowerCase());
1640+
}
1641+
}
1642+
}
1643+
}
1644+
16081645
private MultivaluedMap<String, String> makeDocumentParams(String uri,
16091646
Set<Metadata> categories, String transactionId,
16101647
RequestParameters extraParams) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.junit.Test;
4040

4141
import com.marklogic.client.document.DocumentDescriptor;
42+
import com.marklogic.client.document.DocumentManager.Metadata;
4243
import com.marklogic.client.document.DocumentPage;
4344
import com.marklogic.client.document.DocumentRecord;
4445
import com.marklogic.client.document.DocumentWriteSet;
@@ -305,6 +306,8 @@ public void testF_DefaultMetadata() {
305306
// Execute the write operation
306307
jdm.write(batch);
307308

309+
// need the "synthetic response" format to be XML
310+
jdm.setResponseFormat(Format.XML);
308311
// Check the results
309312
assertEquals("Doc1 should have the system default quality of 0", 0,
310313
jdm.readMetadata("doc1.json", new DocumentMetadataHandle()).getQuality());
@@ -325,6 +328,26 @@ public void testF_DefaultMetadata() {
325328
jdm.readMetadata("doc4.json", new DocumentMetadataHandle()).getQuality());
326329
assertEquals("Doc5 should use the 2nd batch default metadata, with quality 2", defaultMetadata2.getQuality(),
327330
jdm.readMetadata("doc5.json", new DocumentMetadataHandle()).getQuality());
331+
332+
// let's check getting just quality metadata with content in bulk read
333+
jdm.setMetadataCategories(Metadata.QUALITY);
334+
DocumentPage documents = jdm.read("doc1.json", "doc2.json", "doc3.json");
335+
336+
for ( DocumentRecord doc: documents ) {
337+
DocumentMetadataHandle metadata = doc.getMetadata(new DocumentMetadataHandle());
338+
StringHandle content = doc.getContent(new StringHandle());
339+
if ( "doc1.json".equals(doc.getUri()) ) {
340+
assertEquals("Doc 1 should have the system default quality of 0", 0, metadata.getQuality());
341+
assertTrue("Doc 1 contents are wrong", content.get().matches("\\{\"number\": ?1\\}"));
342+
} else if ( "doc2.json".equals(doc.getUri()) ) {
343+
assertEquals("Doc 2 should use the first batch default metadata, with quality 1", 1, metadata.getQuality());
344+
assertTrue("Doc 2 contents are wrong", content.get().matches("\\{\"number\": ?2\\}"));
345+
} else if ( "doc3.json".equals(doc.getUri()) ) {
346+
assertEquals("Doc 3 should have the system default document quality (0) because quality " +
347+
"was not included in the document-specific metadata.", 0, metadata.getQuality());
348+
assertTrue("Doc 3 contents are wrong", content.get().matches("\\{\"number\": ?3\\}"));
349+
}
350+
}
328351
}
329352

330353

0 commit comments

Comments
 (0)