Search in sources :

Example 11 with TupleSink

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);
}
Also used : TupleSink(edu.uci.ics.textdb.exp.sink.tuple.TupleSink) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) Test(org.junit.Test)

Example 12 with TupleSink

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());
    }
}
Also used : TupleSink(edu.uci.ics.textdb.exp.sink.tuple.TupleSink) TextdbWebResponse(edu.uci.ics.textdb.web.response.TextdbWebResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IOException(java.io.IOException) LogicalPlan(edu.uci.ics.textdb.exp.plangen.LogicalPlan) Plan(edu.uci.ics.textdb.api.engine.Plan) ISink(edu.uci.ics.textdb.api.dataflow.ISink) TextdbWebException(edu.uci.ics.textdb.web.TextdbWebException) LogicalPlan(edu.uci.ics.textdb.exp.plangen.LogicalPlan) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Tuple(edu.uci.ics.textdb.api.tuple.Tuple) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

TupleSink (edu.uci.ics.textdb.exp.sink.tuple.TupleSink)12 Test (org.junit.Test)11 Tuple (edu.uci.ics.textdb.api.tuple.Tuple)8 TupleSourceOperator (edu.uci.ics.textdb.exp.source.tuple.TupleSourceOperator)5 ISink (edu.uci.ics.textdb.api.dataflow.ISink)4 Plan (edu.uci.ics.textdb.api.engine.Plan)4 IOperator (edu.uci.ics.textdb.api.dataflow.IOperator)3 KeywordMatcherSourceOperator (edu.uci.ics.textdb.exp.keywordmatcher.KeywordMatcherSourceOperator)3 RegexMatcher (edu.uci.ics.textdb.exp.regexmatcher.RegexMatcher)3 HashSet (java.util.HashSet)3 OneToNBroadcastConnector (edu.uci.ics.textdb.exp.connector.OneToNBroadcastConnector)2 ConnectorOutputOperator (edu.uci.ics.textdb.exp.connector.OneToNBroadcastConnector.ConnectorOutputOperator)2 Join (edu.uci.ics.textdb.exp.join.Join)2 NlpEntityOperator (edu.uci.ics.textdb.exp.nlp.entity.NlpEntityOperator)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IDField (edu.uci.ics.textdb.api.field.IDField)1 Attribute (edu.uci.ics.textdb.api.schema.Attribute)1 Schema (edu.uci.ics.textdb.api.schema.Schema)1