⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 absdiskengine.~pas

📁 Absolute Database 是来替代BDE[Borland数据库引擎]的用于Delphi 和 C++ Builder 开发用的数据库引擎. 它小巧, 高速, 健壮, 易于使用. 它能直接编译进
💻 ~PAS
📖 第 1 页 / 共 5 页
字号:
    FEAMPage_ExtentsAddressed:  Integer;  // count of extents addressed by EAMPage
    FEAMPage_PagesAddressed:    Integer;  // count of pages addressed by EAMPage

    FLastUsedPageNo:            TABSPageNo;
    FTotalPageCount:            TABSPageNo;
    FSuppressDBHeaderErrors:    Boolean;

    function GetAddPagesStep: Integer;
    function GetDelPagesStep: Integer;

    function NewPage(PageNo: TABSPageNo): TABSPage;
    function ReadPage(PageNo: TABSPageNo): TABSPage;
    procedure WriteAndFreePage(var Page: TABSPage);


    function CorrectEamPageNo(EamPageNo: TABSPageNo): TABSPageNo;
    function UncorrectEamPageNo(EamPageNo: TABSPageNo): TABSPageNo;
    function EamPageNoForEamPagePosition(EamPagePosition: Integer): TABSPageNo;
    function EamPageNoToEamPagePosition(EamPageNo: TABSPageNo): Integer;
    function EamPageNoForPageNo(PageNo: TABSPageNo): TABSPageNo;

    function GetLastEamPagePosition: Integer;
    function GetLastEamPageNo: TABSPageNo;
    function GetLastPfsPagePosition: Integer;
    function PfsPositionsForExtentPosition(const ExtentPosition: Integer;
                                            out PfsFirstPagePosition: Integer;
                                            out PfsLastPagePosition: Integer
                                            ): Boolean;
    function PfsPageNoForPfsPagePosition(PfsPagePosition: Integer): TABSPageNo;
    function PfsPageNoForPageNo(PageNo: TABSPageNo): TABSPageNo;


    function IsPagePfsOrEam(PageNo: TABSPageNo): Boolean;

    function FindAndReusePage: TABSPageNo;
    function FindLastUsedPageNo: TABSPageNo;

    // Set in PFS and EAM maps that page with number PageNo is USED or FREE
    procedure SetPageUsage(PageNo: TABSPageNo; UsedFlag: Boolean);
    procedure SetPageUsageToPFS(PageNo: TABSPageNo; UsedFlag: Boolean);
    procedure SetPageUsageToEAM(PageNo: TABSPageNo; UsedFlag: Boolean);

   public
    function GetPageUsageFromPFS(PageNo: TABSPageNo) : Boolean;

   private
    function AddNewPageAndExtentFile: TABSPageNo;
    procedure TruncateFile;

    procedure ReReadPageCountVariables;
    procedure ReWritePageCountVariables;

   public
    constructor Create(aPageManager: TABSDiskPageManager);
    function GetPage(DesiredStartPageNo: TABSPageNo = INVALID_PAGE_NO): TABSPageNo;
    procedure FreePage(PageNo: TABSPageNo);
    procedure CheckPageNoForSystemPages(PageNo: TABSPageNo);

    property AddPagesStep: Integer read GetAddPagesStep;
    property DelPagesStep: Integer read GetDelPagesStep;
    property SuppressDBHeaderErrors: Boolean read FSuppressDBHeaderErrors write FSuppressDBHeaderErrors;

{
    procedure GetPages(
           PageCount:          TABSPageNo;
           var Pages:          TABSPagesArray;
           bUniform:           Boolean;
           DesiredStartPageNo: TABSPageNo
                     ); virtual; abstract;
    procedure FreePages(Pages: TABSPagesArray); virtual; abstract;
}
  end;// TABSDatabaseFreeSpaceManager




////////////////////////////////////////////////////////////////////////////////
//
// TABSDTPCTableInfo
//
////////////////////////////////////////////////////////////////////////////////

  TABSDTPCTableInfo = class
   private
    TableID: TABSTableID;
    TableSLockCount: Integer;
    TableRWLockCount: Integer;
    IsMostUpdatedLoaded: Boolean;

    procedure Clear;
   public
    TableState: Integer;

    constructor Create(aTableID: TABSTableID);
    destructor Destroy; override;
    procedure TableIsSLocked;
    procedure TableIsSUnlocked;
    procedure TableIsRWLocked;
    procedure TableIsRWUnlocked;
    function GetIsMostUpdatedLoaded: Boolean;
    procedure SetIsMostUpdatedLoaded(aTableState: Integer);
    function IsTableLockedFromModification: Boolean;
  end;

////////////////////////////////////////////////////////////////////////////////
//
// TABSDTPCSessionInfo
//
////////////////////////////////////////////////////////////////////////////////

  TABSDTPCSessionInfo = class
   private
    FTableInfo: array of TABSDTPCTableInfo;
    FWorkTableIDs: TList; // stack of work table IDs

   public
    constructor Create;
    destructor Destroy; override;
    function GetTableInfo(TableID: TABSTableID): TABSDTPCTableInfo;
    procedure BeginWorkWithTable(TableID: TABSTableID);
    procedure EndWorkWithTable(TableID: TABSTableID);
    function GetWorkTableState(var WorkTableState: Integer): Boolean;
  end;// TABSDTPCSessionInfo

