Skip to content

Commit 8c37f79

Browse files
committed
schema caching can be used
1 parent afca7f4 commit 8c37f79

File tree

5 files changed

+132
-36
lines changed

5 files changed

+132
-36
lines changed

src/Contracts/Manager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,18 @@ public function getClient();
4040
*/
4141
public function getSchema();
4242

43+
/**
44+
* @param Schema
45+
*/
46+
public function setSchema(Schema $schema);
47+
4348
/**
4449
* @return Meta
4550
*/
4651
public function getMeta();
52+
53+
/**
54+
* @param Meta
55+
*/
56+
public function setMeta(Meta $meta);
4757
}

src/Manager.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ public function getSchema()
9696
return $this->schema;
9797
}
9898

99+
/**
100+
* @param Schema
101+
*/
102+
public function setSchema(Contracts\Schema $schema)
103+
{
104+
if (isset($this->schema)) {
105+
throw new Exception("Schema is defined");
106+
}
107+
$this->schema = $schema;
108+
}
109+
99110
/**
100111
* @return Meta
101112
*/
@@ -107,4 +118,15 @@ public function getMeta()
107118

108119
return $this->meta;
109120
}
121+
122+
/**
123+
* @param Meta
124+
*/
125+
public function setMeta(Contracts\Meta $meta)
126+
{
127+
if (isset($this->meta)) {
128+
throw new Exception("Meta is defined");
129+
}
130+
$this->meta = $meta;
131+
}
110132
}

src/Schema/Meta.php

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,17 @@ class Meta implements Contracts\Meta
1414
protected $types = [];
1515
protected $instances = [];
1616

17-
public function __construct(Contracts\Manager $manager)
17+
public function __construct(Contracts\Manager $manager, array $data = null)
1818
{
1919
$this->manager = $manager;
2020

21-
$client = $manager->getClient();
22-
foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
23-
list($id, $spaceId, $line, $name, $type) = $property;
24-
if (!array_key_exists($spaceId, $this->property)) {
25-
$this->property[$spaceId] = [];
26-
$this->types[$spaceId] = [];
27-
}
28-
$this->property[$spaceId][$line] = $name;
29-
$this->types[$spaceId][$name] = $type;
30-
}
31-
foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
32-
list($spaceId, $num, $name, $type, $params, $properties) = $index;
33-
if (!array_key_exists($spaceId, $this->property)) {
34-
// tarantool space index
35-
continue;
36-
}
37-
if (!isset($this->indexes[$spaceId])) {
38-
$this->indexes[$spaceId] = [];
39-
}
40-
$this->indexes[$spaceId][$num] = [];
41-
foreach ($properties as $row) {
42-
list($part, $type) = $row;
43-
$this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part];
44-
}
45-
}
46-
foreach ($this->property as $spaceId => $collection) {
47-
ksort($collection);
48-
$this->property[$spaceId] = $collection;
21+
if($data) {
22+
$this->property = $data['property'];
23+
$this->indexes = $data['indexes'];
24+
$this->types = $data['types'];
25+
26+
} else {
27+
$this->collectData();
4928
}
5029
}
5130

@@ -170,4 +149,49 @@ public function getConvention()
170149

171150
return $this->convention;
172151
}
152+
153+
private function collectData()
154+
{
155+
$client = $this->manager->getClient();
156+
foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
157+
list($id, $spaceId, $line, $name, $type) = $property;
158+
if (!array_key_exists($spaceId, $this->property)) {
159+
$this->property[$spaceId] = [];
160+
$this->types[$spaceId] = [];
161+
}
162+
$this->property[$spaceId][$line] = $name;
163+
$this->types[$spaceId][$name] = $type;
164+
}
165+
foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
166+
list($spaceId, $num, $name, $type, $params, $properties) = $index;
167+
if (!array_key_exists($spaceId, $this->property)) {
168+
// tarantool space index
169+
continue;
170+
}
171+
if (!isset($this->indexes[$spaceId])) {
172+
$this->indexes[$spaceId] = [];
173+
}
174+
$this->indexes[$spaceId][$num] = [];
175+
foreach ($properties as $row) {
176+
list($part, $type) = $row;
177+
$this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part];
178+
}
179+
}
180+
foreach ($this->property as $spaceId => $collection) {
181+
ksort($collection);
182+
$this->property[$spaceId] = $collection;
183+
}
184+
185+
}
186+
187+
public function toArray()
188+
{
189+
$this->collectData();
190+
191+
return [
192+
'property' => $this->property,
193+
'indexes' => $this->indexes,
194+
'types' => $this->types,
195+
];
196+
}
173197
}

src/Schema/Schema.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ class Schema implements Contracts\Schema
1414
protected $indexSpace;
1515
protected $spaceId = [];
1616

17-
public function __construct(Client $client)
17+
public function __construct(Client $client, array $data = null)
1818
{
1919
$this->client = $client;
20+
$this->spaceSpace = $client->getSpace(Space::VSPACE);
21+
$this->indexSpace = $client->getSpace(Space::VINDEX);
2022

21-
$this->spaceSpace = $client->getSpace('_vspace');
22-
$this->indexSpace = $client->getSpace('_vindex');
23-
$spaces = $this->spaceSpace->select([])->getData();
24-
foreach ($spaces as $row) {
25-
list($id, $sys, $name) = $row;
26-
$this->spaceId[$name] = $id;
23+
if($data) {
24+
$this->spaceId = $data;
25+
26+
} else {
27+
$this->collectData();
2728
}
29+
2830
}
2931

3032
public function getSpaceId($space)
@@ -125,4 +127,19 @@ public function dropIndex($spaceId, $index)
125127
$indexName = $row[0][2];
126128
$this->client->evaluate("box.space.$spaceName.index.$indexName:drop{}");
127129
}
130+
131+
private function collectData()
132+
{
133+
$spaces = $this->spaceSpace->select([])->getData();
134+
foreach ($spaces as $row) {
135+
list($id, $sys, $name) = $row;
136+
$this->spaceId[$name] = $id;
137+
}
138+
}
139+
140+
public function toArray()
141+
{
142+
$this->collectData();
143+
return $this->spaceId;
144+
}
128145
}

tests/SchemaTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
<?php
22

3+
use Tarantool\Mapper\Client;
4+
use Tarantool\Mapper\Manager;
5+
use Tarantool\Mapper\Schema\Meta;
6+
use Tarantool\Mapper\Schema\Schema;
7+
use Tarantool\Client\Packer\PurePacker;
8+
39
class SchemaTest extends PHPUnit_Framework_TestCase
410
{
11+
public function testCachedMeta()
12+
{
13+
$manager = Helper::createManager();
14+
$meta = $manager->getMeta()->toArray();
15+
$schema = $manager->getSchema()->toArray();
16+
$this->assertNotCount(0, $manager->getClient()->getLog());
17+
$conn = $manager->getClient()->getConnection();
18+
19+
$manager = new Manager(new Client($conn, new PurePacker()));
20+
$manager->setMeta(new Meta($manager, $meta));
21+
$manager->setSchema(new Schema($manager->getClient(), $schema));
22+
$manager->getMeta()->get('migrations');
23+
$manager->getMeta()->get('sequence');
24+
$manager->getMeta()->get('property');
25+
26+
$this->assertCount(0, $manager->getClient()->getLog());
27+
}
528
public function testPropertiesCheck()
629
{
730
$manager = Helper::createManager();

0 commit comments

Comments
 (0)