/// <summary> /// Initialize an IndexDatabase with the path to the index file, /// whether it exists or not /// </summary> /// <param name="pathToIndexFile">The path to the index file</param> /// <param name="method">The serialization method to use</param> /// <remarks> /// If the file exists, this object will attempt to deserialize it. If it does /// not exist, a new file will be created /// </remarks> public IndexDatabase(string pathToIndexFile, SerializationMethod method) : this() { _pathToIndexFile = pathToIndexFile; _indexEntries = DeserializeIndexDatabase(pathToIndexFile, method); _serializationMethod = method; }
private static void SerializeNewEntriesBinary(string pathToIndexFile, IndexEntries entries) { SerializeNewEntries(pathToIndexFile, () => { using (var stream = new FileStream(pathToIndexFile, FileMode.Create, FileAccess.Write, FileShare.None)) { var formatter = new BinaryFormatter(); formatter.Serialize(stream, entries); stream.Close(); } }); }
/// <summary> /// Flush any queued entries for addition to disk /// </summary> public void Flush() { var updatedIndexEntriesObject = new IndexEntries { Entries = _queuedIndexEntriesForAddition.ToArray(), }; if (_serializationMethod == SerializationMethod.JSON) { SerializeNewEntriesJson(_pathToIndexFile, JsonConvert.SerializeObject(updatedIndexEntriesObject)); } else { SerializeNewEntriesBinary(_pathToIndexFile, updatedIndexEntriesObject); } _indexEntries = updatedIndexEntriesObject; ClearQueuedEntries(); _hashToEntriesMap = new Lazy<IDictionary<ImageFingerPrint, ICollection<IndexEntry>>>(IndexAllEntries); }