////////////////////////////////////////////////////////////////////////////////
//
// TABSDiskTablePagesCacheManager
//
////////////////////////////////////////////////////////////////////////////////

  TABSDiskTablePagesCacheManager = class
   private
    FSessionInfo: array of TABSDTPCSessionInfo;

    function GetSessionInfo(SessionID: TABSSessionID): TABSDTPCSessionInfo;
   public
    constructor Create;
    destructor Destroy; override;
    procedure TableIsSLocked(SessionID: TABSSessionID; TableID: TABSTableID);
    procedure TableIsSUnlocked(SessionID: TABSSessionID; TableID: TABSTableID);
    procedure TableIsRWLocked(SessionID: TABSSessionID; TableID: TABSTableID);
    procedure TableIsRWUnlocked(SessionID: TABSSessionID; TableID: TABSTableID);
    function GetIsMostUpdatedLoaded(SessionID: TABSSessionID; TableID: TABSTableID): Boolean;
    procedure SetIsMostUpdatedLoaded(SessionID: TABSSessionID; TableID: TABSTableID; TableState: Integer);
    procedure BeginWorkWithTable(SessionID: TABSSessionID; TableID: TABSTableID);
    procedure EndWorkWithTable(SessionID: TABSSessionID; TableID: TABSTableID);
    function GetWorkTableState(SessionID: TABSSessionID; var WorkTableState: Integer): Boolean;
  end;// TABSDiskTablePagesCacheManager



////////////////////////////////////////////////////////////////////////////////
//
// TABSDiskPageManager
//
////////////////////////////////////////////////////////////////////////////////


  TABSDiskPageManager = class (TABSPageManager)
   private
    FDatabaseFreeSpaceManager:  TABSDatabaseFreeSpaceManager;
    FDatabaseFile:              TABSDatabaseFile;
    FDBHeader:                  TABSDBHeader;
    FCryptoHeader:              TABSCryptoHeader;
    FPassword:                  TABSPassword;
    FLockedBytes:               TABSLockedBytes;
    FLockDBHeaderCount:         Integer;
    FExclusive:                 Boolean;
    FTablePagesCacheManager:    TABSDiskTablePagesCacheManager;

    FOffsetToDBHeader:          Int64;
    FOffsetToCryptoHeader:      Int64;
    FOffsetToLockedBytes:       Int64;
    FOffsetToLastObjectID:      Int64;
    FOffsetToFirstPage:         Int64;

    procedure InitHeaders;

    procedure LoadDBHeader;
    procedure SaveDBHeader(PerformExternalAppUpdateCheck: boolean = True);
    function GetDbHeader: PABSDBHeader;

    procedure LoadCryptoHeader;
    procedure SaveCryptoHeader;

    procedure LoadLockedBytes;
    procedure SaveLockedBytes;

    function GetPassword: PABSPassword;
    function GetEncrypted: Boolean;
    function GetIsOpened: Boolean;
    function IsFileReadOnly(DatabaseFileName: String; var MultiUser: Boolean): Boolean;
    function GetPageOffset(PageNo: TABSPageNo): Int64;
    procedure OpenFileForDesignTime;
    procedure CloseFileForDesignTime;
   protected
    function GetPageCount: TABSPageNo; override;

    function IsSafeNotToSyncPage(SessionID: TABSSessionID; Page: TABSPage): Boolean; override;
    procedure UpdatePageTableState(SessionID: TABSSessionID; Page: TABSPage); override;
   public
    procedure ReadPageRegion(var Buffer; PageNo: TABSPageNo; Offset, Count: Word);
    procedure WritePageRegion(const Buffer; PageNo: TABSPageNo; Offset, Count: Word);

    procedure WriteEmptyPage(PageNo: TABSPageNo);
    procedure InternalAddPage(aPage: TABSPage); override;
    procedure InternalRemovePage(PageNo: TABSPageNo); override;
    function InternalReadPage(aPage: TABSPage): Boolean; override;
    procedure InternalWritePage(aPage: TABSPage); override;
    function InternalReadPageState(PageNo: Integer): Integer;
    procedure InternalWritePageState(PageNo: Integer; PageState: Integer);

    function GetPage(SessionID: TABSSessionID; PageNo: TABSPageNo;
                     PageType: TABSPageTypeID; SynchronizeAllowed: Boolean = True): TABSPage; override;

    procedure RemovePage(SessionID: TABSSessionID; PageNo: TABSPageNo); override;

    // lock Free Space Manager byte
    function LockFreeSpaceManager: Boolean;
    // unlock Free Space Manager byte
    function UnlockFreeSpaceManager: Boolean;
    // lock Free Space Manager byte
    function LockTables: Boolean;
    // unlock Free Space Manager byte
    function UnlockTables: Boolean;
    function LockDBHeader: Boolean;
    function UnlockDBHeader: Boolean;
    // LastObjectID
    function LockLastObjectID: Boolean;
    function UnlockLastObjectID: Boolean;

    // Lock Byte (return TRUE if success)
    function LockPageByte(PageNo: TABSPageNo; Offset: Word): Boolean;
    // Unlock Byte
    function UnlockPageByte(PageNo: TABSPageNo; Offset: Word): Boolean;
    // return True if byte is locked
    function IsPageByteLocked(PageNo: TABSPageNo; Offset: Word): Boolean;
    // return True if any byte of region is locked
    function IsPageRegionLocked(PageNo: TABSPageNo; Offset: Word; Count: Word): Boolean;
   public
    constructor Create;
    destructor Destroy; override;
    procedure CreateAndOpenDatabase(
                              DatabaseFileName: String;
                              Password:         TABSPassword;
                              PageSize:         Word = DefaultPageSize;
                              ExtentPageCount:  Word = DefaultExtentPageCount
                              );
    procedure OpenDatabase(
                DatabaseFileName: String;
                Password:         TABSPassword;
                var ReadOnly:     Boolean;
                var RepairNeeded: Boolean;
                Exclusive:        Boolean;
                var MultiUser:    Boolean
                          );
    procedure CloseDatabase;
    procedure TruncateDatabase;
    // extend file by number of pages specified by PageCount
    procedure ExtendFile(PageCount: TABSPageNo);
    // Truncate file by number of pages specified by PageCount
    procedure TruncateFile(PageCount: TABSPageNo);
    // Increment LastObjectID and return it
    function GetNextObjectID(TryCount: Integer=5; DelayMS: Integer=100): TABSLastObjectID;
    procedure InitObjectID(TryCount: Integer=5; DelayMS: Integer=100);
    procedure ApplyChanges(SessionID: TABSSessionID); override;

   public
    property DatabaseFile: TABSDatabaseFile read FDatabaseFile;
    property DBHeader: PABSDBHeader read GetDbHeader;
    property LockedBytes: TABSLockedBytes read FLockedBytes;
    property Password: PABSPassword read GetPassword;
    property IsOpened: Boolean read GetIsOpened;
    property OffsetToFirstPage: Int64 read FOffsetToFirstPage;
    property OffsetToLockedBytes: Int64 read FOffsetToLockedBytes;
    property FreeSpaceManager: TABSDatabaseFreeSpaceManager read FDatabaseFreeSpaceManager;
    property TablePagesCacheManager: TABSDiskTablePagesCacheManager read FTablePagesCacheManager;
    property Exclusive: Boolean read FExclusive;
    property Encrypted: Boolean read GetEncrypted;
  end; // TABSDiskPageManager


