Search in sources :

Example 1 with StorageException

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

the class PlanStore method addPlan.

/**
 * Adds a Logical Plan JSON to the plan store.
 *
 * @param planName, the name of the plan.
 * @param description, the description of the plan.
 * @param logicalPlanJson, the logical plan JSON string
 * @Return IDField, the id field of the plan stored.
 * @throws TexeraException, when there are null fields or the given name is invalid or there is an existing plan with same name.
 */
public IDField addPlan(String planName, String description, String logicalPlanJson) throws TexeraException {
    if (planName == null || description == null || logicalPlanJson == null) {
        throw new TexeraException("Arguments cannot be null when adding a plan");
    }
    if (!PlanStoreConstants.VALID_PLAN_NAME.matcher(planName).find()) {
        throw new TexeraException("Plan name is not valid. It can only contain alphanumeric characters, " + "underscore, and hyphen.");
    }
    if (getPlan(planName) != null) {
        throw new TexeraException("A plan with the same name already exists");
    }
    try {
        // Converting the JSON String to a JSON Node to minimize space usage and to check validity of JSON string
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readValue(logicalPlanJson, JsonNode.class);
        logicalPlanJson = objectMapper.writeValueAsString(jsonNode);
    } catch (IOException e) {
        throw new StorageException("logical plan json is an invalid json string: " + logicalPlanJson);
    }
    Tuple tuple = new Tuple(PlanStoreConstants.SCHEMA_PLAN, new StringField(planName), new StringField(description), new StringField(logicalPlanJson));
    DataWriter dataWriter = relationManager.getTableDataWriter(PlanStoreConstants.TABLE_NAME);
    dataWriter.open();
    IDField id = dataWriter.insertTuple(tuple);
    dataWriter.close();
    return id;
}
Also used : IDField(edu.uci.ics.texera.api.field.IDField) StringField(edu.uci.ics.texera.api.field.StringField) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) TexeraException(edu.uci.ics.texera.api.exception.TexeraException) StorageException(edu.uci.ics.texera.api.exception.StorageException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Tuple(edu.uci.ics.texera.api.tuple.Tuple) DataWriter(edu.uci.ics.texera.storage.DataWriter)

Example 2 with StorageException

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

the class PlanStore method updatePlanInternal.

/**
 * Updates both plan description and plan json of a plan with the given plan name.
 * If description is null, it will not update plan description.
 * If plan json is NULL, it will not update the plan's JSON file.
 *
 * @param planName, the name of the plan.
 * @param description, the new description of the plan.
 * @param logicalPlanJson, the new plan json string.
 * @throws TexeraException
 */
private void updatePlanInternal(String planName, String description, String logicalPlanJson) throws TexeraException {
    Tuple existingPlan = getPlan(planName);
    if (existingPlan == null) {
        return;
    }
    // Checking if an updated description or logical plan JSON string has been provided
    if (description == null && logicalPlanJson == null) {
        return;
    }
    // Checking if the logical plan JSON string needs to be updated
    if (logicalPlanJson != null) {
        // Compressing and checking the validity of the logical plan JSON string
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readValue(logicalPlanJson, JsonNode.class);
            logicalPlanJson = objectMapper.writeValueAsString(jsonNode);
        } catch (IOException e) {
            throw new StorageException("logical plan json is an invalid json string: " + logicalPlanJson);
        }
    }
    // Getting the fields in order for performing the update
    IDField idField = (IDField) existingPlan.getField(SchemaConstants._ID);
    IField descriptionField = description != null ? new StringField(description) : existingPlan.getField(PlanStoreConstants.DESCRIPTION);
    IField logicalPlanJsonField = logicalPlanJson != null ? new StringField(logicalPlanJson) : existingPlan.getField(PlanStoreConstants.LOGICAL_PLAN_JSON);
    // Creating a tuple out of all the fields
    Tuple newTuple = new Tuple(PlanStoreConstants.SCHEMA_PLAN, new StringField(planName), descriptionField, logicalPlanJsonField);
    // Writing the updated tuple
    DataWriter dataWriter = relationManager.getTableDataWriter(PlanStoreConstants.TABLE_NAME);
    dataWriter.open();
    dataWriter.updateTuple(newTuple, idField);
    dataWriter.close();
}
Also used : IDField(edu.uci.ics.texera.api.field.IDField) StringField(edu.uci.ics.texera.api.field.StringField) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) IField(edu.uci.ics.texera.api.field.IField) StorageException(edu.uci.ics.texera.api.exception.StorageException) Tuple(edu.uci.ics.texera.api.tuple.Tuple) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataWriter(edu.uci.ics.texera.storage.DataWriter)

