Skip to content

Commit

Permalink
Merge branch 'bulkReadWrite'
Browse files Browse the repository at this point in the history
  • Loading branch information
sammefford committed Jun 17, 2014
2 parents 06fad2b + d6d0021 commit 0842bce
Show file tree
Hide file tree
Showing 11 changed files with 3,097 additions and 230 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/marklogic/client/document/DocumentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
import com.marklogic.client.io.marker.DocumentPatchHandle;
import com.marklogic.client.io.marker.SearchReadHandle;
import com.marklogic.client.query.QueryDefinition;

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

public DocumentPage read(String... uris);

public DocumentPage read(ServerTransform transform, String... uris);

public DocumentPage read(Transaction transaction, String... uris);

public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris);

public DocumentPage search(QueryDefinition querydef, long start);

public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction);

public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle);

public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction);

public long getPageLength();

public void setPageLength(long length);

public DocumentWriteSet newWriteSet();

public void write(DocumentWriteSet writeSet);

public void write(DocumentWriteSet writeSet, ServerTransform transform);

public void write(DocumentWriteSet writeSet, Transaction transaction);

public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction);

/**
* Writes the document content to the database from an object of an IO class.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2012-2014 MarkLogic Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.marklogic.client.document;

import com.marklogic.client.io.marker.AbstractWriteHandle;
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;

public interface DocumentWriteOperation {
public enum OperationType { METADATA_DEFAULT, DOCUMENT_WRITE };

public OperationType getOperationType();

public String getUri();

public DocumentMetadataWriteHandle getMetadata();

public AbstractWriteHandle getContent();
}
34 changes: 34 additions & 0 deletions src/main/java/com/marklogic/client/document/DocumentWriteSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2012-2014 MarkLogic Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.marklogic.client.document;

import com.marklogic.client.io.marker.AbstractWriteHandle;
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;

import java.util.Set;

public interface DocumentWriteSet extends Set<DocumentWriteOperation> {
public DocumentWriteSet addDefault(DocumentMetadataWriteHandle metadataHandle);

public DocumentWriteSet add(String docId, AbstractWriteHandle contentHandle);

public DocumentWriteSet add(String docId, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle);

public DocumentWriteSet add(DocumentDescriptor desc, AbstractWriteHandle contentHandle);

public DocumentWriteSet add(DocumentDescriptor desc, DocumentMetadataWriteHandle metadataHandle, AbstractWriteHandle contentHandle);
}

170 changes: 159 additions & 11 deletions src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package com.marklogic.client.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry;
import com.marklogic.client.FailedRequestException;
import com.marklogic.client.ForbiddenUserException;
Expand All @@ -30,6 +33,10 @@
import com.marklogic.client.document.DocumentManager;
import com.marklogic.client.document.DocumentMetadataPatchBuilder;
import com.marklogic.client.document.DocumentUriTemplate;
import com.marklogic.client.document.DocumentPage;
import com.marklogic.client.document.DocumentRecord;
import com.marklogic.client.document.DocumentWriteOperation;
import com.marklogic.client.document.DocumentWriteSet;
import com.marklogic.client.document.ServerTransform;
import com.marklogic.client.impl.DocumentMetadataPatchBuilderImpl.DocumentPatchHandleImpl;
import com.marklogic.client.io.Format;
Expand All @@ -39,22 +46,44 @@
import com.marklogic.client.io.marker.DocumentMetadataReadHandle;
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;
import com.marklogic.client.io.marker.DocumentPatchHandle;
import com.marklogic.client.io.marker.SearchReadHandle;
import com.marklogic.client.io.JacksonHandle;
import com.marklogic.client.io.SearchHandle;
import com.marklogic.client.query.QueryDefinition;
import com.marklogic.client.query.QueryManager.QueryView;
import com.marklogic.client.util.RequestParameters;

abstract class DocumentManagerImpl<R extends AbstractReadHandle, W extends AbstractWriteHandle>
extends AbstractLoggingManager
implements DocumentManager<R, W>
{
static final private long DEFAULT_PAGE_LENGTH = 50;

static final private Logger logger = LoggerFactory.getLogger(DocumentManagerImpl.class);

final private Set<Metadata> processedMetadata;
private boolean isProcessedMetadataModified = false;
final private Set<Metadata> processedMetadata = new HashSet<Metadata>() {
public boolean add(Metadata e) {
isProcessedMetadataModified = true;
return super.add(e);
}
};
{
processedMetadata.add(Metadata.ALL);
// we need to know if the user modifies after us
isProcessedMetadataModified = false;
}


private RESTServices services;
private Format contentFormat;
private HandleFactoryRegistry handleRegistry;
private ServerTransform readTransform;
private ServerTransform writeTransform;
private String forestName;
private long pageLength = DEFAULT_PAGE_LENGTH;
private QueryView searchView = QueryView.RESULTS;
private Format responseFormat = Format.XML;

DocumentManagerImpl(RESTServices services, Format contentFormat) {
super();
Expand Down Expand Up @@ -82,15 +111,6 @@ public Format getContentFormat() {
}

// select categories of metadata to read, write, or reset
{
HashSet<Metadata> metadata = new HashSet<Metadata>();
metadata.add(Metadata.ALL);
processedMetadata = metadata;
}
@Override
public Set<Metadata> getMetadataCategories() {
return processedMetadata;
}
@Override
public void setMetadataCategories(Set<Metadata> categories) {
clearMetadataCategories();
Expand All @@ -103,6 +123,10 @@ public void setMetadataCategories(Metadata... categories) {
processedMetadata.add(category);
}
@Override
public Set<Metadata> getMetadataCategories() {
return processedMetadata;
}
@Override
public void clearMetadataCategories() {
processedMetadata.clear();
}
Expand Down Expand Up @@ -250,7 +274,7 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle metadataHandle, T contentHandle, ServerTransform transform, Transaction transaction, RequestParameters extraParams)
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
if (desc == null)
throw new IllegalArgumentException("Reading document with null identifier");
throw new IllegalArgumentException("Attempt to call read with null DocumentDescriptor");

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

public DocumentPage read(String... uris) {
return read(null, null, uris);
}

public DocumentPage read(Transaction transaction, String... uris) {
return read(null, transaction, uris);
}

public DocumentPage read(ServerTransform transform, String... uris) {
return read(transform, null, uris);
}

public DocumentPage read(ServerTransform transform, Transaction transaction, String... uris) {
if (uris == null || uris.length == 0)
throw new IllegalArgumentException("Attempt to call read with no uris");

if (logger.isInfoEnabled())
logger.info("Reading metadata and content for multiple uris beginning with {}", uris[0]);

return services.getBulkDocuments(
requestLogger,
(transaction == null) ? null : transaction.getTransactionId(),
// the default for bulk is no metadata, which differs from the normal default of ALL
isProcessedMetadataModified ? processedMetadata : null,
responseFormat,
null,
uris);
}

public DocumentPage search(QueryDefinition querydef, long start) {
return search(querydef, start, null, null);
}

public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle) {
return search(querydef, start, searchHandle, null);
}

public DocumentPage search(QueryDefinition querydef, long start, Transaction transaction) {
return search(querydef, start, null, transaction);
}

public DocumentPage search(QueryDefinition querydef, long start, SearchReadHandle searchHandle, Transaction transaction) {

if ( searchHandle != null ) {
HandleImplementation searchBase = HandleAccessor.checkHandle(searchHandle, "search");
if (searchHandle instanceof SearchHandle) {
SearchHandle responseHandle = (SearchHandle) searchHandle;
responseHandle.setHandleRegistry(getHandleRegistry());
responseHandle.setQueryCriteria(querydef);
}
if ( responseFormat != searchBase.getFormat() ) {
throw new UnsupportedOperationException("The format supported by your handle:[" +
searchBase.getFormat() + "] does not match your setResponseFormat:[" + responseFormat + "]");
}
}

String tid = transaction == null ? null : transaction.getTransactionId();
// the default for bulk is no metadata, which differs from the normal default of ALL
Set<Metadata> metadata = isProcessedMetadataModified ? processedMetadata : null;
return services.getBulkDocuments( requestLogger, querydef, start, getPageLength(),
tid, searchHandle, searchView, metadata, responseFormat, null);
}

public long getPageLength() {
return pageLength;
}

public void setPageLength(long length) {
this.pageLength = length;
}

public QueryView getSearchView() {
return searchView;
}

public void setSearchView(QueryView view) {
this.searchView = view;
}

public Format getResponseFormat() {
return responseFormat;
}

public void setResponseFormat(Format responseFormat) {
if ( responseFormat != Format.XML && responseFormat != Format.JSON ) {
throw new UnsupportedOperationException("Only XML and JSON are valid response formats. You specified:[" +
responseFormat + "]");
}
this.responseFormat = responseFormat;
}

public DocumentWriteSet newWriteSet() {
return new DocumentWriteSetImpl();
}

public void write(DocumentWriteSet writeSet) {
write(writeSet, null, null);
}

public void write(DocumentWriteSet writeSet, ServerTransform transform) {
write(writeSet, transform, null);
}

public void write(DocumentWriteSet writeSet, Transaction transaction) {
write(writeSet, null, transaction);
}

public void write(DocumentWriteSet writeSet, ServerTransform transform, Transaction transaction) {
JacksonHandle jacksonHandle = new JacksonHandle();
jacksonHandle = services.postBulkDocuments(
requestLogger,
writeSet,
(transform != null) ? transform : getWriteTransform(),
(transaction == null) ? null : transaction.getTransactionId(),
jacksonHandle);
JsonNode root = jacksonHandle.get();
for (JsonNode item: root.get("documents")) {
String uri = item.get("uri").asText();
String mimetype = item.get("mime-type").asText();
String category = item.get("category").get(0).asText();
}

}

// shortcut writers
@Override
public void writeAs(String uri, Object content)
Expand Down
Loading

0 comments on commit 0842bce

Please sign in to comment.