use of herddb.utils.ManagedFile in project herddb by diennea.
the class FileDataStorageManager method writeIndexPage.
@Override
public void writeIndexPage(String tableSpace, String indexName, long pageId, DataWriter writer) throws DataStorageManagerException {
long _start = System.currentTimeMillis();
Path tableDir = getIndexDirectory(tableSpace, indexName);
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);
size = oo.size();
writer.write(dataOutput);
size = oo.size() - 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, time {2} ms", new Object[] { (size / 1024) + "", (now - _start) + "" });
}
}
use of herddb.utils.ManagedFile in project herddb by diennea.
the class FileDataStorageManager method writeTables.
@Override
public Collection<PostCheckpointAction> writeTables(String tableSpace, LogSequenceNumber sequenceNumber, List<Table> tables, List<Index> indexlist) throws DataStorageManagerException {
if (sequenceNumber.isStartOfTime() && !tables.isEmpty()) {
throw new DataStorageManagerException("impossible to write a non empty table list at start-of-time");
}
Path tableSpaceDirectory = getTablespaceDirectory(tableSpace);
try {
Files.createDirectories(tableSpaceDirectory);
Path fileTables = getTablespaceTablesMetadataFile(tableSpace, sequenceNumber);
Path fileIndexes = getTablespaceIndexesMetadataFile(tableSpace, sequenceNumber);
Path parent = getParent(fileTables);
Files.createDirectories(parent);
LOGGER.log(Level.FINE, "writeTables for tableSpace " + tableSpace + " sequenceNumber " + sequenceNumber + " to " + fileTables.toAbsolutePath().toString());
try (ManagedFile file = ManagedFile.open(fileTables);
SimpleBufferedOutputStream buffer = new SimpleBufferedOutputStream(file.getOutputStream(), COPY_BUFFERS_SIZE);
ExtendedDataOutputStream dout = new ExtendedDataOutputStream(buffer)) {
// version
dout.writeVLong(1);
// flags for future implementations
dout.writeVLong(0);
dout.writeUTF(tableSpace);
dout.writeZLong(sequenceNumber.ledgerId);
dout.writeZLong(sequenceNumber.offset);
dout.writeInt(tables.size());
for (Table t : tables) {
byte[] tableSerialized = t.serialize();
dout.writeArray(tableSerialized);
}
dout.flush();
file.sync();
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
try (ManagedFile file = ManagedFile.open(fileIndexes);
SimpleBufferedOutputStream buffer = new SimpleBufferedOutputStream(file.getOutputStream(), COPY_BUFFERS_SIZE);
ExtendedDataOutputStream dout = new ExtendedDataOutputStream(buffer)) {
// version
dout.writeVLong(1);
// flags for future implementations
dout.writeVLong(0);
dout.writeUTF(tableSpace);
dout.writeZLong(sequenceNumber.ledgerId);
dout.writeZLong(sequenceNumber.offset);
if (indexlist != null) {
dout.writeInt(indexlist.size());
for (Index t : indexlist) {
byte[] indexSerialized = t.serialize();
dout.writeArray(indexSerialized);
}
} else {
dout.writeInt(0);
}
dout.flush();
file.sync();
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
Collection<PostCheckpointAction> result = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(tableSpaceDirectory)) {
for (Path p : stream) {
if (isTablespaceIndexesMetadataFile(p)) {
try {
LogSequenceNumber logPositionInFile = readLogSequenceNumberFromIndexMetadataFile(tableSpace, p);
if (sequenceNumber.after(logPositionInFile)) {
LOGGER.log(Level.FINEST, "indexes metadata file " + p.toAbsolutePath() + ". will be deleted after checkpoint end");
result.add(new DeleteFileAction("indexes", "delete indexesmetadata file " + p.toAbsolutePath(), p));
}
} catch (DataStorageManagerException ignore) {
LOGGER.log(Level.SEVERE, "Unparsable indexesmetadata file " + p.toAbsolutePath(), ignore);
result.add(new DeleteFileAction("indexes", "delete unparsable indexesmetadata file " + p.toAbsolutePath(), p));
}
} else if (isTablespaceTablesMetadataFile(p)) {
try {
LogSequenceNumber logPositionInFile = readLogSequenceNumberFromTablesMetadataFile(tableSpace, p);
if (sequenceNumber.after(logPositionInFile)) {
LOGGER.log(Level.FINEST, "tables metadata file " + p.toAbsolutePath() + ". will be deleted after checkpoint end");
result.add(new DeleteFileAction("tables", "delete tablesmetadata file " + p.toAbsolutePath(), p));
}
} catch (DataStorageManagerException ignore) {
LOGGER.log(Level.SEVERE, "Unparsable tablesmetadata file " + p.toAbsolutePath(), ignore);
result.add(new DeleteFileAction("transactions", "delete unparsable tablesmetadata file " + p.toAbsolutePath(), p));
}
}
}
} catch (IOException err) {
LOGGER.log(Level.SEVERE, "Could not list dir " + tableSpaceDirectory, err);
}
return result;
}
use of herddb.utils.ManagedFile 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) + "" });
}
}
Aggregations