Skip to content

Commit bdd7b65

Browse files
author
ehennum
committed
default format for raw CTS query to XML #965
1 parent a3d8e7e commit bdd7b65

File tree

6 files changed

+945
-929
lines changed

6 files changed

+945
-929
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
import com.marklogic.client.datamovement.QueryBatchException;
2828
import com.marklogic.client.datamovement.QueryEvent;
2929
import com.marklogic.client.datamovement.QueryBatcherListener;
30+
import com.marklogic.client.impl.HandleAccessor;
31+
import com.marklogic.client.impl.HandleImplementation;
32+
import com.marklogic.client.io.Format;
33+
import com.marklogic.client.io.marker.StructureWriteHandle;
34+
import com.marklogic.client.query.RawQueryDefinition;
3035
import org.slf4j.Logger;
3136
import org.slf4j.LoggerFactory;
3237

@@ -90,6 +95,26 @@ public QueryBatcherImpl(QueryDefinition query, DataMovementManager moveMgr, Fore
9095
this.query = query;
9196
withForestConfig(forestConfig);
9297
withBatchSize(1000);
98+
if (query instanceof RawQueryDefinition) {
99+
RawQueryDefinition rawQuery = (RawQueryDefinition) query;
100+
101+
StructureWriteHandle handle = rawQuery.getHandle();
102+
103+
@SuppressWarnings("rawtypes")
104+
HandleImplementation baseHandle = HandleAccessor.checkHandle(handle, "queryBatcher");
105+
106+
Format inputFormat = baseHandle.getFormat();
107+
switch(inputFormat) {
108+
case UNKNOWN:
109+
baseHandle.setFormat(Format.XML);
110+
break;
111+
case JSON:
112+
case XML:
113+
break;
114+
default:
115+
throw new UnsupportedOperationException("Only XML and JSON raw query definitions are possible.");
116+
}
117+
}
93118
}
94119

