Search in sources :

Example 16 with ExtendedDataInputStream

use of herddb.utils.ExtendedDataInputStream in project herddb by diennea.

the class FileDataStorageManager method rawReadDataPage.

public static List<Record> rawReadDataPage(Path pageFile) throws DataStorageManagerException, IOException {
    List<Record> result;
    long hashFromFile;
    long hashFromDigest;
    try (ODirectFileInputStream odirect = new ODirectFileInputStream(pageFile, O_DIRECT_BLOCK_BATCH);
        XXHash64Utils.HashingStream hash = new XXHash64Utils.HashingStream(odirect);
        ExtendedDataInputStream dataIn = new ExtendedDataInputStream(hash)) {
        // version
        long version = dataIn.readVLong();
        // flags for future implementations
        long flags = dataIn.readVLong();
        if (version != 1 || flags != 0) {
            throw new DataStorageManagerException("corrupted data file " + pageFile.toAbsolutePath());
        }
        int numRecords = dataIn.readInt();
        result = new ArrayList<>(numRecords);
        for (int i = 0; i < numRecords; i++) {
            Bytes key = dataIn.readBytes();
            Bytes value = dataIn.readBytes();
            result.add(new Record(key, value));
        }
        hashFromDigest = hash.hash();
        hashFromFile = dataIn.readLong();
    }
    if (hashFromFile != NO_HASH_PRESENT && hashFromDigest != hashFromFile) {
        throw new DataStorageManagerException("Corrupted datafile " + pageFile + ". Bad hash " + hashFromFile + " <> " + hashFromDigest);
    }
    return result;
}
Also used : Bytes(herddb.utils.Bytes) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) ODirectFileInputStream(herddb.utils.ODirectFileInputStream) Record(herddb.model.Record) XXHash64Utils(herddb.utils.XXHash64Utils)

Example 17 with ExtendedDataInputStream

use of herddb.utils.ExtendedDataInputStream 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) ODirectFileInputStream(herddb.utils.ODirectFileInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream)

Example 18 with ExtendedDataInputStream

use of herddb.utils.ExtendedDataInputStream 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) ODirectFileInputStream(herddb.utils.ODirectFileInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 19 with ExtendedDataInputStream

use of herddb.utils.ExtendedDataInputStream in project herddb by diennea.

the class FileDataStorageManager method readLogSequenceNumberFromTransactionsFile.

private static LogSequenceNumber readLogSequenceNumberFromTransactionsFile(String tableSpace, Path file) throws DataStorageManagerException {
    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();
        return new LogSequenceNumber(ledgerId, offset);
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ODirectFileInputStream(herddb.utils.ODirectFileInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) LogSequenceNumber(herddb.log.LogSequenceNumber) IOException(java.io.IOException)

Example 20 with ExtendedDataInputStream

use of herddb.utils.ExtendedDataInputStream in project herddb by diennea.

the class LogEntry method deserialize.

public static LogEntry deserialize(byte[] data) throws EOFException {
    SimpleByteArrayInputStream in = new SimpleByteArrayInputStream(data);
    ExtendedDataInputStream dis = new ExtendedDataInputStream(in);
    return deserialize(dis);
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream)

Aggregations

ExtendedDataInputStream (herddb.utils.ExtendedDataInputStream)28 SimpleByteArrayInputStream (herddb.utils.SimpleByteArrayInputStream)27 IOException (java.io.IOException)23 DataStorageManagerException (herddb.storage.DataStorageManagerException)22 InputStream (java.io.InputStream)22 ODirectFileInputStream (herddb.utils.ODirectFileInputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)10 LogSequenceNumber (herddb.log.LogSequenceNumber)9 BufferedInputStream (java.io.BufferedInputStream)9 ArrayList (java.util.ArrayList)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 KeyToPageIndex (herddb.index.KeyToPageIndex)2 BLinkKeyToPageIndex (herddb.index.blink.BLinkKeyToPageIndex)2 Index (herddb.model.Index)2 Table (herddb.model.Table)2 Transaction (herddb.model.Transaction)2 Bytes (herddb.utils.Bytes)2 RawString (herddb.utils.RawString)2 Path (java.nio.file.Path)2 Stat (org.apache.zookeeper.data.Stat)2