Skip to content

Commit 68ba8eb

Browse files
author
Burak Serdar
committed
Move associateDocs to Assemble
1 parent b32fe3a commit 68ba8eb

File tree

2 files changed

+69
-67
lines changed

2 files changed

+69
-67
lines changed

crud/src/main/java/com/redhat/lightblue/assoc/ep/Assemble.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
import com.redhat.lightblue.query.QueryExpression;
3939
import com.redhat.lightblue.query.NaryLogicalOperator;
4040

41+
import com.redhat.lightblue.assoc.BindQuery;
42+
43+
import com.redhat.lightblue.eval.QueryEvaluator;
44+
4145
/**
4246
* There are two sides to an Assemble step: Assemble gets results from the
4347
* source, and for each of those documents, it runs the associated queries on
@@ -182,7 +186,7 @@ public void commit() {
182186
}
183187
List<ResultDocument> destResults = dest.getResultList(combinedQuery, ctx);
184188
for (DocAndQ parentDocAndQ : docs) {
185-
Searches.associateDocs(parentDocAndQ.doc, destResults, aq);
189+
associateDocs(parentDocAndQ.doc, destResults, aq);
186190
}
187191
}
188192
docs = new ArrayList<>();
@@ -208,6 +212,70 @@ private JsonNode toJson(Step.ToJsonCb<Step> scb,Step.ToJsonCb<ExecutionBlock> bc
208212
return o;
209213
}
210214

215+
/**
216+
* Associates child documents obtained from 'aq' to all the slots in the
217+
* parent document
218+
*/
219+
public static void associateDocs(ResultDocument parentDoc,
220+
List<ResultDocument> childDocs,
221+
AssociationQuery aq) {
222+
List<ChildSlot> slots = parentDoc.getSlots().get(aq.getReference());
223+
for (ChildSlot slot : slots) {
224+
associateDocs(parentDoc, slot, childDocs, aq);
225+
}
226+
}
227+
228+
/**
229+
* Associate child documents with their parents. The association query is
230+
* for the association from the child to the parent, so caller must flip it
231+
* before sending it in if necessary. The caller also make sure parentDocs
232+
* is a unique stream.
233+
*
234+
* @param parentDoc The parent document
235+
* @param parentSlot The slot in parent docuemnt to which the results will
236+
* be attached
237+
* @param childDocs The child documents
238+
* @param aq The association query from parent to child. This may not be the
239+
* same association query between the blocks. If the child block is before
240+
* the parent block, a new aq must be constructed for the association from
241+
* the parent to the child
242+
*/
243+
public static void associateDocs(ResultDocument parentDoc,
244+
ChildSlot parentSlot,
245+
List<ResultDocument> childDocs,
246+
AssociationQuery aq) {
247+
if (!childDocs.isEmpty()) {
248+
LOGGER.debug("Associating docs");
249+
ExecutionBlock childBlock = childDocs.get(0).getBlock();
250+
ArrayNode destNode = (ArrayNode) parentDoc.getDoc().get(parentSlot.getSlotFieldName());
251+
BindQuery binders = parentDoc.getBindersForSlot(parentSlot, aq);
252+
// No binders means all child docs will be added to the parent
253+
if (binders.getBindings().isEmpty()) {
254+
if (destNode == null) {
255+
destNode = JsonNodeFactory.instance.arrayNode();
256+
parentDoc.getDoc().modify(parentSlot.getSlotFieldName(), destNode, true);
257+
}
258+
for (ResultDocument d : childDocs) {
259+
destNode.add(d.getDoc().getRoot());
260+
}
261+
} else {
262+
QueryExpression boundQuery = binders.iterate(aq.getQuery());
263+
LOGGER.debug("Association query:{}", boundQuery);
264+
QueryEvaluator qeval = QueryEvaluator.getInstance(boundQuery, childBlock.getMetadata());
265+
for (ResultDocument childDoc : childDocs) {
266+
if (qeval.evaluate(childDoc.getDoc()).getResult()) {
267+
if (destNode == null) {
268+
destNode = JsonNodeFactory.instance.arrayNode();
269+
parentDoc.getDoc().modify(parentSlot.getSlotFieldName(), destNode, true);
270+
}
271+
destNode.add(childDoc.getDoc().getRoot());
272+
}
273+
}
274+
}
275+
}
276+
}
277+
278+
211279
@Override
212280
public JsonNode toJson() {
213281
return toJson(Step::toJson,ExecutionBlock::toJson);

crud/src/main/java/com/redhat/lightblue/assoc/ep/Searches.java

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
3535

3636
import com.redhat.lightblue.assoc.BindQuery;
37-
38-
import com.redhat.lightblue.eval.QueryEvaluator;
39-
4037
import com.redhat.lightblue.query.QueryExpression;
4138
import com.redhat.lightblue.query.NaryLogicalExpression;
4239
import com.redhat.lightblue.query.NaryLogicalOperator;
@@ -116,69 +113,6 @@ public static List<QueryExpression> writeQueriesForJoinTuple(JoinTuple tuple, Ex
116113
return ret;
117114
}
118115

119-
/**
120-
* Associates child documents obtained from 'aq' to all the slots in the
121-
* parent document
122-
*/
123-
public static void associateDocs(ResultDocument parentDoc,
124-
List<ResultDocument> childDocs,
125-
AssociationQuery aq) {
126-
List<ChildSlot> slots = parentDoc.getSlots().get(aq.getReference());
127-
for (ChildSlot slot : slots) {
128-
associateDocs(parentDoc, slot, childDocs, aq);
129-
}
130-
}
131-
132-
/**
133-
* Associate child documents with their parents. The association query is
134-
* for the association from the child to the parent, so caller must flip it
135-
* before sending it in if necessary. The caller also make sure parentDocs
136-
* is a unique stream.
137-
*
138-
* @param parentDoc The parent document
139-
* @param parentSlot The slot in parent docuemnt to which the results will
140-
* be attached
141-
* @param childDocs The child documents
142-
* @param aq The association query from parent to child. This may not be the
143-
* same association query between the blocks. If the child block is before
144-
* the parent block, a new aq must be constructed for the association from
145-
* the parent to the child
146-
*/
147-
public static void associateDocs(ResultDocument parentDoc,
148-
ChildSlot parentSlot,
149-
List<ResultDocument> childDocs,
150-
AssociationQuery aq) {
151-
if (!childDocs.isEmpty()) {
152-
LOGGER.debug("Associating docs");
153-
ExecutionBlock childBlock = childDocs.get(0).getBlock();
154-
ArrayNode destNode = (ArrayNode) parentDoc.getDoc().get(parentSlot.getSlotFieldName());
155-
BindQuery binders = parentDoc.getBindersForSlot(parentSlot, aq);
156-
// No binders means all child docs will be added to the parent
157-
if (binders.getBindings().isEmpty()) {
158-
if (destNode == null) {
159-
destNode = JsonNodeFactory.instance.arrayNode();
160-
parentDoc.getDoc().modify(parentSlot.getSlotFieldName(), destNode, true);
161-
}
162-
for (ResultDocument d : childDocs) {
163-
destNode.add(d.getDoc().getRoot());
164-
}
165-
} else {
166-
QueryExpression boundQuery = binders.iterate(aq.getQuery());
167-
LOGGER.debug("Association query:{}", boundQuery);
168-
QueryEvaluator qeval = QueryEvaluator.getInstance(boundQuery, childBlock.getMetadata());
169-
for (ResultDocument childDoc : childDocs) {
170-
if (qeval.evaluate(childDoc.getDoc()).getResult()) {
171-
if (destNode == null) {
172-
destNode = JsonNodeFactory.instance.arrayNode();
173-
parentDoc.getDoc().modify(parentSlot.getSlotFieldName(), destNode, true);
174-
}
175-
destNode.add(childDoc.getDoc().getRoot());
176-
}
177-
}
178-
}
179-
}
180-
}
181-
182116
/**
183117
* Combines queries with AND. Queries can be null, but at least one of them
184118
* must be non-null

0 commit comments

Comments
 (0)