Search in sources :

Example 11 with StorageException

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

the class RelationManager method getTableAnalyzerString.

/**
 * Gets the Lucene analyzer string of a table.
 *
 * @param tableName, the name of the table, case insensitive
 * @return
 * @throws StorageException
 */
public String getTableAnalyzerString(String tableName) throws StorageException {
    // get the tuples with tableName from the table catalog
    Tuple tableCatalogTuple = getTableCatalogTuple(tableName);
    // if the tuple is not found, then the table name is not found
    if (tableCatalogTuple == null) {
        throw new StorageException(String.format("The analyzer for table %s is not found.", tableName));
    }
    // get the lucene analyzer string
    IField analyzerField = tableCatalogTuple.getField(CatalogConstants.TABLE_LUCENE_ANALYZER);
    String analyzerString = analyzerField.getValue().toString();
    return analyzerString;
}
Also used : IField(edu.uci.ics.texera.api.field.IField) StorageException(edu.uci.ics.texera.api.exception.StorageException) Tuple(edu.uci.ics.texera.api.tuple.Tuple)

Example 12 with StorageException

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

the class RelationManager method getTableCatalogTuple.

/*
     * Gets the a tuple of a table from table catalog.
     */
private static Tuple getTableCatalogTuple(String tableName) throws StorageException {
    tableName = tableName.toLowerCase();
    Query tableNameQuery = new TermQuery(new Term(CatalogConstants.TABLE_NAME, tableName));
    DataReader tableCatalogDataReader = new DataReader(CatalogConstants.TABLE_CATALOG_DATASTORE, tableNameQuery);
    tableCatalogDataReader.setPayloadAdded(false);
    tableCatalogDataReader.open();
    List<Tuple> tupleList = new ArrayList<>();
    Tuple nextTuple;
    while ((nextTuple = tableCatalogDataReader.getNextTuple()) != null) {
        tupleList.add(nextTuple);
    }
    tableCatalogDataReader.close();
    if (tupleList.size() == 0) {
        return null;
    } else if (tupleList.size() == 1) {
        return tupleList.get(0);
    } else {
        throw new StorageException("Catalog corrupted: duplicate table name found in catalog.");
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) StorageException(edu.uci.ics.texera.api.exception.StorageException) Tuple(edu.uci.ics.texera.api.tuple.Tuple)

Example 13 with StorageException

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

the class RelationManager method createTable.

/**
 * Creates a new table.
 *   Table name must be unique (case insensitive).
 *   LuceneAnalyzer must be a valid analyzer string.
 *
 * The "_id" attribute will be added to the table schema.
 * System automatically generates a unique ID for each tuple inserted to a table,
 *   the generated ID will be in "_id" field.
 *
 * @param tableName, the name of the table, must be unique, case is not sensitive
 * @param indexDirectory, the directory to store the index and data, must not duplicate with other tables' directories
 * @param schema, the schema of the table
 * @param luceneAnalyzerString, the string representing the lucene analyzer used
 * @throws StorageException
 */
public void createTable(String tableName, Path indexDirectory, Schema schema, String luceneAnalyzerString) throws StorageException {
    // convert the table name to lower case
    tableName = tableName.toLowerCase();
    // table should not exist
    if (checkTableExistence(tableName)) {
        throw new StorageException(String.format("Table %s already exists.", tableName));
    }
    // create folder if it's not there
    // and convert the index directory to its absolute path
    String indexDirectoryStr;
    try {
        if (Files.notExists(indexDirectory)) {
            Files.createDirectories(indexDirectory);
        }
        indexDirectoryStr = indexDirectory.toRealPath().toString();
    } catch (IOException e) {
        throw new StorageException(e);
    }
    // check if the indexDirectory overlaps with another table's index directory
    Query indexDirectoryQuery = new TermQuery(new Term(CatalogConstants.TABLE_DIRECTORY, indexDirectoryStr));
    DataReader tableCatalogDataReader = new DataReader(CatalogConstants.TABLE_CATALOG_DATASTORE, indexDirectoryQuery);
    tableCatalogDataReader.setPayloadAdded(false);
    tableCatalogDataReader.open();
    Tuple nextTuple = tableCatalogDataReader.getNextTuple();
    tableCatalogDataReader.close();
    // if the index directory is already taken by another table, throws an exception
    if (nextTuple != null) {
        String overlapTableName = nextTuple.getField(CatalogConstants.TABLE_NAME).getValue().toString();
        throw new StorageException(String.format("Table %s already takes the index directory %s. Please choose another directory.", overlapTableName, indexDirectory));
    }
    // check if the lucene analyzer string is valid
    Analyzer luceneAnalyzer = null;
    try {
        luceneAnalyzer = LuceneAnalyzerConstants.getLuceneAnalyzer(luceneAnalyzerString);
    } catch (DataflowException e) {
        throw new StorageException("Lucene Analyzer String is not valid.");
    }
    // create the directory and clear all data in the index directory
    Schema tableSchema = Schema.Builder.getSchemaWithID(schema);
    DataStore tableDataStore = new DataStore(indexDirectory, tableSchema);
    DataWriter dataWriter = new DataWriter(tableDataStore, luceneAnalyzer);
    dataWriter.open();
    dataWriter.clearData();
    dataWriter.close();
    // write table info to catalog
    writeTableInfoToCatalog(tableName, indexDirectory, schema, luceneAnalyzerString);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) Schema(edu.uci.ics.texera.api.schema.Schema) IOException(java.io.IOException) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) DataflowException(edu.uci.ics.texera.api.exception.DataflowException) StorageException(edu.uci.ics.texera.api.exception.StorageException) Tuple(edu.uci.ics.texera.api.tuple.Tuple)

