use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class FileDataStorageManager method writePage.
@Override
public void writePage(String tableSpace, String tableName, long pageId, Collection<Record> newPage) throws DataStorageManagerException {
// synch on table is done by the TableManager
long _start = System.currentTimeMillis();
Path tableDir = getTableDirectory(tableSpace, tableName);
try {
Files.createDirectories(tableDir);
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
Path pageFile = getPageFile(tableDir, pageId);
long size;
try (ManagedFile file = ManagedFile.open(pageFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
SimpleBufferedOutputStream buffer = new SimpleBufferedOutputStream(file.getOutputStream(), COPY_BUFFERS_SIZE);
XXHash64Utils.HashingOutputStream oo = new XXHash64Utils.HashingOutputStream(buffer);
ExtendedDataOutputStream dataOutput = new ExtendedDataOutputStream(oo)) {
// version
dataOutput.writeVLong(1);
// flags for future implementations
dataOutput.writeVLong(0);
dataOutput.writeInt(newPage.size());
for (Record record : newPage) {
dataOutput.writeArray(record.key.data);
dataOutput.writeArray(record.value.data);
}
size = oo.size();
// footer
dataOutput.writeLong(oo.hash());
dataOutput.flush();
file.sync();
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
long now = System.currentTimeMillis();
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "writePage {0} KBytes,{1} records, time {2} ms", new Object[] { (size / 1024) + "", newPage.size(), (now - _start) + "" });
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class FileDataStorageManager method readTableStatusFromFile.
public static TableStatus readTableStatusFromFile(Path checkpointsFile) throws IOException {
byte[] fileContent = FileUtils.fastReadFile(checkpointsFile);
XXHash64Utils.verifyBlockWithFooter(fileContent, 0, fileContent.length);
try (InputStream input = new SimpleByteArrayInputStream(fileContent);
ExtendedDataInputStream dataIn = new ExtendedDataInputStream(input)) {
// version
long version = dataIn.readVLong();
// flags for future implementations
long flags = dataIn.readVLong();
if (version != 1 || flags != 0) {
throw new DataStorageManagerException("corrupted table status file " + checkpointsFile.toAbsolutePath());
}
return TableStatus.deserialize(dataIn);
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class FileDataStorageManager method loadTransactions.
@Override
public void loadTransactions(LogSequenceNumber sequenceNumber, String tableSpace, Consumer<Transaction> consumer) throws DataStorageManagerException {
try {
Path tableSpaceDirectory = getTablespaceDirectory(tableSpace);
Files.createDirectories(tableSpaceDirectory);
Path file = getTablespaceTransactionsFile(tableSpace, sequenceNumber);
boolean exists = Files.isRegularFile(file);
LOGGER.log(Level.INFO, "loadTransactions " + sequenceNumber + " for tableSpace " + tableSpace + " from file " + file + " (exists: " + exists + ")");
if (!exists) {
return;
}
try (InputStream input = new BufferedInputStream(Files.newInputStream(file, StandardOpenOption.READ), 4 * 1024 * 1024);
ExtendedDataInputStream din = new ExtendedDataInputStream(input)) {
// version
long version = din.readVLong();
// flags for future implementations
long flags = din.readVLong();
if (version != 1 || flags != 0) {
throw new DataStorageManagerException("corrupted transaction list file " + file.toAbsolutePath());
}
String readname = din.readUTF();
if (!readname.equals(tableSpace)) {
throw new DataStorageManagerException("file " + file.toAbsolutePath() + " is not for spablespace " + tableSpace);
}
long ledgerId = din.readZLong();
long offset = din.readZLong();
if (ledgerId != sequenceNumber.ledgerId || offset != sequenceNumber.offset) {
throw new DataStorageManagerException("file " + file.toAbsolutePath() + " is not for sequence number " + sequenceNumber);
}
int numTransactions = din.readInt();
for (int i = 0; i < numTransactions; i++) {
Transaction tx = Transaction.deserialize(tableSpace, din);
consumer.accept(tx);
}
}
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class BLinkKeyToPageIndex method checkpoint.
@Override
public List<PostCheckpointAction> checkpoint(LogSequenceNumber sequenceNumber, boolean pin) throws DataStorageManagerException {
try {
/* Tree can be null if no data was inserted (tree creation deferred to check evaluate key size) */
final BLink<Bytes, Long> tree = this.tree;
if (tree == null) {
return Collections.emptyList();
}
BLinkMetadata<Bytes> metadata = getTree().checkpoint();
byte[] metaPage = MetadataSerializer.INSTANCE.write(metadata);
Set<Long> activePages = new HashSet<>();
metadata.nodes.forEach(node -> activePages.add(node.storeId));
IndexStatus indexStatus = new IndexStatus(indexName, sequenceNumber, newPageId.get(), activePages, metaPage);
List<PostCheckpointAction> result = new ArrayList<>();
result.addAll(dataStorageManager.indexCheckpoint(tableSpace, indexName, indexStatus, pin));
LOGGER.log(Level.INFO, "checkpoint index {0} finished: logpos {1}, {2} pages", new Object[] { indexName, sequenceNumber, Integer.toString(metadata.nodes.size()) });
LOGGER.log(Level.FINE, "checkpoint index {0} finished: logpos {1}, pages {2}", new Object[] { indexName, sequenceNumber, activePages.toString() });
return result;
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class BLinkKeyToPageIndex method start.
@Override
public void start(LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
LOGGER.log(Level.SEVERE, " start index {0}", new Object[] { indexName });
/* Actually the same size */
final long pageSize = memoryManager.getMaxLogicalPageSize();
if (LogSequenceNumber.START_OF_TIME.equals(sequenceNumber)) {
/* Empty index (booting from the start) */
tree = new BLink<>(pageSize, SizeEvaluatorImpl.INSTANCE, memoryManager.getPKPageReplacementPolicy(), indexDataStorage);
LOGGER.log(Level.SEVERE, "loaded empty index {0}", new Object[] { indexName });
} else {
IndexStatus status = dataStorageManager.getIndexStatus(tableSpace, indexName, sequenceNumber);
try {
BLinkMetadata<Bytes> metadata = MetadataSerializer.INSTANCE.read(status.indexData);
tree = new BLink<>(pageSize, SizeEvaluatorImpl.INSTANCE, memoryManager.getPKPageReplacementPolicy(), indexDataStorage, metadata);
} catch (IOException e) {
throw new DataStorageManagerException(e);
}
newPageId.set(status.newPageId);
LOGGER.log(Level.SEVERE, "loaded index {0}: {1} keys", new Object[] { indexName, tree.size() });
}
}
Aggregations