Click or drag to resize

IsamDataFileBaseT Class

An abstract class that defines the read/write capabilities for ISAM (Indexed Sequential Access Method) file.
Inheritance Hierarchy

Namespace: GSF.IO
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.207-beta+1781b796b2aa7a54013a031eb432fe4ccee31867
Syntax
public abstract class IsamDataFileBase<T> : ISupportLifecycle, 
	IDisposable, IProvideStatus, IPersistSettings
where T : ISupportBinaryImage
View Source

Type Parameters

T
Type of the records the file contains. This Type must implement the ISupportBinaryImage interface.

The IsamDataFileBaseT type exposes the following members.

Constructors
 NameDescription
Protected methodIsamDataFileBaseT Initializes a new instance of the IsamDataFileBaseT class.
Top
Properties
 NameDescription
Public propertyAutoSaveInterval Gets or sets the interval in milliseconds at which the records loaded in memory are to be persisted to disk.
Public propertyEnabled Gets or sets a boolean value that indicates whether the file is currently enabled.
Public propertyFileAccessMode Gets or sets the FileAccess value to use when opening the file.
Protected propertyFileData Gets the underlying FileStream of the file.
Protected propertyFileDataLock Gets the locking object for the FileData stream.
Public propertyFileName Gets or sets the name of the file.
Public propertyIsCorrupt Gets a boolean value that indicates whether the file data on disk is corrupt.
Public propertyIsDisposed Gets a flag that indicates whether the object has been disposed.
Public propertyIsOpen Gets a boolean value that indicates whether the file is open.
Public propertyLoadOnOpen Gets or sets a boolean value that indicates whether records are to be loaded automatically in memory when the file is opened.
Protected propertyLoadWaitHandle Gets wait handle for loading data.
Public propertyMemoryUsage Gets the approximate memory consumption (in KB) of the file.
Public propertyName Gets the unique identifier of the file.
Public propertyPersistSettings Gets or sets a boolean value that indicates whether the file settings are to be saved to the config file.
Public propertyRecordsInMemory Gets the number of file records loaded in memory.
Public propertyRecordsOnDisk Gets the number of file records on the disk.
Public propertyReloadOnModify 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.
Public propertySaveOnClose Gets or sets a boolean value that indicates whether records loaded in memory are to be persisted to disk when the file is closed.
Protected propertySaveWaitHandle Gets wait handle for saving data.
Public propertySettingsCategory 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.
Public propertyStatus Gets the descriptive status of the file.
Top
Methods
 NameDescription
Public methodClose Closes the file.
Protected methodCreateNewRecord When overridden in a derived class, returns a new empty record.
Public methodDispose Releases all the resources used by the file.
Protected methodDispose(Boolean) Releases the unmanaged resources used by the file and optionally releases the managed resources.
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodFinalize Releases the unmanaged resources before the file is reclaimed by GC.
(Overrides ObjectFinalize)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Protected methodGetRecordSize When overridden in a derived class, gets the size of a record (in bytes).
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodInitialize Initializes the file.
Public methodLoad Loads records from disk into memory.
Public methodLoadSettings Loads saved settings of the file from the config file if the PersistSettings property is set to true.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Protected methodOnDataLoaded Raises the DataLoaded event.
Protected methodOnDataLoading Raises the DataLoading event.
Protected methodOnDataSaved Raises the DataSaved event.
Protected methodOnDataSaving Raises the DataSaving event.
Protected methodOnFileModified Raises the FileModified event.
Public methodOpen Opens the file.
Public methodRead Reads file records from disk if records were not loaded in memory otherwise returns the records in memory.
Public methodRead(Int32) Reads specified file record from disk if records were not loaded in memory otherwise returns the record in memory.
Public methodSave Saves records loaded in memory to disk.
Public methodSaveSettings Saves settings of the file to the config file if the PersistSettings property is set to true.
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Public methodWrite(IEnumerableT) Writes specified records to disk if records were not loaded in memory otherwise updates the records in memory.
Public methodWrite(Int32, T) Writes specified record to disk if records were not loaded in memory otherwise updates the record in memory.
Top
Events
 NameDescription
Public eventDataLoaded Occurs when data has been read from disk into memory.
Public eventDataLoading Occurs when data is being read from disk into memory.
Public eventDataSaved Occurs when data has been saved from memory onto disk.
Public eventDataSaving Occurs when data is being saved from memory onto disk.
Public eventDisposed Occurs when the class has been disposed.
Public eventFileModified Occurs when file data on the disk is modified.
Top
Fields
 NameDescription
Public fieldStatic memberDefaultAutoSaveInterval Specifies the default value for the AutoSaveInterval property.
Public fieldStatic memberDefaultFileAccessMode Specifies the default value for the FileAccessMode property.
Public fieldStatic memberDefaultFileName Specifies the default value for the FileName property.
Public fieldStatic memberDefaultLoadOnOpen Specifies the default value for the LoadOnOpen property.
Public fieldStatic memberDefaultPersistSettings Specifies the default value for the PersistSettings property.
Public fieldStatic memberDefaultReloadOnModify Specifies the default value for the ReloadOnModify property.
Public fieldStatic memberDefaultSaveOnClose Specifies the default value for the SaveOnClose property.
Public fieldStatic memberDefaultSettingsCategory Specifies the default value for the SettingsCategory property.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Remarks

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.

Example
This example shows a sample implementation of IsamDataFileBaseT:
C#
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>
{
    /// <summary>
    /// Size of a single file record.
    /// </summary>
    protected override int GetRecordSize()
    {
        return TestIsamFileRecord.RecordLength;
    }

    /// <summary>
    /// Creates a new empty file record.
    /// </summary>
    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;
    }

    /// <summary>
    /// 1-based index of the record.
    /// </summary>
    public int Index
    {
        get { return m_index; }
    }

    /// <summary>
    /// Name of the record.
    /// </summary>
    public string Name
    {
        get { return m_name; }
        set { m_name = value.TruncateRight(20).PadRight(20); }
    }

    /// <summary>
    /// Value of the record.
    /// </summary>
    public double Value
    {
        get { return m_value; }
        set { m_value = value; }
    }

    /// <summary>
    /// Description of the record.
    /// </summary>
    public string Description
    {
        get { return m_description; }
        set { m_description = value.TruncateRight(100).PadRight(100); }
    }

    /// <summary>
    /// Serialized record length.
    /// </summary>
    public int BinaryLength
    {
        get { return RecordLength; }
    }

    /// <summary>
    /// Serialized record data.
    /// </summary>
    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;
        }
    }

    /// <summary>
    /// Deserializes the record.
    /// </summary>
    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;
    }

    /// <summary>
    /// String representation of the record.
    /// </summary>
    public override string ToString()
    {
        return string.Format("Name: {0}, Value: {1}, Description: {2}", Name, Value, Description);
    }
}
See Also