Search in sources :

Example 6 with ByteArrayOutputStreamWithPos

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

the class TypeSerializerSerializationProxyTest method testStateSerializerSerializationProxy.

@Test
public void testStateSerializerSerializationProxy() throws Exception {
    TypeSerializer<?> serializer = IntSerializer.INSTANCE;
    TypeSerializerSerializationProxy<?> proxy = new TypeSerializerSerializationProxy<>(serializer);
    byte[] serialized;
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        proxy.write(new DataOutputViewStreamWrapper(out));
        serialized = out.toByteArray();
    }
    proxy = new TypeSerializerSerializationProxy<>(Thread.currentThread().getContextClassLoader());
    try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
        proxy.read(new DataInputViewStreamWrapper(in));
    }
    Assert.assertEquals(serializer, proxy.getTypeSerializer());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 7 with ByteArrayOutputStreamWithPos

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

the class VersionedIOWriteableTest method testReadSameVersion.

@Test
public void testReadSameVersion() throws Exception {
    String payload = "test";
    TestWriteable testWriteable = new TestWriteable(1, payload);
    byte[] serialized;
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        testWriteable.write(new DataOutputViewStreamWrapper(out));
        serialized = out.toByteArray();
    }
    testWriteable = new TestWriteable(1);
    try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
        testWriteable.read(new DataInputViewStreamWrapper(in));
    }
    Assert.assertEquals(payload, testWriteable.getData());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 8 with ByteArrayOutputStreamWithPos

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

the class VersionedIOWriteableTest method testReadMismatchVersion.

@Test
public void testReadMismatchVersion() throws Exception {
    String payload = "test";
    TestWriteable testWriteable = new TestWriteable(1, payload);
    byte[] serialized;
    try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) {
        testWriteable.write(new DataOutputViewStreamWrapper(out));
        serialized = out.toByteArray();
    }
    testWriteable = new TestWriteable(2);
    try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) {
        testWriteable.read(new DataInputViewStreamWrapper(in));
        Assert.fail("Version mismatch expected.");
    } catch (VersionMismatchException ignored) {
    }
    Assert.assertEquals(null, testWriteable.getData());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 9 with ByteArrayOutputStreamWithPos

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

the class AbstractRocksDBState 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.<K, N>deserializeKeyAndNamespace(serializedKeyAndNamespace, backend.getKeySerializer(), namespaceSerializer);
    int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(des.f0, backend.getNumberOfKeyGroups());
    // we cannot reuse the keySerializationStream member since this method
    // is called concurrently to the other ones and it may thus contain garbage
    ByteArrayOutputStreamWithPos tmpKeySerializationStream = new ByteArrayOutputStreamWithPos(128);
    DataOutputViewStreamWrapper tmpKeySerializationDateDataOutputView = new DataOutputViewStreamWrapper(tmpKeySerializationStream);
    writeKeyWithGroupAndNamespace(keyGroup, des.f0, des.f1, tmpKeySerializationStream, tmpKeySerializationDateDataOutputView);
    return backend.db.get(columnFamily, tmpKeySerializationStream.toByteArray());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)

Example 10 with ByteArrayOutputStreamWithPos

use of org.apache.flink.core.memory.ByteArrayOutputStreamWithPos 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

ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)16 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)14 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)11 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)10 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)4 Random (java.util.Random)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 OperatorStateStore (org.apache.flink.api.common.state.OperatorStateStore)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)1 DataOutputView (org.apache.flink.core.memory.DataOutputView)1