DownloadRFC-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).
??????????? ???????? ????????
???????? ???????? ????? ????????????? ??????? ?? ??????????????:
-
Lifecycle callbacks ? `prePersist` ?????????? ??? ?????? ???????? ?? ???????? ??????? (?? ?????), ? `postPersist` ? ????? ???? ???????? ??????? (???? ???). ??? ?????????? ?? ??????????????? ??????, ??? ?????? ???? pre/post ????????? ???? `insert()`.
-
Events ? `PrePersistEvent` / `PostPersistEvent` ? ???????? ??????: ???? ???? ??????? ? ???????? ?????????, ???? ?? ?????? ??????? ?? ????????, ?? ? ???????????? ?? ????????. ??? ??????? ??????????.
-
????????? failures ? ??? ???????? ??????? 100 ???????, ???? 50-? ???????? constraint violation, ????????? ??????? ?? ??. PostgreSQL ?????????? ???? `INSERT`, MySQL ????? ???????? ?????? 49 (? ??????????? ?? engine). ??????? ?????? ??????????????? ???? ?????????.
-
Identity retrieval ? ??? ???????? `INSERT` ? PostgreSQL ????? ???????????? `RETURNING` ??? ????????? ???? ??????????????? ID. MySQL `LAST_INSERT_ID()` ???????? ?????? ??? ?????? ?????? multi-row INSERT. ??? ??????????? ????? ??? `EntityPersister`, ??????? ?????? ?????????? ID ????? ???????.
???? ??????????
???? 1: ????????? + ????????? ?????????? (patch/minor release)
-
???????? `bulkInsert()`, `bulkUpdate()`, `bulkDelete()`, `supportsBulkOperations()` ? `EntityDataAdapter`.
-
??????????? ????????? fallback-?????? ? `AbstractDataAdapter`.
-
???????? `EntityPersister` ? ???????????? ???????? ?????? ??? `supportsBulkOperations() === true`.
-
???????? `bulkThreshold` ? ??????????? `EntityPersister`.
-
???????? unit-????? ??? ????????? fallback-??????????.
-
???????? integration-???? ? mock-?????????, ?????????????? ???????? ????????.
???? 2: SQL-??????? (????????? release)
-
??????????? `bulkInsert()`, `bulkUpdate()`, `bulkDelete()` ? SQL-????????.
-
???????? ????? ? ???????? ?? (SQLite in-memory ??? CI, MySQL/PostgreSQL ???????????).
-
????????: ???????? 1000 insert ?? ?????? vs. bulk insert.
???? 3: ??????????? lifecycle ? events
-
?????????? ????????? lifecycle callbacks ??? ???????? ????????.
-
??? ????????????? ?????? `BulkPrePersistEvent` / `BulkPostPersistEvent`.
-
??????????????? ????????? ??? 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 ???, ?????? ?????????.
|