Skip to content

Commit

Permalink
improve performance and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 18, 2023
1 parent 26006a2 commit e6e74d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
36 changes: 13 additions & 23 deletions src/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\EventBus\HeaderNotFound;
Expand All @@ -30,7 +31,6 @@ public function __construct(
private readonly EventSerializer $serializer,
private readonly AggregateRootRegistry $aggregateRootRegistry,
private readonly string $storeTableName = 'eventstore',
private readonly int $batch = 1000,
) {
}

Expand Down Expand Up @@ -118,7 +118,14 @@ public function save(Message ...$messages): void
{
$this->connection->transactional(
function (Connection $connection) use ($messages): void {
$booleanType = Type::getType(Types::BOOLEAN);
$jsonType = Type::getType(Types::JSON);
$dateTimeType = Type::getType(Types::DATETIMETZ_IMMUTABLE);

$parameters = [];
$placeholders = [];

/** @var array<int, Type> $types */
$types = [];

$columns = [
Expand All @@ -134,7 +141,6 @@ function (Connection $connection) use ($messages): void {
];

$columnsLength = count($columns);

$placeholder = implode(', ', array_fill(0, count($columns), '?'));

foreach ($messages as $position => $message) {
Expand All @@ -154,12 +160,12 @@ function (Connection $connection) use ($messages): void {
throw new MissingDataForStorage($e->name, $e);
}

$offset = $position * $columnsLength;
$offset = (int)$position * $columnsLength;

$types[$offset + 5] = Types::DATETIMETZ_IMMUTABLE;
$types[$offset + 6] = Types::BOOLEAN;
$types[$offset + 7] = Types::BOOLEAN;
$types[$offset + 8] = Types::JSON;
$types[$offset + 5] = $dateTimeType;
$types[$offset + 6] = $booleanType;
$types[$offset + 7] = $booleanType;
$types[$offset + 8] = $jsonType;

$placeholders[] = $placeholder;
}
Expand All @@ -180,22 +186,6 @@ function (Connection $connection) use ($messages): void {
);
}

/**
* @param string[] $fields
*
* @return string[]
*/
private function placeholder(array $fields): array
{
$placeholders = [];

foreach ($fields as $field) {
$placeholders[] = ':' . $field;
}

return $placeholders;
}

/**
* @param Closure():ClosureReturn $function
*
Expand Down
28 changes: 8 additions & 20 deletions tests/Unit/Store/DoctrineDbalStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use EmptyIterator;
use Patchlevel\EventSourcing\EventBus\Message;
Expand Down Expand Up @@ -191,27 +192,14 @@ public function testSaveWithOneEvent(): void

$innerMockedConnection = $this->prophesize(Connection::class);

$platform = $this->prophesize(AbstractPlatform::class);
$innerMockedConnection->getDatabasePlatform()->willReturn($platform->reveal());

$innerMockedConnection->insert(
'eventstore',
[
'aggregate' => 'profile',
'aggregate_id' => '1',
'playhead' => 1,
'event' => 'profile_created',
'payload' => '',
'recorded_on' => $recordedOn,
'new_stream_start' => false,
'archived' => false,
'custom_headers' => [],
],
$innerMockedConnection->executeStatement(
"INSERT INTO eventstore (aggregate, aggregate_id, playhead, event, payload, recorded_on, new_stream_start, archived, custom_headers) VALUES\n(?, ?, ?, ?, ?, ?, ?, ?, ?)",
['profile', '1', 1, 'profile_created', '', $recordedOn, false, false, []],
[
'recorded_on' => 'datetimetz_immutable',
'custom_headers' => 'json',
'archived' => Types::BOOLEAN,
'new_stream_start' => Types::BOOLEAN,
5 => Type::getType(Types::DATETIMETZ_IMMUTABLE),
6 => Type::getType(Types::BOOLEAN),
7 => Type::getType(Types::BOOLEAN),
8 => Type::getType(Types::JSON),
],
)->shouldBeCalledOnce();

Expand Down

0 comments on commit e6e74d9

Please sign in to comment.