Example 14 with StorageException

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

the class RelationManager method getTableAnalyzer.

/**
 * Gets the Lucene analyzer of a table.
 *
 * @param tableName, the name of the table, case insensitive
 * @return
 * @throws StorageException
 */
public Analyzer getTableAnalyzer(String tableName) throws StorageException {
    String analyzerString = getTableAnalyzerString(tableName);
    // convert a lucene analyzer string to an analyzer object
    Analyzer luceneAnalyzer = null;
    try {
        luceneAnalyzer = LuceneAnalyzerConstants.getLuceneAnalyzer(analyzerString);
    } catch (DataflowException e) {
        throw new StorageException(e);
    }
    return luceneAnalyzer;
}
Also used : DataflowException(edu.uci.ics.texera.api.exception.DataflowException) Analyzer(org.apache.lucene.analysis.Analyzer) StorageException(edu.uci.ics.texera.api.exception.StorageException)

Example 15 with StorageException

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

the class RelationManagerTest method test15.

@Test
public void test15() throws Exception {
    String tableName1 = "relation_manager_test_table_15_1";
    String tableName2 = "relation_manager_test_table_15_2";
    String indexDirectory = "./index/test_table/relation_manager_test_table_15";
    Schema schema = new Schema(new Attribute("content", AttributeType.TEXT));
    String luceneAnalyzerString = "standard";
    relationManager.deleteTable(tableName1);
    relationManager.deleteTable(tableName2);
    relationManager.createTable(tableName1, Paths.get(indexDirectory), schema, luceneAnalyzerString);
    // create another table with the same directory should fail
    try {
        relationManager.createTable(tableName2, Paths.get(indexDirectory), schema, luceneAnalyzerString);
        Assert.fail("Storage exception should be thrown because of duplicate index directories");
    } catch (StorageException e) {
    }
    relationManager.deleteTable(tableName1);
}
Also used : Attribute(edu.uci.ics.texera.api.schema.Attribute) Schema(edu.uci.ics.texera.api.schema.Schema) StorageException(edu.uci.ics.texera.api.exception.StorageException) Test(org.junit.Test)

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