Search in sources :

Example 21 with ExtendedDataInputStream

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

the class MemoryDataStorageManager method getLatestTableStatus.

@Override
public TableStatus getLatestTableStatus(String tableSpace, String tableName) throws DataStorageManagerException {
    LogSequenceNumber max = null;
    String prefix = tableSpace + "." + tableName + "_";
    for (String status : tableStatuses.keySet()) {
        if (status.startsWith(prefix)) {
            final LogSequenceNumber log = evaluateLogSequenceNumber(prefix);
            if (log != null) {
                if (max == null || log.after(max)) {
                    max = log;
                }
            }
        }
    }
    TableStatus latestStatus;
    if (max == null) {
        latestStatus = TableStatus.buildTableStatusForNewCreatedTable(tableName);
    } else {
        byte[] data = tableStatuses.get(checkpointName(tableSpace, tableName, max));
        if (data == null) {
            latestStatus = TableStatus.buildTableStatusForNewCreatedTable(tableName);
        } else {
            try {
                try (InputStream input = new SimpleByteArrayInputStream(data);
                    ExtendedDataInputStream dataIn = new ExtendedDataInputStream(input)) {
                    latestStatus = TableStatus.deserialize(dataIn);
                }
            } catch (IOException err) {
                throw new DataStorageManagerException(err);
            }
        }
    }
    return latestStatus;
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) TableStatus(herddb.storage.TableStatus) LogSequenceNumber(herddb.log.LogSequenceNumber) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) IOException(java.io.IOException)

Example 22 with ExtendedDataInputStream

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

the class Table method deserialize.

@SuppressFBWarnings("OS_OPEN_STREAM")
public static Table deserialize(byte[] data) {
    try {
        SimpleByteArrayInputStream ii = new SimpleByteArrayInputStream(data);
        ExtendedDataInputStream dii = new ExtendedDataInputStream(ii);
        // version
        long tversion = dii.readVLong();
        // flags for future implementations
        long tflags = dii.readVLong();
        if (tversion != 1 || tflags != 0) {
            throw new IOException("corrupted table file");
        }
        String tablespace = dii.readUTF();
        String name = dii.readUTF();
        String uuid = dii.readUTF();
        boolean auto_increment = dii.readByte() > 0;
        int maxSerialPosition = dii.readVInt();
        byte pkcols = dii.readByte();
        String[] primaryKey = new String[pkcols];
        for (int i = 0; i < pkcols; i++) {
            primaryKey[i] = dii.readUTF();
        }
        // for future implementations
        int flags = dii.readVInt();
        int ncols = dii.readVInt();
        Column[] columns = new Column[ncols];
        for (int i = 0; i < ncols; i++) {
            // version
            long cversion = dii.readVLong();
            // flags for future implementations
            long cflags = dii.readVLong();
            if (cversion != COLUMNVERSION_1 || (cflags != COLUMNFLAGS_NO_FLAGS && cflags != COLUMNFLAGS_HAS_DEFAULT_VALUE)) {
                throw new IOException("corrupted table file");
            }
            String cname = dii.readUTF();
            int type = dii.readVInt();
            int serialPosition = dii.readVInt();
            Bytes defaultValue = null;
            if ((cflags & COLUMNFLAGS_HAS_DEFAULT_VALUE) == COLUMNFLAGS_HAS_DEFAULT_VALUE) {
                defaultValue = dii.readBytes();
            }
            columns[i] = Column.column(cname, type, serialPosition, defaultValue);
        }
        ForeignKeyDef[] foreignKeys = null;
        if ((flags & TABLEFLAGS_HAS_FOREIGN_KEYS) == TABLEFLAGS_HAS_FOREIGN_KEYS) {
            int numForeignKeys = dii.readVInt();
            if (numForeignKeys > 0) {
                foreignKeys = new ForeignKeyDef[numForeignKeys];
                for (int i = 0; i < numForeignKeys; i++) {
                    ForeignKeyDef.Builder builder = ForeignKeyDef.builder();
                    String fkName = dii.readUTF();
                    String parentTableId = dii.readUTF();
                    builder.parentTableId(parentTableId);
                    builder.name(fkName);
                    int numColumns = dii.readVInt();
                    for (int k = 0; k < numColumns; k++) {
                        String col = dii.readUTF();
                        builder.column(col);
                    }
                    for (int k = 0; k < numColumns; k++) {
                        String col = dii.readUTF();
                        builder.parentTableColumn(col);
                    }
                    builder.onUpdateAction(dii.readVInt());
                    builder.onDeleteAction(dii.readVInt());
                    foreignKeys[i] = builder.build();
                }
            }
        }
        return new Table(uuid, name, columns, primaryKey, tablespace, auto_increment, maxSerialPosition, foreignKeys);
    } catch (IOException err) {
        throw new IllegalArgumentException(err);
    }
}
Also used : IOException(java.io.IOException) Bytes(herddb.utils.Bytes) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 23 with ExtendedDataInputStream

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

