Search in sources :

Example 11 with ByteArrayCursor

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

the class DataAccessorForFullRecord method forEach.

@Override
public void forEach(BiConsumer<String, Object> consumer) {
    // best case
    if (table.physicalLayoutLikeLogicalLayout) {
        // no need to create a Map
        if (table.primaryKey.length == 1) {
            String pkField = table.primaryKey[0];
            Object value = RecordSerializer.deserialize(record.key, table.getColumn(pkField).type);
            consumer.accept(pkField, value);
            if (value instanceof RawString) {
                ((RawString) value).recycle();
            }
        } else {
            try (final ByteArrayCursor din = record.key.newCursor()) {
                for (String primaryKeyColumn : table.primaryKey) {
                    Bytes value = din.readBytesNoCopy();
                    Object theValue = RecordSerializer.deserialize(value, table.getColumn(primaryKeyColumn).type);
                    consumer.accept(primaryKeyColumn, theValue);
                    if (theValue instanceof RawString) {
                        ((RawString) theValue).recycle();
                    }
                }
            } catch (IOException err) {
                throw new IllegalStateException("bad data:" + err, err);
            }
        }
        try (ByteArrayCursor din = record.value.newCursor()) {
            while (!din.isEof()) {
                int serialPosition;
                serialPosition = din.readVIntNoEOFException();
                if (din.isEof()) {
                    break;
                }
                Column col = table.getColumnBySerialPosition(serialPosition);
                if (col != null) {
                    Object value = RecordSerializer.deserializeTypeAndValue(din);
                    consumer.accept(col.name, value);
                } else {
                    // we have to deserialize always the value, even the column is no more present
                    RecordSerializer.skipTypeAndValue(din);
                }
            }
        } catch (IOException err) {
            throw new IllegalStateException("bad data:" + err, err);
        }
    } else {
        // bad case
        for (int i = 0; i < table.columnNames.length; i++) {
            String columnName = table.columnNames[i];
            Object value = get(i);
            consumer.accept(columnName, value);
            if (value instanceof RawString) {
                ((RawString) value).recycle();
            }
        }
    }
}
Also used : Bytes(herddb.utils.Bytes) RawString(herddb.utils.RawString) Column(herddb.model.Column) RawString(herddb.utils.RawString) IOException(java.io.IOException) ByteArrayCursor(herddb.utils.ByteArrayCursor)

Example 12 with ByteArrayCursor

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

the class RecordSerializer method accessRawDataFromValue.

static Object accessRawDataFromValue(int index, Bytes value, Table table) throws IOException {
    Column column = table.getColumn(index);
    try (ByteArrayCursor din = value.newCursor()) {
        while (!din.isEof()) {
            int serialPosition;
            serialPosition = din.readVIntNoEOFException();
            if (din.isEof()) {
                return null;
            }
            Column col = table.getColumnBySerialPosition(serialPosition);
            if (col != null && col.serialPosition == column.serialPosition) {
                return deserializeTypeAndValue(din);
            } else {
                // we have to deserialize always the value, even the column is no more present
                skipTypeAndValue(din);
            }
        }
        return null;
    }
}
Also used : Column(herddb.model.Column) ByteArrayCursor(herddb.utils.ByteArrayCursor)

Aggregations

ByteArrayCursor (herddb.utils.ByteArrayCursor)12 IOException (java.io.IOException)6 Column (herddb.model.Column)5 Bytes (herddb.utils.Bytes)5 DataStorageManagerException (herddb.storage.DataStorageManagerException)4 RawString (herddb.utils.RawString)4 ArrayList (java.util.ArrayList)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Record (herddb.model.Record)2 ImmutableMap (com.google.common.collect.ImmutableMap)1