Search by

adachsoft / json-file-storage

Arkadiusz Adach

A concurrency-safe JSON file storage library with locking and atomic writes.

Package info

gitlab.com/a.adach/json-file-storage

Issues

pkg:composer/adachsoft/json-file-storage

Statistics

Installs: 9

Dependents: 1

Suggesters: 0

Stars: 0

v0.1.0 2026-11-26 20:03 UTC

This package is auto-updated.

Last update: 2026-11-26 19:03:55 UTC


README

adachsoft/json-file-storage provides one shared JSON document store for *-json-file repositories. It centralizes whole-document reads and writes, sidecar locking, and atomic replacement so repositories do not need to maintain separate copies of the storage implementation.

Requirements

PHP 8.3 or newer.

Installation

composer require adachsoft/json-file-storage

Usage

API

Create a store for a JSON document. Its parent directory is created when needed. A missing or empty document reads as an empty array; the document root must otherwise be a JSON object. JSON objects, including nested objects, are returned as associative arrays.

use AdachSoft\JsonFileStorage\JsonFileStorage;

$storage = new JsonFileStorage(__DIR__ . '/var/repository.json');

$document = $storage->read();

$storage->update(static function (array $document): array {
    $document['count'] = (int) ($document['count'] ?? 0) + 1;

    return $document;
});

read(): array reads the whole document. update(callable(array): array): void passes the current document to a read-modify-write callback. transaction(callable(array): JsonFileTransactionResult): mixed runs a callback under the same exclusive lock and returns its result. JsonFileTransactionResult contains the replacement document and the value to return; a null document means that the file is not written.

Atomic repository operations

Use transaction() when a repository must read, check, and modify its document as one operation. For example, a job can be removed and returned exactly once:

use AdachSoft\JsonFileStorage\JsonFileStorage;
use AdachSoft\JsonFileStorage\JsonFileTransactionResult;

final readonly class JobRepository
{
    public function __construct(private JsonFileStorage $storage)
    {
    }

    public function consume(string $jobId): mixed
    {
        return $this->storage->transaction(
            static function (array $document) use ($jobId): JsonFileTransactionResult {
                $jobs = $document['jobs'] ?? [];
                if (!is_array($jobs) || !array_key_exists($jobId, $jobs)) {
                    return new JsonFileTransactionResult(null, null);
                }

                $job = $jobs[$jobId];
                unset($jobs[$jobId]);
                $document['jobs'] = $jobs;

                return new JsonFileTransactionResult($document, $job);
            },
        );
    }
}

The lookup, removal, and returned item all happen while holding one exclusive lock, preventing two cooperating processes from consuming the same item.

Guarantees and limits

  • read() uses a shared lock; update() and transaction() use an exclusive lock for the full read-modify-write operation. The lock is stored in a sidecar file named <document>.lock.
  • Writes are pretty-printed UTF-8 JSON with a trailing newline. A temporary file is created in the document's directory, flushed and synchronized with fsync(), then atomically installed with rename().
  • The store rewrites the entire document on each write. It is intended for small- and medium-sized documents, not large databases.
  • flock() locks are advisory and coordinate only processes that follow the same locking convention. Use a local filesystem; network filesystems such as NFS are not supported.