Search in sources :

Example 6 with ManagedFile

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) + "" });
    }
}
Also used : Path(java.nio.file.Path) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) IOException(java.io.IOException) XXHash64Utils(herddb.utils.XXHash64Utils) ManagedFile(herddb.utils.ManagedFile) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Example 7 with ManagedFile

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;
}
Also used : Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) Table(herddb.model.Table) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) Index(herddb.model.Index) BLinkKeyToPageIndex(herddb.index.blink.BLinkKeyToPageIndex) KeyToPageIndex(herddb.index.KeyToPageIndex) IOException(java.io.IOException) ManagedFile(herddb.utils.ManagedFile) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream) PostCheckpointAction(herddb.core.PostCheckpointAction) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream)

Example 8 with ManagedFile

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) + "" });
    }
}
Also used : Path(java.nio.file.Path) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) Record(herddb.model.Record) IOException(java.io.IOException) XXHash64Utils(herddb.utils.XXHash64Utils) ManagedFile(herddb.utils.ManagedFile) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Aggregations

ExtendedDataOutputStream (herddb.utils.ExtendedDataOutputStream)8 ManagedFile (herddb.utils.ManagedFile)8 SimpleBufferedOutputStream (herddb.utils.SimpleBufferedOutputStream)8 IOException (java.io.IOException)8 Path (java.nio.file.Path)8 DataStorageManagerException (herddb.storage.DataStorageManagerException)7 PostCheckpointAction (herddb.core.PostCheckpointAction)5 LogSequenceNumber (herddb.log.LogSequenceNumber)5 XXHash64Utils (herddb.utils.XXHash64Utils)5 ArrayList (java.util.ArrayList)5 KeyToPageIndex (herddb.index.KeyToPageIndex)1 BLinkKeyToPageIndex (herddb.index.blink.BLinkKeyToPageIndex)1 MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)1 Index (herddb.model.Index)1 Record (herddb.model.Record)1 Table (herddb.model.Table)1 Transaction (herddb.model.Transaction)1 IndexStatus (herddb.storage.IndexStatus)1 TableStatus (herddb.storage.TableStatus)1