Example #1
0
        public async Task DeleteHashBinsAndSubFingerprintsOnTrackDelete()
        {
            var tagInfo  = GetTagInfo();
            var track    = new TrackInfo(tagInfo.ISRC, tagInfo.Title, tagInfo.Artist);
            var hashData = await FingerprintCommandBuilder.Instance
                           .BuildFingerprintCommand()
                           .From(GetAudioSamples())
                           .WithFingerprintConfig(config =>
            {
                config.Stride = new StaticStride(0);
                return(config);
            })
                           .UsingServices(audioService)
                           .Hash();

            var modelReferenceTracker = new UIntModelReferenceTracker();

            var(trackData, subFingerprintData) = modelReferenceTracker.AssignModelReferences(track, hashData);
            trackDao.InsertTrack(trackData);
            subFingerprintDao.InsertSubFingerprints(subFingerprintData);

            var actualTrack = trackDao.ReadTrackById(tagInfo.ISRC);

            Assert.IsNotNull(actualTrack);

            // Act
            int modifiedRows = trackDao.DeleteTrack(trackData.TrackReference) +
                               subFingerprintDao.DeleteSubFingerprintsByTrackReference(trackData.TrackReference);

            Assert.IsNull(trackDao.ReadTrackById(tagInfo.ISRC));
            Assert.IsFalse(subFingerprintDao.ReadHashedFingerprintsByTrackReference(actualTrack.TrackReference).Any());
            Assert.AreEqual(1 + hashData.Count + 25 * hashData.Count, modifiedRows);
        }
        public async Task ReadByTrackGroupIdWorksAsExpectedTest()
        {
            var modelReferenceTracker = new UIntModelReferenceTracker();
            var firstTrack            = new TrackInfo("id-1", string.Empty, string.Empty, new Dictionary <string, string> {
                { "group-id", "first-group-id" }
            }, MediaType.Audio);
            var hashedFingerprintsForFirstTrack = await GetHashedFingerprints();

            var firstTrackData = InsertTrackAndHashes(firstTrack, hashedFingerprintsForFirstTrack, modelReferenceTracker);

            var secondTrack = new TrackInfo("id-2", string.Empty, string.Empty, new Dictionary <string, string> {
                { "group-id", "second-group-id" }
            }, MediaType.Audio);
            var hashedFingerprintsForSecondTrack = await GetHashedFingerprints();

            var secondTrackData = InsertTrackAndHashes(secondTrack, hashedFingerprintsForSecondTrack, modelReferenceTracker);

            const int thresholdVotes = 25;
            var       queryConfigWithFirstGroupId = new DefaultQueryConfiguration
            {
                ThresholdVotes   = thresholdVotes,
                MetaFieldsFilter = new Dictionary <string, string> {
                    { "group-id", "first-group-id" }
                }
            };

            var queryConfigWithSecondGroupId = new DefaultQueryConfiguration
            {
                ThresholdVotes   = thresholdVotes,
                MetaFieldsFilter = new Dictionary <string, string> {
                    { "group-id", "second-group-id" }
                }
            };

            foreach (var hashedFingerprint in hashedFingerprintsForFirstTrack)
            {
                var subFingerprintData = subFingerprintDao.ReadSubFingerprints(new[] { hashedFingerprint.HashBins }, queryConfigWithFirstGroupId).ToList();
                Assert.AreEqual(1, subFingerprintData.Count);
                Assert.AreEqual(firstTrackData.TrackReference, subFingerprintData[0].TrackReference);

                subFingerprintData = subFingerprintDao.ReadSubFingerprints(new[] { hashedFingerprint.HashBins }, queryConfigWithSecondGroupId).ToList();
                Assert.AreEqual(1, subFingerprintData.Count);
                Assert.AreEqual(secondTrackData.TrackReference, subFingerprintData[0].TrackReference);

                subFingerprintData = subFingerprintDao
                                     .ReadSubFingerprints(new[] { hashedFingerprint.HashBins }, new DefaultQueryConfiguration {
                    ThresholdVotes = thresholdVotes
                })
                                     .ToList();

                Assert.AreEqual(2, subFingerprintData.Count);
            }
        }
        public void ShouldResetModelReferences()
        {
            var modelReferenceTracker = new UIntModelReferenceTracker(0, 0);
            var track  = new TrackInfo("id", string.Empty, string.Empty);
            var hashes = GetHashes(1000);

            modelReferenceTracker.AssignModelReferences(track, hashes);

            Assert.IsFalse(modelReferenceTracker.TryResetTrackRef(1));
            Assert.IsTrue(modelReferenceTracker.TryResetTrackRef(2));
            Assert.IsFalse(modelReferenceTracker.TryResetSubFingerprintRef(1000));
            Assert.IsTrue(modelReferenceTracker.TryResetSubFingerprintRef(1001));
        }
