
# API

The methods listed here are the documented API of the 0.5 releases. Some
classes expose additional public methods so storh engines can cooperate
internally; treat those as implementation details unless they are listed on
this page.

## `StorageRoot`

- `StorageRoot::at(string $root): StorageRoot`
- `StorageRoot::resolve(string $root, string $namespace = 'runtime-storage'): string`
- `path(string $namespace = 'runtime-storage'): string`
- `root(): string`

## `DocStore`

Alias of `DocPerFileStore`.

- `put(array $data, ?string $id = null): StorageRecord`
- `putMany(iterable $records): array`
- `putStream(iterable $records): int`
- `get(string $id): ?StorageRecord`
- `delete(string $id): void`
- `stream(?RecordQuery $query = null): Generator`
- `query(): QueryBuilder`
- `indexes(): DocStoreIndexManager`
- `reindex(): array`
- `stats(): array`
- `health(): array`
- `verify(): array`
- `repair(): array`
- `compact(): array`
- `importJsonl(string $path): int`
- `exportJsonl(string $path): int`
- `path_for_id(string $id): string`

## `SegmentedLog`

Alias of `SegmentedLogStore`.

- `put(array $data, ?string $id = null): StorageRecord`
- `appendMany(iterable $records): array`
- `appendStream(iterable $records): int`
- `get(string $id): ?StorageRecord`
- `delete(string $id): void`
- `stream(?RecordQuery $query = null): Generator`
- `query(): QueryBuilder`
- `retain(): SegmentedLogRetention`
- `compact(): void`
- `recover(): void`
- `state_index(): array`
- `stats(): array`
- `health(): array`
- `verify(): array`
- `repair(): array`

## `Queue`

Alias of `LogQueue`.

- `enqueue(array $payload, ?string $id = null): string`
- `enqueueMany(iterable $jobs): array`
- `claim(): ?StorageRecord`
- `claimMany(int $limit): array`
- `complete(string $id, bool $keep_done = true): void`
- `completeMany(iterable $ids, bool $keep_done = true): int`
- `requeue_timed_out(int $timeout_seconds): int`
- `purgeDone(int $olderThanSeconds = 0): int`
- `counts(): array`
- `stats(): array`
- `health(): array`
- `verify(): array`
- `repair(int $processingTimeoutSeconds = 3600): array`

## `QueryBuilder`

- `where(string $field): QueryFieldBuilder`
- `andWhere(callable $callback): QueryBuilder`
- `orWhere(callable $callback): QueryBuilder`
- `orderBy(string $field, string $direction = 'asc'): QueryBuilder`
- `limit(int $limit): QueryBuilder`
- `cursor(string $id): QueryBuilder`
- `page(int $size): QueryBuilder`
- `get(): array`
- `first(): ?StorageRecord`
- `count(): int`
- `explain(): array`

## `DocStoreIndexManager`

- `field(string $field): DocStoreIndexFieldBuilder`
- `define_field(string $field, bool $unique = false, bool $range = false): DocStoreIndexManager`
- `apply_schema(Schema $schema): DocStoreIndexManager`
- `sync(bool $rebuild = true): void`
- `rebuild(): array`
- `definitions(): array`

## `Schema`

- `Schema::collection(string $collection): Schema`
- `string(string $field): SchemaFieldBuilder`
- `int(string $field): SchemaFieldBuilder`
- `float(string $field): SchemaFieldBuilder`
- `bool(string $field): SchemaFieldBuilder`
- `mixed(string $field): SchemaFieldBuilder`
- `required(array $fields): Schema`
- `validate(array $data): void`

## `Cache`

- `Cache::memory(int $maxEntries = 10000, ?int $maxBytes = null): CacheInterface`
- `Cache::apcu(string $prefix = 'storh'): CacheInterface`
- `Cache::null(): CacheInterface`

## `CacheValidation`

- `CacheValidation::HASH`
- `CacheValidation::STAT`
- `CacheValidation::TRUST`

## `SqlMirror`

Requires `ext-pdo` (`sqlite` or `mysql` driver) or `ext-mysqli`.

- `new SqlMirror(PDO|mysqli|SqlMirrorConnection $connection, string $prefix = 'storh_', ?string $driver = null)`
- `collection(FileStoreInterface $store, string $name, ?Schema $schema = null): SqlMirror`
- `install(): void`
- `uninstall(): void`
- `push(?string $name = null): array`
- `flush(string $name, array $ids): array`
- `pull(?string $name = null): array`
- `verify(): array`
- `rebuild(?string $name = null): array`
- `table(string $name): string`

## `SqlMirrorConnection`

Implement this interface (and `SqlMirrorStatement`) to drive `SqlMirror`
through a custom database layer. `PdoSqlMirrorConnection` and
`MysqliSqlMirrorConnection` are the bundled implementations.

- `driver(): string`
- `execute(string $sql): void`
- `rows(string $sql): Generator`
- `statement(string $sql): SqlMirrorStatement`
- `begin(): void`
- `commit(): void`
- `rollback(): void`
- `in_transaction(): bool`

## `RecordQuery`

- `RecordQuery::all(): RecordQuery`
- `after(string $id): RecordQuery`
- `time_range_ms(?int $from_ms, ?int $until_ms): RecordQuery`
- `where_equal(string $field, mixed $value): RecordQuery`
- `limit(int $limit): RecordQuery`
- `continue_on_error(callable $handler): RecordQuery`
- `on_segment_open(callable $handler): RecordQuery`

## `StorageRecord`

- `id(): string`
- `data(): array`
