Search in sources :

Example 66 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class RocksDBAggregatingState method add.

@Override
public void add(T value) throws IOException {
    try {
        // prepare the current key and namespace for RocksDB lookup
        writeCurrentKeyWithGroupAndNamespace();
        final byte[] key = keySerializationStream.toByteArray();
        keySerializationStream.reset();
        // get the current value
        final byte[] valueBytes = backend.db.get(columnFamily, key);
        // deserialize the current accumulator, or create a blank one
        final ACC accumulator = valueBytes == null ? aggFunction.createAccumulator() : valueSerializer.deserialize(new DataInputViewStreamWrapper(new ByteArrayInputStreamWithPos(valueBytes)));
        // aggregate the value into the accumulator
        aggFunction.add(value, accumulator);
        // serialize the new accumulator
        final DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(keySerializationStream);
        valueSerializer.serialize(accumulator, out);
        // write the new value to RocksDB
        backend.db.put(columnFamily, writeOptions, key, keySerializationStream.toByteArray());
    } catch (IOException | RocksDBException e) {
        throw new IOException("Error while adding value to RocksDB", e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) IOException(java.io.IOException) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 67 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class RocksDBFoldingState method add.

@Override
public void add(T value) throws IOException {
    try {
        writeCurrentKeyWithGroupAndNamespace();
        byte[] key = keySerializationStream.toByteArray();
        byte[] valueBytes = backend.db.get(columnFamily, key);
        DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(keySerializationStream);
        if (valueBytes == null) {
            keySerializationStream.reset();
            valueSerializer.serialize(foldFunction.fold(stateDesc.getDefaultValue(), value), out);
            backend.db.put(columnFamily, writeOptions, key, keySerializationStream.toByteArray());
        } else {
            ACC oldValue = valueSerializer.deserialize(new DataInputViewStreamWrapper(new ByteArrayInputStreamWithPos(valueBytes)));
            ACC newValue = foldFunction.fold(oldValue, value);
            keySerializationStream.reset();
            valueSerializer.serialize(newValue, out);
            backend.db.put(columnFamily, writeOptions, key, keySerializationStream.toByteArray());
        }
    } catch (Exception e) {
        throw new RuntimeException("Error while adding data to RocksDB", e);
    }
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) RocksDBException(org.rocksdb.RocksDBException) IOException(java.io.IOException)

Example 68 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class RocksDBValueState method update.

@Override
public void update(V value) throws IOException {
    if (value == null) {
        clear();
        return;
    }
    DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(keySerializationStream);
    try {
        writeCurrentKeyWithGroupAndNamespace();
        byte[] key = keySerializationStream.toByteArray();
        keySerializationStream.reset();
        valueSerializer.serialize(value, out);
        backend.db.put(columnFamily, writeOptions, key, keySerializationStream.toByteArray());
    } catch (Exception e) {
        throw new RuntimeException("Error while adding data to RocksDB", e);
    }
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) RocksDBException(org.rocksdb.RocksDBException) IOException(java.io.IOException)

Example 69 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class RocksDBListState method add.

@Override
public void add(V value) throws IOException {
    try {
        writeCurrentKeyWithGroupAndNamespace();
        byte[] key = keySerializationStream.toByteArray();
        keySerializationStream.reset();
        DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(keySerializationStream);
        valueSerializer.serialize(value, out);
        backend.db.merge(columnFamily, writeOptions, key, keySerializationStream.toByteArray());
    } catch (Exception e) {
        throw new RuntimeException("Error while adding data to RocksDB", e);
    }
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) IOException(java.io.IOException) RocksDBException(org.rocksdb.RocksDBException)

Example 70 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class RocksDBMapState method getSerializedValue.

@Override
@SuppressWarnings("unchecked")
public byte[] getSerializedValue(byte[] serializedKeyAndNamespace) throws Exception {
    Preconditions.checkNotNull(serializedKeyAndNamespace, "Serialized key and namespace");
    //TODO make KvStateRequestSerializer key-group aware to save this round trip and key-group computation
    Tuple2<K, N> des = KvStateRequestSerializer.deserializeKeyAndNamespace(serializedKeyAndNamespace, backend.getKeySerializer(), namespaceSerializer);
    int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(des.f0, backend.getNumberOfKeyGroups());
    ByteArrayOutputStreamWithPos outputStream = new ByteArrayOutputStreamWithPos(128);
    DataOutputViewStreamWrapper outputView = new DataOutputViewStreamWrapper(outputStream);
    writeKeyWithGroupAndNamespace(keyGroup, des.f0, des.f1, outputStream, outputView);
    final byte[] keyPrefixBytes = outputStream.toByteArray();
    final Iterator<Map.Entry<UK, UV>> iterator = new RocksDBMapIterator<Map.Entry<UK, UV>>(backend.db, keyPrefixBytes) {

        @Override
        public Map.Entry<UK, UV> next() {
            return nextEntry();
        }
    };
    // Return null to make the behavior consistent with other backends
    if (!iterator.hasNext()) {
        return null;
    }
    return KvStateRequestSerializer.serializeMap(new Iterable<Map.Entry<UK, UV>>() {

        @Override
        public Iterator<Map.Entry<UK, UV>> iterator() {
            return iterator;
        }
    }, userKeySerializer, userValueSerializer);
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) Iterator(java.util.Iterator) RocksIterator(org.rocksdb.RocksIterator) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) Map(java.util.Map)

Aggregations

DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)123 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)49 Test (org.junit.Test)43 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)35 IOException (java.io.IOException)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)23 DataOutputView (org.apache.flink.core.memory.DataOutputView)18 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)11 Map (java.util.Map)10 TypeSerializerSnapshot (org.apache.flink.api.common.typeutils.TypeSerializerSnapshot)7 StateMetaInfoSnapshot (org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot)7 Before (org.junit.Before)6 Socket (java.net.Socket)5 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 List (java.util.List)4