Skip to content

Add CtsQueryBuilder for DMSDK in Java API #1260 #1262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.io.marker.ContentHandle;
import com.marklogic.client.query.RawCtsQueryDefinition;
import com.marklogic.client.query.StringQueryDefinition;
import com.marklogic.client.query.StructuredQueryDefinition;
import com.marklogic.client.query.RawCombinedQueryDefinition;
import com.marklogic.client.query.RawStructuredQueryDefinition;
import com.marklogic.client.query.*;

import java.util.Iterator;

Expand Down Expand Up @@ -127,6 +123,16 @@ public interface DataMovementManager {
*/
public WriteBatcher newWriteBatcher();

/**
* Create a new QueryBatcher instance configured to retrieve uris that
* match this query.
*
* @param query the query used to find matching uris
*
* @return the new QueryBatcher instance
*/
public QueryBatcher newQueryBatcher(CtsQueryDefinition query);

/**
* Create a new QueryBatcher instance configured to retrieve uris that
* match this query.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
import com.marklogic.client.datamovement.*;
import com.marklogic.client.impl.DatabaseClientImpl;
import com.marklogic.client.io.marker.ContentHandle;
import com.marklogic.client.query.QueryDefinition;
import com.marklogic.client.query.RawCtsQueryDefinition;
import com.marklogic.client.query.StringQueryDefinition;
import com.marklogic.client.query.StructuredQueryDefinition;
import com.marklogic.client.query.RawCombinedQueryDefinition;
import com.marklogic.client.query.RawStructuredQueryDefinition;
import com.marklogic.client.query.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -102,6 +97,11 @@ public WriteBatcher newWriteBatcher() {
return batcher;
}

@Override
public QueryBatcher newQueryBatcher(CtsQueryDefinition query) {
return newQueryBatcherImpl(query);
}

@Override
public QueryBatcher newQueryBatcher(StructuredQueryDefinition query) {
return newQueryBatcherImpl(query);
Expand All @@ -126,7 +126,7 @@ public QueryBatcher newQueryBatcher(RawCtsQueryDefinition query) {
return newQueryBatcherImpl(query);
}

private QueryBatcher newQueryBatcherImpl(QueryDefinition query) {
private QueryBatcher newQueryBatcherImpl(SearchQueryDefinition query) {
if ( query == null ) throw new IllegalArgumentException("query must not be null");

QueryBatcherImpl queryBatcher = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.marklogic.client.impl.DatabaseClientImpl;
import com.marklogic.client.io.JacksonHandle;
import com.marklogic.client.datamovement.JobTicket.JobType;
import com.marklogic.client.query.QueryDefinition;
import com.marklogic.client.query.SearchQueryDefinition;
import com.marklogic.client.util.RequestParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -48,7 +48,7 @@ public DataMovementServices setClient(DatabaseClient client) {
return this;
}

QueryConfig initConfig(String method, QueryDefinition qdef) {
QueryConfig initConfig(String method, SearchQueryDefinition qdef) {
logger.debug("initializing forest configuration with query");
if (qdef == null) throw new IllegalArgumentException("null query definition");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import com.marklogic.client.io.StringHandle;
import com.marklogic.client.io.marker.StructureWriteHandle;
import com.marklogic.client.query.RawQueryDefinition;
import com.marklogic.client.query.SearchQueryDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.ResourceNotFoundException;
import com.marklogic.client.query.QueryDefinition;
import com.marklogic.client.impl.QueryManagerImpl;
import com.marklogic.client.impl.UrisHandle;

Expand All @@ -59,8 +59,8 @@
public class QueryBatcherImpl extends BatcherImpl implements QueryBatcher {
private static Logger logger = LoggerFactory.getLogger(QueryBatcherImpl.class);
private String queryMethod;
private QueryDefinition query;
private QueryDefinition originalQuery;
private SearchQueryDefinition query;
private SearchQueryDefinition originalQuery;
private Boolean filtered;
private Iterator<String> iterator;
private boolean threadCountSet = false;
Expand All @@ -84,7 +84,7 @@ public class QueryBatcherImpl extends BatcherImpl implements QueryBatcher {
private long maxBatches = Long.MAX_VALUE;

QueryBatcherImpl(
QueryDefinition originalQuery, DataMovementManager moveMgr, ForestConfiguration forestConfig,
SearchQueryDefinition originalQuery, DataMovementManager moveMgr, ForestConfiguration forestConfig,
String serializedCtsQuery, Boolean filtered
) {
this(moveMgr, forestConfig);
Expand All @@ -100,7 +100,7 @@ public class QueryBatcherImpl extends BatcherImpl implements QueryBatcher {
initQuery(originalQuery);
}
}
public QueryBatcherImpl(QueryDefinition query, DataMovementManager moveMgr, ForestConfiguration forestConfig) {
public QueryBatcherImpl(SearchQueryDefinition query, DataMovementManager moveMgr, ForestConfiguration forestConfig) {
this(moveMgr, forestConfig);
initQuery(query);
}
Expand All @@ -113,7 +113,7 @@ private QueryBatcherImpl(DataMovementManager moveMgr, ForestConfiguration forest
withForestConfig(forestConfig);
withBatchSize(1000);
}
private void initQuery(QueryDefinition query) {
private void initQuery(SearchQueryDefinition query) {
if (query == null) {
throw new IllegalArgumentException("Cannot create QueryBatcher with null query");
}
Expand Down Expand Up @@ -589,7 +589,7 @@ private class QueryTask implements Runnable {
private QueryBatcherImpl batcher;
private Forest forest;
private String queryMethod;
private QueryDefinition query;
private SearchQueryDefinition query;
private Boolean filtered;
private long forestBatchNum;
private long start;
Expand All @@ -599,17 +599,17 @@ private class QueryTask implements Runnable {
private String nextAfterUri;

QueryTask(DataMovementManager moveMgr, QueryBatcherImpl batcher, Forest forest,
String queryMethod, QueryDefinition query, Boolean filtered, long forestBatchNum, long start
String queryMethod, SearchQueryDefinition query, Boolean filtered, long forestBatchNum, long start
) {
this(moveMgr, batcher, forest, queryMethod, query, filtered, forestBatchNum, start, null, -1, true);
}
QueryTask(DataMovementManager moveMgr, QueryBatcherImpl batcher, Forest forest,
String queryMethod, QueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri
String queryMethod, SearchQueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri
) {
this(moveMgr, batcher, forest, queryMethod, query, filtered, forestBatchNum, start, afterUri, -1, true);
}
QueryTask(DataMovementManager moveMgr, QueryBatcherImpl batcher, Forest forest,
String queryMethod, QueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri,
String queryMethod, SearchQueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri,
long retryBatchNumber, boolean callFailListeners
) {
this.moveMgr = moveMgr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,69 @@
import com.marklogic.client.query.CtsQueryDefinition;
import com.marklogic.client.type.CtsQueryExpr;

/**
* CtsQueryBuilder builds a query for documents in the database.
*/
public abstract class CtsQueryBuilder {

/**
* Builds expressions with cts server functions.
*/
public final CtsExpr cts;

/**
* Builds expressions with fn server functions.
*/
public final FnExpr fn;

/**
* Builds expressions with geo server functions.
*/
public final GeoExpr geo;

/**
* Builds expressions with json server functions.
*/
public final JsonExpr json;

/**
* Builds expressions with map server functions.
*/
public final MapExpr map;

/**
* Builds expressions with math server functions.
*/
public final MathExpr math;

/**
* Builds expressions with rdf server functions.
*/
public final RdfExpr rdf;

/**
* Builds expressions with sem server functions.
*/
public final SemExpr sem;

/**
* Builds expressions with spell server functions.
*/
public final SpellExpr spell;

/**
* Builds expressions with sql server functions.
*/
public final SqlExpr sql;

/**
* Builds expressions with xdmp server functions.
*/
public final XdmpExpr xdmp;

/**
* Builds expressions with xs server functions.
*/
public final XsExpr xs;

protected CtsQueryBuilder(
Expand All @@ -54,9 +104,27 @@ protected CtsQueryBuilder(
}


/**
* Create a CtsQueryDefinition based on a cts query
* @param query a cts query
* @return a CtsQueryDefinition
*/
public abstract CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query);

/**
* Create a CtsQueryDefinition based on a cts query and query options
* @param query a cts query
* @param queryOptions query options
* @return a CtsQueryDefinition
*/
public abstract CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query, JSONWriteHandle queryOptions);

/**
* Export cts query into a handle in AST format
* @param query the cts query to be exported
* @param handle the handle to store exported query
* @param <T> the handle type
* @return a handle which contains exported cts query in AST format
*/
public abstract <T extends JSONReadHandle> T export(CtsQueryExpr query, T handle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3010,7 +3010,7 @@ public <R extends AbstractReadHandle> R getSystemSchema(RequestLogger reqlog, St
return getResource(reqlog, "internal/schemas", null, params, output);
}
@Override
public <R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, QueryDefinition qdef,
public <R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, SearchQueryDefinition qdef,
Boolean filtered, long start, String afterUri, long pageLength, String forestName, R output
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
logger.debug("Querying for uris");
Expand All @@ -3024,19 +3024,25 @@ public <R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, Qu
}
@Override
public <R extends AbstractReadHandle> R forestInfo(RequestLogger reqlog,
String method, RequestParameters params, QueryDefinition qdef, R output
String method, RequestParameters params, SearchQueryDefinition qdef, R output
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
return processQuery(reqlog, "internal/forestinfo", method, params, qdef, output);
}
private <R extends AbstractReadHandle> R processQuery(RequestLogger reqlog, String path,
String method, RequestParameters params, QueryDefinition qdef, R output
String method, RequestParameters params, SearchQueryDefinition qdef, R output
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
if (qdef.getDirectory() != null) params.add("directory", qdef.getDirectory());
if (qdef.getCollections() != null ) {
for ( String collection : qdef.getCollections() ) {
params.add("collection", collection);
if (qdef instanceof QueryDefinition) {
if (((QueryDefinition)qdef).getDirectory() != null) {
params.add("directory", ((QueryDefinition)qdef).getDirectory());
}

if (((QueryDefinition)qdef).getCollections() != null ) {
for ( String collection : ((QueryDefinition)qdef).getCollections() ) {
params.add("collection", collection);
}
}
}
}

if (qdef.getOptionsName()!= null && qdef.getOptionsName().length() > 0) {
params.add("options", qdef.getOptionsName());
}
Expand Down Expand Up @@ -3098,7 +3104,14 @@ private <R extends AbstractReadHandle> R processQuery(RequestLogger reqlog, Stri
RawQueryDefinition rawQuery = (RawQueryDefinition) qdef;
logger.debug("{} processing raw query", path);
input = checkFormat(rawQuery.getHandle());
} else {
} else if (qdef instanceof CtsQueryDefinition) {
CtsQueryDefinition builtCtsQuery = (CtsQueryDefinition) qdef;
structure = builtCtsQuery.serialize();
logger.debug("{} processing cts query {}", path, structure);
if (sendQueryAsPayload && structure != null) {
input = new StringHandle(structure).withFormat(Format.JSON);
}
}else {
throw new UnsupportedOperationException(path+" cannot process query of "+qdef.getClass().getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public <T extends SearchReadHandle> T search(SearchQueryDefinition querydef, T s
return services.search(requestLogger, searchHandle, querydef, start, pageLen, view, transaction, forestName);
}

public <T extends UrisReadHandle> T uris(String method, QueryDefinition querydef, Boolean filtered, T urisHandle,
public <T extends UrisReadHandle> T uris(String method, SearchQueryDefinition querydef, Boolean filtered, T urisHandle,
long start, String afterUri, String forestName) {
return services.uris(requestLogger, method, querydef, filtered, start, afterUri, pageLen, forestName, urisHandle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ void deleteValues(RequestLogger logger, String type)
<R extends AbstractReadHandle> R getSystemSchema(RequestLogger reqlog, String schemaName, R output)
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;

<R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, QueryDefinition qdef,
<R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, SearchQueryDefinition qdef,
Boolean filtered, long start, String afterUri, long pageLength, String forestName, R output)
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
<R extends AbstractReadHandle> R forestInfo(RequestLogger reqlog,
String method, RequestParameters params, QueryDefinition qdef, R output
String method, RequestParameters params, SearchQueryDefinition qdef, R output
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;

<R extends AbstractReadHandle> R getResource(RequestLogger reqlog, String path,
Expand Down
Loading