@Test
  public void readingWithSegmentRegistryTest() throws IOException {
    final Buffer byteBuffer = preparePayload();
    final long fullSize = byteBuffer.getLong();
    // full size without 4 bytes for segment_code value
    Assert.assertEquals(fullSize + 4, byteBuffer.remaining());

    final int code = byteBuffer.getInt();
    Payload payloadSegment = (Payload) SegmentRegistry.read(code, byteBuffer);

    for (int i = 0; i < 15; i++) {
      final Buffer currentPayload = payloadSegment.getPayload(i);
      final byte[] expectedPayloadBytes = ("payload" + i).getBytes();

      for (byte expectedByte : expectedPayloadBytes) {
        final byte actualByte = currentPayload.get();
        Assert.assertEquals(actualByte, expectedByte);
      }
      Assert.assertFalse(currentPayload.hasRemaining());
    }
  }
  @Test
  public void hugePayload() throws IOException {
    final byte[] payload = new byte[1024 * 1024];
    for (int i = 0; i < payload.length; i++) payload[i] = (byte) i;

    final Collection<UnsignedByteArray> elements = new LinkedList<>();
    final int docs = 4 * 1024;
    for (int i = 0; i < docs; i++) {
      elements.add(UnsignedByteArrays.from(payload));
    }

    final com.yandex.yoctodb.v1.mutable.segment.V1FullPayloadSegment v1PayloadSegment =
        new V1FullPayloadSegment(elements);

    final OutputStreamWritable outputStreamWritable = v1PayloadSegment.buildWritable();

    final File f = File.createTempFile("huge", "dat");
    f.deleteOnExit();

    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(f))) {
      outputStreamWritable.writeTo(os);
    }

    final Buffer buf = Buffer.from(new RandomAccessFile(f, "r").getChannel());

    final long segmentSize = buf.getLong();
    Assert.assertEquals(segmentSize + 4, buf.remaining());

    final int code = buf.getInt();
    final Payload segment = (Payload) SegmentRegistry.read(code, buf);

    final Buffer expectedPayload = Buffer.from(payload);
    for (int i = 0; i < docs; i += docs >> 2) {
      final Buffer currentPayload = segment.getPayload(i);
      Assert.assertEquals(expectedPayload, currentPayload);
    }
  }