-- File DBFileImpl.Mesa
-- Last edited by:
--   MBrown on February 22, 1983 11:59 am
--   Willie-sue, February 3, 1983 10:46 am

  DIRECTORY
    DBCommon,
    DBFile,
    DBFileAlpine,
    DBFilePilot,
    Rope;

DBFileImpl: PROGRAM
  IMPORTS
    DBFileAlpine,
    DBFilePilot,
    Rope
  EXPORTS
    DBFile
  = BEGIN
  ROPE: TYPE = Rope.ROPE;
  OpenFileHandle: TYPE = DBCommon.OpenFileHandle;
  Trans: TYPE = DBCommon.Trans;
  FileSystem: TYPE = DBCommon.FileSystem;
  SegmentIndex: TYPE = DBCommon.SegmentIndex;
  VersionOptions: TYPE = DBCommon.VersionOptions;
  DBPage: TYPE = DBCommon.DBPage;
  NullDBPage: DBPage = DBCommon.NullDBPage;

  FileServerFromFileName: PUBLIC PROC [file: ROPE]
    RETURNS [server: ROPE] = {
    rightSquareBracket: INT;
    IF file.Fetch[0] # '[ THEN ERROR;
    rightSquareBracket ← file.SkipTo[1, "]"];
    IF rightSquareBracket = file.Length[] THEN ERROR;
    RETURN [file.Substr[start: 1, len: rightSquareBracket-1]];
    };

  PagesFromBytes: PUBLIC PROC [bytes: INT]
    RETURNS [pages: CARDINAL] = {
    IF bytes < 0 OR bytes > LONG[CARDINAL.LAST - 1]*DBCommon.BytesPerPage THEN
      ERROR;
    RETURN [(bytes+DBCommon.BytesPerPage-1)/DBCommon.BytesPerPage];
    };

  CreateTransaction: PUBLIC PROC [server: ROPE] RETURNS [t: Trans] = {
    IF FileSystemFromServerName[server] = pilot THEN
      RETURN [DBFilePilot.CreateTransaction[]]
    ELSE
      RETURN [DBFileAlpine.CreateTransaction[server]];
    };
  
  FileSystemFromServerName: PROC [server: ROPE]
    RETURNS [fileSystem: FileSystem] = {
    IF Rope.Equal[s1: server, s2: "Local", case: FALSE] THEN RETURN [pilot]
    ELSE RETURN [alpine];
    };

  FinishTransaction: PUBLIC PROC [t: Trans, abort: BOOL, continue: BOOL] = {
    WITH t SELECT FROM
      pt: DBFilePilot.PilotTrans => DBFilePilot.FinishTransaction[pt, abort, continue];
      ENDCASE => DBFileAlpine.FinishTransaction[t, abort, continue];
    };

  OpenFile: PUBLIC PROC [t: Trans, file: ROPE,
    version: VersionOptions, discardFileContents: BOOL, nPagesInitial: INT, noLog: BOOL]
    RETURNS [f: OpenFileHandle, createdFile: BOOL] = {
    flatFile: Rope.Text = Rope.Flatten[file];
    WITH t SELECT FROM
      pt: DBFilePilot.PilotTrans => [f, createdFile] ←
        DBFilePilot.OpenFile[pt, flatFile, version, nPagesInitial];
      ENDCASE => [f, createdFile] ←
        DBFileAlpine.OpenFile[t, flatFile, version, discardFileContents, nPagesInitial, noLog];
    };

  ReadFilePage: PUBLIC PROC [
    f: OpenFileHandle, p: CARDINAL, corePage: LONG POINTER] = {
    WITH f SELECT FROM
      pf: DBFilePilot.PilotOpenFileHandle =>
        DBFilePilot.ReadFilePage[pf, p, corePage];
      ENDCASE => DBFileAlpine.ReadFilePage[f, p, corePage];
    };

  WriteFilePage: PUBLIC PROC [
    f: OpenFileHandle, p: CARDINAL, corePage: LONG POINTER] = {
    WITH f SELECT FROM
      pf: DBFilePilot.PilotOpenFileHandle =>
        DBFilePilot.WriteFilePage[pf, p, corePage];
      ENDCASE => DBFileAlpine.WriteFilePage[f, p, corePage];
    };

  GetSize: PUBLIC PROC [f: OpenFileHandle] RETURNS [nPages: CARDINAL] = {
    WITH f SELECT FROM
      pf: DBFilePilot.PilotOpenFileHandle => RETURN[DBFilePilot.GetSize[pf]];
      ENDCASE => RETURN[DBFileAlpine.GetSize[f]];
    };

  SetSize: PUBLIC PROC [f: OpenFileHandle, nPages: CARDINAL] = {
    WITH f SELECT FROM
      pf: DBFilePilot.PilotOpenFileHandle => DBFilePilot.SetSize[pf, nPages];
      ENDCASE => DBFileAlpine.SetSize[f, nPages];
    };

  END.


CHANGE LOG

Created by MBrown on February 26, 1981  10:17 PM

Changed by MBrown on February 28, 1981  5:15 PM
-- Created Pilot version: Pilot file system is server named "Local".

Changed by MBrown on 19-Jun-81 16:22:29
-- Conversion to Cedar (use Rope.Match, make fileProcs a REF.)

Changed by Willie-Sue on June 24, 1982 12:11 pm
--  Rope.Ref => Rope.ROPE

Changed by MBrown on November 29, 1982 11:18 am
-- Allow multiple file systems active at once.

Changed by MBrown on January 15, 1983 8:10 pm
-- Enable Alpine files (used to ERROR).

Changed by Willie-Sue on February 3, 1983
--  added noLog arg to OpenFile

Changed by MBrown on February 7, 1983 12:19 pm
-- Add discardFileContents parm to OpenFile, flush Juniper support, flush initialized
--array (was used to init Pine).

Changed by MBrown on February 22, 1983 11:58 am
-- Eliminate compilation dependency on AlpineEnvironment.