/// <summary> /// Saves records loaded in memory to disk. /// </summary> /// <remarks> /// <see cref="IsamDataFileBase{T}.Save"/> is equivalent to <see cref="FileStream.Flush()"/> when records are not loaded in memory. /// </remarks> public override void Save() { if (m_legacyMode == MetadataFileLegacyMode.Enabled) { base.Save(); } if (m_legacyMode == MetadataFileLegacyMode.Compatible) { SaveLegacyMetadataFile(); } if (m_legacyMode == MetadataFileLegacyMode.Disabled || m_legacyMode == MetadataFileLegacyMode.Compatible) { if (!IsOpen) { throw new InvalidOperationException(string.Format("MetadataFile \"{0}\" is not open", FileName)); } // Waits for any pending request to save records before completing. SaveWaitHandle.Wait(); // Waits for any prior request to load records before completing. LoadWaitHandle.Wait(); SaveWaitHandle.Reset(); try { OnDataSaving(); lock (FileDataLock) { FileData.SetLength(0); BinaryWriter writer = new BinaryWriter(FileData); writer.Write(m_records.Count); foreach (MetadataRecord record in m_records.Values) { record.WriteImage(writer); } FileData.Flush(); File.SetLastWriteTime(FileName, DateTime.Now); } OnDataSaved(); } finally { SaveWaitHandle.Set(); } } }
/// <summary> /// Loads records from disk into memory. /// </summary> public override void Load() { if (m_legacyMode == MetadataFileLegacyMode.Enabled) { base.Load(); return; } if (!IsOpen) { throw new InvalidOperationException(string.Format("MetadataFile \"{0}\" is not open", FileName)); } // Waits for any pending request to save records before completing. SaveWaitHandle.Wait(); // Waits for any prior request to load records before completing. LoadWaitHandle.Wait(); LoadWaitHandle.Reset(); try { OnDataLoading(); lock (FileDataLock) { if (FileData.Length > 0) { FileData.Seek(0, SeekOrigin.Begin); BinaryReader reader = new BinaryReader(FileData); int count = reader.ReadInt32(); for (int i = 0; i < count; i++) { MetadataRecord record = new MetadataRecord(reader); m_records[record.HistorianID] = record; } } } OnDataLoaded(); } finally { LoadWaitHandle.Set(); } }