@@ -75,6 +75,7 @@ public class ReIndexServiceImpl implements ReIndexService {
7575 private static final String MAP_COUNT_KEY = "count" ;
7676 private static final String MAP_STATUS_KEY = "status" ;
7777 private static final String MAP_UPDATED_KEY = "lastUpdated" ;
78+ private static final String MAP_SEPARATOR = "|||" ;
7879
7980
8081 private final AllApplicationsObservable allApplicationsObservable ;
@@ -140,7 +141,9 @@ public ReIndexStatus rebuildIndex( final ReIndexRequestBuilder reIndexRequestBui
140141
141142 // create an observable that loads a batch to be indexed
142143
143- if (reIndexRequestBuilder .getCollectionName ().isPresent ()) {
144+ final boolean isForCollection = reIndexRequestBuilder .getCollectionName ().isPresent ();
145+
146+ if (isForCollection ) {
144147
145148 String collectionName = InflectionUtils .pluralize (
146149 CpNamingUtils .getNameFromEdgeType (reIndexRequestBuilder .getCollectionName ().get () ));
@@ -175,12 +178,36 @@ public ReIndexStatus rebuildIndex( final ReIndexRequestBuilder reIndexRequestBui
175178 if ( edgeScopes .size () > 0 ) {
176179 writeCursorState (jobId , edgeScopes .get (edgeScopes .size () - 1 ));
177180 }
178- writeStateMeta ( jobId , Status .INPROGRESS , count .get (), System .currentTimeMillis () ); })
179- .doOnCompleted (() -> writeStateMeta ( jobId , Status .COMPLETE , count .get (), System .currentTimeMillis () ))
181+ if ( isForCollection ){
182+ writeStateMetaForCollection (
183+ appId .get ().getApplication ().getUuid ().toString (),
184+ reIndexRequestBuilder .getCollectionName ().get (),
185+ Status .INPROGRESS , count .get (),
186+ System .currentTimeMillis () );
187+ }else {
188+ writeStateMeta ( jobId , Status .INPROGRESS , count .get (), System .currentTimeMillis () );
189+ }
190+ })
191+ .doOnCompleted (() ->{
192+ if ( isForCollection ){
193+ writeStateMetaForCollection (
194+ appId .get ().getApplication ().getUuid ().toString (),
195+ reIndexRequestBuilder .getCollectionName ().get (),
196+ Status .COMPLETE , count .get (),
197+ System .currentTimeMillis () );
198+ }else {
199+ writeStateMeta (jobId , Status .COMPLETE , count .get (), System .currentTimeMillis ());
200+ }
201+ })
180202 .subscribeOn ( Schedulers .io () ).subscribe ();
181203
204+ if (isForCollection ){
205+ return new ReIndexStatus ( "" , Status .STARTED , 0 , 0 , reIndexRequestBuilder .getCollectionName ().get () );
206+
207+ }
208+
182209
183- return new ReIndexStatus ( jobId , Status .STARTED , 0 , 0 );
210+ return new ReIndexStatus ( jobId , Status .STARTED , 0 , 0 , "" );
184211 }
185212
186213
@@ -196,38 +223,15 @@ public ReIndexStatus getStatus( final String jobId ) {
196223 return getIndexResponse ( jobId );
197224 }
198225
199-
200- /**
201- * Simple collector that counts state, then flushed every time a buffer is provided. Writes final state when complete
202- */
203- private class FlushingCollector {
204-
205- private final String jobId ;
206- private long count ;
207-
208-
209- private FlushingCollector ( final String jobId ) {
210- this .jobId = jobId ;
211- }
212-
213-
214- public void flushBuffer ( final List <EdgeScope > buffer ) {
215- count += buffer .size ();
216-
217- //write our cursor state
218- if ( buffer .size () > 0 ) {
219- writeCursorState ( jobId , buffer .get ( buffer .size () - 1 ) );
220- }
221-
222- writeStateMeta ( jobId , Status .INPROGRESS , count , System .currentTimeMillis () );
223- }
224-
225- public void complete (){
226- writeStateMeta ( jobId , Status .COMPLETE , count , System .currentTimeMillis () );
227- }
226+ @ Override
227+ public ReIndexStatus getStatusForCollection ( final String appIdString , final String collectionName ) {
228+ Preconditions .checkNotNull ( collectionName , "appIdString must not be null" );
229+ Preconditions .checkNotNull ( collectionName , "collectionName must not be null" );
230+ return getIndexResponseForCollection ( appIdString , collectionName );
228231 }
229232
230233
234+
231235 /**
232236 * Get the resume edge scope
233237 *
@@ -346,15 +350,47 @@ private ReIndexStatus getIndexResponse( final String jobId ) {
346350 final String stringStatus = mapManager .getString ( jobId +MAP_STATUS_KEY );
347351
348352 if (stringStatus == null ){
349- return new ReIndexStatus ( jobId , Status .UNKNOWN , 0 , 0 );
353+ return new ReIndexStatus ( jobId , Status .UNKNOWN , 0 , 0 , "" );
350354 }
351355
352356 final Status status = Status .valueOf ( stringStatus );
353357
354358 final long processedCount = mapManager .getLong ( jobId + MAP_COUNT_KEY );
355359 final long lastUpdated = mapManager .getLong ( jobId + MAP_UPDATED_KEY );
356360
357- return new ReIndexStatus ( jobId , status , processedCount , lastUpdated );
361+ return new ReIndexStatus ( jobId , status , processedCount , lastUpdated , "" );
362+ }
363+
364+
365+ private void writeStateMetaForCollection (final String appIdString , final String collectionName ,
366+ final Status status , final long processedCount , final long lastUpdated ) {
367+
368+ if (logger .isDebugEnabled ()) {
369+ logger .debug ( "Flushing state for collection {}, status {}, processedCount {}, lastUpdated {}" ,
370+ collectionName , status , processedCount , lastUpdated );
371+ }
372+
373+ mapManager .putString ( appIdString + MAP_SEPARATOR + collectionName + MAP_STATUS_KEY , status .name () );
374+ mapManager .putLong ( appIdString + MAP_SEPARATOR + collectionName + MAP_COUNT_KEY , processedCount );
375+ mapManager .putLong ( appIdString + MAP_SEPARATOR + collectionName + MAP_UPDATED_KEY , lastUpdated );
376+ }
377+
378+
379+ private ReIndexStatus getIndexResponseForCollection ( final String appIdString , final String collectionName ) {
380+
381+ final String stringStatus =
382+ mapManager .getString ( appIdString + MAP_SEPARATOR + collectionName + MAP_STATUS_KEY );
383+
384+ if (stringStatus == null ){
385+ return new ReIndexStatus ( "" , Status .UNKNOWN , 0 , 0 , collectionName );
386+ }
387+
388+ final Status status = Status .valueOf ( stringStatus );
389+
390+ final long processedCount = mapManager .getLong ( appIdString + MAP_SEPARATOR + collectionName + MAP_COUNT_KEY );
391+ final long lastUpdated = mapManager .getLong ( appIdString + MAP_SEPARATOR + collectionName + MAP_UPDATED_KEY );
392+
393+ return new ReIndexStatus ( "" , status , processedCount , lastUpdated , collectionName );
358394 }
359395}
360396
0 commit comments