95120
public QueryBatcherImpl(Iterator<String> iterator, DataMovementManager moveMgr, ForestConfiguration forestConfig) {

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

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,64 +3036,63 @@ public <R extends UrisReadHandle> R uris(RequestLogger reqlog, Transaction trans
30363036
if (qdef.getOptionsName()!= null && qdef.getOptionsName().length() > 0) {
30373037
params.add("options", qdef.getOptionsName());
30383038
}
3039+
30393040
if (qdef instanceof RawQueryByExampleDefinition) {
30403041
throw new UnsupportedOperationException("Cannot search with RawQueryByExampleDefinition");
3042+
}
3043+
3044+
String text = null;
3045+
if (qdef instanceof StringQueryDefinition) {
3046+
text = ((StringQueryDefinition) qdef).getCriteria();
3047+
} else if (qdef instanceof StructuredQueryDefinition) {
3048+
text = ((StructuredQueryDefinition) qdef).getCriteria();
3049+
} else if (qdef instanceof RawStructuredQueryDefinition) {
3050+
text = ((RawStructuredQueryDefinition) qdef).getCriteria();
3051+
} else if (qdef instanceof RawCtsQueryDefinition) {
3052+
text = ((RawCtsQueryDefinition) qdef).getCriteria();
3053+
}
3054+
3055+
String qtextMessage = "";
3056+
if (text != null) {
3057+
params.add("q", text);
3058+
qtextMessage = " and string query \"" + text + "\"";
3059+
}
3060+
3061+
if (qdef instanceof RawCtsQueryDefinition) {
3062+
String structure = qdef instanceof RawQueryDefinitionImpl.CtsQuery ?
3063+
((RawQueryDefinitionImpl.CtsQuery) qdef).serialize() : "";
3064+
logger.debug("Query uris with raw cts query {}{}", structure, qtextMessage);
3065+
CtsQueryWriteHandle input = ((RawCtsQueryDefinition) qdef).getHandle();
3066+
return postResource(reqlog, "internal/uris", transaction, params, input, output);
3067+
} else if (qdef instanceof StructuredQueryDefinition) {
3068+
String structure = ((StructuredQueryDefinition) qdef).serialize();
3069+
logger.debug("Query uris with structured query {}{}", structure, qtextMessage);
3070+
if (structure != null) {
3071+
params.add("structuredQuery", structure);
3072+
}
3073+
} else if (qdef instanceof RawStructuredQueryDefinition) {
3074+
String structure = ((RawStructuredQueryDefinition) qdef).serialize();
3075+
logger.debug("Query uris with raw structured query {}{}", structure, qtextMessage);
3076+
if (structure != null) {
3077+
params.add("structuredQuery", structure);
3078+
}
3079+
} else if (qdef instanceof CombinedQueryDefinition) {
3080+
String structure = ((CombinedQueryDefinition) qdef).serialize();
3081+
logger.debug("Query uris with combined query {}", structure);
3082+
if (structure != null) {
3083+
params.add("structuredQuery", structure);
3084+
}
3085+
} else if (qdef instanceof StringQueryDefinition) {
3086+
logger.debug("Query uris with string query \"{}\"", text);
30413087
} else if (qdef instanceof RawQueryDefinition) {
30423088
logger.debug("Raw uris query");
3043-
30443089
StructureWriteHandle input = ((RawQueryDefinition) qdef).getHandle();
3045-
30463090
return postResource(reqlog, "internal/uris", transaction, params, input, output);
30473091
} else {
3048-
String text = null;
3049-
if (qdef instanceof StringQueryDefinition) {
3050-
text = ((StringQueryDefinition) qdef).getCriteria();
3051-
} else if (qdef instanceof StructuredQueryDefinition) {
3052-
text = ((StructuredQueryDefinition) qdef).getCriteria();
3053-
} else if (qdef instanceof RawStructuredQueryDefinition) {
3054-
text = ((RawStructuredQueryDefinition) qdef).getCriteria();
3055-
} else if (qdef instanceof RawCtsQueryDefinition) {
3056-
text = ((RawCtsQueryDefinition) qdef).getCriteria();
3057-
}
3058-
String qtextMessage = "";
3059-
if (text != null) {
3060-
params.add("q", text);
3061-
qtextMessage = " and string query \"" + text + "\"";
3062-
}
3063-
if (qdef instanceof RawCtsQueryDefinition) {
3064-
String structure = qdef instanceof RawQueryDefinitionImpl.CtsQuery ? ((RawQueryDefinitionImpl.CtsQuery) qdef).serialize() : "";
3065-
logger.debug("Query uris with raw cts query {}{}", structure, qtextMessage);
3066-
CtsQueryWriteHandle input = ((RawCtsQueryDefinition) qdef).getHandle();
3067-
return postResource(reqlog, "internal/uris", transaction, params, input, output);
3068-
} else if (qdef instanceof StructuredQueryDefinition) {
3069-
String structure = ((StructuredQueryDefinition) qdef).serialize();
3070-
3071-
logger.debug("Query uris with structured query {}{}", structure, qtextMessage);
3072-
if (structure != null) {
3073-
params.add("structuredQuery", structure);
3074-
}
3075-
} else if (qdef instanceof RawStructuredQueryDefinition) {
3076-
String structure = ((RawStructuredQueryDefinition) qdef).serialize();
3077-
logger.debug("Query uris with raw structured query {}{}", structure, qtextMessage);
3078-
if (structure != null) {
3079-
params.add("structuredQuery", structure);
3080-
}
3081-
} else if (qdef instanceof CombinedQueryDefinition) {
3082-
String structure = ((CombinedQueryDefinition) qdef).serialize();
3083-
3084-
logger.debug("Query uris with combined query {}", structure);
3085-
if (structure != null) {
3086-
params.add("structuredQuery", structure);
3087-
}
3088-
} else if (qdef instanceof StringQueryDefinition) {
3089-
logger.debug("Query uris with string query \"{}\"", text);
3090-
} else {
3091-
throw new UnsupportedOperationException("Cannot query uris with " +
3092-
qdef.getClass().getName());
3093-
}
3094-
return getResource(reqlog, "internal/uris", transaction, params, output);
3095-
}
3092+
throw new UnsupportedOperationException("Cannot query uris with " + qdef.getClass().getName());
30963093
}
3094+
return getResource(reqlog, "internal/uris", transaction, params, output);
3095+
}
30973096

30983097

30993098
@Override

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

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import com.marklogic.client.query.RawQueryDefinition;
2424
import com.marklogic.client.query.RawStructuredQueryDefinition;
2525

