@@ -10,6 +10,8 @@ class IndexRotator
10
10
{
11
11
const INDEX_NAME_CONFIG = '.%s_configuration ' ;
12
12
const TYPE_CONFIGURATION = 'configuration ' ;
13
+ const SECONDARY_NAME_ONLY = 0 ;
14
+ const SECONDARY_INCLUDE_ID = 1 ;
13
15
const PRIMARY_ID = 'primary ' ;
14
16
const RETRY_TIME_COPY = 500000 ;
15
17
const MAX_RETRY_COUNT = 5 ;
@@ -158,9 +160,10 @@ public function copyPrimaryIndexToSecondary($retryCount = 0)
158
160
* Note, if date is not provided, it will find all secondary indexes.
159
161
*
160
162
* @param string $olderThan
163
+ * @param integer $disposition Controls the return style (defaults to name only)
161
164
* @return array
162
165
*/
163
- public function getSecondaryIndices (\DateTime $ olderThan = null )
166
+ public function getSecondaryIndices (\DateTime $ olderThan = null , $ disposition = self :: SECONDARY_NAME_ONLY )
164
167
{
165
168
if ($ olderThan === null ) {
166
169
$ olderThan = new \DateTime ();
@@ -199,10 +202,17 @@ public function getSecondaryIndices(\DateTime $olderThan = null)
199
202
if ($ results ['hits ' ]['total ' ] == 0 ) {
200
203
return [];
201
204
}
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 ' ]);
206
216
}
207
217
208
218
/**
@@ -216,17 +226,39 @@ public function getSecondaryIndices(\DateTime $olderThan = null)
216
226
public function deleteSecondaryIndices (\DateTime $ olderThan = null )
217
227
{
218
228
$ 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
+ ];
222
235
$ this ->logger ->debug ('Deleted secondary index. ' , compact ('indexToDelete ' ));
223
236
} else {
237
+ $ results [$ indexToDelete ] = [
238
+ 'index ' => null ,
239
+ 'config ' => $ this ->deleteConfigurationEntry ($ indexToDelete ['configuration_id ' ])
240
+ ];
224
241
$ this ->logger ->debug ('Index not found to delete. ' , compact ('indexToDelete ' ));
225
242
}
226
243
}
227
244
return $ results ;
228
245
}
229
246
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
+
230
262
/**
231
263
* Create the index needed to store the primary index name.
232
264
*
0 commit comments