public void TestVideoIndexerSerialization() { var frameFingerPrint = new FrameFingerPrintWrapper { FrameNumber = 0, PHashCode = 0x1010, EdgeGrayScaleThumb = new byte[] { 0, 1, 0 }, }; var videoFingerPrint = new VideoFingerPrintWrapper { FilePath = "test.mkv", FingerPrints = new[] { frameFingerPrint }, }; var database = new VideoFingerPrintDatabaseWrapper { VideoFingerPrints = new[] { videoFingerPrint }, }; using (var memoryStream = new MemoryStream()) { VideoFingerPrintDatabaseSaver.Save(database, memoryStream); byte[] savedDatabase = memoryStream.ToArray(); VideoFingerPrintDatabaseWrapper reloadedDatabase = VideoFingerPrintDatabaseLoader.Load(savedDatabase); Assert.AreEqual(database, reloadedDatabase); } }
private static VideoFingerPrintDatabaseMetaTableEntryWrapper CoalesceDatabases(IEnumerable <VideoFingerPrintDatabaseMetaTableEntryWrapper> coalescedEntries) { // Load each database IEnumerable <VideoFingerPrintDatabaseWrapper> databases = from entry in coalescedEntries select VideoFingerPrintDatabaseLoader.Load(entry.FileName); // Merge fingerprints IEnumerable <VideoFingerPrintWrapper> allVideoFingerPrints = from database in databases from videoFingerPrint in database.VideoFingerPrints select videoFingerPrint; // Create a new database var freshDatabase = new VideoFingerPrintDatabaseWrapper { VideoFingerPrints = allVideoFingerPrints.ToArray(), }; // Save the database string databaseFileName = Path.GetRandomFileName() + ".bin"; VideoFingerPrintDatabaseSaver.Save(freshDatabase, databaseFileName); FileInfo databaseFileInfo = new FileInfo(databaseFileName); return(new VideoFingerPrintDatabaseMetaTableEntryWrapper { FileName = databaseFileName, FileSize = (ulong)databaseFileInfo.Length, }); }
private Tuple <VideoFingerPrintDatabaseWrapper, string> CreateNewDatabaseAndAddToMetatable() { VideoFingerPrintDatabaseMetaTableWrapper metatable = CreateOrLoadMetatable(_metatablePath); string emptyDatabaseFileName = Path.GetRandomFileName() + ".bin"; VideoFingerPrintDatabaseWrapper emptyDatabase = new VideoFingerPrintDatabaseWrapper(); VideoFingerPrintDatabaseSaver.Save(emptyDatabase, emptyDatabaseFileName); // Add to the metatable var databaseEntries = new List <VideoFingerPrintDatabaseMetaTableEntryWrapper>(metatable.DatabaseMetaTableEntries); databaseEntries.Add(new VideoFingerPrintDatabaseMetaTableEntryWrapper { FileName = emptyDatabaseFileName, FileSize = 0ul, }); metatable.DatabaseMetaTableEntries = databaseEntries.ToArray(); VideoFingerPrintDatabaseMetaTableSaver.Save(metatable, _metatablePath); return(Tuple.Create(emptyDatabase, emptyDatabaseFileName)); }
private void RunQueue() { var fingerprintBuffer = new List <VideoFingerPrintWrapper>(); Tuple <VideoFingerPrintDatabaseWrapper, string> currentDatabaseTuple = GetNextEligibleDatabase(); bool needsFinalFlush = false; foreach (VideoFingerPrintWrapper fingerprint in _workItems.GetConsumingEnumerable()) { try { Console.WriteLine("Adding fingerprint: {0}", Path.GetFileName(fingerprint.FilePath)); fingerprintBuffer.Add(fingerprint); if (fingerprintBuffer.Count > 5) { VideoFingerPrintDatabaseWrapper currentDatabase = currentDatabaseTuple.Item1; string currentDatabasePath = currentDatabaseTuple.Item2; Console.WriteLine("Flushing database"); // Flush the buffer needsFinalFlush = false; // Add entries to database currentDatabase.VideoFingerPrints = currentDatabase.VideoFingerPrints.Concat(fingerprintBuffer).ToArray(); // Save entries to disk VideoFingerPrintDatabaseSaver.Save(currentDatabase, currentDatabasePath); FileInfo fileInfo = new FileInfo(currentDatabasePath); ulong fileSize = (ulong)fileInfo.Length; // Save metatable to disk UpdateMetatable(currentDatabasePath, fileSize); // Now, check if we need to update the current database if (fileSize > MaxDatabaseSize) { currentDatabaseTuple = GetNextEligibleDatabase(); } // Lastly, clear the buffer fingerprintBuffer.Clear(); } else { needsFinalFlush = true; } } catch (Exception e) { Console.WriteLine("Could not write database. {0}", e.Message); } } if (needsFinalFlush) { try { Console.WriteLine("Flushing database for the final time"); // Flush the buffer one last time // Add entries to database currentDatabaseTuple.Item1.VideoFingerPrints = currentDatabaseTuple.Item1.VideoFingerPrints.Concat(fingerprintBuffer).ToArray(); // Save entries to disk VideoFingerPrintDatabaseSaver.Save(currentDatabaseTuple.Item1, currentDatabaseTuple.Item2); } catch (Exception e) { Console.WriteLine("Could not perform final flush of database. {0}", e.Message); } } }