forked from solariumphp/solarium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferedDeleteLite.php
207 lines (179 loc) · 5.32 KB
/
BufferedDeleteLite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/*
* This file is part of the Solarium package.
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
namespace Solarium\Plugin\BufferedDelete;
use Solarium\Exception\InvalidArgumentException;
use Solarium\Exception\RuntimeException;
use Solarium\Plugin\AbstractBufferedUpdate\AbstractBufferedUpdate;
use Solarium\Plugin\BufferedDelete\Delete\Id as DeleteById;
use Solarium\Plugin\BufferedDelete\Delete\Query as DeleteQuery;
use Solarium\QueryType\Update\Query\Command\Delete as DeleteCommand;
use Solarium\QueryType\Update\Result as UpdateResult;
/**
* Buffered delete lite plugin.
*
* If you need to delete a big number of documents in Solr it's much more efficient to do so in batches.
* This plugin makes this as easy as possible.
*
* Unlike {@see BufferedDelete}, this plugin doesn't dispatch any events.
*/
class BufferedDeleteLite extends AbstractBufferedUpdate
{
/**
* Buffered document ids and/or queries to delete.
*
* @var AbstractDelete[]
*/
protected $buffer = [];
/**
* Add a document id to delete.
*
* @param int|string $id
*
* @return self Provides fluent interface
*/
public function addDeleteById($id): self
{
$this->buffer[] = new DeleteById($id);
if (\count($this->buffer) === $this->options['buffersize']) {
$this->flush();
}
return $this;
}
/**
* Add multiple document ids to delete.
*
* @param (int|string)[] $ids
*
* @return self Provides fluent interface
*/
public function addDeleteByIds(array $ids): self
{
foreach ($ids as $id) {
$this->addDeleteById($id);
}
return $this;
}
/**
* Add a query to delete matching documents.
*
* @param string $query
*
* @return self Provides fluent interface
*/
public function addDeleteQuery(string $query): self
{
$this->buffer[] = new DeleteQuery($query);
if (\count($this->buffer) === $this->options['buffersize']) {
$this->flush();
}
return $this;
}
/**
* Add multiple queries to delete matching documents.
*
* @param string[] $queries
*
* @return self Provides fluent interface
*/
public function addDeleteQueries(array $queries): self
{
foreach ($queries as $query) {
$this->addDeleteQuery($query);
}
return $this;
}
/**
* Get all deletes currently in the buffer.
*
* Any previously flushed deletes will not be included!
*
* @return AbstractDelete[]
*/
public function getDeletes(): array
{
return $this->buffer;
}
/**
* Set the request format for the updates.
*
* Use UpdateQuery::REQUEST_FORMAT_JSON or UpdateQuery::REQUEST_FORMAT_XML as value.
*
* @param string $requestFormat
*
* @throws InvalidArgumentException
*
* @return self Provides fluent interface
*/
public function setRequestFormat(string $requestFormat): self
{
if ($this->updateQuery::REQUEST_FORMAT_CBOR === strtolower($requestFormat)) {
throw new InvalidArgumentException('Unsupported request format: CBOR can only be used to add documents');
}
return parent::setRequestFormat($requestFormat);
}
/**
* Flush any buffered deletes to Solr.
*
* @return UpdateResult|false
*/
public function flush()
{
if (0 === \count($this->buffer)) {
// nothing to do
return false;
}
$this->addBufferToQuery($this->buffer);
$result = $this->client->update($this->updateQuery, $this->getEndpoint());
$this->clear();
return $result;
}
/**
* Commit changes.
*
* Any remaining deletes in the buffer will also be flushed
*
* @param bool|null $softCommit
* @param bool|null $waitSearcher
* @param bool|null $expungeDeletes
*
* @return UpdateResult
*/
public function commit(?bool $softCommit = null, ?bool $waitSearcher = null, ?bool $expungeDeletes = null): UpdateResult
{
$this->addBufferToQuery($this->buffer);
$this->updateQuery->addCommit($softCommit, $waitSearcher, $expungeDeletes);
$result = $this->client->update($this->updateQuery, $this->getEndpoint());
$this->clear();
return $result;
}
/**
* Add all deletes from the buffer as commands to the update query.
*
* @param AbstractDelete[] $buffer
*/
protected function addBufferToQuery(array $buffer): void
{
$it = new \ArrayIterator($buffer);
$command = new DeleteCommand();
while ($it->valid()) {
$delete = $it->current();
switch ($delete->getType()) {
case AbstractDelete::TYPE_ID:
$command->addId($delete->getId());
break;
case AbstractDelete::TYPE_QUERY:
$command->addQuery($delete->getQuery());
break;
default:
throw new RuntimeException('Unsupported delete type in buffer');
}
$it->next();
}
$this->updateQuery->add(null, $command);
}
}