@@ -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 *
0 commit comments