Skip to content

Commit 50fee8b

Browse files
author
ehennum
committed
RowBatcher for raw AST and QueryDSL #1271
1 parent 5d379d8 commit 50fee8b

File tree

10 files changed

+206
-33
lines changed

10 files changed

+206
-33
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import com.marklogic.client.expression.PlanBuilder;
1919
import com.marklogic.client.expression.PlanBuilder.ModifyPlan;
2020
import com.marklogic.client.io.marker.ContentHandle;
21+
import com.marklogic.client.io.marker.JSONWriteHandle;
22+
import com.marklogic.client.io.marker.TextWriteHandle;
23+
import com.marklogic.client.row.RawPlanDefinition;
24+
import com.marklogic.client.row.RawQueryDSLPlan;
25+
import com.marklogic.client.row.RawSQLPlan;
2126
import com.marklogic.client.row.RowManager;
2227
import com.marklogic.client.type.PlanCondition;
2328
import com.marklogic.client.type.PlanExprColSeq;
@@ -114,11 +119,31 @@ public interface RowBatcher<T> extends Batcher {
114119
RowManager getRowManager();
115120

116121
/**
117-
* Specifies the plan for getting rows from a view.
118-
* @param viewPlan the view providing the rows exported by the RowBatcher
122+
* Specifies the plan for getting rows from a view
123+
* with PlanBuilder.
124+
* @see RowManager#newPlanBuilder()
125+
* @see PlanBuilder#fromView(String, String)
126+
* @param viewPlan the PlanBuilder view providing the rows exported by the RowBatcher
119127
* @return the RowBatcher for chaining other initializations
120128
*/
121129
RowBatcher<T> withBatchView(PlanBuilder.ModifyPlan viewPlan);
130+
/**
131+
* Specifies the plan for getting rows from a view
132+
* from a serialized AST in JSON format.
133+
* @see RowManager#newRawPlanDefinition(JSONWriteHandle)
134+
* @param viewPlan the raw AST for the rows exported by the RowBatcher
135+
* @return the RowBatcher for chaining other initializations
136+
*/
137+
RowBatcher<T> withBatchView(RawPlanDefinition viewPlan);
138+
/**
139+
* Specifies the plan for getting rows from a view
140+
* from a serialized Query DSL in JavaScript format.
141+
* @see RowManager#newRawQueryDSLPlan(TextWriteHandle)
142+
* @param viewPlan the raw Query DSL for the rows exported by the RowBatcher
143+
* @return the RowBatcher for chaining other initializations
144+
*/
145+
RowBatcher<T> withBatchView(RawQueryDSLPlan viewPlan);
146+
122147
/**
123148
* Enables retrieval of rows that were present in the view
124149
* at the time that the first batch was retrieved, ignoring

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
import com.marklogic.client.io.Format;
2727
import com.marklogic.client.io.JacksonHandle;
2828
import com.marklogic.client.io.StringHandle;
29+
import com.marklogic.client.io.marker.AbstractWriteHandle;
2930
import com.marklogic.client.io.marker.ContentHandle;
3031
import com.marklogic.client.io.marker.StructureReadHandle;
3132
import com.marklogic.client.row.RawPlanDefinition;
33+
import com.marklogic.client.row.RawQueryDSLPlan;
34+
import com.marklogic.client.row.RawSQLPlan;
3235
import com.marklogic.client.row.RowManager;
3336
import org.slf4j.Logger;
3437
import org.slf4j.LoggerFactory;
@@ -56,7 +59,6 @@ class RowBatcherImpl<T> extends BatcherImpl implements RowBatcher<T> {
5659
private RowBatchFailureListener[] failureListeners;
5760
private RowBatchSuccessListener[] sucessListeners;
5861

59-
private PlanBuilder.ModifyPlan inputPlan;
6062
private String schemaName;
6163
private String viewName;
6264

@@ -103,16 +105,27 @@ public RowManager getRowManager() {
103105

104106
@Override
105107
public RowBatcher<T> withBatchView(PlanBuilder.ModifyPlan inputPlan) {
106-
requireNotStarted("Must specify batch view before starting job");
107-
analyzePlan(inputPlan);
108+
if (inputPlan == null)
109+
throw new IllegalArgumentException("Plan cannot be null");
110+
analyzePlan(inputPlan.export(new StringHandle().withFormat(Format.JSON)));
108111
return this;
109112
}
110-
private void analyzePlan(PlanBuilder.ModifyPlan inputPlan) {
111-
if (inputPlan == null)
112-
throw new IllegalArgumentException("modify plan cannot be null");
113-
this.inputPlan = inputPlan;
114-
115-
StringHandle initialPlan = inputPlan.export(new StringHandle().withFormat(Format.JSON));
113+
@Override
114+
public RowBatcher<T> withBatchView(RawPlanDefinition viewPlan) {
115+
if (viewPlan == null)
116+
throw new IllegalArgumentException("Raw plan definition cannot be null");
117+
analyzePlan(viewPlan.getHandle());
118+
return this;
119+
}
120+
@Override
121+
public RowBatcher<T> withBatchView(RawQueryDSLPlan viewPlan) {
122+
if (viewPlan == null)
123+
throw new IllegalArgumentException("Raw query DSL plan cannot be null");
124+
analyzePlan(viewPlan.getHandle());
125+
return this;
126+
}
127+
private void analyzePlan(AbstractWriteHandle initialPlan) {
128+
requireNotStarted("Must specify batch view before starting job");
116129

117130
DatabaseClientImpl client = (DatabaseClientImpl) getPrimaryClient();
118131
JsonNode viewInfo = client.getServices().postResource(

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public RawPlanDefinition newRawPlanDefinition(JSONWriteHandle handle) {
119119
}
120120

121121
@Override
122-
public RawQueryDSLPlan newQueryDSLPlan(TextWriteHandle handle) {
122+
public RawQueryDSLPlan newRawQueryDSLPlan(TextWriteHandle handle) {
123123
return new RawQueryDSLPlanImpl(handle);
124124
}
125125
@Override
@@ -1604,16 +1604,19 @@ static class RawSQLPlanImpl extends RawPlanImpl<TextWriteHandle> implements RawS
16041604
super(handle, params);
16051605
}
16061606

1607+
@Override
16071608
RawSQLPlanImpl parameterize(
16081609
TextWriteHandle handle, Map<PlanBuilderBaseImpl.PlanParamBase,BaseTypeImpl.ParamBinder> params
16091610
) {
16101611
return new RawSQLPlanImpl(handle, params);
16111612
}
1613+
@Override
16121614
void configHandle(BaseHandle handle) {
16131615
handle.setFormat(Format.TEXT);
16141616
handle.setMimetype("application/sql");
16151617
}
16161618

1619+
@Override
16171620
public RawSQLPlan withHandle(TextWriteHandle handle) {
16181621
setHandle(handle);
16191622
return this;
@@ -1629,16 +1632,19 @@ static class RawSPARQLSelectPlanImpl extends RawPlanImpl<TextWriteHandle> implem
16291632
super(handle, params);
16301633
}
16311634

1635+
@Override
16321636
RawSPARQLSelectPlanImpl parameterize(
16331637
TextWriteHandle handle, Map<PlanBuilderBaseImpl.PlanParamBase,BaseTypeImpl.ParamBinder> params
16341638
) {
16351639
return new RawSPARQLSelectPlanImpl(handle, params);
16361640
}
1641+
@Override
16371642
void configHandle(BaseHandle handle) {
16381643
handle.setFormat(Format.TEXT);
16391644
handle.setMimetype("application/sparql-query");
16401645
}
16411646

1647+
@Override
16421648
public RawSPARQLSelectPlan withHandle(TextWriteHandle handle) {
16431649
setHandle(handle);
16441650
return this;
@@ -1654,16 +1660,19 @@ static class RawQueryDSLPlanImpl extends RawPlanImpl<TextWriteHandle> implements
16541660
super(handle, params);
16551661
}
16561662

1663+
@Override
16571664
RawQueryDSLPlanImpl parameterize(
16581665
TextWriteHandle handle, Map<PlanBuilderBaseImpl.PlanParamBase,BaseTypeImpl.ParamBinder> params
16591666
) {
16601667
return new RawQueryDSLPlanImpl(handle, params);
16611668
}
1669+
@Override
16621670
void configHandle(BaseHandle handle) {
16631671
handle.setFormat(Format.TEXT);
16641672
handle.setMimetype("application/vnd.marklogic.querydsl+javascript");
16651673
}
16661674

1675+
@Override
16671676
public RawQueryDSLPlan withHandle(TextWriteHandle handle) {
16681677
setHandle(handle);
16691678
return this;
@@ -1679,11 +1688,13 @@ private RawPlanDefinitionImpl(
16791688
super(handle, params);
16801689
}
16811690

1691+
@Override
16821692
RawPlanDefinitionImpl parameterize(
16831693
JSONWriteHandle handle, Map<PlanBuilderBaseImpl.PlanParamBase,BaseTypeImpl.ParamBinder> params
16841694
) {
16851695
return new RawPlanDefinitionImpl(handle, params);
16861696
}
1697+
@Override
16871698
void configHandle(BaseHandle handle) {
16881699
handle.setFormat(Format.JSON);
16891700
handle.setMimetype("application/json");

marklogic-client-api/src/main/java/com/marklogic/client/row/RawPlan.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717

1818
import com.marklogic.client.expression.PlanBuilder;
1919

20+
/**
21+
* An abstraction for a serialization of a plan
22+
* such as an AST in JSON format, a Query DSL in JavaScript syntax,
23+
* or a SQL or SPARQL SELECT query.
24+
*/
2025
public interface RawPlan extends PlanBuilder.Plan {
2126
}

marklogic-client-api/src/main/java/com/marklogic/client/row/RawQueryDSLPlan.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,25 @@
1717

1818
import com.marklogic.client.io.marker.TextWriteHandle;
1919

20+
/**
21+
* A Raw Query DSL Plan provides access to a plan
22+
* expressed in JavaScript syntax.
23+
*/
2024
public interface RawQueryDSLPlan extends RawPlan {
25+
/**
26+
* Returns the handle for the text of the JavaScript representation of the Query DSL.
27+
* @return the text handle for the JavaScript serialization
28+
*/
2129
TextWriteHandle getHandle();
30+
/**
31+
* Specifies the handle for the text of the JavaScript representation of the Query DSL.
32+
* @param handle the text handle for the JavaScript serialization
33+
*/
2234
void setHandle(TextWriteHandle handle);
35+
/**
36+
* Assigns the handle and returns the raw plan as a convenience.
37+
* @param handle the text handle for the JavaScript serialization
38+
* @return this raw plan object
39+
*/
2340
RawQueryDSLPlan withHandle(TextWriteHandle handle);
2441
}

marklogic-client-api/src/main/java/com/marklogic/client/row/RawSPARQLSelectPlan.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,26 @@
1717

1818
import com.marklogic.client.io.marker.TextWriteHandle;
1919

20+
/**
21+
* A Raw SPARQL Select Plan provides access to a plan
22+
* expressed as a SPARQL SELECT statement. A SPARQL query can only
23+
* represent a subset of the capabilities of a plan.
24+
*/
2025
public interface RawSPARQLSelectPlan extends RawPlan {
26+
/**
27+
* Returns the handle for the text of the SPARQL query.
28+
* @return the text handle for the SPARQL query
29+
*/
2130
TextWriteHandle getHandle();
31+
/**
32+
* Specifies the handle for the text of the SPARQL query.
33+
* @param handle the text handle for the SPARQL query
34+
*/
2235
void setHandle(TextWriteHandle handle);
36+
/**
37+
* Assigns the handle and returns the raw plan as a convenience.
38+
* @param handle the text handle for the SPARQL query
39+
* @return this raw plan object
40+
*/
2341
RawSPARQLSelectPlan withHandle(TextWriteHandle handle);
2442
}

marklogic-client-api/src/main/java/com/marklogic/client/row/RawSQLPlan.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,26 @@
1717

1818
import com.marklogic.client.io.marker.TextWriteHandle;
1919

20+
/**
21+
* A Raw SQL Plan provides access to a plan
22+
* expressed as an SQL SELECT statement. An SQL query can only
23+
* represent a subset of the capabilities of a plan.
24+
*/
2025
public interface RawSQLPlan extends RawPlan {
26+
/**
27+
* Returns the handle for the text of the SQL query.
28+
* @return the text handle for the SQL query
29+
*/
2130
TextWriteHandle getHandle();
31+
/**
32+
* Specifies the handle for the text of the SQL query.
33+
* @param handle the text handle for the SQL query
34+
*/
2235
void setHandle(TextWriteHandle handle);
36+
/**
37+
* Assigns the handle and returns the raw plan as a convenience.
38+
* @param handle the text handle for the SQL query
39+
* @return this raw plan object
40+
*/
2341
RawSQLPlan withHandle(TextWriteHandle handle);
2442
}

marklogic-client-api/src/main/java/com/marklogic/client/row/RowManager.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,29 @@ public enum RowStructure{ARRAY, OBJECT}
8080

8181
/**
8282
* Defines a plan from a JSON serialization of the plan AST (Abstract Syntax Tree).
83-
* @param handle a handle for a JSON serialization of a PlanAST
83+
* @param handle a handle for a JSON serialization of a plan AST
8484
* @return a plan for constructing and retrieving database rows
8585
*/
8686
RawPlanDefinition newRawPlanDefinition(JSONWriteHandle handle);
87-
88-
// TODO: JavaDoc
89-
RawQueryDSLPlan newQueryDSLPlan(TextWriteHandle handle);
87+
/**
88+
* Defines a plan from a Query DSL in a JavaScript serialization.
89+
* @param handle a textual handle for Query DSL in JavaScript format
90+
* @return a plan for constructing and retrieving database rows
91+
*/
92+
RawQueryDSLPlan newRawQueryDSLPlan(TextWriteHandle handle);
93+
/**
94+
* Defines a plan from an SQL query.
95+
* @param handle a textual handle for the SQL serialization
96+
* @return a plan for constructing and retrieving database rows
97+
*/
9098
RawSQLPlan newRawSQLPlan(TextWriteHandle handle);
99+
/**
100+
* Defines a plan from a SPARQL SELECT query.
101+
* @param handle a textual handle for the SPARQL serialization
102+
* @return a plan for constructing and retrieving database rows
103+
*/
91104
RawSPARQLSelectPlan newRawSPARQLSelectPlan(TextWriteHandle handle);
92105

93-
94106
/**
95107
* Constructs and retrieves a set of database rows based on a plan using
96108
* a map interface for the column values in each row.

marklogic-client-api/src/test/java/com/marklogic/client/test/RowManagerTest.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -846,20 +846,40 @@ private DOMHandle initNamespaces(DOMHandle handle) {
846846

847847
return handle;
848848
}
849-
// TODO: query and results
850-
// @Test
849+
@Test
851850
public void testRawSQL() throws IOException {
852851
String plan = "SELECT *\n" +
853-
"FROM opticUnitTest.musician\n" +
852+
"FROM opticUnitTest.musician AS ''\n" +
853+
"WHERE lastName IN ('Armstrong', 'Davis')" +
854854
"ORDER BY lastName;\n";
855+
856+
RowManager rowMgr = Common.client.newRowManager();
857+
858+
testViewRows(rowMgr.resultRows(rowMgr.newRawSQLPlan(new StringHandle(plan))));
855859
}
856-
// TODO: query and results
857-
// @Test
860+
@Test
858861
public void testRawSPARQLSelect() throws IOException {
859-
String plan = "PREFIX dc: <http://purl.org/dc/terms/>\n" +
860-
"SELECT ?datastore ?title\n" +
861-
"WHERE {?datastore dc:type <http://purl.org/dc/dcmitype/Dataset> ; dc:title ?title .}\n" +
862-
"ORDER BY ?title\n";
862+
String plan = "PREFIX rg: <http://example.org/rowgraph/>\n" +
863+
"SELECT ?graph ?object1 ?object2\n" +
864+
"WHERE {?graph rg:p1 ?object1 ; rg:p2 ?object2}\n" +
865+
"ORDER BY ?graph";
866+
867+
String[] graph = {"http://example.org/rowgraph/s1", "http://example.org/rowgraph/s2"};
868+
String[] object1 = {"http://example.org/rowgraph/o1", "http://example.org/rowgraph/o3"};
869+
String[] object2 = {"http://example.org/rowgraph/o2", "http://example.org/rowgraph/o4"};
870+
871+
RowManager rowMgr = Common.client.newRowManager();
872+
873+
RowSet<RowRecord> rows = rowMgr.resultRows(rowMgr.newRawSPARQLSelectPlan(new StringHandle(plan)));
874+
875+
int rowNum = 0;
876+
for (RowRecord row: rows) {
877+
assertEquals("unexpected graph value in row record "+rowNum, graph[rowNum], row.getString("graph"));
878+
assertEquals("unexpected object1 value in row record "+rowNum, object1[rowNum], row.getString("object1"));
879+
assertEquals("unexpected object2 value in row record "+rowNum, object2[rowNum], row.getString("object2"));
880+
rowNum++;
881+
}
882+
assertEquals("unexpected count of result records", 2, rowNum);
863883
}
864884
@Test
865885
public void testRawQueryDSL() throws IOException {
@@ -874,7 +894,7 @@ public void testRawQueryDSL() throws IOException {
874894

875895
RowManager rowMgr = Common.client.newRowManager();
876896

877-
testViewRows(rowMgr.resultRows(rowMgr.newQueryDSLPlan(new StringHandle(plan))));
897+
testViewRows(rowMgr.resultRows(rowMgr.newRawQueryDSLPlan(new StringHandle(plan))));
878898
}
879899
private void checkSingleRow(NodeList row, RowSetPart datatypeStyle) {
880900
assertEquals("unexpected column count in XML", 2, row.getLength());

0 commit comments

Comments
 (0)