Search in sources :

Example 41 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class MemoryDataStorageManager method getTableStatus.

@Override
public TableStatus getTableStatus(String tableSpace, String tableName, LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
    final String checkPoint = checkpointName(tableSpace, tableName, sequenceNumber);
    byte[] data = tableStatuses.get(checkPoint);
    if (data == null) {
        throw new DataStorageManagerException("no such tablee checkpoint: " + checkPoint);
    }
    try {
        try (InputStream input = new SimpleByteArrayInputStream(data);
            ExtendedDataInputStream dataIn = new ExtendedDataInputStream(input)) {
            return TableStatus.deserialize(dataIn);
        }
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) IOException(java.io.IOException)

Example 42 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class MemoryDataStorageManager method writeIndexPage.

@Override
public void writeIndexPage(String tableSpace, String indexName, long pageId, DataWriter writer) throws DataStorageManagerException {
    Bytes page_wrapper;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        ExtendedDataOutputStream eout = new ExtendedDataOutputStream(out)) {
        writer.write(eout);
        eout.flush();
        page_wrapper = Bytes.from_array(out.toByteArray());
    } catch (IOException ex) {
        throw new DataStorageManagerException(ex);
    }
    indexpages.put(tableSpace + "." + indexName + "_" + pageId, page_wrapper);
}
Also used : Bytes(herddb.utils.Bytes) DataStorageManagerException(herddb.storage.DataStorageManagerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) IOException(java.io.IOException) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Example 43 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class MemoryDataStorageManager method getIndexStatus.

@Override
public IndexStatus getIndexStatus(String tableSpace, String indexName, LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
    final String checkPoint = checkpointName(tableSpace, indexName, sequenceNumber);
    byte[] data = indexStatuses.get(checkPoint);
    if (data == null) {
        throw new DataStorageManagerException("no such index checkpoint: " + checkPoint);
    }
    try {
        try (InputStream input = new SimpleByteArrayInputStream(data);
            ExtendedDataInputStream dataIn = new ExtendedDataInputStream(input)) {
            return IndexStatus.deserialize(dataIn);
        }
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) IOException(java.io.IOException)

Example 44 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class MemoryDataStorageManager method tableCheckpoint.

@Override
public List<PostCheckpointAction> tableCheckpoint(String tableSpace, String tableName, TableStatus tableStatus, boolean pin) throws DataStorageManagerException {
    /* Checkpoint pinning */
    final Map<Long, Integer> pins = pinTableAndGetPages(tableSpace, tableName, tableStatus, pin);
    final Set<LogSequenceNumber> checkpoints = pinTableAndGetCheckpoints(tableSpace, tableName, tableStatus, pin);
    List<Long> pagesForTable = new ArrayList<>();
    String prefix = tableSpace + "." + tableName + "_";
    for (String key : pages.keySet()) {
        if (key.startsWith(prefix)) {
            long pageId = Long.parseLong(key.substring(prefix.length()));
            if (!pins.containsKey(pageId)) {
                pagesForTable.add(pageId);
            }
        }
    }
    pagesForTable.removeAll(tableStatus.activePages.keySet());
    List<PostCheckpointAction> result = new ArrayList<>();
    for (long pageId : pagesForTable) {
        result.add(new PostCheckpointAction(tableSpace, tableName, "drop page " + pageId) {

            @Override
            public void run() {
                // remove only after checkpoint completed
                pages.remove(prefix + pageId);
                LOGGER.log(Level.SEVERE, "removing " + (prefix + pageId));
            }
        });
    }
    for (String oldStatus : tableStatuses.keySet()) {
        if (oldStatus.startsWith(prefix)) {
            /* Check for checkpoint skip only if match expected structure */
            final LogSequenceNumber log = evaluateLogSequenceNumber(prefix);
            if (log != null) {
                /* If is pinned skip this status*/
                if (checkpoints.contains(log)) {
                    continue;
                }
            }
            result.add(new PostCheckpointAction(tableSpace, tableName, "drop table checkpoint " + oldStatus) {

                @Override
                public void run() {
                    // remove only after checkpoint completed
                    tableStatuses.remove(oldStatus);
                }
            });
        }
    }
    VisibleByteArrayOutputStream oo = new VisibleByteArrayOutputStream(1024);
    try (ExtendedDataOutputStream dataOutputKeys = new ExtendedDataOutputStream(oo)) {
        tableStatus.serialize(dataOutputKeys);
        dataOutputKeys.flush();
        oo.write(oo.xxhash64());
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
    /* Uses a copy to limit byte[] size at the min needed */
    tableStatuses.put(checkpointName(tableSpace, tableName, tableStatus.sequenceNumber), oo.toByteArray());
    return result;
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) IOException(java.io.IOException) PostCheckpointAction(herddb.core.PostCheckpointAction) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Example 45 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class BlockRangeIndex method boot.

public void boot(BlockRangeIndexMetadata<K> metadata) throws DataStorageManagerException {
    if (metadata.getBlocksMetadata().size() > 0) {
        LOG.info("boot index, with " + metadata.getBlocksMetadata().size() + " blocks");
    }
    if (metadata.getBlocksMetadata().size() == 0) {
        reset();
    } else {
        clear();
        /* Metadata are saved/recovered in reverse order so "next" block has been already created */
        Block<K, V> next = null;
        for (BlockRangeIndexMetadata.BlockMetadata<K> blockData : metadata.getBlocksMetadata()) {
            /* Medatada safety check (do not trust blindly ordering) */
            if (blockData.nextBlockId != null) {
                if (next == null) {
                    throw new DataStorageManagerException("Wrong next block, expected notingh but " + blockData.nextBlockId + " found");
                } else if (next.key.blockId != blockData.nextBlockId.longValue()) {
                    throw new DataStorageManagerException("Wrong next block, expected " + next.key.blockId + " but " + blockData.nextBlockId + " found");
                }
            } else {
                if (next != null) {
                    throw new DataStorageManagerException("Wrong next block, expected " + next.key.blockId + " but nothing found");
                }
            }
            final BlockStartKey<K> key = BlockStartKey.valueOf(blockData.firstKey, blockData.blockId);
            next = new Block<>(this, key, blockData.size, blockData.pageId, next);
            blocks.put(key, next);
            currentBlockId.accumulateAndGet(Math.abs(next.key.blockId), EnsureLongIncrementAccumulator.INSTANCE);
        }
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException)

Aggregations

DataStorageManagerException (herddb.storage.DataStorageManagerException)140 IOException (java.io.IOException)92 ArrayList (java.util.ArrayList)46 LogSequenceNumber (herddb.log.LogSequenceNumber)42 Bytes (herddb.utils.Bytes)30 Path (java.nio.file.Path)30 ExtendedDataInputStream (herddb.utils.ExtendedDataInputStream)25 SimpleByteArrayInputStream (herddb.utils.SimpleByteArrayInputStream)24 InputStream (java.io.InputStream)24 StatementExecutionException (herddb.model.StatementExecutionException)23 Table (herddb.model.Table)22 HashMap (java.util.HashMap)22 LogEntry (herddb.log.LogEntry)21 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)21 LogNotAvailableException (herddb.log.LogNotAvailableException)20 Record (herddb.model.Record)20 Index (herddb.model.Index)19 CommitLogResult (herddb.log.CommitLogResult)18 Transaction (herddb.model.Transaction)18 PostCheckpointAction (herddb.core.PostCheckpointAction)16