Skip to content

Commit 40df91c

Browse files
committed
fix #559 - finish what we started with commits for #136 and #527, so RawStructuredQuery can extend StructuredQuery; also fix an unexpected bug in StructuredSearchTest which required a missing geo path index
1 parent 3416127 commit 40df91c

File tree

3 files changed

+96
-41
lines changed

3 files changed

+96
-41
lines changed

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

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,29 +2115,37 @@ void init() {
21152115
addEncodedParam(params, "q", text);
21162116
}
21172117
}
2118-
if (queryDef instanceof RawQueryDefinition) {
2118+
if (queryDef instanceof StructuredQueryDefinition) {
2119+
structure = ((StructuredQueryDefinition) queryDef).serialize();
2120+
2121+
if (logger.isDebugEnabled()) {
2122+
String text = ((StringQueryDefinition) queryDef).getCriteria();
2123+
String qtextMessage = text == null ? "" : " and string query \"" + text + "\"";
2124+
logger.debug("Searching for structure {}{}", structure, qtextMessage);
2125+
}
2126+
2127+
String payloadMimetype = "application/xml";
2128+
if (queryDef instanceof RawQueryDefinition) {
2129+
StructureWriteHandle handle = ((RawQueryDefinition) queryDef).getHandle();
2130+
baseHandle = HandleAccessor.checkHandle(handle, "search");
2131+
2132+
Format payloadFormat = getStructuredQueryFormat(baseHandle);
2133+
payloadMimetype = getMimetypeWithDefaultXML(payloadFormat, baseHandle);
2134+
}
2135+
2136+
webResource = getConnection().path("search").queryParams(params);
2137+
builder = (payloadMimetype != null) ?
2138+
webResource.type(payloadMimetype).accept(mimetype) :
2139+
webResource.accept(mimetype);
2140+
} else if (queryDef instanceof RawQueryDefinition) {
21192141
if (logger.isDebugEnabled())
21202142
logger.debug("Raw search");
21212143

2122-
StructureWriteHandle handle =
2123-
((RawQueryDefinition) queryDef).getHandle();
2124-
2144+
StructureWriteHandle handle = ((RawQueryDefinition) queryDef).getHandle();
21252145
baseHandle = HandleAccessor.checkHandle(handle, "search");
21262146

2127-
Format payloadFormat = baseHandle.getFormat();
2128-
if (payloadFormat == Format.UNKNOWN)
2129-
payloadFormat = null;
2130-
else if (payloadFormat != Format.XML && payloadFormat != Format.JSON)
2131-
throw new IllegalArgumentException(
2132-
"Cannot perform raw search for "+payloadFormat.name());
2133-
2134-
String payloadMimetype = baseHandle.getMimetype();
2135-
if (payloadFormat != null) {
2136-
if (payloadMimetype == null)
2137-
payloadMimetype = payloadFormat.getDefaultMimetype();
2138-
} else if (payloadMimetype == null) {
2139-
payloadMimetype = "application/xml";
2140-
}
2147+
Format payloadFormat = getStructuredQueryFormat(baseHandle);
2148+
String payloadMimetype = getMimetypeWithDefaultXML(payloadFormat, baseHandle);
21412149

21422150
String path = (queryDef instanceof RawQueryByExampleDefinition) ?
21432151
"qbe" : "search";
@@ -2167,14 +2175,6 @@ else if (payloadFormat != Format.XML && payloadFormat != Format.JSON)
21672175

21682176
webResource = getConnection().path("keyvalue").queryParams(params);
21692177
builder = webResource.accept(mimetype);
2170-
} else if (queryDef instanceof StructuredQueryDefinition) {
2171-
structure = ((StructuredQueryDefinition) queryDef).serialize();
2172-
2173-
if (logger.isDebugEnabled())
2174-
logger.debug("Searching for structure {}", structure);
2175-
2176-
webResource = getConnection().path("search").queryParams(params);
2177-
builder = webResource.type("application/xml").accept(mimetype);
21782178
} else if (queryDef instanceof CombinedQueryDefinition) {
21792179
structure = ((CombinedQueryDefinition) queryDef).serialize();
21802180

@@ -2275,6 +2275,28 @@ ClientResponse getResponse() {
22752275
}
22762276
}
22772277

2278+
private Format getStructuredQueryFormat(HandleImplementation baseHandle) {
2279+
Format payloadFormat = baseHandle.getFormat();
2280+
if (payloadFormat == Format.UNKNOWN) {
2281+
payloadFormat = null;
2282+
} else if (payloadFormat != Format.XML && payloadFormat != Format.JSON) {
2283+
throw new IllegalArgumentException(
2284+
"Cannot perform raw search for format "+payloadFormat.name());
2285+
}
2286+
return payloadFormat;
2287+
}
2288+
2289+
private String getMimetypeWithDefaultXML(Format payloadFormat, HandleImplementation baseHandle) {
2290+
String payloadMimetype = baseHandle.getMimetype();
2291+
if (payloadFormat != null) {
2292+
if (payloadMimetype == null)
2293+
payloadMimetype = payloadFormat.getDefaultMimetype();
2294+
} else if (payloadMimetype == null) {
2295+
payloadMimetype = "application/xml";
2296+
}
2297+
return payloadMimetype;
2298+
}
2299+
22782300
@Override
22792301
public void deleteSearch(RequestLogger reqlog, DeleteQueryDefinition queryDef,
22802302
Transaction transaction) throws ForbiddenUserException,
@@ -2908,7 +2930,9 @@ public <R extends UrisReadHandle> R uris(RequestLogger reqlog, Transaction trans
29082930
if (qdef instanceof StructuredQueryDefinition) {
29092931
String structure = ((StructuredQueryDefinition) qdef).serialize();
29102932

2911-
logger.debug("Query uris with structured query {}", structure);
2933+
String text = ((StringQueryDefinition) qdef).getCriteria();
2934+
String qtextMessage = text == null ? "" : " and string query \"" + text + "\"";
2935+
logger.debug("Query uris with structured query {}{}", structure, qtextMessage);
29122936
if (structure != null) {
29132937
params.add("structuredQuery", structure);
29142938
}
@@ -4794,20 +4818,20 @@ public InputStream match(QueryDefinition queryDef,
47944818
addEncodedParam(params, "q", text);
47954819
}
47964820
}
4797-
if (queryDef instanceof RawQueryDefinition) {
4798-
StructureWriteHandle handle = ((RawQueryDefinition) queryDef).getHandle();
4799-
baseHandle = HandleAccessor.checkHandle(handle, "match");
4821+
if (queryDef instanceof StructuredQueryDefinition) {
4822+
structure = ((StructuredQueryDefinition) queryDef).serialize();
48004823

48014824
if (logger.isDebugEnabled())
4802-
logger.debug("Searching for structure {}", structure);
4825+
logger.debug("Searching for structure {}",
4826+
structure);
48034827

48044828
builder = makeBuilder("alert/match", params, "application/xml", "application/xml");
4805-
} else if (queryDef instanceof StructuredQueryDefinition) {
4806-
structure = ((StructuredQueryDefinition) queryDef).serialize();
4829+
} else if (queryDef instanceof RawQueryDefinition) {
4830+
StructureWriteHandle handle = ((RawQueryDefinition) queryDef).getHandle();
4831+
baseHandle = HandleAccessor.checkHandle(handle, "match");
48074832

48084833
if (logger.isDebugEnabled())
4809-
logger.debug("Searching for structure {}",
4810-
structure);
4834+
logger.debug("Searching for structure {}", structure);
48114835

48124836
builder = makeBuilder("alert/match", params, "application/xml", "application/xml");
48134837
} else if (queryDef instanceof StringQueryDefinition) {

src/test/java/com/marklogic/client/test/datamovement/QueryBatcherTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,10 @@ public static void beforeClass() throws Exception {
8383

8484
@AfterClass
8585
public static void afterClass() {
86-
/*
8786
QueryManager queryMgr = client.newQueryManager();
8887
DeleteQueryDefinition deleteQuery = queryMgr.newDeleteDefinition();
8988
deleteQuery.setCollections(collection);
9089
queryMgr.delete(deleteQuery);
91-
*/
9290

9391
Common.release();
9492
}
@@ -275,8 +273,8 @@ public void runQueryBatcher(QueryBatcher queryBatcher, QueryDefinition query, Ma
275273
assertEquals(numExpected, totalResults.get());
276274

277275
report = moveMgr.getJobReport(ticket);
278-
long maxTime = new Date().getTime()+500;
279-
long minTime = new Date().getTime()-500;
276+
long maxTime = new Date().getTime()+1000;
277+
long minTime = new Date().getTime()-1000;
280278
Date batchDate = batchTimestamp.get().getTime();
281279
assertTrue("Batch has incorrect timestamp", batchDate.getTime() >= minTime && batchDate.getTime() <= maxTime);
282280
Date reportDate = report.getReportTimestamp().getTime();

src/test/resources/bootstrap.xqy

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ declare function bootstrap:database-configure(
2424
let $c := bootstrap:create-geospatial-element-indexes($c, $dbid)
2525
let $c := bootstrap:create-geospatial-element-child-indexes($c, $dbid)
2626
let $c := bootstrap:create-geospatial-element-pair-indexes($c, $dbid)
27+
let $c := bootstrap:create-path-namespaces($c, $dbid)
2728
let $c := bootstrap:create-geospatial-path-indexes($c, $dbid)
2829
let $c := bootstrap:create-geospatial-region-path-indexes($c, $dbid)
2930
let $c := bootstrap:create-path-range-indexes($c, $dbid)
@@ -298,6 +299,38 @@ declare function bootstrap:create-geospatial-element-pair-indexes(
298299
else admin:database-add-geospatial-element-pair-index($c, $dbid, $index-specs)
299300
};
300301

302+
declare function bootstrap:create-path-namespaces(
303+
$c as element(configuration),
304+
$dbid as xs:unsignedLong
305+
) as element(configuration)
306+
{
307+
let $index-specs :=
308+
let $curr-idx := admin:database-get-path-namespaces($c, $dbid)
309+
let $new-idx := (
310+
"rootOrg", "root.org",
311+
"targetOrg", "target.org"
312+
)
313+
let $n := 2
314+
for $i in 1 to (count($new-idx) idiv $n)
315+
let $offset := ($i * $n) - ($n - 1)
316+
let $prefix := subsequence($new-idx, $offset, 1)
317+
let $namespace := subsequence($new-idx, $offset + 1, 1)
318+
let $curr := $curr-idx[
319+
string(dbx:prefix) eq $prefix and
320+
string(dbx:namespace-uri) eq $namespace
321+
]
322+
return
323+
if (exists($curr)) then (
324+
xdmp:log(concat("Namespace already exists:", $prefix))
325+
) else (
326+
admin:database-path-namespace( $prefix, $namespace ),
327+
xdmp:log(concat("Creating path-namespace:", $prefix))
328+
)
329+
return
330+
if (empty($index-specs)) then $c
331+
else admin:database-add-path-namespace($c, $dbid, $index-specs)
332+
};
333+
301334
declare function bootstrap:create-geospatial-path-indexes(
302335
$c as element(configuration),
303336
$dbid as xs:unsignedLong
@@ -306,9 +339,9 @@ declare function bootstrap:create-geospatial-path-indexes(
306339
let $index-specs :=
307340
let $curr-idx := admin:database-get-geospatial-path-indexes($c, $dbid)
308341
let $new-idx := (
309-
"com.marklogic.client.test.City/latLong"
342+
"com.marklogic.client.test.City/latLong",
343+
"/rootOrg:geo/targetOrg:path"
310344
)
311-
(: no offset necessary now since each new-idx only has one item, but that may change :)
312345
let $n := 1
313346
for $i in 1 to (count($new-idx) idiv $n)
314347
let $offset := ($i * $n) - ($n - 1)

0 commit comments

Comments
 (0)