Skip to content

Commit

Permalink
Removing IAdvancedModelService, we don't need it anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
AddictedCS committed Oct 3, 2024
1 parent 16bba27 commit 6f00a77
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

using NUnit.Framework;
using SoundFingerprinting.Builder;
using SoundFingerprinting.Configuration;
using SoundFingerprinting.Data;
using SoundFingerprinting.FFT;
using SoundFingerprinting.InMemory;

[TestFixture]
Expand Down Expand Up @@ -68,34 +66,5 @@ public void ShouldSerializeAndIncrementNextIdCorrectly()
Assert.IsTrue(tracks.Any(track => track == "id1"));
Assert.IsTrue(tracks.Any(track => track == "id2"));
}

[Test]
public void ShouldSerializeSpectralImages()
{
var spectrumService = new SpectrumService(new LomontFFT(), new LogUtility());

var spectrums = spectrumService.CreateLogSpectrogram(GetAudioSamples(), new DefaultSpectrogramConfig())
.Select(spectrum => spectrum.ImageRowCols)
.ToList();

var modelService = new InMemoryModelService();

var track = new TrackInfo("id", string.Empty, string.Empty);
var hashes = new Hashes(GetGenericHashes(), 10, MediaType.Audio);

modelService.Insert(track, new AVHashes(hashes, null));
modelService.InsertSpectralImages(spectrums, "id");

var tempDirectory = Path.Combine(Path.GetTempPath(), "sftests");
modelService.Snapshot(tempDirectory);

var fromFileService = new InMemoryModelService(tempDirectory);

Directory.Delete(tempDirectory, true);

var allSpectrums = fromFileService.GetSpectralImagesByTrackId("id").ToList();

Assert.AreEqual(spectrums.Count, allSpectrums.Count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[TestFixture]
public class InMemoryModelServiceTest : AbstractTest
{
private IAdvancedModelService modelService;
private IModelService modelService;

[SetUp]
public void SetUp()
Expand Down
25 changes: 0 additions & 25 deletions src/SoundFingerprinting.Tests/Unit/InMemory/RAMStorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,6 @@ public void ShouldNotAllowInsertingBothAudioAndVideoHashes()
Assert.Throws<ArgumentException>(() => storage.InsertTrack(track, new AVHashes(audio, video)));
}

[Test]
public void ShouldInsertSpectralImages()
{
var storage = new RAMStorage("audio", new UIntModelReferenceTracker(), new NullLoggerFactory());
var images = new List<float[]> { Array.Empty<float>(), Array.Empty<float>(), Array.Empty<float>() };

storage.InsertTrack(new TrackInfo("10", string.Empty, string.Empty), AVHashes.Empty);
storage.AddSpectralImages("10", images);

var enumerable = storage.GetSpectralImagesByTrackReference("10").ToList();
Assert.AreEqual(3, enumerable.Count());
var ids = enumerable.Select(dto => dto.SpectralImageReference.Get<uint>()).ToList();
CollectionAssert.AreEqual(Enumerable.Range(1, 3), ids);
}

[Test]
public void ShouldReturnEmptySinceNoSpectralImagesArePresentForTrack()
{
var storage = new RAMStorage("audio", new UIntModelReferenceTracker(), new NullLoggerFactory());

var results = storage.GetSpectralImagesByTrackReference("10");

Assert.IsEmpty(results);
}

[Test]
public void ShouldInsertEntriesInThreadSafeManner()
{
Expand Down
25 changes: 0 additions & 25 deletions src/SoundFingerprinting/IAdvancedModelService.cs

This file was deleted.

16 changes: 2 additions & 14 deletions src/SoundFingerprinting/InMemory/AVRAMStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public void InsertTrack(TrackInfo track, AVHashes hashes)
var (audioHashes, videoHashes) = hashes;
if (track.MediaType.HasFlag(MediaType.Audio) && (audioHashes?.IsEmpty ?? true))
{
logger.LogWarning("Track media type is set to Audio, but audio hashes in AVHashes are empty.");
logger.LogWarning("Track media type is set to Audio, but audio hashes in AVHashes are empty");
}

if (track.MediaType.HasFlag(MediaType.Video) && (videoHashes?.IsEmpty ?? true))
{
logger.LogWarning("Track media type is set to Video, but video hashes in AVHashes are empty.");
logger.LogWarning("Track media type is set to Video, but video hashes in AVHashes are empty");
}

if (!track.MediaType.HasFlag(MediaType.Video) && !(videoHashes?.IsEmpty ?? true))
Expand Down Expand Up @@ -146,17 +146,5 @@ public AVHashes ReadAvHashesByTrackId(string trackId)
{
return new AVHashes(audio.ReadAvHashesByTrackId(trackId).Audio, video.ReadAvHashesByTrackId(trackId).Video);
}

/// <inheritdoc cref="IRAMStorage.AddSpectralImages"/>
public void AddSpectralImages(string trackId, IEnumerable<float[]> images)
{
audio.AddSpectralImages(trackId, images);
}

/// <inheritdoc cref="IRAMStorage.GetSpectralImagesByTrackReference"/>
public IEnumerable<SpectralImageData> GetSpectralImagesByTrackReference(string trackId)
{
return audio.GetSpectralImagesByTrackReference(trackId);
}
}
}
14 changes: 0 additions & 14 deletions src/SoundFingerprinting/InMemory/IRAMStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,5 @@ public interface IRAMStorage
/// <param name="trackId">Track ID.</param>
/// <returns>An instance of the <see cref="AVHashes"/> class.</returns>
AVHashes ReadAvHashesByTrackId(string trackId);

/// <summary>
/// Adds spectral images for track reference.
/// </summary>
/// <param name="trackId">Track associated with spectral images.</param>
/// <param name="images">Spectral images to add.</param>
void AddSpectralImages(string trackId, IEnumerable<float[]> images);

/// <summary>
/// Gets spectral images for track reference.
/// </summary>
/// <param name="trackId">Track reference.</param>
/// <returns>List of spectral images.</returns>
IEnumerable<SpectralImageData> GetSpectralImagesByTrackReference(string trackId);
}
}
14 changes: 1 addition & 13 deletions src/SoundFingerprinting/InMemory/InMemoryModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// <remarks>
/// This implementation is intended to be used for testing purposes only.
/// </remarks>
public class InMemoryModelService : IAdvancedModelService
public class InMemoryModelService : IModelService
{
private readonly IRAMStorage storage;
private readonly IGroupingCounter groupingCounter;
Expand Down Expand Up @@ -191,18 +191,6 @@ public void DeleteTrack(string trackId)
storage.DeleteTrack(track.Id);
}

