1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Doctrine; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
15
|
|
|
use Elastica\Exception\Bulk\ResponseException as BulkResponseException; |
16
|
|
|
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; |
17
|
|
|
use FOS\ElasticaBundle\Provider\AbstractProvider as BaseAbstractProvider; |
18
|
|
|
use FOS\ElasticaBundle\Provider\IndexableInterface; |
19
|
|
|
|
20
|
|
|
abstract class AbstractProvider extends BaseAbstractProvider |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ManagerRegistry |
24
|
|
|
*/ |
25
|
|
|
protected $managerRegistry; |
26
|
|
|
/** |
27
|
|
|
* @var SliceFetcherInterface |
28
|
|
|
*/ |
29
|
|
|
private $sliceFetcher; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param ObjectPersisterInterface $objectPersister |
35
|
|
|
* @param IndexableInterface $indexable |
36
|
|
|
* @param string $objectClass |
37
|
|
|
* @param array $baseOptions |
38
|
|
|
* @param ManagerRegistry $managerRegistry |
39
|
|
|
* @param SliceFetcherInterface $sliceFetcher |
40
|
|
|
*/ |
41
|
9 |
|
public function __construct( |
42
|
|
|
ObjectPersisterInterface $objectPersister, |
43
|
|
|
IndexableInterface $indexable, |
44
|
|
|
$objectClass, |
45
|
|
|
array $baseOptions, |
46
|
|
|
ManagerRegistry $managerRegistry, |
47
|
|
|
SliceFetcherInterface $sliceFetcher = null |
48
|
|
|
) { |
49
|
9 |
|
parent::__construct($objectPersister, $indexable, $objectClass, $baseOptions); |
50
|
|
|
|
51
|
9 |
|
$this->managerRegistry = $managerRegistry; |
52
|
9 |
|
$this->sliceFetcher = $sliceFetcher; |
53
|
9 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Counts objects that would be indexed using the query builder. |
57
|
|
|
* |
58
|
|
|
* @param object $queryBuilder |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
abstract protected function countObjects($queryBuilder); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates the query builder, which will be used to fetch objects to index. |
66
|
|
|
* |
67
|
|
|
* @param string $method |
68
|
|
|
* @param array $arguments |
69
|
|
|
* |
70
|
|
|
* @return object |
71
|
|
|
*/ |
72
|
|
|
abstract protected function createQueryBuilder($method, array $arguments = []); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Fetches a slice of objects using the query builder. |
76
|
|
|
* |
77
|
|
|
* @param object $queryBuilder |
78
|
|
|
* @param int $limit |
79
|
|
|
* @param int $offset |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
abstract protected function fetchSlice($queryBuilder, $limit, $offset); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
9 |
|
protected function doPopulate($options, \Closure $loggerClosure = null) |
89
|
|
|
{ |
90
|
9 |
|
$manager = $this->managerRegistry->getManagerForClass($this->objectClass); |
91
|
|
|
|
92
|
9 |
|
$queryBuilder = $this->createQueryBuilder($options['query_builder_method']); |
93
|
9 |
|
$nbObjects = $this->countObjects($queryBuilder); |
94
|
9 |
|
$offset = $options['offset']; |
95
|
|
|
|
96
|
9 |
|
$objects = []; |
97
|
9 |
|
for (; $offset < $nbObjects; $offset += $options['batch_size']) { |
98
|
9 |
|
$sliceSize = $options['batch_size']; |
99
|
|
|
try { |
100
|
9 |
|
$objects = $this->getSlice($queryBuilder, $options['batch_size'], $offset, $objects); |
101
|
9 |
|
$sliceSize = count($objects); |
102
|
9 |
|
$objects = $this->filterObjects($options, $objects); |
103
|
|
|
|
104
|
9 |
|
if (!empty($objects)) { |
105
|
8 |
|
$this->objectPersister->insertMany($objects); |
106
|
7 |
|
} |
107
|
9 |
|
} catch (BulkResponseException $e) { |
108
|
1 |
|
if (!$options['ignore_errors']) { |
109
|
1 |
|
throw $e; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (null !== $loggerClosure) { |
113
|
|
|
$loggerClosure( |
114
|
|
|
$options['batch_size'], |
115
|
|
|
$nbObjects, |
116
|
|
|
sprintf('<error>%s</error>', $e->getMessage()) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
8 |
|
if ($options['clear_object_manager']) { |
122
|
7 |
|
$manager->clear(); |
123
|
7 |
|
} |
124
|
|
|
|
125
|
8 |
|
usleep($options['sleep']); |
126
|
|
|
|
127
|
8 |
|
if (null !== $loggerClosure) { |
128
|
1 |
|
$loggerClosure($sliceSize, $nbObjects); |
129
|
1 |
|
} |
130
|
8 |
|
} |
131
|
8 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
9 |
View Code Duplication |
protected function configureOptions() |
137
|
|
|
{ |
138
|
9 |
|
parent::configureOptions(); |
139
|
|
|
|
140
|
9 |
|
$this->resolver->setDefaults([ |
141
|
9 |
|
'clear_object_manager' => true, |
142
|
9 |
|
'debug_logging' => false, |
143
|
9 |
|
'ignore_errors' => false, |
144
|
9 |
|
'offset' => 0, |
145
|
9 |
|
'query_builder_method' => 'createQueryBuilder', |
146
|
9 |
|
'sleep' => 0, |
147
|
9 |
|
]); |
148
|
9 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* If this Provider has a SliceFetcher defined, we use it instead of falling back to |
152
|
|
|
* the fetchSlice methods defined in the ORM/MongoDB subclasses. |
153
|
|
|
* |
154
|
|
|
* @param $queryBuilder |
155
|
|
|
* @param int $limit |
156
|
|
|
* @param int $offset |
157
|
|
|
* @param array $lastSlice |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
9 |
|
private function getSlice($queryBuilder, $limit, $offset, $lastSlice) |
162
|
|
|
{ |
163
|
9 |
|
if (!$this->sliceFetcher) { |
164
|
2 |
|
return $this->fetchSlice($queryBuilder, $limit, $offset); |
165
|
|
|
} |
166
|
|
|
|
167
|
7 |
|
$manager = $this->managerRegistry->getManagerForClass($this->objectClass); |
168
|
|
|
$identifierFieldNames = $manager |
169
|
7 |
|
->getClassMetadata($this->objectClass) |
170
|
7 |
|
->getIdentifierFieldNames(); |
171
|
|
|
|
172
|
7 |
|
return $this->sliceFetcher->fetch( |
173
|
7 |
|
$queryBuilder, |
174
|
7 |
|
$limit, |
175
|
7 |
|
$offset, |
176
|
7 |
|
$lastSlice, |
177
|
|
|
$identifierFieldNames |
178
|
7 |
|
); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|