\Drupal\strata\Segment SegmentWriter

Writes a segment manifest to the store.

A manifest goes through the same compression and sealing as a payload frame, because it names every subject that changed and what the change was; an unsealed manifest would leak the shape of the site to anyone with read access to the bucket even if every frame were encrypted.

The stored object is self-describing: a short plaintext header naming the format version, the codec and the plaintext digest, followed by the sealed body. Nothing about reading it depends on provider metadata, which not every endpoint returns and which the local filesystem has nowhere to put. The object KEY is the associated data, so a segment moved to another key fails to open instead of opening as the wrong segment.

The key carries the level and the time range rather than only a digest, so a compaction pass can find the segments covering an hour with one prefix listing rather than reading manifests.

Summary

Methods
Properties
Constants
__construct
write
header
key
levelPrefix
secondPrefix
No public properties found
PREFIX
MAGIC
No protected methods found
No protected properties found
No protected constants found
No private methods found
provider
codecs
cipher
level
No private constants found

Constant

PREFIX

PREFIX = 'segments'

Key prefix segments are written under.

MAGIC

MAGIC = 'STRATA-SEG-1'

Magic bytes and format version leading every stored segment.

Properties

$codecs

$codecs : \Drupal\strata\Codec\CodecRegistry

Type

CodecRegistry

$cipher

$cipher : \Drupal\strata\Crypto\CipherInterface

Type

CipherInterface

$level

$level : ?int

Type

int|null

Methods

__construct()

__construct(\Drupal\strata\Storage\StorageProviderInterface  $provider, \Drupal\strata\Codec\CodecRegistry  $codecs, \Drupal\strata\Crypto\CipherInterface  $cipher, int|null  $level = null) : mixed

Constructs a writer.

Parameters

\Drupal\strata\Storage\StorageProviderInterface $provider

Where segments are written.

\Drupal\strata\Codec\CodecRegistry $codecs

Used to pick the codec that compresses the manifest.

\Drupal\strata\Crypto\CipherInterface $cipher

Seals the manifest.

int|null $level

Compression level, or NULL for the codec's default.

Returns

mixed —

write()

write(\Drupal\strata\Segment\SegmentManifest  $manifest, int|null  $second = null) : string

Writes a manifest and returns its object key.

Parameters

\Drupal\strata\Segment\SegmentManifest $manifest

The manifest.

int|null $second

The epoch second to file the segment under, or NULL to take it from the first operation. A rollup supplies the coarse window's start, because a segment covering an hour belongs to that hour rather than to the second its earliest operation happened to land in.

Throws

\RuntimeException

When the manifest is empty, or the write fails.

\JsonException

When the manifest holds a string JSON cannot represent. JournalOp refuses such a string on capture, so reaching this is a bug rather than bad input; without it (string) false would seal an empty object under Hash::of('') and the flush would report success.

Returns

string —

The object key the segment was written to.

header()

header(string  $codec, string  $cipher, string  $digest) : string

The plaintext header leading a stored segment.

Newline delimited so a reader can find the body without a length prefix, and plaintext because a reader must know which codec and cipher to use before it can open anything.

Parameters

string $codec

Codec id the body was compressed with.

string $cipher

Cipher id the body was sealed with.

string $digest

Digest of the serialized manifest, before compression.

Returns

string —

The header, ending in a newline.

key()

key(\Drupal\strata\Segment\SegmentManifest  $manifest, string  $digest, int|null  $second = null) : string

The object key a manifest is written to.

Shaped segments/<level>/<epoch-second>/<sequence>-<digest>.seg. The level and the epoch second are directories so a compaction pass can list one hour at one level directly; the sequence leads the filename so a listing comes back in capture order.

Parameters

\Drupal\strata\Segment\SegmentManifest $manifest

The manifest.

string $digest

Digest of the serialized manifest.

int|null $second

The epoch second to file it under, or NULL to take it from the first operation.

Returns

string —

The object key.

levelPrefix()

levelPrefix(int  $level) : string

The key prefix covering one level.

Parameters

int $level

Compaction level.

Returns

string —

A prefix suitable for a listing.

secondPrefix()

secondPrefix(int  $level, int  $second) : string

The key prefix covering one second at one level.

Parameters

int $level

Compaction level.

int $second

Unix second.

Returns

string —

A prefix suitable for a listing.