26-
abstract public class RawQueryDefinitionImpl
26+
abstract public class RawQueryDefinitionImpl<T extends StructureWriteHandle>
2727
extends AbstractQueryDefinition
2828
implements RawQueryDefinition
2929
{
3030
static public class Combined
31-
extends RawQueryDefinitionImpl
31+
extends RawQueryDefinitionImpl<StructureWriteHandle>
3232
implements RawCombinedQueryDefinition {
3333
Combined(StructureWriteHandle handle) {
3434
super(handle);
@@ -44,37 +44,42 @@ public RawCombinedQueryDefinition withHandle(StructureWriteHandle handle) {
4444
}
4545
}
4646

47-
static public class Structured
48-
extends RawQueryDefinitionImpl
49-
implements RawStructuredQueryDefinition {
47+
static abstract class RawCriteriaQueryImpl<T extends StructureWriteHandle>
48+
extends RawQueryDefinitionImpl<T>
49+
{
5050
private String criteria = null;
5151

52-
public Structured(StructureWriteHandle handle) {
52+
public RawCriteriaQueryImpl(T handle) {
5353
super(handle);
5454
}
55-
public Structured(StructureWriteHandle handle, String optionsName) {
55+
public RawCriteriaQueryImpl(T handle, String optionsName) {
5656
super(handle, optionsName);
5757
}
5858

59-
@Override
60-
public RawStructuredQueryDefinition withHandle(StructureWriteHandle handle) {
61-
setHandle(handle);
62-
return this;
59+
public String getCriteria() {
60+
return criteria;
6361
}
6462

65-
public String serialize() {
66-
if (getHandle() == null) return "";
67-
return HandleAccessor.contentAsString(getHandle());
63+
public void setCriteria(String criteria) {
64+
this.criteria = criteria;
6865
}
66+
}
6967

70-
@Override
71-
public String getCriteria() {
72-
return criteria;
68+
static public class Structured
69+
extends RawCriteriaQueryImpl<StructureWriteHandle>
70+
implements RawStructuredQueryDefinition
71+
{
72+
public Structured(StructureWriteHandle handle) {
73+
super(handle);
74+
}
75+
public Structured(StructureWriteHandle handle, String optionsName) {
76+
super(handle, optionsName);
7377
}
7478

7579
@Override
76-
public void setCriteria(String criteria) {
77-
this.criteria = criteria;
80+
public RawStructuredQueryDefinition withHandle(StructureWriteHandle handle) {
81+
setHandle(handle);
82+
return this;
7883
}
7984

8085
@Override
@@ -84,33 +89,15 @@ public RawStructuredQueryDefinition withCriteria(String criteria) {
8489
}
8590
}
8691

87-
static public class CtsQuery extends AbstractQueryDefinition implements RawCtsQueryDefinition {
88-
private String criteria = null;
89-
private CtsQueryWriteHandle handle;
90-
92+
static public class CtsQuery
93+
extends RawCriteriaQueryImpl<CtsQueryWriteHandle>
94+
implements RawCtsQueryDefinition
95+
{
9196
public CtsQuery(CtsQueryWriteHandle handle) {
92-
super();
93-
setHandle(handle);
97+
super(handle);
9498
}
9599
public CtsQuery(CtsQueryWriteHandle handle, String optionsName) {
96-
super();
97-
setHandle(handle);
98-
setOptionsName(optionsName);
99-
}
100-
101-
public String serialize() {
102-
if (getHandle() == null) return "";
103-
return HandleAccessor.contentAsString(getHandle());
104-
}
105-
106-
@Override
107-
public String getCriteria() {
108-
return criteria;
109-
}
110-
111-
@Override
112-
public void setCriteria(String criteria) {
113-
this.criteria = criteria;
100+
super(handle, optionsName);
114101
}
115102

116103
@Override
@@ -120,33 +107,27 @@ public RawCtsQueryDefinition withCriteria(String criteria) {
120107
}
121108

122109
@Override
123-
public CtsQueryWriteHandle getHandle() {
124-
return handle;
125-
}
126-
127-
@Override
128-
public void setHandle(CtsQueryWriteHandle handle) {
129-
this.handle = handle;
110+
public void setHandle(StructureWriteHandle handle) {
111+
if (handle != null && !(handle instanceof CtsQueryWriteHandle)) {
112+
throw new IllegalArgumentException(
113+
"handle must be an instance of CtsQueryWriteHandle instead of: "+
114+
handle.getClass().getCanonicalName()
115+
);
116+
}
117+
super.setHandle((CtsQueryWriteHandle) handle);
130118
}
131119

132120
@Override
133-
public RawCtsQueryDefinition withHandle(CtsQueryWriteHandle handle) {
121+
public RawCtsQueryDefinition withHandle(StructureWriteHandle handle) {
134122
setHandle(handle);
135123
return this;
136124
}
137-
138-
@Override
139-
public String toString() {
140-
if (handle == null) {
141-
return "";
142-
}
143-
return handle.toString();
144-
}
145125
}
146126

147127
static public class ByExample
148-
extends RawQueryDefinitionImpl
149-
implements RawQueryByExampleDefinition {
128+
extends RawQueryDefinitionImpl<StructureWriteHandle>
129+
implements RawQueryByExampleDefinition
130+
{
150131
ByExample(StructureWriteHandle handle) {
151132
super(handle);
152133
}
@@ -161,25 +142,31 @@ public RawQueryByExampleDefinition withHandle(StructureWriteHandle handle) {
161142
}
162143
}
163144

164-
private StructureWriteHandle handle;
145+
private T handle;
165146

166-
RawQueryDefinitionImpl(StructureWriteHandle handle) {
147+
RawQueryDefinitionImpl(T handle) {
167148
super();
168149
setHandle(handle);
169150
}
170-
RawQueryDefinitionImpl(StructureWriteHandle handle, String optionsName) {
151+
RawQueryDefinitionImpl(T handle, String optionsName) {
171152
this(handle);
172153
setOptionsName(optionsName);
173154
}
174155

156+
public String serialize() {
157+
if (handle == null) return "";
158+
return HandleAccessor.contentAsString(handle);
159+
}
160+
175161
@Override
176-
public StructureWriteHandle getHandle() {
162+
public T getHandle() {
177163
return handle;
178164
}
179165

166+
// must override with instanceof test if T is not StructureWriteHandle
180167
@Override
181168
public void setHandle(StructureWriteHandle handle) {
182-
this.handle = handle;
169+
this.handle = (T) handle;
183170
}
184171

185172
@Override
@@ -189,5 +176,4 @@ public String toString() {
189176
}
190177
return handle.toString();
191178
}
192-
193179
}

marklogic-client-api/src/main/java/com/marklogic/client/io/marker/CtsQueryWriteHandle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
/**
1919
* A CtsQueryWriteHandle represents a serialized cts query.
2020
*/
21-
public interface CtsQueryWriteHandle extends AbstractWriteHandle {
21+
public interface CtsQueryWriteHandle extends StructureWriteHandle {
2222
}

marklogic-client-api/src/main/java/com/marklogic/client/query/RawCtsQueryDefinition.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
package com.marklogic.client.query;
1717

1818
import com.marklogic.client.io.marker.CtsQueryWriteHandle;
19+
import com.marklogic.client.io.marker.StructureWriteHandle;
1920

2021
/**
2122
* A RawCtsQueryDefinition allows you to create a query with a serialized cts
2223
* query in a JSON or XML representation.
2324
*/
24-
public interface RawCtsQueryDefinition extends QueryDefinition, ValueQueryDefinition {
25+
public interface RawCtsQueryDefinition extends RawQueryDefinition, ValueQueryDefinition {
2526
/**
2627
* Returns the query criteria, that is the query string.
2728
* @return The query string.
@@ -48,17 +49,11 @@ public interface RawCtsQueryDefinition extends QueryDefinition, ValueQueryDefini
4849
*/
4950
CtsQueryWriteHandle getHandle();
5051

51-
/**
52-
* Specifies the handle for the JSON or XML representation of the query.
53-
* @param handle the JSON or XML handle.
54-
*/
55-
void setHandle(CtsQueryWriteHandle handle);
56-
5752
/**
5853
* Specifies the handle for the JSON or XML representation
5954
* of a combined query and returns the query definition.
6055
* @param handle the JSON or XML handle.
6156
* @return the query definition.
6257
*/
63-
RawCtsQueryDefinition withHandle(CtsQueryWriteHandle handle);
58+
RawCtsQueryDefinition withHandle(StructureWriteHandle handle);
6459
}

0 commit comments

Comments
 (0)