|
|
Isam
|
public abstract class IsamDataFileBase<T> : ISupportLifecycle, IDisposable, IProvideStatus, IPersistSettings where T : ISupportBinaryImage
The IsamDataFileBaseT type exposes the following members.
| Name | Description | |
|---|---|---|
| IsamDataFileBaseT | Initializes a new instance of the IsamDataFileBaseT class. |
| Name | Description | |
|---|---|---|
| AutoSaveInterval | Gets or sets the interval in milliseconds at which the records loaded in memory are to be persisted to disk. | |
| Enabled | Gets or sets a boolean value that indicates whether the file is currently enabled. | |
| FileAccessMode | Gets or sets the FileAccess value to use when opening the file. | |
| FileData | Gets the underlying FileStream of the file. | |
| FileDataLock | Gets the locking object for the FileData stream. | |
| FileName | Gets or sets the name of the file. | |
| IsCorrupt | Gets a boolean value that indicates whether the file data on disk is corrupt. | |
| IsDisposed | Gets a flag that indicates whether the object has been disposed. | |
| IsOpen | Gets a boolean value that indicates whether the file is open. | |
| LoadOnOpen | Gets or sets a boolean value that indicates whether records are to be loaded automatically in memory when the file is opened. | |
| LoadWaitHandle | Gets wait handle for loading data. | |
| MemoryUsage | Gets the approximate memory consumption (in KB) of the file. | |
| Name | Gets the unique identifier of the file. | |
| PersistSettings | Gets or sets a boolean value that indicates whether the file settings are to be saved to the config file. | |
| RecordsInMemory | Gets the number of file records loaded in memory. | |
| RecordsOnDisk | Gets the number of file records on the disk. | |
| ReloadOnModify | Gets or sets a boolean value that indicates whether records loaded in memory are to be re-loaded when the file is modified on disk. | |
| SaveOnClose | Gets or sets a boolean value that indicates whether records loaded in memory are to be persisted to disk when the file is closed. | |
| SaveWaitHandle | Gets wait handle for saving data. | |
| SettingsCategory | Gets or sets the category under which the file settings are to be saved to the config file if the PersistSettings property is set to true. | |
| Status | Gets the descriptive status of the file. |
| Name | Description | |
|---|---|---|
| Close | Closes the file. | |
| CreateNewRecord | When overridden in a derived class, returns a new empty record. | |
| Dispose | Releases all the resources used by the file. | |
| Dispose(Boolean) | Releases the unmanaged resources used by the file and optionally releases the managed resources. | |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) | |
| Finalize |
Releases the unmanaged resources before the file is reclaimed by GC.
(Overrides ObjectFinalize) | |
| GetHashCode | Serves as the default hash function. (Inherited from Object) | |
| GetRecordSize | When overridden in a derived class, gets the size of a record (in bytes). | |
| GetType | Gets the Type of the current instance. (Inherited from Object) | |
| Initialize | Initializes the file. | |
| Load | Loads records from disk into memory. | |
| LoadSettings | Loads saved settings of the file from the config file if the PersistSettings property is set to true. | |
| MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) | |
| OnDataLoaded | Raises the DataLoaded event. | |
| OnDataLoading | Raises the DataLoading event. | |
| OnDataSaved | Raises the DataSaved event. | |
| OnDataSaving | Raises the DataSaving event. | |
| OnFileModified | Raises the FileModified event. | |
| Open | Opens the file. | |
| Read | Reads file records from disk if records were not loaded in memory otherwise returns the records in memory. | |
| Read(Int32) | Reads specified file record from disk if records were not loaded in memory otherwise returns the record in memory. | |
| Save | Saves records loaded in memory to disk. | |
| SaveSettings | Saves settings of the file to the config file if the PersistSettings property is set to true. | |
| ToString | Returns a string that represents the current object. (Inherited from Object) | |
| Write(IEnumerableT) | Writes specified records to disk if records were not loaded in memory otherwise updates the records in memory. | |
| Write(Int32, T) | Writes specified record to disk if records were not loaded in memory otherwise updates the record in memory. |
| Name | Description | |
|---|---|---|
| DataLoaded | Occurs when data has been read from disk into memory. | |
| DataLoading | Occurs when data is being read from disk into memory. | |
| DataSaved | Occurs when data has been saved from memory onto disk. | |
| DataSaving | Occurs when data is being saved from memory onto disk. | |
| Disposed | Occurs when the class has been disposed. | |
| FileModified | Occurs when file data on the disk is modified. |
| Name | Description | |
|---|---|---|
| DefaultAutoSaveInterval | Specifies the default value for the AutoSaveInterval property. | |
| DefaultFileAccessMode | Specifies the default value for the FileAccessMode property. | |
| DefaultFileName | Specifies the default value for the FileName property. | |
| DefaultLoadOnOpen | Specifies the default value for the LoadOnOpen property. | |
| DefaultPersistSettings | Specifies the default value for the PersistSettings property. | |
| DefaultReloadOnModify | Specifies the default value for the ReloadOnModify property. | |
| DefaultSaveOnClose | Specifies the default value for the SaveOnClose property. | |
| DefaultSettingsCategory | Specifies the default value for the SettingsCategory property. |
| Name | Description | |
|---|---|---|
| GetEnumValueOrDefault |
Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) | |
| GetEnumValueOrDefaultT |
Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions) |
This ISAM implementation keeps all the records in memory, so it may not be suitable for very large files. Since data is stored in memory using a list, the maximum number of possible supported records will be 2,147,483,647 (i.e., Int32.MaxValue).
See http://en.wikipedia.org/wiki/ISAM for more information on ISAM files.
using System;
using System.Text;
using GSF;
using GSF.IO;
using GSF.Parsing;
class Program
{
static void Main(string[] args)
{
// Create a few test records.
TestIsamFileRecord r1 = new TestIsamFileRecord(1);
r1.Name = "TestRecord1";
r1.Value = double.MinValue;
r1.Description = "Test record with minimum double value";
TestIsamFileRecord r2 = new TestIsamFileRecord(2);
r2.Name = "TestRecord2";
r2.Value = double.MaxValue;
r2.Description = "Test record with maximum double value";
// Open ISAM file.
TestIsamFile testFile = new TestIsamFile();
testFile.FileName = "TestIsamFile.dat";
testFile.Open();
// Write test records.
testFile.Write(r1.Index, r1);
testFile.Write(r2.Index, r2);
// Read test records.
Console.WriteLine(testFile.Read(1));
Console.WriteLine(testFile.Read(2));
// Close ISAM file.
testFile.Close();
Console.ReadLine();
}
}
class TestIsamFile : IsamDataFileBase<TestIsamFileRecord>
{
///
/// Size of a single file record.
///
protected override int GetRecordSize()
{
return TestIsamFileRecord.RecordLength;
}
///
/// Creates a new empty file record.
///
protected override TestIsamFileRecord CreateNewRecord(int id)
{
return new TestIsamFileRecord(id);
}
}
class TestIsamFileRecord : ISupportBinaryImage
{
private int m_index;
private string m_name; // 20 * 1 = 20
private double m_value; // 1 * 8 = 8
private string m_description; // 100 * 1 = 100
public const int RecordLength = 128; // Total = 128
public TestIsamFileRecord(int recordIndex)
{
m_index = recordIndex;
Name = string.Empty;
Value = double.NaN;
Description = string.Empty;
}
///
/// 1-based index of the record.
///
public int Index
{
get { return m_index; }
}
///
/// Name of the record.
///
public string Name
{
get { return m_name; }
set { m_name = value.TruncateRight(20).PadRight(20); }
}
///
/// Value of the record.
///
public double Value
{
get { return m_value; }
set { m_value = value; }
}
///
/// Description of the record.
///
public string Description
{
get { return m_description; }
set { m_description = value.TruncateRight(100).PadRight(100); }
}
///
/// Serialized record length.
///
public int BinaryLength
{
get { return RecordLength; }
}
///
/// Serialized record data.
///
public byte[] BinaryImage
{
get
{
// Serialize TestIsamFileRecord into byte array.
byte[] image = new byte[RecordLength];
Buffer.BlockCopy(Encoding.ASCII.GetBytes(Name), 0, image, 0, 20);
Buffer.BlockCopy(BitConverter.GetBytes(Value), 0, image, 20, 8);
Buffer.BlockCopy(Encoding.ASCII.GetBytes(Description), 0, image, 28, 100);
return image;
}
}
///
/// Deserializes the record.
///
public int Initialize(byte[] binaryImage, int startIndex, int length)
{
if (length >= RecordLength)
{
// Deserialize byte array into TestIsamFileRecord.
Name = Encoding.ASCII.GetString(binaryImage, startIndex, 20);
Value = BitConverter.ToDouble(binaryImage, startIndex + 20);
Description = Encoding.ASCII.GetString(binaryImage, startIndex + 28, 100);
}
else
throw new InvalidOperationException("Invalid record size, not enough data to deserialize record");
return RecordLength;
}
///
/// String representation of the record.
///
public override string ToString()
{
return string.Format("Name: {0}, Value: {1}, Description: {2}", Name, Value, Description);
}
}