A small binary columnar format for flat records, decoded in the browser by
decodeColumns and columnsDecoder from live_slice/data.
Each column is stored as one contiguous little-endian array, so the client makes typed-array views over the response body without copying it, and repeated strings can be stored once in a dictionary. It suits large lists of flat records (points, rows, samples) sent as chunks to the chunk loader:
def tile(conn, %{"z" => z, "x" => x, "y" => y}) do
points = MyApp.Points.in_tile(z, x, y)
conn
|> put_resp_content_type(LiveSlice.Columns.content_type(), nil)
|> send_resp(200, LiveSlice.Columns.encode(points, id: :uint32, lon: :float32, lat: :float32, kind: :dict))
endColumn types
:float64,:float32- IEEE 754 floats (integers are accepted). A:float32value is rounded to the nearest float32; one beyond its range is refused rather than turned into an infinity.:int32,:uint32,:int16,:uint16,:uint8- integers in the range of the type. The 16-bit types halve the size of columns whose values fit (levels, small counts, a coordinate quantized to 65,536 steps).:dict- strings stored once each in a dictionary, plus one index per record. For columns with few distinct values (categories, kinds). Only strings: a numeric column with few distinct values is smaller as:uint8or:uint16codes (no dictionary to send), mapped by the client.:utf8- strings stored one after the other, with offsets. For columns whose values are mostly distinct (names, labels).
Values cannot be nil: every record has a value in every column. Strings
must be valid UTF-8.
Byte layout
All integers are little-endian. The decoder refuses anything that does not follow this layout exactly, including trailing bytes and non-zero padding.
offset size field
0 4 magic "LSCF"
4 1 format version, 1
5 3 reserved, zero
8 4 record count N (uint32)
12 4 column count C (uint32)
16 ... C column descriptors, each:
type (uint8): 1 float64, 2 float32, 3 int32, 4 uint32,
5 uint8, 6 dict, 7 utf8, 8 int16, 9 uint16
name length L (uint8, 1 to 255)
name (L bytes of UTF-8, unique among the columns)
... zero padding to a multiple of 8Then one data section per column, in descriptor order. Each section starts at a multiple of 8 from the start of the data (zero padding before it), so every numeric array is aligned for a typed-array view:
- float64: N x 8 bytes; float32, int32, uint32: N x 4 bytes; int16, uint16: N x 2 bytes; uint8: N bytes.
- dict: the entry count D (uint32); D + 1 offsets (uint32) into the string
bytes, starting at 0 and never decreasing; the UTF-8 bytes of the
entries (
offsets[D]bytes); zero padding to a multiple of 4; then N indexes into the entries, of 1 byte when D <= 256, 2 bytes (uint16) when D <= 65536, 4 bytes (uint32) otherwise. - utf8: N + 1 offsets (uint32), starting at 0 and never decreasing; then
the UTF-8 bytes of the values (
offsets[N]bytes).
The data ends right after the last section.
Summary
Types
A column type.
Decoded columns: the record count and a list of values per column name.
Column names and types, in the order of the columns.
Functions
The media type to send encoded columns with: application/octet-stream.
Decodes data produced by encode/2. Numeric values come back as integers
or floats (a :float32 value as the float it was rounded to), strings as
binaries. Returns {:error, message} for data that does not follow the
layout exactly, or that holds a float that is not a finite number.
Encodes data as described in the moduledoc, returning iodata.
Types
@type column_type() ::
:float64
| :float32
| :int32
| :uint32
| :int16
| :uint16
| :uint8
| :dict
| :utf8
A column type.
@type decoded() :: %{ count: non_neg_integer(), columns: %{required(String.t()) => list()} }
Decoded columns: the record count and a list of values per column name.
@type schema() :: [{atom() | String.t(), column_type()}]
Column names and types, in the order of the columns.
Functions
@spec content_type() :: String.t()
The media type to send encoded columns with: application/octet-stream.
Decodes data produced by encode/2. Numeric values come back as integers
or floats (a :float32 value as the float it was rounded to), strings as
binaries. Returns {:error, message} for data that does not follow the
layout exactly, or that holds a float that is not a finite number.
Encodes data as described in the moduledoc, returning iodata.
data is either a list of records (maps holding a value for every column
name of schema, with the same keys as the schema, atoms or strings) or a
map of column name to a list of values, all of the same length. In the map
form, a numeric column may also be a binary that already holds its values
little-endian (for example a slice of a larger column kept in memory); it is
passed through without copying, and its size must be the record count times
the width of the type.
Raises ArgumentError for an invalid schema, a missing column or key, a
value out of the range of its type, an invalid string, or columns of
different lengths.
iex> [%{id: 1, kind: "a"}, %{id: 2, kind: "a"}]
...> |> LiveSlice.Columns.encode(id: :uint32, kind: :dict)
...> |> IO.iodata_to_binary()
...> |> LiveSlice.Columns.decode()
{:ok, %{count: 2, columns: %{"id" => [1, 2], "kind" => ["a", "a"]}}}