The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Sereal::Splitter - splits a Sereal blob in chunks of roughly the same size

SYNOPSIS

  use Sereal::Splitter qw(SRL_ZLIB create_header_data_template);

  my $splitter = Sereal::Splitter->new(
    { input => $data, chunk_size => 1, compress => SRL_ZLIB,
      header_data_template => create_header_data_template(
        { date => time(), elements_count => '__$CNT__' }
      )
    }
  );
  while (defined( my $chunk = $splitter->next_chunk())) {
    # do stuff with $chunk;
  }

DESCRIPTION

This library implements an efficient way of splitting a Sereal blob into smaller chunks. Currently, it only works with ArrayRefs Sereal blobs, like this:

  [ $element_1, $element_2, ..., $element_n ]

In the future, it may also work with HashRefs.

CONSTRUCTOR

new

Takes a HashRef with options:

input

Mandatory, String, the Sereal blob to split

chunk_size

Mandatory, positive Int, the approximate size of the uncompressed chunk

compress

Optional, Int, one of SRL_UNCOMPRESSED, SRL_SNAPPY or SRL_ZLIB. These constant can be exported at use time. If set, indicates how chunks must be compressed. Defaults to SRL_UNCOMPRESSED.

header_data_template

Optional, Str, the header_data to inject in each chunk. This header_data can contain special scalar values, that will be replaced by values. Special scalar values are:

'__$CNT__'

This will be replaced by the number of elements that the chunks contains. It must be encoded as SHORT_BINARY_08. It'll be replaced by a VARINT.

REMARK: In theory, it should be a string of lentgh 11, because varint max size are 11 bytes. However, the Sereal decoder code, can't cope with varint bigger than 8 bytes, because of a bug, and even if the varint is forged like 0x8180808080808080808080.

To make things easier, you can use create_header_data_template (see below) to create it for you.

METHODS

next_chunk

returns the next chunk as a String, or Undef if it was the last chunk

EXPORTED FUNCTIONS

create_header_data_template

Given a structure, will return a Sereal *body*, that can be used as value for the header_data_template constructor option.

This function loads Sereal::Encoder if it's not already loaded.