Пример #1
0
        private async Task <long> UpdateTracksAsync()
        {
            long numberUpdatedTracks = 0;

            var args = new IndexingStatusEventArgs()
            {
                IndexingAction  = IndexingAction.UpdateTracks,
                ProgressPercent = 0
            };

            await Task.Run(() =>
            {
                try
                {
                    using (var conn = this.factory.GetConnection())
                    {
                        conn.BeginTransaction();

                        List <Track> alltracks = conn.Table <Track>().Select((t) => t).ToList();

                        long currentValue = 0;
                        long totalValue   = alltracks.Count;
                        int lastPercent   = 0;

                        foreach (Track dbTrack in alltracks)
                        {
                            try
                            {
                                if (IndexerUtils.IsTrackOutdated(dbTrack) | dbTrack.NeedsIndexing == 1)
                                {
                                    this.ProcessTrack(dbTrack, conn);
                                    conn.Update(dbTrack);
                                    numberUpdatedTracks += 1;
                                }
                            }
                            catch (Exception ex)
                            {
                                LogClient.Error("There was a problem while updating Track with path='{0}'. Exception: {1}", dbTrack.Path, ex.Message);
                            }

                            currentValue += 1;

                            int percent = IndexerUtils.CalculatePercent(currentValue, totalValue);

                            // Report progress if at least 1 track is updated OR when the progress
                            // interval has been exceeded OR the maximum has been reached.
                            bool mustReportProgress = numberUpdatedTracks == 1 || percent >= lastPercent + 5 || percent == 100;

                            if (mustReportProgress)
                            {
                                lastPercent          = percent;
                                args.ProgressPercent = percent;
                                this.IndexingStatusChanged(args);
                            }
                        }

                        conn.Commit();
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Error("There was a problem while updating Tracks. Exception: {0}", ex.Message);
                }
            });

            return(numberUpdatedTracks);
        }
        private async Task <long> UpdateTracksAsync()
        {
            long numberUpdatedTracks = 0;

            var args = new IndexingStatusEventArgs()
            {
                IndexingAction  = IndexingAction.UpdateTracks,
                ProgressPercent = 0
            };

            await Task.Run(() =>
            {
                try
                {
                    using (var conn = this.sqliteConnectionFactory.GetConnection())
                    {
                        LogClient.Info("Starting updating tracks");

                        List <Track> alltracks = conn.Table <Track>().Select((t) => t).ToList();

                        long currentValue = 0;
                        long totalValue   = alltracks.Count;
                        int lastPercent   = 0;

                        int batchSize = IndexerUtils.GetParallelBatchSize(alltracks.Count);

                        Parallel.ForEach(
                            Partitioner.Create(0, alltracks.Count, batchSize),
                            new ParallelOptions
                        {
                            CancellationToken      = cancellationService.CancellationToken,
                            MaxDegreeOfParallelism = Environment.ProcessorCount
                        },
                            range =>
                        {
                            for (int i = range.Item1; i < range.Item2 && cancellationService.KeepRunning; ++i)
                            {
                                var dbTrack        = alltracks[i];
                                var tracksToUpdate = new List <Track>();

                                try
                                {
                                    if (IndexerUtils.IsTrackOutdated(dbTrack) || dbTrack.NeedsIndexing == 1)
                                    {
                                        this.ProcessTrack(dbTrack);
                                        tracksToUpdate.Add(dbTrack);
                                        conn.Update(dbTrack);
                                        numberUpdatedTracks += 1;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    LogClient.Error("There was a problem while updating Track with path='{0}'. Exception: {1}", dbTrack.Path, ex.Message);
                                }

                                lock (conn)
                                {
                                    if (tracksToUpdate.Count > 0)
                                    {
                                        conn.BeginTransaction();
                                        conn.UpdateAll(tracksToUpdate);
                                        conn.Commit();
                                    }

                                    currentValue += (range.Item2 - range.Item1);

                                    int percent = IndexerUtils.CalculatePercent(currentValue, totalValue);

                                    lastPercent          = percent;
                                    args.ProgressPercent = percent;
                                }
                            }
                        });

                        LogClient.Info("Finished updating tracks");
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Error("There was a problem while updating Tracks. Exception: {0}", ex.Message);
                }
            });

            return(numberUpdatedTracks);
        }