Skip to content

Commit

Permalink
fixed #28: standard sequence names support
Browse files Browse the repository at this point in the history
  • Loading branch information
nekufa committed Sep 7, 2021
1 parent c324715 commit d7bf631
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/Plugin/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,44 @@ public function initializeSequence(Space $space)

$name = $space->getName();

if (!array_key_exists($name, $this->sequences)) {
[$primaryIndex] = $space->getIndexes();
if (count($primaryIndex['parts']) !== 1) {
throw new Exception("Composite primary key");
}
$this->mapper
->getPlugin(Procedure::class)
->get(CreateSequence::class)
->execute($name, $primaryIndex['name'], $primaryIndex['parts'][0][0] + 1);
if (array_key_exists($name, $this->sequences)) {
// sequence exists
return;
}

$this->mapper->getRepository('_vsequence')->flushCache();
if (array_key_exists($name . '_seq', $this->sequences)) {
// use tarantool standard sequence name
return;
}

$this->sequences[$name] = true;
[$primaryIndex] = $space->getIndexes();
if (count($primaryIndex['parts']) !== 1) {
throw new Exception("Composite primary key");
}

$this->mapper
->getPlugin(Procedure::class)
->get(CreateSequence::class)
->execute($name, $primaryIndex['name'], $primaryIndex['parts'][0][0] + 1);

$this->mapper->getRepository('_vsequence')->flushCache();

$this->sequences[$name] = true;
}

private function generateValue(Space $space): int
{
$this->initializeSequence($space);

$next = $this->mapper->getClient()
->call('box.sequence.' . $space->getName() . ':next');
$name = $space->getName();
if (!array_key_exists($name, $this->sequences)) {
if (array_key_exists($name . '_seq', $this->sequences)) {
// use tarantool standard sequence name
$name .= '_seq';
}
}

$next = $this->mapper->getClient()->call('box.sequence.' . $name . ':next');

return $next[0];
}
Expand Down
44 changes: 44 additions & 0 deletions tests/SequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,50 @@

class SequenceTest extends TestCase
{
public function testSequenceIndexing()
{
$mapper = $this->createMapper();
$this->clean($mapper);

// no sequences in schema
$this->assertCount(0, $mapper->find('_vsequence'));

$mapper->getSchema()->createSpace('some_space', [
'if_not_exists' => true,
'engine' => 'memtx',
'properties' => [
'id' => 'unsigned',
'value' => 'string',
],
])
->addIndex([
'fields' => 'id',
'if_not_exists' => true,
'sequence' => true,
]);

// sequence was created
$mapper->getRepository('_vsequence')->flushCache();
$this->assertCount(1, $mapper->find('_vsequence'));
$seq = $mapper->findOne('_vsequence');
$this->assertSame($seq->name, 'some_space_seq');

// no new sequence should be created
$mapper->getPlugin(Sequence::class);
$mapper->getRepository('_vsequence')->flushCache();
$this->assertCount(1, $mapper->find('_vsequence'));

$result1 = $mapper->create('some_space', ['value' => 27]);
$result2 = $mapper->create('some_space', ['value' => 42]);

// no new sequence should be created
$mapper->getRepository('_vsequence')->flushCache();
$this->assertCount(1, $mapper->find('_vsequence'));

$this->assertSame($result1->id, 1);
$this->assertSame($result2->id, 2);
}

public function testInstanceOverwriteOnPluginAdd()
{
$mapper = $this->createMapper();
Expand Down

0 comments on commit d7bf631

Please sign in to comment.