the class Index method deserialize.

@SuppressFBWarnings("OS_OPEN_STREAM")
public static Index deserialize(byte[] data) {
    try {
        SimpleByteArrayInputStream ii = new SimpleByteArrayInputStream(data);
        ExtendedDataInputStream dii = new ExtendedDataInputStream(ii);
        // version
        long iversion = dii.readVLong();
        // flags for future implementations
        long iflags = dii.readVLong();
        if (iversion != 1 || iflags != 0) {
            throw new IOException("corrupted index file");
        }
        String tablespace = dii.readUTF();
        String name = dii.readUTF();
        String uuid = dii.readUTF();
        String table = dii.readUTF();
        // extensible for future implementations
        int properties = dii.readVInt();
        boolean unique = false;
        if ((properties & PROPERTY_UNIQUE) == PROPERTY_UNIQUE) {
            unique = true;
        }
        String type = dii.readUTF();
        int ncols = dii.readVInt();
        Column[] columns = new Column[ncols];
        for (int i = 0; i < ncols; i++) {
            // version
            long cversion = dii.readVLong();
            // flags for future implementations
            long cflags = dii.readVLong();
            if (cversion != 1 || cflags != 0) {
                throw new IOException("corrupted index file");
            }
            String cname = dii.readUTF();
            int ctype = dii.readVInt();
            int serialPosition = dii.readVInt();
            // for future implementations
            dii.readVInt();
            columns[i] = Column.column(cname, ctype, serialPosition);
        }
        return new Index(uuid, name, table, tablespace, type, columns, unique);
    } catch (IOException err) {
        throw new IllegalArgumentException(err);
    }
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) IOException(java.io.IOException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 24 with ExtendedDataInputStream

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

the class BookKeeperDataStorageManager method loadTransactions.

@Override
public void loadTransactions(LogSequenceNumber sequenceNumber, String tableSpace, Consumer<Transaction> consumer) throws DataStorageManagerException {
    try {
        String file = getTablespaceTransactionsFile(tableSpace, sequenceNumber);
        byte[] content = readZNode(file, new Stat());
        boolean exists = content != null;
        LOGGER.log(Level.INFO, "loadTransactions " + sequenceNumber + " for tableSpace " + tableSpace + " from file " + file + " (exists: " + exists + ")");
        if (!exists) {
            return;
        }
        try (InputStream input = new ByteArrayInputStream(content);
            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);
            }
            String readname = din.readUTF();
            if (!readname.equals(tableSpace)) {
                throw new DataStorageManagerException("file " + file + " 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 + " 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 : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) Stat(org.apache.zookeeper.data.Stat) Transaction(herddb.model.Transaction) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 25 with ExtendedDataInputStream

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

the class BookKeeperDataStorageManager method readTablespaceStructure.

public static List<Table> readTablespaceStructure(byte[] file, String tableSpace, LogSequenceNumber sequenceNumber) throws IOException, DataStorageManagerException {
    try (InputStream input = new ByteArrayInputStream(file);
        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 table list file");
        }
        String readname = din.readUTF();
        if (!readname.equals(tableSpace)) {
            throw new DataStorageManagerException("file is not for spablespace " + tableSpace + " but for " + readname);
        }
        long ledgerId = din.readZLong();
        long offset = din.readZLong();
        if (sequenceNumber != null) {
            if (ledgerId != sequenceNumber.ledgerId || offset != sequenceNumber.offset) {
                throw new DataStorageManagerException("file is not for sequence number " + sequenceNumber);
            }
        }
        int numTables = din.readInt();
        List<Table> res = new ArrayList<>();
        for (int i = 0; i < numTables; i++) {
            byte[] tableData = din.readArray();
            Table table = Table.deserialize(tableData);
            res.add(table);
        }
        return Collections.unmodifiableList(res);
    }
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) Table(herddb.model.Table) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

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