PHP Classes

File: docs/rfc/RFC-001-batch-operations-interface.md

Recommend this page to a friend!
  Packages of ?????? ???????   Adaptive Entity Manager   docs/rfc/RFC-001-batch-operations-interface.md   Download  
File: docs/rfc/RFC-001-batch-operations-interface.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Adaptive Entity Manager
Entity manager to work with multiple data sources
Author: By
Last change:
Date: 3 months ago
Size: 12,953 bytes
 

 

Contents

Class file image Download

RFC-001: ????????? ???????? ???????? ??? EntityDataAdapter

| ???? | ???????? | |-----------------|-------------------------------------------| | ?????? | Proposed | | ???? | 2026-05-05 | | ????? | Ruslan Kabirov | | ?????????? ????? | Adapter/EntityDataAdapter.php, ??? ?????????? ?????????, Persistence/EntityPersister.php |

?????????

??????? ????????? EntityDataAdapter ???????????? ?????? ?????????????? ?????????:

interface EntityDataAdapter
{
    public function insert(array $row): void;
    public function update(array $row, mixed $identifier): void;
    public function delete(mixed $identifier): void;
    // ...
}

????? UnitOfWork::commit() flushed ?????????, ?? ???????? insert()/update()/delete() ??? ?????? ???????? ?? ???????????. ??? ?????????, ?????????? ? REST API ??? GraphQL, ??? ????????? ? ?????? ????? ????????????? ?????? HTTP-???????. ?????? ??? SQL-????????? ??? ??????????? ???????? ??????????????????:

  • ??????? 1000 ??????? = 1000 ????????? `INSERT`-???????? ? ??.
  • ?????? ?????? ? ????????? round-trip ? ??????? ???? ??????.
  • ??? network latency 1ms ? 1 ??????? ?????? ?? ???????? ???????. ??? 5ms ? 5 ??????.
  • Multi-row `INSERT` ????????? ????? ? 10-50x.

?????? ???????? ???????? ????? ? ????????? ??????????? ???????????????? ?????????? ??? ??????????? ???????? ?????? ?? legacy-?????? ? ?????? ??? ???????? ?????? ?????? ???????????.

???????????? ???????

?????????? ?????????? EntityDataAdapter

???????? ???????????? ?????? ??? ???????? ???????? ? ????????? ??????????? ????? ?????????????? ??????:

interface EntityDataAdapter
{
    // ???????????? ??????
    public function insert(array $row): void;
    public function update(array $row, mixed $identifier): void;
    public function delete(mixed $identifier): void;

    // ????? ???????? ??????
    public function bulkInsert(array $rows): void;
    public function bulkUpdate(array $rowsWithIdentifiers): void;
    public function bulkDelete(array $identifiers): void;

    // ?????????? ? ????????? ???????? ????????
    public function supportsBulkOperations(): bool;
}

????????? ?????????? ? AbstractDataAdapter

abstract class AbstractDataAdapter implements EntityDataAdapter
{
    public function bulkInsert(array $rows): void
    {
        foreach ($rows as $row) {
            $this->insert($row);
        }
    }

    public function bulkUpdate(array $rowsWithIdentifiers): void
    {
        foreach ($rowsWithIdentifiers as ['row' => $row, 'identifier' => $identifier]) {
            $this->update($row, $identifier);
        }
    }

    public function bulkDelete(array $identifiers): void
    {
        foreach ($identifiers as $identifier) {
            $this->delete($identifier);
        }
    }

    public function supportsBulkOperations(): bool
    {
        return false;
    }
}

????????, ?? ?????????????? ???????? ???????? (REST, GraphQL), ?????? ?? ?????????????? ? ????????? ?????????? ???????? ?????????.

SQL-??????????? ??????????

class SqlEntityDataAdapter extends AbstractDataAdapter
{
    public function bulkInsert(array $rows): void
    {
        if (empty($rows)) {
            return;
        }

        $columns = array_keys($rows[0]);
        $placeholders = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
        $allPlaceholders = implode(', ', array_fill(0, count($rows), $placeholders));

        $sql = sprintf(
            'INSERT INTO %s (%s) VALUES %s',
            $this->table,
            implode(', ', $columns),
            $allPlaceholders
        );

        $flatParams = [];
        foreach ($rows as $row) {
            foreach ($columns as $column) {
                $flatParams[] = $row[$column];
            }
        }

        $this->connection->executeStatement($sql, $flatParams);
    }

    public function supportsBulkOperations(): bool
    {
        return true;
    }
}

????????? ? EntityPersister

EntityPersister ??????????? ???????? ??? ???????/??????????/???????? ? SplObjectStorage. ??? commit ?? ????? ??????? ?????????:

class EntityPersister
{
    public function performInsert(EntityDataAdapter $adapter): void
    {
        $rows = [];
        foreach ($this->insertions as $entity) {
            $rows[] = $this->entityFactory->extractRow($entity);
        }

        if ($adapter->supportsBulkOperations() && count($rows) > 1) {
            $adapter->bulkInsert($rows);
        } else {
            foreach ($rows as $row) {
                $adapter->insert($row);
            }
        }
    }
}

????????????? threshold

???????? ???????????? ????? ??? ???????????? ?? ???????? ?????:

class EntityPersister
{
    public function __construct(
        // ... existing params
        private readonly int $bulkThreshold = 1,
    ) {}
}

  • `bulkThreshold = 1` ? ???????????? ???????? ???????? ?????? (???? ??????????????).
  • `bulkThreshold = 10` ? ????????????? ?? ???????? ????? ??? 10+ ???????.
  • ???????? ??? ????????? ???????? ???????? ?????????? threshold.

