Skip to content

Commit 6c3ec76

Browse files
authored
Merge pull request #280 from php-enqueue/dbal-uuid-as-binary-data
[0.9][BC break][dbal] Store UUIDs as binary data. Improves performance
2 parents aedee50 + bf6095d commit 6c3ec76

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

Diff for: pkg/dbal/DbalConnectionFactory.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ public function __construct($config = 'mysql:')
4545
throw new \LogicException('The config must be either an array of options, a DSN string or null');
4646
}
4747

48-
$this->config = $config;
48+
$this->config = array_replace_recursive([
49+
'connection' => [],
50+
'table_name' => 'enqueue',
51+
'polling_interval' => 1000,
52+
'lazy' => true,
53+
], $config);
4954
}
5055

5156
/**

Diff for: pkg/dbal/DbalConsumer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function receiveMessage()
155155

156156
// remove message
157157
$affectedRows = $this->dbal->delete($this->context->getTableName(), ['id' => $dbalMessage['id']], [
158-
'id' => Type::INTEGER,
158+
'id' => Type::BINARY,
159159
]);
160160

161161
if (1 !== $affectedRows) {

Diff for: pkg/dbal/DbalContext.php

+25-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\Schema\Table;
7+
use Doctrine\DBAL\Types\Type;
78
use Interop\Queue\InvalidDestinationException;
89
use Interop\Queue\PsrContext;
910
use Interop\Queue\PsrDestination;
@@ -171,16 +172,18 @@ public function createDataBaseTable()
171172
}
172173

173174
$table = new Table($this->getTableName());
174-
$table->addColumn('id', 'guid');
175-
$table->addColumn('published_at', 'bigint');
176-
$table->addColumn('body', 'text', ['notnull' => false]);
177-
$table->addColumn('headers', 'text', ['notnull' => false]);
178-
$table->addColumn('properties', 'text', ['notnull' => false]);
179-
$table->addColumn('redelivered', 'boolean', ['notnull' => false]);
180-
$table->addColumn('queue', 'string');
181-
$table->addColumn('priority', 'smallint');
182-
$table->addColumn('delayed_until', 'integer', ['notnull' => false]);
183-
$table->addColumn('time_to_live', 'integer', ['notnull' => false]);
175+
176+
$table->addColumn('id', Type::BINARY, ['length' => 16, 'fixed' => true]);
177+
$table->addColumn('human_id', Type::STRING, ['length' => 36]);
178+
$table->addColumn('published_at', Type::BIGINT);
179+
$table->addColumn('body', Type::TEXT, ['notnull' => false]);
180+
$table->addColumn('headers', Type::TEXT, ['notnull' => false]);
181+
$table->addColumn('properties', Type::TEXT, ['notnull' => false]);
182+
$table->addColumn('redelivered', Type::BOOLEAN, ['notnull' => false]);
183+
$table->addColumn('queue', Type::STRING);
184+
$table->addColumn('priority', Type::SMALLINT);
185+
$table->addColumn('delayed_until', Type::INTEGER, ['notnull' => false]);
186+
$table->addColumn('time_to_live', Type::INTEGER, ['notnull' => false]);
184187

185188
$table->setPrimaryKey(['id']);
186189
$table->addIndex(['published_at']);
@@ -190,4 +193,16 @@ public function createDataBaseTable()
190193

191194
$sm->createTable($table);
192195
}
196+
197+
/**
198+
* @param DbalDestination $queue
199+
*/
200+
public function purgeQueue(DbalDestination $queue)
201+
{
202+
$this->getDbalConnection()->delete(
203+
$this->getTableName(),
204+
['queue' => $queue->getQueueName()],
205+
['queue' => Type::STRING]
206+
);
207+
}
193208
}

Diff for: pkg/dbal/DbalProducer.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Interop\Queue\PsrDestination;
1111
use Interop\Queue\PsrMessage;
1212
use Interop\Queue\PsrProducer;
13+
use Ramsey\Uuid\Codec\OrderedTimeCodec;
14+
use Ramsey\Uuid\Uuid;
15+
use Ramsey\Uuid\UuidFactory;
1316

1417
class DbalProducer implements PsrProducer
1518
{
@@ -33,12 +36,18 @@ class DbalProducer implements PsrProducer
3336
*/
3437
private $context;
3538

39+
/**
40+
* @var OrderedTimeCodec
41+
*/
42+
private $uuidCodec;
43+
3644
/**
3745
* @param DbalContext $context
3846
*/
3947
public function __construct(DbalContext $context)
4048
{
4149
$this->context = $context;
50+
$this->uuidCodec = new OrderedTimeCodec((new UuidFactory())->getUuidBuilder());
4251
}
4352

4453
/**
@@ -74,15 +83,11 @@ public function send(PsrDestination $destination, PsrMessage $message)
7483
));
7584
}
7685

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-
}
86+
$uuid = Uuid::uuid1();
8387

8488
$dbalMessage = [
85-
'id' => $uuid,
89+
'id' => $this->uuidCodec->encodeBinary($uuid),
90+
'human_id' => $uuid->toString(),
8691
'published_at' => (int) microtime(true) * 10000,
8792
'body' => $body,
8893
'headers' => JSON::encode($message->getHeaders()),

Diff for: pkg/dbal/composer.json

+2-1
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)