Skip to content

Commit 7556fa8

Browse files
author
lling
committed
Add CtsQueryBuilder for DMSDK in Java API marklogic#1260
1 parent 9da93fc commit 7556fa8

File tree

9 files changed

+142
-47
lines changed

9 files changed

+142
-47
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/DataMovementManager.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717

1818
import com.marklogic.client.DatabaseClient;
1919
import com.marklogic.client.io.marker.ContentHandle;
20-
import com.marklogic.client.query.RawCtsQueryDefinition;
21-
import com.marklogic.client.query.StringQueryDefinition;
22-
import com.marklogic.client.query.StructuredQueryDefinition;
23-
import com.marklogic.client.query.RawCombinedQueryDefinition;
24-
import com.marklogic.client.query.RawStructuredQueryDefinition;
20+
import com.marklogic.client.query.*;
2521

2622
import java.util.Iterator;
2723

@@ -127,6 +123,16 @@ public interface DataMovementManager {
127123
*/
128124
public WriteBatcher newWriteBatcher();
129125

126+
/**
127+
* Create a new QueryBatcher instance configured to retrieve uris that
128+
* match this query.
129+
*
130+
* @param query the query used to find matching uris
131+
*
132+
* @return the new QueryBatcher instance
133+
*/
134+
public QueryBatcher newQueryBatcher(CtsQueryDefinition query);
135+
130136
/**
131137
* Create a new QueryBatcher instance configured to retrieve uris that
132138
* match this query.

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/DataMovementManagerImpl.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@
2020
import com.marklogic.client.datamovement.*;
2121
import com.marklogic.client.impl.DatabaseClientImpl;
2222
import com.marklogic.client.io.marker.ContentHandle;
23-
import com.marklogic.client.query.QueryDefinition;
24-
import com.marklogic.client.query.RawCtsQueryDefinition;
25-
import com.marklogic.client.query.StringQueryDefinition;
26-
import com.marklogic.client.query.StructuredQueryDefinition;
27-
import com.marklogic.client.query.RawCombinedQueryDefinition;
28-
import com.marklogic.client.query.RawStructuredQueryDefinition;
23+
import com.marklogic.client.query.*;
2924

3025
import org.slf4j.Logger;
3126
import org.slf4j.LoggerFactory;
@@ -102,6 +97,11 @@ public WriteBatcher newWriteBatcher() {
10297
return batcher;
10398
}
10499

100+
@Override
101+
public QueryBatcher newQueryBatcher(CtsQueryDefinition query) {
102+
return newQueryBatcherImpl(query);
103+
}
104+
105105
@Override
106106
public QueryBatcher newQueryBatcher(StructuredQueryDefinition query) {
107107
return newQueryBatcherImpl(query);
@@ -126,7 +126,7 @@ public QueryBatcher newQueryBatcher(RawCtsQueryDefinition query) {
126126
return newQueryBatcherImpl(query);
127127
}
128128

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

132132
QueryBatcherImpl queryBatcher = null;

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/DataMovementServices.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.marklogic.client.impl.DatabaseClientImpl;
2828
import com.marklogic.client.io.JacksonHandle;
2929
import com.marklogic.client.datamovement.JobTicket.JobType;
30-
import com.marklogic.client.query.QueryDefinition;
30+
import com.marklogic.client.query.SearchQueryDefinition;
3131
import com.marklogic.client.util.RequestParameters;
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
@@ -48,7 +48,7 @@ public DataMovementServices setClient(DatabaseClient client) {
4848
return this;
4949
}
5050

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

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/impl/QueryBatcherImpl.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
import com.marklogic.client.io.StringHandle;
3434
import com.marklogic.client.io.marker.StructureWriteHandle;
3535
import com.marklogic.client.query.RawQueryDefinition;
36+
import com.marklogic.client.query.SearchQueryDefinition;
3637
import org.slf4j.Logger;
3738
import org.slf4j.LoggerFactory;
3839

3940
import com.marklogic.client.DatabaseClient;
4041
import com.marklogic.client.ResourceNotFoundException;
41-
import com.marklogic.client.query.QueryDefinition;
4242
import com.marklogic.client.impl.QueryManagerImpl;
4343
import com.marklogic.client.impl.UrisHandle;
4444

@@ -59,8 +59,8 @@
5959
public class QueryBatcherImpl extends BatcherImpl implements QueryBatcher {
6060
private static Logger logger = LoggerFactory.getLogger(QueryBatcherImpl.class);
6161
private String queryMethod;
62-
private QueryDefinition query;
63-
private QueryDefinition originalQuery;
62+
private SearchQueryDefinition query;
63+
private SearchQueryDefinition originalQuery;
6464
private Boolean filtered;
6565
private Iterator<String> iterator;
6666
private boolean threadCountSet = false;
@@ -84,7 +84,7 @@ public class QueryBatcherImpl extends BatcherImpl implements QueryBatcher {
8484
private long maxBatches = Long.MAX_VALUE;
8585

8686
QueryBatcherImpl(
87-
QueryDefinition originalQuery, DataMovementManager moveMgr, ForestConfiguration forestConfig,
87+
SearchQueryDefinition originalQuery, DataMovementManager moveMgr, ForestConfiguration forestConfig,
8888
String serializedCtsQuery, Boolean filtered
8989
) {
9090
this(moveMgr, forestConfig);
@@ -100,7 +100,7 @@ public class QueryBatcherImpl extends BatcherImpl implements QueryBatcher {
100100
initQuery(originalQuery);
101101
}
102102
}
103-
public QueryBatcherImpl(QueryDefinition query, DataMovementManager moveMgr, ForestConfiguration forestConfig) {
103+
public QueryBatcherImpl(SearchQueryDefinition query, DataMovementManager moveMgr, ForestConfiguration forestConfig) {
104104
this(moveMgr, forestConfig);
105105
initQuery(query);
106106
}
@@ -113,7 +113,7 @@ private QueryBatcherImpl(DataMovementManager moveMgr, ForestConfiguration forest
113113
withForestConfig(forestConfig);
114114
withBatchSize(1000);
115115
}
116-
private void initQuery(QueryDefinition query) {
116+
private void initQuery(SearchQueryDefinition query) {
117117
if (query == null) {
118118
throw new IllegalArgumentException("Cannot create QueryBatcher with null query");
119119
}
@@ -589,7 +589,7 @@ private class QueryTask implements Runnable {
589589
private QueryBatcherImpl batcher;
590590
private Forest forest;
591591
private String queryMethod;
592-
private QueryDefinition query;
592+
private SearchQueryDefinition query;
593593
private Boolean filtered;
594594
private long forestBatchNum;
595595
private long start;
@@ -599,17 +599,17 @@ private class QueryTask implements Runnable {
599599
private String nextAfterUri;
600600

601601
QueryTask(DataMovementManager moveMgr, QueryBatcherImpl batcher, Forest forest,
602-
String queryMethod, QueryDefinition query, Boolean filtered, long forestBatchNum, long start
602+
String queryMethod, SearchQueryDefinition query, Boolean filtered, long forestBatchNum, long start
603603
) {
604604
this(moveMgr, batcher, forest, queryMethod, query, filtered, forestBatchNum, start, null, -1, true);
605605
}
606606
QueryTask(DataMovementManager moveMgr, QueryBatcherImpl batcher, Forest forest,
607-
String queryMethod, QueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri
607+
String queryMethod, SearchQueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri
608608
) {
609609
this(moveMgr, batcher, forest, queryMethod, query, filtered, forestBatchNum, start, afterUri, -1, true);
610610
}
611611
QueryTask(DataMovementManager moveMgr, QueryBatcherImpl batcher, Forest forest,
612-
String queryMethod, QueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri,
612+
String queryMethod, SearchQueryDefinition query, Boolean filtered, long forestBatchNum, long start, String afterUri,
613613
long retryBatchNumber, boolean callFailListeners
614614
) {
615615
this.moveMgr = moveMgr;

marklogic-client-api/src/main/java/com/marklogic/client/expression/CtsQueryBuilder.java

+68
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,69 @@
2020
import com.marklogic.client.query.CtsQueryDefinition;
2121
import com.marklogic.client.type.CtsQueryExpr;
2222

23+
/**
24+
* CtsQueryBuilder builds a query for documents in the database.
25+
*/
2326
public abstract class CtsQueryBuilder {
2427

28+
/**
29+
* Builds expressions with cts server functions.
30+
*/
2531
public final CtsExpr cts;
32+
33+
/**
34+
* Builds expressions with fn server functions.
35+
*/
2636
public final FnExpr fn;
37+
38+
/**
39+
* Builds expressions with geo server functions.
40+
*/
2741
public final GeoExpr geo;
42+
43+
/**
44+
* Builds expressions with json server functions.
45+
*/
2846
public final JsonExpr json;
47+
48+
/**
49+
* Builds expressions with map server functions.
50+
*/
2951
public final MapExpr map;
52+
53+
/**
54+
* Builds expressions with math server functions.
55+
*/
3056
public final MathExpr math;
57+
58+
/**
59+
* Builds expressions with rdf server functions.
60+
*/
3161
public final RdfExpr rdf;
62+
63+
/**
64+
* Builds expressions with sem server functions.
65+
*/
3266
public final SemExpr sem;
67+
68+
/**
69+
* Builds expressions with spell server functions.
70+
*/
3371
public final SpellExpr spell;
72+
73+
/**
74+
* Builds expressions with sql server functions.
75+
*/
3476
public final SqlExpr sql;
77+
78+
/**
79+
* Builds expressions with xdmp server functions.
80+
*/
3581
public final XdmpExpr xdmp;
82+
83+
/**
84+
* Builds expressions with xs server functions.
85+
*/
3686
public final XsExpr xs;
3787

3888
protected CtsQueryBuilder(
@@ -54,9 +104,27 @@ protected CtsQueryBuilder(
54104
}
55105

56106

107+
/**
108+
* Create a CtsQueryDefinition based on a cts query
109+
* @param query a cts query
110+
* @return a CtsQueryDefinition
111+
*/
57112
public abstract CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query);
58113

114+
/**
115+
* Create a CtsQueryDefinition based on a cts query and query options
116+
* @param query a cts query
117+
* @param queryOptions query options
118+
* @return a CtsQueryDefinition
119+
*/
59120
public abstract CtsQueryDefinition newCtsQueryDefinition(CtsQueryExpr query, JSONWriteHandle queryOptions);
60121

122+
/**
123+
* Export cts query into a handle in AST format
124+
* @param query the cts query to be exported
125+
* @param handle the handle to store exported query
126+
* @param <T> the handle type
127+
* @return a handle which contains exported cts query in AST format
128+
*/
61129
public abstract <T extends JSONReadHandle> T export(CtsQueryExpr query, T handle);
62130
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

+22-9
Original file line numberDiff line numberDiff line change
@@ -3010,7 +3010,7 @@ public <R extends AbstractReadHandle> R getSystemSchema(RequestLogger reqlog, St
30103010
return getResource(reqlog, "internal/schemas", null, params, output);
30113011
}
30123012
@Override
3013-
public <R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, QueryDefinition qdef,
3013+
public <R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, SearchQueryDefinition qdef,
30143014
Boolean filtered, long start, String afterUri, long pageLength, String forestName, R output
30153015
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
30163016
logger.debug("Querying for uris");
@@ -3024,19 +3024,25 @@ public <R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, Qu
30243024
}
30253025
@Override
30263026
public <R extends AbstractReadHandle> R forestInfo(RequestLogger reqlog,
3027-
String method, RequestParameters params, QueryDefinition qdef, R output
3027+
String method, RequestParameters params, SearchQueryDefinition qdef, R output
30283028
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
30293029
return processQuery(reqlog, "internal/forestinfo", method, params, qdef, output);
30303030
}
30313031
private <R extends AbstractReadHandle> R processQuery(RequestLogger reqlog, String path,
3032-
String method, RequestParameters params, QueryDefinition qdef, R output
3032+
String method, RequestParameters params, SearchQueryDefinition qdef, R output
30333033
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException {
3034-
if (qdef.getDirectory() != null) params.add("directory", qdef.getDirectory());
3035-
if (qdef.getCollections() != null ) {
3036-
for ( String collection : qdef.getCollections() ) {
3037-
params.add("collection", collection);
3034+
if (qdef instanceof QueryDefinition) {
3035+
if (((QueryDefinition)qdef).getDirectory() != null) {
3036+
params.add("directory", ((QueryDefinition)qdef).getDirectory());
3037+
}
3038+
3039+
if (((QueryDefinition)qdef).getCollections() != null ) {
3040+
for ( String collection : ((QueryDefinition)qdef).getCollections() ) {
3041+
params.add("collection", collection);
3042+
}
3043+
}
30383044
}
3039-
}
3045+
30403046
if (qdef.getOptionsName()!= null && qdef.getOptionsName().length() > 0) {
30413047
params.add("options", qdef.getOptionsName());
30423048
}
@@ -3098,7 +3104,14 @@ private <R extends AbstractReadHandle> R processQuery(RequestLogger reqlog, Stri
30983104
RawQueryDefinition rawQuery = (RawQueryDefinition) qdef;
30993105
logger.debug("{} processing raw query", path);
31003106
input = checkFormat(rawQuery.getHandle());
3101-
} else {
3107+
} else if (qdef instanceof CtsQueryDefinition) {
3108+
CtsQueryDefinition builtCtsQuery = (CtsQueryDefinition) qdef;
3109+
structure = builtCtsQuery.serialize();
3110+
logger.debug("{} processing cts query {}", path, structure);
3111+
if (sendQueryAsPayload && structure != null) {
3112+
input = new StringHandle(structure).withFormat(Format.TEXT);
3113+
}
3114+
}else {
31023115
throw new UnsupportedOperationException(path+" cannot process query of "+qdef.getClass().getName());
31033116
}
31043117

marklogic-client-api/src/main/java/com/marklogic/client/impl/QueryManagerImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public <T extends SearchReadHandle> T search(SearchQueryDefinition querydef, T s
164164
return services.search(requestLogger, searchHandle, querydef, start, pageLen, view, transaction, forestName);
165165
}
166166

167-
public <T extends UrisReadHandle> T uris(String method, QueryDefinition querydef, Boolean filtered, T urisHandle,
167+
public <T extends UrisReadHandle> T uris(String method, SearchQueryDefinition querydef, Boolean filtered, T urisHandle,
168168
long start, String afterUri, String forestName) {
169169
return services.uris(requestLogger, method, querydef, filtered, start, afterUri, pageLen, forestName, urisHandle);
170170
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ void deleteValues(RequestLogger logger, String type)
219219
<R extends AbstractReadHandle> R getSystemSchema(RequestLogger reqlog, String schemaName, R output)
220220
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
221221

222-
<R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, QueryDefinition qdef,
222+
<R extends UrisReadHandle> R uris(RequestLogger reqlog, String method, SearchQueryDefinition qdef,
223223
Boolean filtered, long start, String afterUri, long pageLength, String forestName, R output)
224224
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
225225
<R extends AbstractReadHandle> R forestInfo(RequestLogger reqlog,
226-
String method, RequestParameters params, QueryDefinition qdef, R output
226+
String method, RequestParameters params, SearchQueryDefinition qdef, R output
227227
) throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
228228

229229
<R extends AbstractReadHandle> R getResource(RequestLogger reqlog, String path,

0 commit comments

Comments
 (0)