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;
}
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.");
}
}
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);
}
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;
}
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);
}
Aggregations