// Internal features private AudioLogEntry CreateLogEntry(HistorySaveData saveData) { if (string.IsNullOrWhiteSpace(saveData.Resource.ResourceTitle)) { return(null); } uint nextHid; if (ReuseUnusedIds && unusedIds.Any()) { nextHid = unusedIds.First.Value; unusedIds.RemoveFirst(); } else { nextHid = CurrentID; CurrentID++; } var ale = new AudioLogEntry(nextHid, saveData.Resource) { UserInvokeId = (uint)(saveData.OwnerDbId ?? 0), Timestamp = Util.GetNow(), PlayCount = 1, }; return(ale); }
public AudioLogEntry Store(HistorySaveData saveData) { if (saveData == null) { throw new ArgumentNullException(nameof(saveData)); } AudioLogEntry ale; uint? index = Contains(saveData.Resource); if (!index.HasValue) { ale = CreateLogEntry(saveData); if (ale != null) { AddToMemoryIndex(ale); AppendToFile(ale); } else { Log.Write(Log.Level.Error, "AudioLogEntry could not be created!"); } } else { ale = GetEntryById(index.Value); LogEntryPlay(ale); } return(ale); }
public void HistoryFileIntergrityTest() { string testFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "history.test"); if (File.Exists(testFile)) { File.Delete(testFile); } var inv1 = new ClientData { ClientId = 10, DatabaseId = 101, Name = "Invoker1" }; var inv2 = new ClientData { ClientId = 20, DatabaseId = 102, Name = "Invoker2" }; var ar1 = new AudioResource("asdf", "sc_ar1", "soundcloud"); var ar2 = new AudioResource("./File.mp3", "me_ar2", "media"); var ar3 = new AudioResource("kitty", "tw_ar3", "twitch"); var data1 = new HistorySaveData(ar1, inv1.DatabaseId); var data2 = new HistorySaveData(ar2, inv2.DatabaseId); var data3 = new HistorySaveData(ar3, 103); var memcfg = ConfigFile.CreateDummy(); var hmf = memcfg.GetDataStruct <HistoryManagerData>("HistoryManager", true); hmf.HistoryFile = testFile; hmf.FillDeletedIds = false; DbStore db; HistoryManager hf; void CreateDbStore() { db = new DbStore(hmf); hf = new HistoryManager(hmf) { Database = db }; hf.Initialize(); } CreateDbStore(); hf.LogAudioResource(data1); var lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); var lastEntry = lastXEntries.First(); Assert.AreEqual(ar1, lastEntry.AudioResource); db.Dispose(); CreateDbStore(); lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); lastEntry = lastXEntries.First(); Assert.AreEqual(ar1, lastEntry.AudioResource); hf.LogAudioResource(data1); hf.LogAudioResource(data2); lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); lastEntry = lastXEntries.First(); Assert.AreEqual(ar2, lastEntry.AudioResource); db.Dispose(); // store and order check CreateDbStore(); var lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar2, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar1, lastXEntriesArray[1].AudioResource); var ale1 = hf.FindEntryByResource(ar1); hf.RenameEntry(ale1, "sc_ar1X"); hf.LogAudioResource(new HistorySaveData(ale1.AudioResource, 42)); db.Dispose(); // check entry renaming CreateDbStore(); lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar1, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar2, lastXEntriesArray[1].AudioResource); var ale2 = hf.FindEntryByResource(ar2); hf.RenameEntry(ale2, "me_ar2_loong1"); hf.LogAudioResource(new HistorySaveData(ale2.AudioResource, 42)); ale1 = hf.FindEntryByResource(ar1); hf.RenameEntry(ale1, "sc_ar1X_loong1"); hf.LogAudioResource(new HistorySaveData(ale1.AudioResource, 42)); hf.RenameEntry(ale2, "me_ar2_exxxxxtra_loong1"); hf.LogAudioResource(new HistorySaveData(ale2.AudioResource, 42)); db.Dispose(); // recheck order CreateDbStore(); lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar2, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar1, lastXEntriesArray[1].AudioResource); db.Dispose(); // delete entry 1 CreateDbStore(); hf.RemoveEntry(hf.FindEntryByResource(ar1)); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(1, lastXEntriesArray.Length); // .. store new entry to check correct stream position writes hf.LogAudioResource(data3); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); db.Dispose(); // delete entry 2 CreateDbStore(); // .. check integrity from previous store lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); // .. delete and recheck hf.RemoveEntry(hf.FindEntryByResource(ar2)); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(1, lastXEntriesArray.Length); Assert.AreEqual(ar3, lastXEntriesArray[0].AudioResource); db.Dispose(); File.Delete(testFile); }
public void HistoryFileIntergrityTest() { string testFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "history.test"); if (File.Exists(testFile)) { File.Delete(testFile); } var inv1 = new ClientData { ClientId = 10, DatabaseId = 101, NickName = "Invoker1" }; var inv2 = new ClientData { ClientId = 20, DatabaseId = 102, NickName = "Invoker2" }; var ar1 = new AudioResource("asdf", "sc_ar1", AudioType.Soundcloud); var ar2 = new AudioResource("./File.mp3", "me_ar2", AudioType.MediaLink); var ar3 = new AudioResource("kitty", "tw_ar3", AudioType.Twitch); var data1 = new HistorySaveData(ar1, inv1.DatabaseId); var data2 = new HistorySaveData(ar2, inv2.DatabaseId); var data3 = new HistorySaveData(ar3, 103); var hf = new HistoryManager(new HistoryManagerData() { HistoryFile = testFile, FillDeletedIds = false }); hf.LogAudioResource(data1); var lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); var lastEntry = lastXEntries.First(); Assert.AreEqual(ar1, lastEntry.AudioResource); hf.Dispose(); hf = new HistoryManager(new HistoryManagerData() { HistoryFile = testFile, FillDeletedIds = false }); lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); lastEntry = lastXEntries.First(); Assert.AreEqual(ar1, lastEntry.AudioResource); hf.LogAudioResource(data1); hf.LogAudioResource(data2); lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); lastEntry = lastXEntries.First(); Assert.AreEqual(ar2, lastEntry.AudioResource); hf.Dispose(); // store and order check hf = new HistoryManager(new HistoryManagerData() { HistoryFile = testFile, FillDeletedIds = false }); var lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar2, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar1, lastXEntriesArray[1].AudioResource); var ale1 = hf.FindEntryByResource(ar1); hf.RenameEntry(ale1, "sc_ar1X"); hf.LogAudioResource(new HistorySaveData(ale1.AudioResource, 42)); hf.Dispose(); // check entry renaming hf = new HistoryManager(new HistoryManagerData() { HistoryFile = testFile, FillDeletedIds = false }); lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar1, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar2, lastXEntriesArray[1].AudioResource); var ale2 = hf.FindEntryByResource(ar2); hf.RenameEntry(ale2, "me_ar2_loong1"); hf.LogAudioResource(new HistorySaveData(ale2.AudioResource, 42)); ale1 = hf.FindEntryByResource(ar1); hf.RenameEntry(ale1, "sc_ar1X_loong1"); hf.LogAudioResource(new HistorySaveData(ale1.AudioResource, 42)); hf.RenameEntry(ale2, "me_ar2_exxxxxtra_loong1"); hf.LogAudioResource(new HistorySaveData(ale2.AudioResource, 42)); hf.Dispose(); // recheck order hf = new HistoryManager(new HistoryManagerData() { HistoryFile = testFile, FillDeletedIds = false }); lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar2, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar1, lastXEntriesArray[1].AudioResource); hf.Dispose(); // delete entry 1 hf = new HistoryManager(new HistoryManagerData() { HistoryFile = testFile, FillDeletedIds = false }); hf.RemoveEntry(hf.FindEntryByResource(ar1)); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(1, lastXEntriesArray.Length); // .. store new entry to check correct stream position writes hf.LogAudioResource(data3); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); hf.Dispose(); // delete entry 2 hf = new HistoryManager(new HistoryManagerData() { HistoryFile = testFile, FillDeletedIds = false }); // .. check integrity from previous store lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); // .. delete and recheck hf.RemoveEntry(hf.FindEntryByResource(ar2)); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(1, lastXEntriesArray.Length); Assert.AreEqual(ar3, lastXEntriesArray[0].AudioResource); hf.Dispose(); File.Delete(testFile); }
public void HistoryFileIntergrityTest() { string testFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "history.test"); if (File.Exists(testFile)) { File.Delete(testFile); } var inv1 = new ClientList { ClientId = (ClientId)10, Uid = (Uid)"Uid1", Name = "Invoker1" }; var inv2 = new ClientList { ClientId = (ClientId)20, Uid = (Uid)"Uid2", Name = "Invoker2" }; var ar1 = new AudioResource("asdf", "sc_ar1", "soundcloud"); var ar2 = new AudioResource("./File.mp3", "me_ar2", "media"); var ar3 = new AudioResource("kitty", "tw_ar3", "twitch"); var data1 = new HistorySaveData(ar1, inv1.Uid); var data2 = new HistorySaveData(ar2, inv2.Uid); var data3 = new HistorySaveData(ar3, (Uid)"Uid3"); var confHistory = ConfigTable.CreateRoot <ConfHistory>(); confHistory.FillDeletedIds.Value = false; var confDb = ConfigTable.CreateRoot <ConfDb>(); confDb.Path.Value = testFile; DbStore db; HistoryManager hf; void CreateDbStore() { db = new DbStore(confDb); hf = new HistoryManager(confHistory, db); } CreateDbStore(); hf.LogAudioResourceDelayed(data1); var lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); var lastEntry = lastXEntries.First(); Assert.AreEqual(ar1, lastEntry.AudioResource); db.Dispose(); CreateDbStore(); lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); lastEntry = lastXEntries.First(); Assert.AreEqual(ar1, lastEntry.AudioResource); hf.LogAudioResourceDelayed(data1); hf.LogAudioResourceDelayed(data2); lastXEntries = hf.GetLastXEntrys(1); Assert.True(lastXEntries.Any()); lastEntry = lastXEntries.First(); Assert.AreEqual(ar2, lastEntry.AudioResource); db.Dispose(); // store and order check CreateDbStore(); var lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar2, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar1, lastXEntriesArray[1].AudioResource); var ale1 = hf.FindEntryByResource(ar1); hf.RenameEntry(ale1, "sc_ar1X"); hf.LogAudioResourceDelayed(new HistorySaveData(ale1.AudioResource, (Uid)"Uid4")); db.Dispose(); // check entry renaming CreateDbStore(); lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar1, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar2, lastXEntriesArray[1].AudioResource); var ale2 = hf.FindEntryByResource(ar2); hf.RenameEntry(ale2, "me_ar2_loong1"); hf.LogAudioResourceDelayed(new HistorySaveData(ale2.AudioResource, (Uid)"Uid4")); ale1 = hf.FindEntryByResource(ar1); hf.RenameEntry(ale1, "sc_ar1X_loong1"); hf.LogAudioResourceDelayed(new HistorySaveData(ale1.AudioResource, (Uid)"Uid4")); hf.RenameEntry(ale2, "me_ar2_exxxxxtra_loong1"); hf.LogAudioResourceDelayed(new HistorySaveData(ale2.AudioResource, (Uid)"Uid4")); db.Dispose(); // recheck order CreateDbStore(); lastXEntriesArray = hf.GetLastXEntrys(2).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); Assert.AreEqual(ar2, lastXEntriesArray[0].AudioResource); Assert.AreEqual(ar1, lastXEntriesArray[1].AudioResource); db.Dispose(); // delete entry 1 CreateDbStore(); hf.RemoveEntry(hf.FindEntryByResource(ar1)); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(1, lastXEntriesArray.Length); // .. store new entry to check correct stream position writes hf.LogAudioResourceDelayed(data3); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); db.Dispose(); // delete entry 2 CreateDbStore(); // .. check integrity from previous store lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(2, lastXEntriesArray.Length); // .. delete and recheck hf.RemoveEntry(hf.FindEntryByResource(ar2)); lastXEntriesArray = hf.GetLastXEntrys(3).ToArray(); Assert.AreEqual(1, lastXEntriesArray.Length); Assert.AreEqual(ar3, lastXEntriesArray[0].AudioResource); db.Dispose(); File.Delete(testFile); }
public static void LogAudioResourceDelayed(this HistoryManager hf, HistorySaveData data) { Thread.Sleep(1); hf.LogAudioResource(data); }