/// <inheritdoc cref="IAdvancedModelService.InsertSpectralImages"/>
public void InsertSpectralImages(IEnumerable<float[]> spectralImages, string trackId)
{
storage.AddSpectralImages(trackId, spectralImages);
}

/// <inheritdoc cref="IAdvancedModelService.GetSpectralImagesByTrackId"/>
public IEnumerable<SpectralImageData> GetSpectralImagesByTrackId(string trackId)
{
return storage.GetSpectralImagesByTrackReference(trackId);
}

private TrackInfo? GetTrackById(string trackId)
{
return storage.ReadByTrackId(trackId);
Expand Down
79 changes: 3 additions & 76 deletions src/SoundFingerprinting/InMemory/RAMStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
public class RAMStorage : IRAMStorage
{
private readonly IModelReferenceTracker<uint> modelReferenceTracker;
private readonly IModelReferenceProvider spectralImagesTracker;
private readonly ILogger<RAMStorage> logger;
private readonly ConcurrentDictionary<int, List<uint>>[] hashTables;

Expand All @@ -32,9 +31,6 @@ public class RAMStorage : IRAMStorage
[ProtoMember(5)]
private ConcurrentDictionary<uint, SubFingerprintData> subFingerprints;

[ProtoMember(7)]
private IDictionary<IModelReference, List<SpectralImageData>> spectralImages;

/// <summary>
/// Initializes a new instance of the <see cref="RAMStorage"/> class.
/// </summary>
Expand All @@ -50,10 +46,9 @@ public RAMStorage(string id, IModelReferenceTracker<uint> modelReferenceTracker,
this.numberOfHashTables = numberOfHashTables;
this.modelReferenceTracker = modelReferenceTracker;
tracks = new ConcurrentDictionary<IModelReference, TrackData>();
spectralImages = new ConcurrentDictionary<IModelReference, List<SpectralImageData>>();
subFingerprints = new ConcurrentDictionary<uint, SubFingerprintData>();

logger.LogDebug("Initializing {0} hash tables.", numberOfHashTables);
logger.LogDebug("Initializing {Count} hash tables", numberOfHashTables);
hashTables = new ConcurrentDictionary<int, List<uint>>[numberOfHashTables];
for (int table = 0; table < numberOfHashTables; table++)
{
Expand All @@ -78,22 +73,9 @@ public RAMStorage(string id, IModelReferenceTracker<uint> modelReferenceTracker,
maxSubFingerprintId = ReadSubFingerprintByTrackReference(lastTrackReference).Max(_ => _.SubFingerprintReference.Get<uint>());
}

logger.LogDebug("Resetting track ref to {0}, fingerprints ref to {1}", maxTrackId, maxSubFingerprintId);
logger.LogDebug("Resetting track ref to {MaxTrackId}, fingerprints ref to {MaxSubFingerprintId}", maxTrackId, maxSubFingerprintId);
modelReferenceTracker.TryResetTrackRef(maxTrackId);
modelReferenceTracker.TryResetSubFingerprintRef(maxSubFingerprintId);

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

logger.LogDebug("Spectral image reference reset to {0}", maxSpectralImageId);
spectralImagesTracker = new UIntModelReferenceProvider(maxSpectralImageId);
}

/// <inheritdoc cref="IRAMStorage.TracksCount"/>
Expand Down Expand Up @@ -270,8 +252,7 @@ private void InitializeFromFile(string path)
InsertHashes(pair.Value.Hashes, pair.Key);
}

spectralImages = obj.spectralImages ?? new ConcurrentDictionary<IModelReference, List<SpectralImageData>>();
logger.LogInformation($"Reloaded storage from {path}, tracks={TracksCount}, fingerprints={SubFingerprintsCount}.");
logger.LogInformation("Reloaded storage from {Path}, tracks=[{TracksCount}], fingerprints=[{SubFingerprintsCount}]", path, TracksCount, SubFingerprintsCount);
}

/// <inheritdoc cref="IRAMStorage.GetSubFingerprintsByHashTableAndHash"/>
Expand Down Expand Up @@ -313,60 +294,6 @@ private void InsertHashes(int[] hashBins, uint subFingerprintId)
}
}

