Search in sources :

Example 21 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class PlanStoreResource method getQueryPlan.

@GET
@Path("/{plan_name}")
public QueryPlanBean getQueryPlan(@PathParam("plan_name") String planName) {
    try {
        Tuple tuple = planStore.getPlan(planName);
        if (tuple == null) {
            throw new TexeraWebException("Plan with the given name does not exist");
        }
        QueryPlanBean queryPlanBean = new QueryPlanBean(tuple.getField(PlanStoreConstants.NAME).getValue().toString(), tuple.getField(PlanStoreConstants.DESCRIPTION).getValue().toString(), mapper.readValue(tuple.getField(PlanStoreConstants.LOGICAL_PLAN_JSON).getValue().toString(), LogicalPlan.class));
        return queryPlanBean;
    } catch (TexeraException e) {
        throw new TexeraWebException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : LogicalPlan(edu.uci.ics.texera.dataflow.plangen.LogicalPlan) IOException(java.io.IOException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) TexeraWebException(edu.uci.ics.texera.web.TexeraWebException) Tuple(edu.uci.ics.texera.api.tuple.Tuple) QueryPlanBean(edu.uci.ics.texera.web.response.planstore.QueryPlanBean)

Example 22 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class PlanStoreResource method getAllQueryPlans.

@GET
public QueryPlanListBean getAllQueryPlans() {
    ArrayList<QueryPlanBean> queryPlans = new ArrayList<>();
    try {
        // Getting an iterator for the plan store
        DataReader reader = planStore.getPlanIterator();
        reader.open();
        // Iterating through the stored plans, and mapping them to a QueryPlanRequest object
        Tuple tuple;
        while ((tuple = reader.getNextTuple()) != null) {
            String name = tuple.getField(PlanStoreConstants.NAME).getValue().toString();
            String description = tuple.getField(PlanStoreConstants.DESCRIPTION).getValue().toString();
            String logicalPlanJson = tuple.getField(PlanStoreConstants.LOGICAL_PLAN_JSON).getValue().toString();
            queryPlans.add(new QueryPlanBean(name, description, mapper.readValue(logicalPlanJson, LogicalPlan.class)));
        }
    } catch (TexeraException e) {
        throw new TexeraWebException(e.getMessage());
    } catch (IOException e) {
        throw new TexeraWebException("fail to parse json:\n" + e.getMessage());
    }
    return new QueryPlanListBean(queryPlans);
}
Also used : QueryPlanListBean(edu.uci.ics.texera.web.response.planstore.QueryPlanListBean) DataReader(edu.uci.ics.texera.storage.DataReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) TexeraWebException(edu.uci.ics.texera.web.TexeraWebException) QueryPlanBean(edu.uci.ics.texera.web.response.planstore.QueryPlanBean) Tuple(edu.uci.ics.texera.api.tuple.Tuple)

Example 23 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class QueryPlanResource method executeQueryPlan.

/**
 * This is the edu.uci.ics.texera.web.request handler for the execution of a Query Plan.
 * @param logicalPlanJson, the json representation of the logical plan
 * @return - Generic TexeraWebResponse object
 */
@POST
@Path("/execute")
public // TODO: investigate how to use LogicalPlan directly
JsonNode 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();
            // make sure result directory is created
            if (Files.notExists(resultDirectory)) {
                Files.createDirectories(resultDirectory);
            }
            // clean up old result files
            cleanupOldResults();
            // generate new UUID as the result id
            String resultID = UUID.randomUUID().toString();
            // write original json of the result into a file
            java.nio.file.Path resultFile = resultDirectory.resolve(resultID + ".json");
            Files.createFile(resultFile);
            Files.write(resultFile, new ObjectMapper().writeValueAsBytes(results));
            // put readable json of the result into response
            ArrayNode resultNode = new ObjectMapper().createArrayNode();
            for (Tuple tuple : results) {
                resultNode.add(tuple.getReadableJson());
            }
            ObjectNode response = new ObjectMapper().createObjectNode();
            response.put("code", 0);
            response.set("result", resultNode);
            response.put("resultID", resultID);
            return response;
        } else {
            // execute the plan and return success message
            Engine.getEngine().evaluate(plan);
            ObjectNode response = new ObjectMapper().createObjectNode();
            response.put("code", 1);
            response.put("message", "plan sucessfully executed");
            return response;
        }
    } catch (IOException | TexeraException e) {
        throw new TexeraWebException(e.getMessage());
    }
}
Also used : TupleSink(edu.uci.ics.texera.dataflow.sink.tuple.TupleSink) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IOException(java.io.IOException) Plan(edu.uci.ics.texera.api.engine.Plan) LogicalPlan(edu.uci.ics.texera.dataflow.plangen.LogicalPlan) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) ISink(edu.uci.ics.texera.api.dataflow.ISink) LogicalPlan(edu.uci.ics.texera.dataflow.plangen.LogicalPlan) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TexeraWebException(edu.uci.ics.texera.web.TexeraWebException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Tuple(edu.uci.ics.texera.api.tuple.Tuple) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 24 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class TestUtils method testJsonSerialization.

public static JsonNode testJsonSerialization(Object object, boolean printResults) {
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(object);
        Object resultObject = objectMapper.readValue(json, object.getClass());
        String resultJson = objectMapper.writeValueAsString(resultObject);
        JsonNode jsonNode = objectMapper.readValue(json, JsonNode.class);
        JsonNode resultJsonNode = objectMapper.readValue(resultJson, JsonNode.class);
        if (printResults) {
            System.out.println(resultJson);
        }
        Assert.assertEquals(object, resultObject);
        Assert.assertEquals(jsonNode, resultJsonNode);
        return jsonNode;
    } catch (IOException e) {
        throw new TexeraException(e);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 25 with TexeraException

use of edu.uci.ics.texera.api.exception.TexeraException in project textdb by TextDB.

the class AbstractSingleInputOperator method getNextTuple.

@Override
public Tuple getNextTuple() throws TexeraException {
    if (cursor == CLOSED) {
        throw new DataflowException(ErrorMessages.OPERATOR_NOT_OPENED);
    }
    if (cursor >= limit + offset) {
        return null;
    }
    try {
        Tuple resultTuple = null;
        while (true) {
            resultTuple = computeNextMatchingTuple();
            if (resultTuple == null) {
                break;
            }
            cursor++;
            if (cursor > offset) {
                break;
            }
        }
        return resultTuple;
    } catch (Exception e) {
        throw new DataflowException(e.getMessage(), e);
    }
}
Also used : DataflowException(edu.uci.ics.texera.api.exception.DataflowException) Tuple(edu.uci.ics.texera.api.tuple.Tuple) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException)

Aggregations

TexeraException (edu.uci.ics.texera.api.exception.TexeraException)25 DataflowException (edu.uci.ics.texera.api.exception.DataflowException)17 IOException (java.io.IOException)12 Tuple (edu.uci.ics.texera.api.tuple.Tuple)11 Schema (edu.uci.ics.texera.api.schema.Schema)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 AttributeType (edu.uci.ics.texera.api.schema.AttributeType)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Attribute (edu.uci.ics.texera.api.schema.Attribute)5 TexeraWebException (edu.uci.ics.texera.web.TexeraWebException)5 IField (edu.uci.ics.texera.api.field.IField)4 QueryPlanBean (edu.uci.ics.texera.web.response.planstore.QueryPlanBean)4 SchemaConstants (edu.uci.ics.texera.api.constants.SchemaConstants)3 IOperator (edu.uci.ics.texera.api.dataflow.IOperator)3 ISink (edu.uci.ics.texera.api.dataflow.ISink)3 StorageException (edu.uci.ics.texera.api.exception.StorageException)3 IntegerField (edu.uci.ics.texera.api.field.IntegerField)3 Collectors (java.util.stream.Collectors)3