Example #4
0
        private InMemoryModelService(ITrackDao trackDao, ISubFingerprintDao subFingerprintDao, ISpectralImageDao spectralImageDao, IRAMStorage ramStorage)
        {
            this.ramStorage = ramStorage;
            Id                = "in-memory-model-service";
            TrackDao          = trackDao;
            SubFingerprintDao = subFingerprintDao;
            SpectralImageDao  = spectralImageDao;

            IModelReference?lastTrackReference = null;
            uint            maxTrackId         = 0;

            if (ramStorage.Tracks.Any())
            {
                (lastTrackReference, maxTrackId) = ramStorage.Tracks.Keys
                                                   .Select(_ => (_, _.Get <uint>()))
                                                   .OrderByDescending(_ => _.Item2)
                                                   .First();
            }

            uint maxSubFingerprintId = 0;

            if (lastTrackReference != null)
            {
                maxSubFingerprintId = ramStorage
                                      .ReadSubFingerprintByTrackReference(lastTrackReference)
                                      .Max(_ => _.SubFingerprintReference.Get <uint>());
            }

            modelReferenceTracker = new UIntModelReferenceTracker(maxTrackId, maxSubFingerprintId);

            uint maxSpectralImageId = 0;

            if (lastTrackReference != null)
            {
                var spectralImages = ramStorage.GetSpectralImagesByTrackReference(lastTrackReference).ToList();
                if (spectralImages.Any())
                {
                    maxSpectralImageId = spectralImages.Max(_ => _.SpectralImageReference.Get <uint>());
                }
            }

            spectralReferenceProvider = new UIntModelReferenceProvider(maxSpectralImageId);
        }
        public async Task ReadHashDataByTrackTest()
        {
            var modelReferenceTracker = new UIntModelReferenceTracker();
            var firstHashData         = await GetHashedFingerprints();

            var firstTrack = InsertTrackAndHashes(new TrackInfo("id-1", string.Empty, string.Empty), firstHashData, modelReferenceTracker);

            var secondHashData = await GetHashedFingerprints();

            var secondTrack = InsertTrackAndHashes(new TrackInfo("id-2", string.Empty, string.Empty), secondHashData);

            var resultFirstHashData = subFingerprintDao.ReadHashedFingerprintsByTrackReference(firstTrack.TrackReference)
                                      .Select(ToHashedFingerprint())
                                      .ToList();

            AssertHashDatasAreTheSame(firstHashData, new Hashes(resultFirstHashData, firstHashData.DurationInSeconds, DateTime.Now, Enumerable.Empty <string>()));

            var resultSecondHashData = subFingerprintDao.ReadHashedFingerprintsByTrackReference(secondTrack.TrackReference)
                                       .Select(ToHashedFingerprint())
                                       .ToList();

            AssertHashDatasAreTheSame(secondHashData, new Hashes(resultSecondHashData, secondHashData.DurationInSeconds, DateTime.Now, Enumerable.Empty <string>()));
        }
        public void ShouldAssignModelReferences()
        {
            var modelReferenceTracker = new UIntModelReferenceTracker();
            int numberOfHashBins      = 100;
            var trackRefs             = new ConcurrentBag <uint>();
            var subFingerprintRefs    = new ConcurrentBag <uint>();

            Parallel.For(0, 1000, _ =>
            {
                var trackInfo = new TrackInfo(Guid.NewGuid().ToString(), string.Empty, string.Empty);
                var hashes    = GetHashes(numberOfHashBins);

                var(trackData, subFingerprints) = modelReferenceTracker.AssignModelReferences(trackInfo, hashes);

                trackRefs.Add(trackData.TrackReference.Get <uint>());
                foreach (uint subId in subFingerprints.Select(data => data.SubFingerprintReference.Get <uint>()))
                {
                    subFingerprintRefs.Add(subId);
                }
            });

            Assert.AreEqual(trackRefs.Count, trackRefs.Distinct().Count());
            Assert.AreEqual(subFingerprintRefs.Count, subFingerprintRefs.Distinct().Count());
        }
        public void ShouldCopyAllFieldsCorrectly()
        {
            var modelReferenceTracker = new UIntModelReferenceTracker(0, 0);
            var track = new TrackInfo("id", "title", "artist", new Dictionary <string, string>()
            {
                { "key", "value" }
            }, MediaType.Audio | MediaType.Video);
            var hashes = GetHashes(1);

            var(trackData, subFingerprints) = modelReferenceTracker.AssignModelReferences(track, hashes);

            AssertTracksAreEqual(track, trackData);
            var enumerable = subFingerprints as SubFingerprintData[] ?? subFingerprints.ToArray();

            Assert.AreEqual(1, enumerable.Count());

            var hash = hashes.First();
            var subFingerprintData = enumerable.First();

            CollectionAssert.AreEqual(hash.HashBins, subFingerprintData.Hashes);
            CollectionAssert.AreEqual(hash.OriginalPoint, subFingerprintData.OriginalPoint);
            Assert.AreEqual(hash.SequenceNumber, subFingerprintData.SequenceNumber);
            Assert.AreEqual(hash.StartsAt, subFingerprintData.SequenceAt);
        }