Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indexed queries to MySqlEventStore #225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/MySqlEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ private function createWhereClause(?MetadataMatcher $metadataMatcher): array
}

foreach ($metadataMatcher->data() as $key => $match) {
$this->convertToColumn($match);
/** @var FieldType $fieldType */
$fieldType = $match['fieldType'];
$field = $match['field'];
Expand Down Expand Up @@ -866,4 +867,20 @@ private function createSchemaFor(string $tableName): void
}
}
}

/**
* Convert metadata fields into indexed columns
*
* @example `_aggregate__id` => `aggregate_id`
*/
private function convertToColumn(array &$match): void
{
if ($this->persistenceStrategy instanceof MySqlIndexedPersistenceStrategy) {
$indexedColumns = $this->persistenceStrategy->indexedMetadataFields();
if (\in_array($match['field'], \array_keys($indexedColumns), true)) {
$match['field'] = $indexedColumns[$match['field']];
$match['fieldType'] = FieldType::MESSAGE_PROPERTY();
}
}
}
}
29 changes: 29 additions & 0 deletions src/MySqlIndexedPersistenceStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of prooph/pdo-event-store.
* (c) 2016-2019 prooph software GmbH <[email protected]>
* (c) 2016-2019 Sascha-Oliver Prolic <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Prooph\EventStore\Pdo;

interface MySqlIndexedPersistenceStrategy
{
/**
* Return an array of indexed columns to enable the use of indexes in MariaDB
*
* @example
* [
* '_aggregate_id' => 'aggregate_id',
* '_aggregate_type' => 'aggregate_type',
* '_aggregate_version' => 'aggregate_version',
* ]
*/
public function indexedMetadataFields(): array;
}
12 changes: 11 additions & 1 deletion src/PersistenceStrategy/MySqlSingleStreamStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
use Prooph\Common\Messaging\MessageConverter;
use Prooph\EventStore\Pdo\DefaultMessageConverter;
use Prooph\EventStore\Pdo\HasQueryHint;
use Prooph\EventStore\Pdo\MySqlIndexedPersistenceStrategy;
use Prooph\EventStore\Pdo\Util\Json;
use Prooph\EventStore\StreamName;

final class MySqlSingleStreamStrategy implements MySqlPersistenceStrategy, HasQueryHint
final class MySqlSingleStreamStrategy implements MySqlPersistenceStrategy, HasQueryHint, MySqlIndexedPersistenceStrategy
{
/**
* @var MessageConverter
Expand Down Expand Up @@ -70,6 +71,15 @@ public function columnNames(): array
];
}

public function indexedMetadataFields(): array
{
return [
'_aggregate_id' => 'aggregate_id',
'_aggregate_type' => 'aggregate_type',
'_aggregate_version' => 'aggregate_version',
];
}

public function prepareData(Iterator $streamEvents): array
{
$data = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

use Iterator;
use Prooph\EventStore\Pdo\HasQueryHint;
use Prooph\EventStore\Pdo\MySqlIndexedPersistenceStrategy;
use Prooph\EventStore\Pdo\PersistenceStrategy\MySqlPersistenceStrategy;
use Prooph\EventStore\Pdo\Util\Json;
use Prooph\EventStore\StreamName;

final class CustomMySqlSingleStreamStrategy implements MySqlPersistenceStrategy, HasQueryHint
final class CustomMySqlSingleStreamStrategy implements MySqlPersistenceStrategy, HasQueryHint, MySqlIndexedPersistenceStrategy
{
/**
* @param string $tableName
Expand Down Expand Up @@ -59,6 +60,15 @@ public function columnNames(): array
];
}

public function indexedMetadataFields(): array
{
return [
'_aggregate_id' => 'aggregate_id',
'_aggregate_type' => 'aggregate_type',
'_aggregate_version' => 'aggregate_version',
];
}

public function prepareData(Iterator $streamEvents): array
{
$data = [];
Expand Down