예제 #1
0
        internal async Task SynchronizeIndexWithFileSystemAsync(IndexDifferences diff, CancellationToken cancellationToken)
        {
            if (diff.IsEmpty)
            {
                return;
            }

            var tasks = new ConcurrentQueue <Task>();

            Log.Info(string.Format("Updates to process: {0} packages added, {1} packages updated, {2} packages removed.", diff.NewPackages.Count(), diff.ModifiedPackages.Count(), diff.MissingPackages.Count()));

            foreach (var path in diff.MissingPackages)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var package = new LucenePackage(FileSystem)
                {
                    Path = path
                };
                var update = new Update(package, UpdateType.RemoveByPath);
                pendingUpdates.Add(update, cancellationToken);
                tasks.Enqueue(update.Task);
            }

            var pathsToIndex    = diff.NewPackages.Union(diff.ModifiedPackages).OrderBy(p => p).ToArray();
            var packagesToIndex = pathsToIndex.Length;
            var i = 0;

            Parallel.ForEach(pathsToIndex, new ParallelOptions {
                MaxDegreeOfParallelism = 4, CancellationToken = cancellationToken
            }, (p, s) =>
            {
                UpdateSynchronizationStatus(SynchronizationState.Indexing, Interlocked.Increment(ref i),
                                            packagesToIndex);
                tasks.Enqueue(SynchronizePackage(p, cancellationToken));
            });

            var task = TaskEx.WhenAll(tasks.ToArray());

            try
            {
                await task;
            }
            finally
            {
                if (task.IsFaulted && task.Exception.InnerExceptions.Count > 1)
                {
                    throw new AggregateException(task.Exception.InnerExceptions);
                }
            }
        }
예제 #2
0
        internal void SynchronizeIndexWithFileSystem(IndexDifferences diff, CancellationToken cancellationToken)
        {
            if (diff.IsEmpty) return;

            var tasks = new ConcurrentQueue<Task>();

            Log.Info(string.Format("Updates to process: {0} packages added, {1} packages updated, {2} packages removed.", diff.NewPackages.Count(), diff.ModifiedPackages.Count(), diff.MissingPackages.Count()));

            foreach (var path in diff.MissingPackages)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var package = new LucenePackage(FileSystem) { Path = path };
                var update = new Update(package, UpdateType.RemoveByPath);
                pendingUpdates.Add(update);
                tasks.Enqueue(update.Task);
            }
            
            var pathsToIndex = diff.NewPackages.Union(diff.ModifiedPackages).OrderBy(p => p).ToArray();
            var packagesToIndex = pathsToIndex.Length;
            var i = 0;

            Parallel.ForEach(pathsToIndex, new ParallelOptions {MaxDegreeOfParallelism = 4, CancellationToken = cancellationToken}, (p, s) =>
                {
                    UpdateSynchronizationStatus(SynchronizationState.Indexing, Interlocked.Increment(ref i),
                                                packagesToIndex);
                    tasks.Enqueue(SynchronizePackage(p));
                });

            Task.WaitAll(tasks.ToArray(), cancellationToken);
        }