diff --git a/src/NzbDrone.Common/Crypto/HashConverter.cs b/src/NzbDrone.Common/Crypto/HashConverter.cs new file mode 100644 index 000000000..3bd521f16 --- /dev/null +++ b/src/NzbDrone.Common/Crypto/HashConverter.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace NzbDrone.Common.Crypto +{ + public static class HashConverter + { + private static SHA1 HashAlgorithm = SHA1.Create(); + + public static int GetHashInt31(string target) + { + var hash = HashAlgorithm.ComputeHash(Encoding.Default.GetBytes(target)); + + return BitConverter.ToInt32(hash, 0) & 0x7fffffff; + } + } +} diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index 246b23919..d7966efc0 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -132,6 +132,7 @@ + diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 046d8238b..1074dbc46 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -236,6 +236,7 @@ + diff --git a/src/NzbDrone.Core.Test/QueueTests/QueueServiceFixture.cs b/src/NzbDrone.Core.Test/QueueTests/QueueServiceFixture.cs new file mode 100644 index 000000000..069ef9f2c --- /dev/null +++ b/src/NzbDrone.Core.Test/QueueTests/QueueServiceFixture.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using NzbDrone.Core.Download.TrackedDownloads; +using NzbDrone.Core.Queue; +using NzbDrone.Core.Test.Framework; +using FizzWare.NBuilder; +using FluentAssertions; +using NzbDrone.Core.Tv; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.Test.QueueTests +{ + [TestFixture] + public class QueueServiceFixture : CoreTest + { + private List _trackedDownloads; + + [SetUp] + public void SetUp() + { + var downloadItem = Builder.CreateNew() + .With(v => v.RemainingTime = TimeSpan.FromSeconds(10)) + .Build(); + + var series = Builder.CreateNew() + .Build(); + + var episodes = Builder.CreateListOfSize(3) + .All() + .With(e => e.SeriesId = series.Id) + .Build(); + + var remoteEpisode = Builder.CreateNew() + .With(r => r.Series = series) + .With(r => r.Episodes = new List(episodes)) + .With(r => r.ParsedEpisodeInfo = new ParsedEpisodeInfo()) + .Build(); + + _trackedDownloads = Builder.CreateListOfSize(1) + .All() + .With(v => v.DownloadItem = downloadItem) + .With(v => v.RemoteEpisode = remoteEpisode) + .Build() + .ToList(); + } + + [Test] + public void queue_items_should_have_id() + { + Subject.Handle(new TrackedDownloadRefreshedEvent(_trackedDownloads)); + + var queue = Subject.GetQueue(); + + queue.Should().HaveCount(3); + + queue.All(v => v.Id > 0).Should().BeTrue(); + + var distinct = queue.Select(v => v.Id).Distinct().ToArray(); + + distinct.Should().HaveCount(3); + } + } +} diff --git a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs index c879f1b23..8108b1c98 100644 --- a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs +++ b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Crypto; using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.DecisionEngine; @@ -101,7 +102,7 @@ namespace NzbDrone.Core.Download.Pending var queue = new Queue.Queue { - Id = episode.Id ^ (pendingRelease.Id << 16), + Id = HashConverter.GetHashInt31(string.Format("pending-{0}-ep{1}", pendingRelease.Id, episode.Id)), Series = pendingRelease.RemoteEpisode.Series, Episode = episode, Quality = pendingRelease.RemoteEpisode.ParsedEpisodeInfo.Quality, diff --git a/src/NzbDrone.Core/Queue/QueueService.cs b/src/NzbDrone.Core/Queue/QueueService.cs index 525c5d66e..4ee499f9d 100644 --- a/src/NzbDrone.Core/Queue/QueueService.cs +++ b/src/NzbDrone.Core/Queue/QueueService.cs @@ -1,6 +1,7 @@ using System; -using System.Linq; using System.Collections.Generic; +using System.Linq; +using NzbDrone.Common.Crypto; using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.Messaging.Events; @@ -40,13 +41,13 @@ namespace NzbDrone.Core.Queue _eventAggregator.PublishEvent(new QueueUpdatedEvent()); } - private static IEnumerable MapQueue(TrackedDownload trackedDownload) + private IEnumerable MapQueue(TrackedDownload trackedDownload) { foreach (var episode in trackedDownload.RemoteEpisode.Episodes) { var queue = new Queue { - Id = episode.Id ^ (trackedDownload.DownloadItem.DownloadId.GetHashCode() << 16), + Id = HashConverter.GetHashInt31(string.Format("trackedDownload-{0}-ep{1}", trackedDownload.DownloadItem.DownloadId, episode.Id)), Series = trackedDownload.RemoteEpisode.Series, Episode = episode, Quality = trackedDownload.RemoteEpisode.ParsedEpisodeInfo.Quality,