use of org.bboxdb.storage.StorageManagerException in project bboxdb by jnidzwetzki.
the class SSTableWriter method open.
/**
* Open all required files
* @throws StorageManagerException
*/
public void open() throws StorageManagerException {
final String directoryName = SSTableHelper.getSSTableDir(directory, name);
final File directoryHandle = new File(directoryName);
if (!directoryHandle.isDirectory()) {
final String error = "Directory for SSTable " + name + " does not exist: " + directoryName;
logger.error(error);
throw new StorageManagerException(error);
}
final String sstableOutputFileName = SSTableHelper.getSSTableFilename(directory, name, tablenumber);
sstableFile = new File(sstableOutputFileName);
final String outputIndexFileName = SSTableHelper.getSSTableIndexFilename(directory, name, tablenumber);
sstableIndexFile = new File(outputIndexFileName);
// Don't overwrite old data
if (sstableFile.exists()) {
throw new StorageManagerException("Table file already exists: " + sstableOutputFileName);
}
if (sstableIndexFile.exists()) {
throw new StorageManagerException("Table file already exists: " + sstableIndexFile);
}
if (sstableBloomFilterFile.exists()) {
throw new StorageManagerException("Bloom filter file already exists: " + sstableBloomFilterFile);
}
try {
logger.info("Writing new SSTable for relation: {} file: {}", name.getFullname(), sstableOutputFileName);
final BufferedOutputStream sstableFileOutputStream = new BufferedOutputStream(new FileOutputStream(sstableFile));
sstableOutputStream = new CountingOutputStream(sstableFileOutputStream);
sstableOutputStream.write(SSTableConst.MAGIC_BYTES);
sstableIndexOutputStream = new BufferedOutputStream(new FileOutputStream(sstableIndexFile));
sstableIndexOutputStream.write(SSTableConst.MAGIC_BYTES_INDEX);
} catch (FileNotFoundException e) {
exceptionDuringWrite = true;
throw new StorageManagerException("Unable to open output file", e);
} catch (IOException e) {
exceptionDuringWrite = true;
throw new StorageManagerException("Unable to write into output file", e);
}
}
use of org.bboxdb.storage.StorageManagerException in project bboxdb by jnidzwetzki.
the class SSTableWriter method close.
/**
* Close all open file handles and write the meta data
*/
public void close() throws StorageManagerException {
try {
logger.debug("Closing new written SSTable for relation: {} number {}. File: {} ", name.getFullname(), tablenumber, sstableFile.getName());
if (sstableOutputStream != null) {
sstableOutputStream.close();
sstableOutputStream = null;
}
if (sstableIndexOutputStream != null) {
sstableIndexOutputStream.close();
sstableIndexOutputStream = null;
}
writeSpatialIndex();
writeBloomFilter();
writeMetadata();
} catch (IOException e) {
exceptionDuringWrite = true;
throw new StorageManagerException("Exception while closing streams", e);
} finally {
checkForWriteException();
}
}
use of org.bboxdb.storage.StorageManagerException in project bboxdb by jnidzwetzki.
the class SSTableWriter method addData.
/**
* Add the list of tuples to the sstable
* @param tuples
* @throws StorageManagerException
*/
public void addData(final List<Tuple> tuples) throws StorageManagerException {
if (sstableOutputStream == null) {
final String error = "Trying to add a memtable to a non ready SSTable writer";
logger.error(error);
throw new StorageManagerException(error);
}
try {
for (final Tuple tuple : tuples) {
addNextTuple(tuple);
}
} catch (StorageManagerException e) {
exceptionDuringWrite = true;
throw e;
}
}
use of org.bboxdb.storage.StorageManagerException in project bboxdb by jnidzwetzki.
the class SSTableWriter method addNextTuple.
/**
* Add the next tuple into the result sstable
* @param tuple
* @throws IOException
* @throws StorageManagerException
*/
public void addNextTuple(final Tuple tuple) throws StorageManagerException {
try {
// Add Tuple to the index
final int tuplePosition = (int) sstableOutputStream.getCount();
writeIndexEntry(tuplePosition);
final int newPosition = (int) sstableOutputStream.getCount();
final int writtenBytes = newPosition - tuplePosition;
// Add Tuple to the SSTable file
TupleHelper.writeTupleToStream(tuple, sstableOutputStream);
metadataBuilder.addTuple(tuple);
// Add tuple to the bloom filter
bloomFilter.put(tuple.getKey());
// Add tuple to the spatial index
final SpatialIndexEntry sIndexentry = new SpatialIndexEntry(tuple.getBoundingBox(), tuplePosition);
spatialIndex.insert(sIndexentry);
writtenTuplesTotal.inc();
writtenTuplesBytes.inc(writtenBytes);
} catch (IOException e) {
exceptionDuringWrite = true;
throw new StorageManagerException("Unable to write tuple to SSTable", e);
}
}
use of org.bboxdb.storage.StorageManagerException in project bboxdb by jnidzwetzki.
the class SSTableCompactor method executeCompactation.
/**
* Execute the compactation of the input sstables
*
* @return success or failure
*/
public void executeCompactation() throws StorageManagerException {
try {
// The iterators
final List<Iterator<Tuple>> iterators = sstableIndexReader.stream().map(r -> r.iterator()).collect(Collectors.toList());
final DuplicateResolver<Tuple> newestKeyResolver = TupleDuplicateResolverFactory.build(tupleStoreManager.getTupleStoreConfiguration());
final SortedIteratorMerger<Tuple> sortedIteratorMerger = new SortedIteratorMerger<>(iterators, TupleHelper.TUPLE_KEY_COMPARATOR, newestKeyResolver);
for (final Tuple tuple : sortedIteratorMerger) {
checkForThreadTermination();
addTupleToWriter(tuple);
}
readTuples = sortedIteratorMerger.getReadElements();
} catch (StorageManagerException e) {
handleErrorDuringCompact();
throw e;
} finally {
closeSSTableWriter();
}
}
Aggregations