Skip to content

Commit 1e6a4d9

Browse files
committed
Include new secondary index output that includes ID.
1 parent 7e2050a commit 1e6a4d9

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

src/IndexRotator.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class IndexRotator
1010
{
1111
const INDEX_NAME_CONFIG = '.%s_configuration';
1212
const TYPE_CONFIGURATION = 'configuration';
13+
const SECONDARY_NAME_ONLY = 0;
14+
const SECONDARY_INCLUDE_ID = 1;
1315
const PRIMARY_ID = 'primary';
1416
const RETRY_TIME_COPY = 500000;
1517
const MAX_RETRY_COUNT = 5;
@@ -158,9 +160,10 @@ public function copyPrimaryIndexToSecondary($retryCount = 0)
158160
* Note, if date is not provided, it will find all secondary indexes.
159161
*
160162
* @param string $olderThan
163+
* @param integer $disposition Controls the return style (defaults to name only)
161164
* @return array
162165
*/
163-
public function getSecondaryIndices(\DateTime $olderThan = null)
166+
public function getSecondaryIndices(\DateTime $olderThan = null, $disposition = self::SECONDARY_NAME_ONLY)
164167
{
165168
if ($olderThan === null) {
166169
$olderThan = new \DateTime();
@@ -199,10 +202,17 @@ public function getSecondaryIndices(\DateTime $olderThan = null)
199202
if ($results['hits']['total'] == 0) {
200203
return [];
201204
}
202-
return array_map(function($entry) {
203-
return $entry['_source']['name'];
204-
}, $results['hits']['hits']);
205-
return $results['hits']['total'] > 0 ? array_column($results['hits']['hits'], '_source') : [];
205+
$mapper = $disposition === static::SECONDARY_INCLUDE_ID ?
206+
function($entry) {
207+
return [
208+
'index' => $entry['_source']['name'],
209+
'configuration_id' => $entry['_id']
210+
];
211+
} :
212+
function($entry) {
213+
return $entry['_source']['name'];
214+
};
215+
return array_map($mapper, $results['hits']['hits']);
206216
}
207217

208218
/**
@@ -216,17 +226,39 @@ public function getSecondaryIndices(\DateTime $olderThan = null)
216226
public function deleteSecondaryIndices(\DateTime $olderThan = null)
217227
{
218228
$results = [];
219-
foreach ($this->getSecondaryIndices($olderThan) as $indexToDelete) {
220-
if ($this->engine->indices()->exists(['index' => $indexToDelete])) {
221-
$results[$indexToDelete] = $this->engine->indices()->delete(['index' => $indexToDelete]);
229+
foreach ($this->getSecondaryIndices($olderThan, static::SECONDARY_INCLUDE_ID) as $indexToDelete) {
230+
if ($this->engine->indices()->exists(['index' => $indexToDelete['index']])) {
231+
$results[$indexToDelete['index']] = [
232+
'index' => $this->engine->indices()->delete(['index' => $indexToDelete['index']]),
233+
'config' => $this->deleteConfigurationEntry($indexToDelete['configuration_id'])
234+
];
222235
$this->logger->debug('Deleted secondary index.', compact('indexToDelete'));
223236
} else {
237+
$results[$indexToDelete] = [
238+
'index' => null,
239+
'config' => $this->deleteConfigurationEntry($indexToDelete['configuration_id'])
240+
];
224241
$this->logger->debug('Index not found to delete.', compact('indexToDelete'));
225242
}
226243
}
227244
return $results;
228245
}
229246

247+
/**
248+
* Delete an entry from the configuration index.
249+
*
250+
* @param string $id
251+
* @return array
252+
*/
253+
private function deleteConfigurationEntry($id)
254+
{
255+
return $this->engine->delete([
256+
'index' => $this->configurationIndexName,
257+
'type' => static::TYPE_CONFIGURATION,
258+
'id' => $id
259+
]);
260+
}
261+
230262
/**
231263
* Create the index needed to store the primary index name.
232264
*

tests/IndexRotatorTest.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,41 @@ public function testCopyPrimaryIndexToSecondary()
9393
/**
9494
* @dataProvider secondaryIndexConditionProvider
9595
*/
96-
public function testGetSecondaryIndices($olderThan, $expectedIndices) {
96+
public function testGetSecondaryIndices($olderThan, $expectedIndices)
97+
{
9798
$results = $this->indexRotator->getSecondaryIndices($olderThan);
9899
$this->assertEmpty(array_diff($results, $expectedIndices));
99100
}
100101

101102
/**
102103
* @dataProvider secondaryIndexConditionProvider
103104
*/
104-
public function testDeleteSecondaryIndices($olderThan, $expectedToDelete) {
105+
public function testGetSecondaryIndicesIncludeIds($olderThan, $expectedIndices, $expectedConfigurationIds)
106+
{
107+
$results = $this->indexRotator->getSecondaryIndices($olderThan, IndexRotator::SECONDARY_INCLUDE_ID);
108+
$this->assertEmpty(array_diff(array_column($results, 'index'), $expectedIndices));
109+
$this->assertEmpty(array_diff(array_column($results, 'configuration_id'), $expectedConfigurationIds));
110+
}
111+
112+
/**
113+
* @dataProvider secondaryIndexConditionProvider
114+
*/
115+
public function testDeleteSecondaryIndices($olderThan, $expectedToDelete)
116+
{
105117
$results = $this->indexRotator->deleteSecondaryIndices($olderThan);
106118
$this->assertEmpty(array_diff(array_keys($results), $expectedToDelete));
107119
foreach ($results as $result) {
108-
$this->assertEquals(['acknowledged' => true], $result);
120+
$this->assertEquals(['acknowledged' => true], $result['index']);
121+
$this->assertTrue($result['config']['found']);
109122
}
110123
}
111124

112-
public function secondaryIndexConditionProvider() {
125+
public function secondaryIndexConditionProvider()
126+
{
113127
return [
114-
'all' => [null, ['some_index_3', 'some_index_2']],
115-
'older than 2015-02-01' => [new \DateTime('2015-02-01'), ['some_index_2']],
116-
'older than 2015-01-01' => [new \DateTime('2015-01-01'), []]
128+
'all' => [null, ['some_index_3', 'some_index_2'], ['somesecondary2', 'somesecondary1']],
129+
'older than 2015-02-01' => [new \DateTime('2015-02-01'), ['some_index_2'], ['somesecondary1']],
130+
'older than 2015-01-01' => [new \DateTime('2015-01-01'), [], []]
117131
];
118132
}
119133
}

0 commit comments

Comments
 (0)