Example 3 with StorageException

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

the class DictionaryManager method writeToFile.

/**
 * Write uploaded file at the given location (if the file exists, remove it and write a new one.)
 *
 * @param content
 * @param fileUploadDirectory
 * @param fileName
 */
private void writeToFile(String fileName, String dictionaryContent) throws StorageException {
    try {
        Path filePath = DictionaryManagerConstants.DICTIONARY_DIR_PATH.resolve(fileName);
        ;
        Files.deleteIfExists(filePath);
        Files.createFile(filePath);
        Files.write(filePath, dictionaryContent.getBytes());
    } catch (IOException e) {
        throw new StorageException("Error occurred whlie uploading dictionary");
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) StorageException(edu.uci.ics.texera.api.exception.StorageException)

Example 4 with StorageException

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

the class DictionaryManager method getDictionary.

public String getDictionary(String dictionaryName) throws StorageException {
    DataReader dataReader = relationManager.getTableDataReader(DictionaryManagerConstants.TABLE_NAME, new TermQuery(new Term(DictionaryManagerConstants.NAME, dictionaryName)));
    dataReader.open();
    if (dataReader.getNextTuple() == null) {
        throw new StorageException("Dictionary " + dictionaryName + "does not exist");
    }
    dataReader.close();
    try {
        return Files.lines(DictionaryManagerConstants.DICTIONARY_DIR_PATH.resolve(dictionaryName)).collect(Collectors.joining(","));
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) DataReader(edu.uci.ics.texera.storage.DataReader) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) StorageException(edu.uci.ics.texera.api.exception.StorageException)

Example 5 with StorageException

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

the class PlanStoreTest method assertCorrectPlanExists.

/**
 * This is a helper function that checks whether the plan corresponding to the plan name corresponds to the
 * logical plan JSON string that is fed to this function
 * @param planName - Name of the plan name to check with
 * @param logicalPlanJson - Expected LogicalPlan JSON string
 * @throws TexeraException
 */
public static void assertCorrectPlanExists(String planName, String logicalPlanJson) throws TexeraException {
    Tuple res = planStore.getPlan(planName);
    Assert.assertNotNull(res);
    try {
        String returnedPlan = res.getField(PlanStoreConstants.LOGICAL_PLAN_JSON).getValue().toString();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readValue(logicalPlanJson, JsonNode.class);
        JsonNode returnedJsonNode = objectMapper.readValue(returnedPlan, JsonNode.class);
        Assert.assertEquals(jsonNode, returnedJsonNode);
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) StorageException(edu.uci.ics.texera.api.exception.StorageException) Tuple(edu.uci.ics.texera.api.tuple.Tuple) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

StorageException (edu.uci.ics.texera.api.exception.StorageException)24 IOException (java.io.IOException)16 Tuple (edu.uci.ics.texera.api.tuple.Tuple)8 Schema (edu.uci.ics.texera.api.schema.Schema)4 Term (org.apache.lucene.index.Term)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 Query (org.apache.lucene.search.Query)4 TermQuery (org.apache.lucene.search.TermQuery)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 DataflowException (edu.uci.ics.texera.api.exception.DataflowException)3 IDField (edu.uci.ics.texera.api.field.IDField)3 IField (edu.uci.ics.texera.api.field.IField)3 DataWriter (edu.uci.ics.texera.storage.DataWriter)3 StringField (edu.uci.ics.texera.api.field.StringField)2 Attribute (edu.uci.ics.texera.api.schema.Attribute)2 File (java.io.File)2 Path (java.nio.file.Path)2 ParseException (java.text.ParseException)2 Analyzer (org.apache.lucene.analysis.Analyzer)2