/// <inheritdoc cref="IRAMStorage.AddSpectralImages"/>
public void AddSpectralImages(string trackId, IEnumerable<float[]> images)
{
var track = ReadTrackDataByTrackId(trackId);
if (track == null)
{
throw new ArgumentException($"{nameof(trackId)} is not present in the storage");
}

var trackReference = track.TrackReference;
var spectres = AssignModelReferences(images, trackReference);
if (spectralImages.TryGetValue(trackReference, out var existing))
{
existing.AddRange(spectres);
}
else
{
spectralImages[trackReference] = spectres;
}
}

private List<SpectralImageData> AssignModelReferences(IEnumerable<float[]> images, IModelReference trackReference)
{
int orderNumber = 0;
return images.Select(spectralImage => new SpectralImageData(
spectralImage,
orderNumber++,
spectralImagesTracker.Next(),
trackReference))
.ToList();
}

/// <inheritdoc cref="IRAMStorage.GetSpectralImagesByTrackReference"/>
public IEnumerable<SpectralImageData> GetSpectralImagesByTrackReference(string trackId)
{
var track = ReadTrackDataByTrackId(trackId);
if (track == null)
{
return Enumerable.Empty<SpectralImageData>();
}

return GetSpectralImagesByTrackReference(track.TrackReference);
}

private IEnumerable<SpectralImageData> GetSpectralImagesByTrackReference(IModelReference trackReference)
{
if (spectralImages.TryGetValue(trackReference, out var spectralImageDatas))
{
return spectralImageDatas;
}

return Enumerable.Empty<SpectralImageData>().ToList();
}

private TrackData? ReadTrackDataByTrackId(string id)
{
return tracks.Values.FirstOrDefault(pair => pair.Id == id);
Expand Down

0 comments on commit 6f00a77

Please sign in to comment.