Search in sources :

Example 71 with DataStorageManagerException

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

Example 72 with DataStorageManagerException

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);
    }
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) BufferedInputStream(java.io.BufferedInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream)

Example 73 with DataStorageManagerException

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);
    }
}
Also used : Path(java.nio.file.Path) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) Transaction(herddb.model.Transaction) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 74 with DataStorageManagerException

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);
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PostCheckpointAction(herddb.core.PostCheckpointAction) Bytes(herddb.utils.Bytes) IndexStatus(herddb.storage.IndexStatus) AtomicLong(java.util.concurrent.atomic.AtomicLong) HashSet(java.util.HashSet)

Example 75 with DataStorageManagerException

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() });
    }
}
Also used : Bytes(herddb.utils.Bytes) IndexStatus(herddb.storage.IndexStatus) DataStorageManagerException(herddb.storage.DataStorageManagerException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

DataStorageManagerException (herddb.storage.DataStorageManagerException)80 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)28 LogSequenceNumber (herddb.log.LogSequenceNumber)23 Path (java.nio.file.Path)22 Bytes (herddb.utils.Bytes)19 Record (herddb.model.Record)15 StatementExecutionException (herddb.model.StatementExecutionException)15 ExtendedDataInputStream (herddb.utils.ExtendedDataInputStream)14 SimpleByteArrayInputStream (herddb.utils.SimpleByteArrayInputStream)14 InputStream (java.io.InputStream)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 LogEntry (herddb.log.LogEntry)13 HashMap (java.util.HashMap)13 LogNotAvailableException (herddb.log.LogNotAvailableException)12 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 BufferedInputStream (java.io.BufferedInputStream)11 CommitLogResult (herddb.log.CommitLogResult)10 Table (herddb.model.Table)10 ExtendedDataOutputStream (herddb.utils.ExtendedDataOutputStream)10