Represents a queue of work to be done to an index.
        public void PartialFlushQueue(int flushLimit)
        {
            if (_forceStop)
            {
                return;
            }

            using (new WriteLockDisposable(_flushLocker))
            {
                if (_forceStop || !Queue.Batches.Any())
                {
                    return;
                }
                using (var worker = CreateTransactionWorker())
                {
                    try
                    {
                        for (int i = 0; i < flushLimit; i++)
                        {
                            if (_forceStop)
                            {
                                break;
                            }

                            IndexModificationBatch batch = null;
                            var success = Queue.Batches.TryDequeue(out batch);
                            if (success)
                            {
                                EnactBatch(worker, batch);
                                _flushCount++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        worker.Commit();
                        if (_flushCount % 50 == 0)
                        {
                            worker.TryOptimizeDeletions();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error <IndexController>("Failed to partially flush the queue: " + ex.Message, ex);
                        worker.TryRollback();
                        throw;
                    }
                }
            }
        }
        protected void FlushQueue()
        {
            if (_forceStop)
            {
                return;
            }
            using (new WriteLockDisposable(_flushLocker))
            {
                if (_forceStop)
                {
                    return;
                }

                LogHelper.TraceIfEnabled <IndexController>("Checking for items in FlushQueue");
                IndexModificationBatch batch = null;
                var success = Queue.Batches.TryDequeue(out batch);

                if (!success)
                {
                    return;
                }

                using (DisposableTimer.TraceDuration <IndexController>("Creating a worker and flushing", "Worker for FlushQueue finished"))
                    using (var worker = CreateTransactionWorker())
                    {
                        try
                        {
                            while (success && !_forceStop)
                            {
                                EnactBatch(worker, batch);
                                _flushCount++;
                                success = Queue.Batches.TryDequeue(out batch);
                            }
                            worker.Commit();
                            if (_flushCount % 50 == 0)
                            {
                                worker.TryOptimizeDeletions();
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error <IndexController>("Failed to flush the queue: " + ex.Message, ex);
                            worker.TryRollback();
                            throw;
                        }
                    }
            }
        }
 private static void EnactBatch(TransactionalIndexWorker worker, IndexModificationBatch batch)
 {
     if (batch == null)
     {
         return;
     }
     try
     {
         foreach (var action in batch.Batch)
         {
             action.Invoke(worker);
         }
     }
     catch (Exception innerEx)
     {
         LogHelper.Error <IndexController>("Failed to perform a batch operation: " + innerEx.Message, innerEx);
         throw;
     }
 }
 private static void EnactBatch(TransactionalIndexWorker worker, IndexModificationBatch batch)
 {
     if (batch == null) return;
     try
     {
         foreach (var action in batch.Batch)
         {
             action.Invoke(worker);
         }
     }
     catch (Exception innerEx)
     {
         LogHelper.Error<IndexController>("Failed to perform a batch operation: " + innerEx.Message, innerEx);
         throw;
     }
 }