Skip to content

Commit 1d9323d

Browse files
committed
[dbal] Store UUIDs as binary data. Improves performance
1 parent 5c538d3 commit 1d9323d

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎pkg/dbal/DbalContext.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,15 @@ public function createDataBaseTable()
170170
return;
171171
}
172172

173+
$table = new Table($this->getTableName());
174+
173175
if ($this->getDbalConnection()->getDatabasePlatform()->hasNativeGuidType()) {
174-
throw new \LogicException('The platform does not support UUIDs natively');
176+
$table->addColumn('id', 'guid');
177+
} else {
178+
$table->addColumn('id', 'binary', ['length' => 16]);
175179
}
176180

177-
$table = new Table($this->getTableName());
178-
$table->addColumn('id', 'guid');
181+
$table->addColumn('human_id', 'string', ['length' => 36]);
179182
$table->addColumn('published_at', 'bigint');
180183
$table->addColumn('body', 'text', ['notnull' => false]);
181184
$table->addColumn('headers', 'text', ['notnull' => false]);

‎pkg/dbal/DbalProducer.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Interop\Queue\PsrDestination;
1111
use Interop\Queue\PsrMessage;
1212
use Interop\Queue\PsrProducer;
13+
use Ramsey\Uuid\Uuid;
1314

1415
class DbalProducer implements PsrProducer
1516
{
@@ -74,15 +75,11 @@ public function send(PsrDestination $destination, PsrMessage $message)
7475
));
7576
}
7677

77-
$sql = 'SELECT '.$this->context->getDbalConnection()->getDatabasePlatform()->getGuidExpression();
78-
$uuid = $this->context->getDbalConnection()->query($sql)->fetchColumn(0);
79-
80-
if (empty($uuid)) {
81-
throw new \LogicException('The generated uuid is empty');
82-
}
78+
$uuid = Uuid::uuid4();
8379

8480
$dbalMessage = [
85-
'id' => $uuid,
81+
'id' => $uuid->getBytes(),
82+
'human_id' => $uuid->toString(),
8683
'published_at' => (int) microtime(true) * 10000,
8784
'body' => $body,
8885
'headers' => JSON::encode($message->getHeaders()),

‎pkg/dbal/composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"require": {
99
"php": ">=5.6",
1010
"queue-interop/queue-interop": "^0.6@dev",
11-
"doctrine/dbal": "~2.5"
11+
"doctrine/dbal": "~2.5",
12+
"ramsey/uuid": "^3"
1213
},
1314
"require-dev": {
1415
"phpunit/phpunit": "~5.4.0",

0 commit comments

Comments
 (0)