?????? ?????? ??? ???????? ????????

bulkInsert

$rows = [
    ['name' => 'Alice', 'email' => 'alice@example.com'],
    ['name' => 'Bob',   'email' => 'bob@example.com'],
    ['name' => 'Carol', 'email' => 'carol@example.com'],
];
$adapter->bulkInsert($rows);

??? ?????? ?????? ????? ?????????? ????????? ???????. ??? ????????????? EntityPersister, ??????? ????????? ?????? ????? EntityFactory::extractRow().

bulkUpdate

$rowsWithIdentifiers = [
    ['row' => ['name' => 'Alice Updated'], 'identifier' => 1],
    ['row' => ['name' => 'Bob Updated'],   'identifier' => 2],
];
$adapter->bulkUpdate($rowsWithIdentifiers);

??? SQL-???????? ??? ????? ???? ??????????? ????? CASE WHEN:

UPDATE users SET name = CASE id
    WHEN 1 THEN 'Alice Updated'
    WHEN 2 THEN 'Bob Updated'
END
WHERE id IN (1, 2)

bulkDelete

$identifiers = [1, 2, 3, 4, 5];
$adapter->bulkDelete($identifiers);

??? SQL: DELETE FROM users WHERE id IN (1, 2, 3, 4, 5).

??????????? ???????? ????????

???????? ???????? ????? ????????????? ??????? ?? ??????????????:

  1. Lifecycle callbacks ? `prePersist` ?????????? ??? ?????? ???????? ?? ???????? ??????? (?? ?????), ? `postPersist` ? ????? ???? ???????? ??????? (???? ???). ??? ?????????? ?? ??????????????? ??????, ??? ?????? ???? pre/post ????????? ???? `insert()`.
  2. Events ? `PrePersistEvent` / `PostPersistEvent` ? ???????? ??????: ???? ???? ??????? ? ???????? ?????????, ???? ?? ?????? ??????? ?? ????????, ?? ? ???????????? ?? ????????. ??? ??????? ??????????.
  3. ????????? failures ? ??? ???????? ??????? 100 ???????, ???? 50-? ???????? constraint violation, ????????? ??????? ?? ??. PostgreSQL ?????????? ???? `INSERT`, MySQL ????? ???????? ?????? 49 (? ??????????? ?? engine). ??????? ?????? ??????????????? ???? ?????????.
  4. Identity retrieval ? ??? ???????? `INSERT` ? PostgreSQL ????? ???????????? `RETURNING` ??? ????????? ???? ??????????????? ID. MySQL `LAST_INSERT_ID()` ???????? ?????? ??? ?????? ?????? multi-row INSERT. ??? ??????????? ????? ??? `EntityPersister`, ??????? ?????? ?????????? ID ????? ???????.

???? ??????????

???? 1: ????????? + ????????? ?????????? (patch/minor release)

  1. ???????? `bulkInsert()`, `bulkUpdate()`, `bulkDelete()`, `supportsBulkOperations()` ? `EntityDataAdapter`.
  2. ??????????? ????????? fallback-?????? ? `AbstractDataAdapter`.
  3. ???????? `EntityPersister` ? ???????????? ???????? ?????? ??? `supportsBulkOperations() === true`.
  4. ???????? `bulkThreshold` ? ??????????? `EntityPersister`.
  5. ???????? unit-????? ??? ????????? fallback-??????????.
  6. ???????? integration-???? ? mock-?????????, ?????????????? ???????? ????????.

???? 2: SQL-??????? (????????? release)

  1. ??????????? `bulkInsert()`, `bulkUpdate()`, `bulkDelete()` ? SQL-????????.
  2. ???????? ????? ? ???????? ?? (SQLite in-memory ??? CI, MySQL/PostgreSQL ???????????).
  3. ????????: ???????? 1000 insert ?? ?????? vs. bulk insert.

???? 3: ??????????? lifecycle ? events

  1. ?????????? ????????? lifecycle callbacks ??? ???????? ????????.
  2. ??? ????????????? ?????? `BulkPrePersistEvent` / `BulkPostPersistEvent`.
  3. ??????????????? ????????? ??? partial failures.

????????????

???????????? 1: ????????? BulkEntityDataAdapter

??????? ???????? ???????? ? ????????? ?????????:

interface BulkEntityDataAdapter
{
    public function bulkInsert(array $rows): void;
    public function bulkUpdate(array $rowsWithIdentifiers): void;
    public function bulkDelete(array $identifiers): void;
}

EntityPersister ????????? instanceof BulkEntityDataAdapter.

????: ?? ?????????? ???????? ?????????. ?????: ????????? ISP (Interface Segregation) ? ???????? ??????? ? ???????, ?????????????? ???????? ????????, ?????? ??????????? ??? ??????????. ???????? ????? instanceof ????? ?????, ??? ????? supportsBulkOperations().

???????????? 2: Buffering decorator

???????? ????? EntityDataAdapter ? BufferingDataAdapter, ??????? ??????????? ???????? ? flush-?? ?? ??????? ??? ?????????? threshold:

$adapter = new BufferingDataAdapter($innerAdapter, threshold: 100);
$adapter->insert($row1);  // buffered
$adapter->insert($row2);  // buffered
// ...
$adapter->insert($row100); // triggers flush ? bulkInsert() or 100x insert()

????: ?? ??????? ????????? ? ?????????? EntityDataAdapter. ???????? ? ????? ?????????. ?????: ???????? ??? flush-???????? ???????? ????? ?????? ???. ?????? ?????????????? ? transactional boundaries. ?? ?????? ???????? ???????????? SQL ? ??????????? ????? decorator ??? ????? ??????? insert() 100 ???, ?????? ?????????.