private async Task <long> RemoveTracksAsync() { long numberRemovedTracks = 0; var args = new IndexingStatusEventArgs() { IndexingAction = IndexingAction.RemoveTracks, ProgressPercent = 0 }; await Task.Run(() => { try { using (var conn = this.factory.GetConnection()) { conn.BeginTransaction(); // Create a list of folderIDs List <long> folderTrackIDs = conn.Table <FolderTrack>().ToList().Select((t) => t.TrackID).Distinct().ToList(); List <Track> alltracks = conn.Table <Track>().Select((t) => t).ToList(); List <Track> tracksInMissingFolders = alltracks.Select((t) => t).Where(t => !folderTrackIDs.Contains(t.TrackID)).ToList(); List <Track> remainingTracks = new List <Track>(); // Processing tracks in missing folders in bulk first, then checking // existence of the remaining tracks, improves speed of removing tracks. if (tracksInMissingFolders.Count > 0 && tracksInMissingFolders.Count < alltracks.Count) { remainingTracks = alltracks.Except(tracksInMissingFolders).ToList(); } else { remainingTracks = alltracks; } // 1. Process tracks in missing folders // ------------------------------------ if (tracksInMissingFolders.Count > 0) { // Report progress immediately, as there are tracks in missing folders. this.IndexingStatusChanged(args); // Delete foreach (Track trk in tracksInMissingFolders) { conn.Delete(trk); } numberRemovedTracks += tracksInMissingFolders.Count; } // 2. Process remaining tracks // --------------------------- if (remainingTracks.Count > 0) { foreach (Track trk in remainingTracks) { // If a remaining track doesn't exist on disk, delete it from the collection. if (!System.IO.File.Exists(trk.Path)) { conn.Delete(trk); numberRemovedTracks += 1; // Report progress as soon as the first track was removed. // This is indeterminate progress. No need to sent it multiple times. if (numberRemovedTracks == 1) { this.IndexingStatusChanged(args); } } } } conn.Commit(); } } catch (Exception ex) { LogClient.Error("There was a problem while removing Tracks. Exception: {0}", ex.Message); } }); return(numberRemovedTracks); }
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> AddTracksAsync() { long numberAddedTracks = 0; var args = new IndexingStatusEventArgs() { IndexingAction = IndexingAction.AddTracks, ProgressPercent = 0 }; await Task.Run(() => { try { long currentValue = 0; long totalValue = this.newDiskPaths.Count; long saveItemCount = IndexerUtils.CalculateSaveItemCount(totalValue); long unsavedItemCount = 0; int lastPercent = 0; using (var conn = this.factory.GetConnection()) { conn.BeginTransaction(); foreach (FolderPathInfo newDiskPath in this.newDiskPaths) { Track diskTrack = Track.CreateDefault(newDiskPath.Path); try { this.ProcessTrack(diskTrack, conn); if (!this.cache.HasCachedTrack(ref diskTrack)) { conn.Insert(diskTrack); this.cache.AddTrack(diskTrack); numberAddedTracks += 1; unsavedItemCount += 1; } conn.Insert(new FolderTrack(newDiskPath.FolderId, diskTrack.TrackID)); // Intermediate save to the database if 20% is reached if (unsavedItemCount == saveItemCount) { unsavedItemCount = 0; conn.Commit(); // Intermediate save conn.BeginTransaction(); } } catch (Exception ex) { LogClient.Error("There was a problem while adding Track with path='{0}'. Exception: {1}", diskTrack.Path, ex.Message); } currentValue += 1; int percent = IndexerUtils.CalculatePercent(currentValue, totalValue); // Report progress if at least 1 track is added OR when the progress // interval has been exceeded OR the maximum has been reached. bool mustReportProgress = numberAddedTracks == 1 || percent >= lastPercent + 5 || percent == 100; if (mustReportProgress) { lastPercent = percent; args.ProgressCurrent = numberAddedTracks; args.ProgressPercent = percent; this.IndexingStatusChanged(args); } } conn.Commit(); // Final save } } catch (Exception ex) { LogClient.Error("There was a problem while adding Tracks. Exception: {0}", ex.Message); } }); return(numberAddedTracks); }