1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Populator; |
4
|
|
|
|
5
|
|
|
use Nimble\ElasticBundle\Document; |
6
|
|
|
use Nimble\ElasticBundle\Transformer\TransformerManager; |
7
|
|
|
use Nimble\ElasticBundle\Type\Type; |
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
9
|
|
|
|
10
|
|
|
class Populator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Type |
14
|
|
|
*/ |
15
|
|
|
private $type; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var PopulationFetcherInterface |
19
|
|
|
*/ |
20
|
|
|
private $fetcher; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var TransformerManager |
24
|
|
|
*/ |
25
|
|
|
private $transformer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $entityCount; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Type $type |
34
|
|
|
* @param PopulationFetcherInterface $fetcher |
35
|
|
|
* @param TransformerManager $transformer |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Type $type, PopulationFetcherInterface $fetcher, TransformerManager $transformer) |
38
|
|
|
{ |
39
|
|
|
$this->type = $type; |
40
|
|
|
$this->fetcher = $fetcher; |
41
|
|
|
$this->transformer = $transformer; |
42
|
|
|
|
43
|
|
|
$this->entityCount = $fetcher->getEntityCount(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $entities |
48
|
|
|
* @return Document[] |
49
|
|
|
*/ |
50
|
|
|
protected function transformEntitiesToDocuments(array $entities) |
51
|
|
|
{ |
52
|
|
|
$documents = []; |
53
|
|
|
|
54
|
|
|
foreach ($entities as $entity) { |
55
|
|
|
$documents = array_merge($documents, $this->transformer->transformToDocuments( |
56
|
|
|
$entity, |
57
|
|
|
$this->type->getIndex()->getId(), |
58
|
|
|
$this->type->getName() |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $documents; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* TODO: make a progress bar interface an an adapter for symfony's ProgressBar |
67
|
|
|
* |
68
|
|
|
* @param int $batchSize |
69
|
|
|
* @param ProgressBar $progress |
70
|
|
|
* @return int Number of documents created/updated. |
71
|
|
|
*/ |
72
|
|
|
public function populate($batchSize, ProgressBar $progress = null) |
73
|
|
|
{ |
74
|
|
|
$count = $this->fetcher->getEntityCount(); |
75
|
|
|
|
76
|
|
|
if ($count === 0) { |
77
|
|
|
return 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$documentCount = 0; |
81
|
|
|
|
82
|
|
|
$offset = 0; |
83
|
|
|
|
84
|
|
|
if (null !== $progress) { |
85
|
|
|
$progress->start($count); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
while ($offset < $count) { |
89
|
|
|
$entities = $this->fetcher->fetchEntities($offset, $batchSize); |
90
|
|
|
$batch = $this->transformEntitiesToDocuments($entities); |
91
|
|
|
|
92
|
|
|
$this->type->putDocuments($batch); |
93
|
|
|
|
94
|
|
|
if (null !== $progress) { |
95
|
|
|
$progress->advance(count($entities)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$documentCount += count($batch); |
99
|
|
|
$offset += $batchSize; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (null !== $progress) { |
103
|
|
|
$progress->finish(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $documentCount; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|