use of edu.uci.ics.textdb.exp.sink.tuple.TupleSink in project textdb by TextDB.
the class TupleSinkTest method testLimitOffset.
/*
* Test tuple sink predicate with limit 1 and offset 1.
*/
@Test
public void testLimitOffset() throws Exception {
TupleSink tupleSink = new TupleSink(new TupleSinkPredicate(1, 1));
tupleSink.setInputOperator(inputOperator);
Tuple sampleTuple1 = Mockito.mock(Tuple.class);
Mockito.when(sampleTuple1.getSchema()).thenReturn(inputSchema);
Tuple sampleTuple2 = Mockito.mock(Tuple.class);
Mockito.when(sampleTuple2.getSchema()).thenReturn(inputSchema);
Tuple sampleTuple3 = Mockito.mock(Tuple.class);
Mockito.when(sampleTuple3.getSchema()).thenReturn(inputSchema);
// Set the behavior for inputOperator,
// it returns 3 tuples, then return null
Mockito.when(inputOperator.getNextTuple()).thenReturn(sampleTuple1).thenReturn(sampleTuple2).thenReturn(sampleTuple3).thenReturn(null);
tupleSink.open();
Tuple resultTuple1 = tupleSink.getNextTuple();
Tuple resultTuple2 = tupleSink.getNextTuple();
tupleSink.close();
Assert.assertTrue(resultTuple1 != null);
Assert.assertTrue(resultTuple2 == null);
}
use of edu.uci.ics.textdb.exp.sink.tuple.TupleSink in project textdb by TextDB.
the class NewQueryPlanResource method executeQueryPlan.
/**
* This is the edu.uci.ics.textdb.web.request handler for the execution of a Query Plan.
* @param logicalPlanJson, the json representation of the logical plan
* @return - Generic TextdbWebResponse object
*/
@POST
@Path("/execute")
public // TODO: investigate how to use LogicalPlan directly
TextdbWebResponse executeQueryPlan(String logicalPlanJson) {
try {
LogicalPlan logicalPlan = new ObjectMapper().readValue(logicalPlanJson, LogicalPlan.class);
Plan plan = logicalPlan.buildQueryPlan();
ISink sink = plan.getRoot();
// send response back to frontend
if (sink instanceof TupleSink) {
TupleSink tupleSink = (TupleSink) sink;
tupleSink.open();
List<Tuple> results = tupleSink.collectAllTuples();
tupleSink.close();
ArrayNode arrayNode = new ObjectMapper().createArrayNode();
for (Tuple tuple : results) {
arrayNode.add(tuple.getReadableJson());
}
return new TextdbWebResponse(0, new ObjectMapper().writeValueAsString(arrayNode));
} else {
// execute the plan and return success message
Engine.getEngine().evaluate(plan);
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("status", "plan sucessfully executed");
return new TextdbWebResponse(0, new ObjectMapper().writeValueAsString(objectNode));
}
} catch (IOException | RuntimeException e) {
// TODO remove RuntimeException after the exception refactor
e.printStackTrace();
throw new TextdbWebException(e.getMessage());
}
}
Aggregations