|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brouzie\Sphinxy; |
|
4
|
|
|
|
|
5
|
|
|
use Brouzie\Sphinxy\Indexer\IndexerInterface; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
7
|
|
|
|
|
8
|
|
|
class IndexManager |
|
9
|
|
|
{ |
|
10
|
|
|
protected $conn; |
|
11
|
|
|
|
|
12
|
|
|
protected $container; |
|
13
|
|
|
|
|
14
|
|
|
protected $indexers = array(); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Registry $registry |
|
18
|
|
|
* @param ContainerInterface $container |
|
19
|
|
|
* @param array $indexers |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(Registry $registry, ContainerInterface $container, array $indexers) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->conn = $registry->getConnection(); |
|
24
|
|
|
$this->container = $container; |
|
25
|
|
|
$this->indexers = $indexers; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function reindex($index, $batchSize = 1000, $batchCallback = null, array $rangeCriterias = array()) |
|
29
|
|
|
{ |
|
30
|
|
|
$logger = $this->conn->getLogger(); |
|
31
|
|
|
$this->conn->setLogger(null); |
|
32
|
|
|
|
|
33
|
|
|
$indexer = $this->getIndexer($index); |
|
34
|
|
|
$range = array_replace($indexer->getRangeCriterias(), $rangeCriterias); |
|
35
|
|
|
|
|
36
|
|
|
$idFrom = $range['min']; |
|
37
|
|
|
do { |
|
38
|
|
|
$idTo = $idFrom + $batchSize; |
|
39
|
|
|
if (is_callable($batchCallback)) { |
|
40
|
|
|
call_user_func($batchCallback, array('id_from' => $idFrom, 'id_to' => $idTo, 'min_id' => $range['min'], 'max_id' => $range['max'])); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$items = $indexer->getItemsByInterval($idFrom, $idTo); |
|
44
|
|
|
$this->processItems($index, $indexer, $items); |
|
45
|
|
|
$idFrom = $idTo; |
|
46
|
|
|
|
|
47
|
|
|
} while ($idFrom <= $range['max']); |
|
48
|
|
|
$this->conn->setLogger($logger); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function reindexItems($index, $itemsIds, $batchSize = 100) |
|
52
|
|
|
{ |
|
53
|
|
|
$indexer = $this->getIndexer($index); |
|
54
|
|
|
|
|
55
|
|
|
do { |
|
56
|
|
|
$itemsIdsToProcess = array_splice($itemsIds, 0, $batchSize); |
|
57
|
|
|
$items = $indexer->getItemsByIds($itemsIdsToProcess); |
|
58
|
|
|
$this->processItems($index, $indexer, $items); |
|
59
|
|
|
} while ($itemsIdsToProcess); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function removeItems($index, $itemsIds) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->conn->createQueryBuilder() |
|
65
|
|
|
->delete($this->conn->getEscaper()->quoteIdentifier($index)) |
|
66
|
|
|
->where('id IN :ids') |
|
67
|
|
|
->setParameter('ids', $itemsIds) |
|
68
|
|
|
->execute(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getIndexRange($index) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->conn |
|
74
|
|
|
->createQueryBuilder() |
|
75
|
|
|
->select('MIN(id) AS `min`, MAX(id) AS `max`') |
|
76
|
|
|
->from($this->conn->getEscaper()->quoteIdentifier($index)) |
|
77
|
|
|
->getResult() |
|
78
|
|
|
->getIterator() |
|
79
|
|
|
->current(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function truncate($index) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->conn->executeUpdate(sprintf('TRUNCATE RTINDEX %s', $this->conn->getEscaper()->quoteIdentifier($index))); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $index |
|
89
|
|
|
* |
|
90
|
|
|
* @return IndexerInterface |
|
91
|
|
|
* |
|
92
|
|
|
* @throws \InvalidArgumentException When index not defined |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getIndexer($index) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!isset($this->indexers[$index])) { |
|
97
|
|
|
throw new \InvalidArgumentException('Unknown index'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->container->get($this->indexers[$index]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $index |
|
105
|
|
|
* @param IndexerInterface $indexer |
|
106
|
|
|
* @param $items |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function processItems($index, IndexerInterface $indexer, $items) |
|
109
|
|
|
{ |
|
110
|
|
|
$items = $indexer->processItems($items); |
|
111
|
|
|
if (!count($items)) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$insertQb = $this->conn |
|
116
|
|
|
->createQueryBuilder() |
|
117
|
|
|
->replace($this->conn->getEscaper()->quoteIdentifier($index)); |
|
118
|
|
|
|
|
119
|
|
|
foreach ($items as $item) { |
|
120
|
|
|
$insertQb->addValues( |
|
121
|
|
|
$this->conn->getEscaper()->quoteSetArr( |
|
122
|
|
|
$indexer->serializeItem($item) |
|
123
|
|
|
) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$insertQb->execute(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.