JsonColumn
in package
implements
Countable, IteratorAggregate, JsonSerializable
Store a long list of values already encoded in JSON.
Each value is encoded when it is added. The encoded values are buffered in a string, then written in a temporary stream kept in memory up to MAX_MEMORY bytes, and in a temporary file beyond. So the memory used does not depend on the number of values.
The column can be written several times to an output stream with writeTo(), read value by value with foreach, which decodes the values by blocks, or decoded at once with jsonSerialize(). It can also be created from a JSON array already encoded, with fromJson().
Tags
Table of Contents
Interfaces
- Countable
- IteratorAggregate
- JsonSerializable
Constants
- BUFFER_SIZE : mixed = 65536
- Size of the string buffer written at once in the stream
- MAX_CUT_ATTEMPTS : mixed = 64
- Maximum number of commas tried in a block before reading the next one
- MAX_MEMORY : mixed = 262144
- Size of the temporary stream kept in memory
Properties
Methods
- __destruct() : mixed
- Close the temporary stream, which deletes its temporary file if any.
- append() : void
- Add a value at the end of the column.
- count() : int
- Get the number of values.
- fromJson() : self
- Create a column from a JSON array already encoded, for example by PostgreSQL.
- getIterator() : Generator<int, mixed>
- Get the values one by one, decoded from JSON by blocks.
- jsonSerialize() : array<string|int, mixed>
- Get the values, decoded from JSON.
- writeTo() : void
- Write the column as a JSON array to the output stream.
- decodeCompleteValues() : array<string|int, mixed>
- Decode the complete values at the beginning of the encoded values, and remove them.
- flush() : void
- Write the buffer at the end of the temporary stream and empty it.
Constants
BUFFER_SIZE
Size of the string buffer written at once in the stream
private
mixed
BUFFER_SIZE
= 65536
MAX_CUT_ATTEMPTS
Maximum number of commas tried in a block before reading the next one
private
mixed
MAX_CUT_ATTEMPTS
= 64
MAX_MEMORY
Size of the temporary stream kept in memory
private
mixed
MAX_MEMORY
= 262144
Properties
$buffer
private
string
$buffer
= ''
The encoded values not yet written in the stream
$count
private
int
$count
= 0
The number of values
$stream
private
null|resource
$stream
The temporary stream, created at the first flush
Methods
__destruct()
Close the temporary stream, which deletes its temporary file if any.
public
__destruct() : mixed
append()
Add a value at the end of the column.
public
append(mixed $value) : void
Parameters
- $value : mixed
-
the value, which must be encodable in JSON
count()
Get the number of values.
public
count() : int
Return values
int —the number of values
fromJson()
Create a column from a JSON array already encoded, for example by PostgreSQL.
public
static fromJson(string $json, int $count) : self
The values are copied in the temporary stream without being decoded, so a large array does not use more memory than its JSON string.
Parameters
- $json : string
-
the JSON array
- $count : int
-
the number of values of the array
Tags
Return values
self —the column
getIterator()
Get the values one by one, decoded from JSON by blocks.
public
getIterator() : Generator<int, mixed>
The temporary stream is read by blocks. The values of a block are decoded together up to its last comma separating two values: a comma inside a value (string, list) makes the decoding fail, and the previous comma is tried. Only one block of values is kept in memory.
The column is not modified, so it can be read several times, and values can be added after the reading.
Return values
Generator<int, mixed> —the values, the lists and objects as arrays
jsonSerialize()
Get the values, decoded from JSON.
public
jsonSerialize() : array<string|int, mixed>
All the values are loaded in memory: use writeTo() instead to keep a low memory usage.
Return values
array<string|int, mixed> —the values
writeTo()
Write the column as a JSON array to the output stream.
public
writeTo(resource $output) : void
The column is not modified, so it can be written several times.
Parameters
- $output : resource
-
the output stream
decodeCompleteValues()
Decode the complete values at the beginning of the encoded values, and remove them.
private
decodeCompleteValues(string &$pending) : array<string|int, mixed>
Parameters
- $pending : string
-
the encoded values separated by commas, the last one may be incomplete
Return values
array<string|int, mixed> —the decoded values, empty if none can be decoded yet
flush()
Write the buffer at the end of the temporary stream and empty it.
private
flush() : void