////////////////////////////////////////////////////////////////////////////////
//
// TABSInternalDBTransactedAccessFile
//
////////////////////////////////////////////////////////////////////////////////

  TABSInternalDBTransactedAccessFile = class(TObject)
   private
    LPageManager: TABSDiskPageManager;
    FStartPageNo: TABSPageNo;
    FPageTypeID:  TABSPageTypeID;
   private
    // SetStartPageNo
    procedure SetStartPageNo(StartPageNo: TABSPageNo);
    // raise if file is Closed
    procedure CheckFileOpened(OperationName: String);
    // return Count of File Pages
    function GetPageCountForDataSize(DataSize: Integer): Integer;
    // return Count of File Pages
    procedure GetPageNoAndOffsetForPosition(Position: Integer; var PagePosition: Integer; var Offset: word);
    // return real page No for file page (firstPageNo=0)
    function GoToPage(SessionID: TABSSessionID; PagePosition: TABSPageNo): TABSPageNo;
   public
    // Constructor
    constructor Create(PageManager: TABSDiskPageManager; PageTypeID: TABSPageTypeID);
    // Destructor
    destructor Destroy; override;

    // Create file
    procedure CreateFile(SessionID: TABSSessionID);
    // Delete file. And free all file pages
    procedure DeleteFile(SessionID: TABSSessionID; StartPageNo: TABSPageNo = INVALID_PAGE_NO);
    // Open file (reading file header)
    procedure OpenFile(StartPageNo: TABSPageNo);

    // Set File Size
    procedure SetFileSize(SessionID: TABSSessionID;
                            NewSize: Integer; DecompressedSize: Integer;
                            CompressionAlgorithm: TABSCompressionAlgorithm);
    // Get File Size
    procedure GetFileSize(SessionID: TABSSessionID; var Size: Integer;
                          var DecompressedSize: Integer;
                          var CompressionAlgorithm: TABSCompressionAlgorithm;
                          var FileWasChanged: Boolean;
                          SyncronizeAllowed: Boolean);
    function GetSize(SessionID: TABSSessionID): Integer;

    // Read File Data
    procedure ReadFile(SessionID: TABSSessionID; var Buffer; const Count: Integer; SynchronizeAllowed: Boolean = true);
    // Write File Data

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -