Skip to content

Commit d6d0021

Browse files
committed
add features #7 and #8 to surface bulk capabilities just added to REST API
1 parent 7a3aa77 commit d6d0021

12 files changed

+3100
-232
lines changed

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

+32
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
2929
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
3030
import com.marklogic.client.io.marker.DocumentPatchHandle;
31+
import com.marklogic.client.io.marker.SearchReadHandle;
32+
import com.marklogic.client.query.QueryDefinition;
3133

3234
/**
3335
* A Document Manager provides database operations on a document.
@@ -365,6 +367,36 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
365367
public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction)
366368
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
367369

370+
public DocumentPage read(String... uris);
371+
372+
public DocumentPage read(ServerTransform transform, String... uris);
373+
374+
public DocumentPage read(Transaction transaction, String... uris);
375+
376+
public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris);
377+
378+
public DocumentPage search(QueryDefinition querydef, long start);
379+
380+
public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction);
381+
382+
public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle);
383+
384+
public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction);
385+
386+
public long getPageLength();
387+
388+
public void setPageLength(long length);
389+
390+
public DocumentWriteSet newWriteSet();
391+
392+
public void write(DocumentWriteSet writeSet);
393+
394+
public void write(DocumentWriteSet writeSet, ServerTransform transform);
395+
396+
public void write(DocumentWriteSet writeSet, Transaction transaction);
397+
398+
public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction);
399+
368400
/**
369401
* Writes the document content to the database from an object of an IO class.
370402
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.document;
17+
18+
import com.marklogic.client.io.marker.AbstractWriteHandle;
19+
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
20+
21+
public interface DocumentWriteOperation {
22+
public enum OperationType { METADATA_DEFAULT, DOCUMENT_WRITE };
23+
24+
public OperationType getOperationType();
25+
26+
public String getUri();
27+
28+
public DocumentMetadataWriteHandle getMetadata();
29+
30+
public AbstractWriteHandle getContent();
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.document;
17+
18+
import com.marklogic.client.io.marker.AbstractWriteHandle;
19+
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
20+
21+
import java.util.Set;
22+
23+
public interface DocumentWriteSet extends Set<DocumentWriteOperation> {
24+
public DocumentWriteSet addDefault(DocumentMetadataWriteHandle metadataHandle);
25+
26+
public DocumentWriteSet add(String docId, AbstractWriteHandle contentHandle);
27+
28+
public DocumentWriteSet add(String docId, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle);
29+
30+
public DocumentWriteSet add(DocumentDescriptor desc, AbstractWriteHandle contentHandle);
31+
32+
public DocumentWriteSet add(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle);
33+
}
34+

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

+159-11
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package com.marklogic.client.impl;
1717

18+
import java.util.ArrayList;
1819
import java.util.HashSet;
1920
import java.util.Set;
2021

2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324

25+
import com.fasterxml.jackson.databind.JsonNode;
26+
2427
import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry;
2528
import com.marklogic.client.FailedRequestException;
2629
import com.marklogic.client.ForbiddenUserException;
@@ -30,6 +33,10 @@
3033
import com.marklogic.client.document.DocumentManager;
3134
import com.marklogic.client.document.DocumentMetadataPatchBuilder;
3235
import com.marklogic.client.document.DocumentUriTemplate;
36+
import com.marklogic.client.document.DocumentPage;
37+
import com.marklogic.client.document.DocumentRecord;
38+
import com.marklogic.client.document.DocumentWriteOperation;
39+
import com.marklogic.client.document.DocumentWriteSet;
3340
import com.marklogic.client.document.ServerTransform;
3441
import com.marklogic.client.impl.DocumentMetadataPatchBuilderImpl.DocumentPatchHandleImpl;
3542
import com.marklogic.client.io.Format;
@@ -39,22 +46,44 @@
3946
import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
4047
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
4148
import com.marklogic.client.io.marker.DocumentPatchHandle;
49+
import com.marklogic.client.io.marker.SearchReadHandle;
50+
import com.marklogic.client.io.JacksonHandle;
51+
import com.marklogic.client.io.SearchHandle;
52+
import com.marklogic.client.query.QueryDefinition;
53+
import com.marklogic.client.query.QueryManager.QueryView;
4254
import com.marklogic.client.util.RequestParameters;
4355

4456
abstract class DocumentManagerImpl<R extends AbstractReadHandle, W extends AbstractWriteHandle>
4557
extends AbstractLoggingManager
4658
implements DocumentManager<R, W>
4759
{
60+
static final private long DEFAULT_PAGE_LENGTH = 50;
61+
4862
static final private Logger logger = LoggerFactory.getLogger(DocumentManagerImpl.class);
4963

50-
final private Set<Metadata> processedMetadata;
64+
private boolean isProcessedMetadataModified = false;
65+
final private Set<Metadata> processedMetadata = new HashSet<Metadata>() {
66+
public boolean add(Metadata e) {
67+
isProcessedMetadataModified = true;
68+
return super.add(e);
69+
}
70+
};
71+
{
72+
processedMetadata.add(Metadata.ALL);
73+
// we need to know if the user modifies after us
74+
isProcessedMetadataModified = false;
75+
}
76+
5177

5278
private RESTServices services;
5379
private Format contentFormat;
5480
private HandleFactoryRegistry handleRegistry;
5581
private ServerTransform readTransform;
5682
private ServerTransform writeTransform;
5783
private String forestName;
84+
private long pageLength = DEFAULT_PAGE_LENGTH;
85+
private QueryView searchView = QueryView.RESULTS;
86+
private Format responseFormat = Format.XML;
5887

5988
DocumentManagerImpl(RESTServices services, Format contentFormat) {
6089
super();
@@ -82,15 +111,6 @@ public Format getContentFormat() {
82111
}
83112

84113
// select categories of metadata to read, write, or reset
85-
{
86-
HashSet<Metadata> metadata = new HashSet<Metadata>();
87-
metadata.add(Metadata.ALL);
88-
processedMetadata = metadata;
89-
}
90-
@Override
91-
public Set<Metadata> getMetadataCategories() {
92-
return processedMetadata;
93-
}
94114
@Override
95115
public void setMetadataCategories(Set<Metadata> categories) {
96116
clearMetadataCategories();
@@ -103,6 +123,10 @@ public void setMetadataCategories(Metadata... categories) {
103123
processedMetadata.add(category);
104124
}
105125
@Override
126+
public Set<Metadata> getMetadataCategories() {
127+
return processedMetadata;
128+
}
129+
@Override
106130
public void clearMetadataCategories() {
107131
processedMetadata.clear();
108132
}
@@ -250,7 +274,7 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
250274
public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction, RequestParameters extraParams)
251275
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
252276
if (desc == null)
253-
throw new IllegalArgumentException("Reading document with null identifier");
277+
throw new IllegalArgumentException("Attempt to call read with null DocumentDescriptor");
254278

255279
if (logger.isInfoEnabled())
256280
logger.info("Reading metadata and content for {}", desc.getUri());
@@ -285,6 +309,130 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
285309
return wasModified ? contentHandle : null;
286310
}
287311

312+
public DocumentPage read(String... uris) {
313+
return read(null, null, uris);
314+
}
315+
316+
public DocumentPage read(Transaction transaction, String... uris) {
317+
return read(null, transaction, uris);
318+
}
319+
320+
public DocumentPage read(ServerTransform transform, String... uris) {
321+
return read(transform, null, uris);
322+
}
323+
324+
public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris) {
325+
if (uris == null || uris.length == 0)
326+
throw new IllegalArgumentException("Attempt to call read with no uris");
327+
328+
if (logger.isInfoEnabled())
329+
logger.info("Reading metadata and content for multiple uris beginning with {}", uris[0]);
330+
331+
return services.getBulkDocuments(
332+
requestLogger,
333+
(transaction == null) ? null : transaction.getTransactionId(),
334+
// the default for bulk is no metadata, which differs from the normal default of ALL
335+
isProcessedMetadataModified ? processedMetadata : null,
336+
responseFormat,
337+
null,
338+
uris);
339+
}
340+
341+
public DocumentPage search(QueryDefinition querydef, long start) {
342+
return search(querydef, start, null, null);
343+
}
344+
345+
public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle) {
346+
return search(querydef, start, searchHandle, null);
347+
}
348+
349+
public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction) {
350+
return search(querydef, start, null, transaction);
351+
}
352+
353+
public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction) {
354+
355+
if ( searchHandle != null ) {
356+
HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search");
357+
if (searchHandle instanceof SearchHandle) {
358+
SearchHandle responseHandle = (SearchHandle) searchHandle;
359+
responseHandle.setHandleRegistry(getHandleRegistry());
360+
responseHandle.setQueryCriteria(querydef);
361+
}
362+
if ( responseFormat != searchBase.getFormat() ) {
363+
throw new UnsupportedOperationException("The format supported by your handle:[" +
364+
searchBase.getFormat() + "] does not match your setResponseFormat:[" + responseFormat + "]");
365+
}
366+
}
367+
368+
String tid = transaction == null ? null : transaction.getTransactionId();
369+
// the default for bulk is no metadata, which differs from the normal default of ALL
370+
Set<Metadata> metadata = isProcessedMetadataModified ? processedMetadata : null;
371+
return services.getBulkDocuments( requestLogger, querydef, start, getPageLength(),
372+
tid, searchHandle, searchView, metadata, responseFormat, null);
373+
}
374+
375+
public long getPageLength() {
376+
return pageLength;
377+
}
378+
379+
public void setPageLength(long length) {
380+
this.pageLength = length;
381+
}
382+
383+
public QueryView getSearchView() {
384+
return searchView;
385+
}
386+
387+
public void setSearchView(QueryView view) {
388+
this.searchView = view;
389+
}
390+
391+
public Format getResponseFormat() {
392+
return responseFormat;
393+
}
394+
395+
public void setResponseFormat(Format responseFormat) {
396+
if ( responseFormat != Format.XML && responseFormat != Format.JSON ) {
397+
throw new UnsupportedOperationException("Only XML and JSON are valid response formats. You specified:[" +
398+
responseFormat + "]");
399+
}
400+
this.responseFormat = responseFormat;
401+
}
402+
403+
public DocumentWriteSet newWriteSet() {
404+
return new DocumentWriteSetImpl();
405+
}
406+
407+
public void write(DocumentWriteSet writeSet) {
408+
write(writeSet, null, null);
409+
}
410+
411+
public void write(DocumentWriteSet writeSet, ServerTransform transform) {
412+
write(writeSet, transform, null);
413+
}
414+
415+
public void write(DocumentWriteSet writeSet, Transaction transaction) {
416+
write(writeSet, null, transaction);
417+
}
418+
419+
public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction) {
420+
JacksonHandle jacksonHandle = new JacksonHandle();
421+
jacksonHandle = services.postBulkDocuments(
422+
requestLogger,
423+
writeSet,
424+
(transform != null) ? transform : getWriteTransform(),
425+
(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+
434+
}
435+
288436
// shortcut writers
289437
@Override
290438
public void writeAs(String uri, Object content)

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class DocumentPageImpl extends BasicPage<DocumentRecord> implements DocumentPage
2525
private Iterable<DocumentRecord> iterable;
2626
private Iterator<DocumentRecord> iterator;
2727

28-
public DocumentPageImpl(Iterable<DocumentRecord> iterable) {
29-
super(iterable);
28+
public DocumentPageImpl(Iterable<DocumentRecord> iterable, long start, long size, long pageSize, long totalSize) {
29+
super(iterable, start, pageSize, totalSize);
30+
this.setSize(size);
3031
this.iterable = iterable;
3132
this.iterator = iterable.iterator();
3233
}

0 commit comments

Comments
 (0)