Search in sources :

Example 6 with RandomAccessInputView

use of org.apache.flink.runtime.io.disk.RandomAccessInputView in project flink by apache.

the class BinaryRowDataTest method testSerialize.

private void testSerialize(BinaryRowData row, MemorySegment[] memorySegments, Map<MemorySegment, Integer> msIndex, BinaryRowDataSerializer serializer, int position, int rowSize) throws IOException {
    RandomAccessOutputView out = new RandomAccessOutputView(memorySegments, 64);
    out.skipBytesToWrite(position);
    row.setTotalSize(rowSize);
    // this `row` contains random bytes, and now we're going to serialize `rowSize` bytes
    // (not including the row header) of the contents
    serializer.serializeToPages(row, out);
    // let's see how many segments we have written
    int segNumber = msIndex.get(out.getCurrentSegment()) + 1;
    int lastSegSize = out.getCurrentPositionInSegment();
    // now deserialize from the written segments
    ArrayList<MemorySegment> segments = new ArrayList<>(Arrays.asList(memorySegments).subList(0, segNumber));
    RandomAccessInputView input = new RandomAccessInputView(segments, 64, lastSegSize);
    input.skipBytesToRead(position);
    BinaryRowData mapRow = serializer.createInstance();
    mapRow = serializer.mapFromPages(mapRow, input);
    assertEquals(row, mapRow);
}
Also used : RandomAccessInputView(org.apache.flink.runtime.io.disk.RandomAccessInputView) ArrayList(java.util.ArrayList) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) RandomAccessOutputView(org.apache.flink.runtime.io.disk.RandomAccessOutputView) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 7 with RandomAccessInputView

use of org.apache.flink.runtime.io.disk.RandomAccessInputView in project flink by apache.

the class BinaryRowDataTest method testPagesSer.

@Test
public void testPagesSer() throws IOException {
    MemorySegment[] memorySegments = new MemorySegment[5];
    ArrayList<MemorySegment> memorySegmentList = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        memorySegments[i] = MemorySegmentFactory.wrap(new byte[64]);
        memorySegmentList.add(memorySegments[i]);
    }
    {
        // multi memorySegments
        String str = "啦啦啦啦啦我是快乐的粉刷匠,啦啦啦啦啦我是快乐的粉刷匠," + "啦啦啦啦啦我是快乐的粉刷匠。";
        BinaryRowData row = new BinaryRowData(1);
        BinaryRowWriter writer = new BinaryRowWriter(row);
        writer.writeString(0, fromString(str));
        writer.complete();
        RandomAccessOutputView out = new RandomAccessOutputView(memorySegments, 64);
        BinaryRowDataSerializer serializer = new BinaryRowDataSerializer(1);
        serializer.serializeToPages(row, out);
        BinaryRowData mapRow = serializer.createInstance();
        mapRow = serializer.mapFromPages(mapRow, new RandomAccessInputView(memorySegmentList, 64));
        writer.reset();
        writer.writeString(0, mapRow.getString(0));
        writer.complete();
        assertEquals(str, row.getString(0).toString());
        BinaryRowData deserRow = serializer.deserializeFromPages(new RandomAccessInputView(memorySegmentList, 64));
        writer.reset();
        writer.writeString(0, deserRow.getString(0));
        writer.complete();
        assertEquals(str, row.getString(0).toString());
    }
    {
        // multi memorySegments
        String str1 = "啦啦啦啦啦我是快乐的粉刷匠,啦啦啦啦啦我是快乐的粉刷匠," + "啦啦啦啦啦我是快乐的粉刷匠。";
        String str2 = "啦啦啦啦啦我是快乐的粉刷匠。";
        BinaryRowData row = new BinaryRowData(2);
        BinaryRowWriter writer = new BinaryRowWriter(row);
        writer.writeString(0, fromString(str1));
        writer.writeString(1, fromString(str2));
        writer.complete();
        RandomAccessOutputView out = new RandomAccessOutputView(memorySegments, 64);
        out.skipBytesToWrite(40);
        BinaryRowDataSerializer serializer = new BinaryRowDataSerializer(2);
        serializer.serializeToPages(row, out);
        RandomAccessInputView in = new RandomAccessInputView(memorySegmentList, 64);
        in.skipBytesToRead(40);
        BinaryRowData mapRow = serializer.createInstance();
        mapRow = serializer.mapFromPages(mapRow, in);
        writer.reset();
        writer.writeString(0, mapRow.getString(0));
        writer.writeString(1, mapRow.getString(1));
        writer.complete();
        assertEquals(str1, row.getString(0).toString());
        assertEquals(str2, row.getString(1).toString());
        in = new RandomAccessInputView(memorySegmentList, 64);
        in.skipBytesToRead(40);
        BinaryRowData deserRow = serializer.deserializeFromPages(in);
        writer.reset();
        writer.writeString(0, deserRow.getString(0));
        writer.writeString(1, deserRow.getString(1));
        writer.complete();
        assertEquals(str1, row.getString(0).toString());
        assertEquals(str2, row.getString(1).toString());
    }
}
Also used : RandomAccessInputView(org.apache.flink.runtime.io.disk.RandomAccessInputView) ArrayList(java.util.ArrayList) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) StringData.fromString(org.apache.flink.table.data.StringData.fromString) RandomAccessOutputView(org.apache.flink.runtime.io.disk.RandomAccessOutputView) MemorySegment(org.apache.flink.core.memory.MemorySegment) BinaryRowDataSerializer(org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer) Test(org.junit.Test)

Aggregations

RandomAccessInputView (org.apache.flink.runtime.io.disk.RandomAccessInputView)7 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)6 ArrayList (java.util.ArrayList)5 RandomAccessOutputView (org.apache.flink.runtime.io.disk.RandomAccessOutputView)5 Test (org.junit.Test)5 MemorySegment (org.apache.flink.core.memory.MemorySegment)4 BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)3 BinaryRowDataSerializer (org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer)3 IOException (java.io.IOException)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 Random (java.util.Random)1 GenericTypeInfo (org.apache.flink.api.java.typeutils.GenericTypeInfo)1 KryoSerializer (org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer)1 MemoryManager (org.apache.flink.runtime.memory.MemoryManager)1 StringData.fromString (org.apache.flink.table.data.StringData.fromString)1 BinaryStringData (org.apache.flink.table.data.binary.BinaryStringData)1 MyObj (org.apache.flink.table.data.util.DataFormatTestUtil.MyObj)1 RawValueDataSerializer (org.apache.flink.table.runtime.typeutils.RawValueDataSerializer)1