class Indexes {

    public static void awaitResampling( SchemaRead schemaRead, long timeout ) throws TimeoutException
    {
        final Iterator<IndexReference> indexes = schemaRead.indexesGetAll();
        final Register.DoubleLongRegister register = Registers.newDoubleLongRegister();
        final long t0 = System.currentTimeMillis();
        final long timeoutMillis = 1000 * timeout;

        while ( indexes.hasNext() )
        {
            final IndexReference index = indexes.next();
            try
            {
                long readUpdates = readUpdates( index, schemaRead, register );
                long updateCount = readUpdates;
                boolean hasTimedOut = false;

                while ( updateCount > 0 && updateCount <= readUpdates && !hasTimedOut )
                {
                    Thread.sleep( 10 );
                    hasTimedOut = System.currentTimeMillis() - t0 >= timeoutMillis;
                    updateCount = Math.max( updateCount, readUpdates );
                    readUpdates = readUpdates( index, schemaRead, register );
                }

                if ( hasTimedOut )
                {
                    throw new TimeoutException( String.format( "Indexes were not resampled within %s %s", timeout, TimeUnit.SECONDS ) );
                }
            }
            catch ( InterruptedException e )
            {
                Thread.currentThread().interrupt();
                throw new RuntimeException( e );
            }
            catch ( IndexNotFoundKernelException e )
            {
                throw new ConcurrentModificationException( "Index was dropped while awaiting resampling", e );
            }
        }
    }

}