-- File: DBStoragePrintImpl.mesa
-- Last edited by:
--   MBrown on February 27, 1981  3:41 PM
--   Cattell on June 16, 1983 3:31 pm
--   Willie-Sue on June 30, 1982 4:31 pm


DIRECTORY
  IO,
  DBCommon,
  DBEnvironment ,
  DBSegment,
  DBStats,
  DBStorageExtTuple,
  DBStorageGroup,
  DBStorageGroupScan,
  DBStoragePagetags,
  DBStoragePrint,
  DBStorageString,
  DBStorageTID,
  DBStorageTSDict,
  DBStorageTuple,
  DBStorageVec,
  DBStorageVectags;

DBStoragePrintImpl: PROGRAM
  IMPORTS
    DBCommon,
    DBEnvironment,
    DBSegment,
    DBStats,
    DBStorageGroup,
    DBStorageGroupScan,
    DBStorageTSDict,
    DBStorageTuple,
    DBStorageVec,
    IO
  EXPORTS DBStoragePrint =

BEGIN OPEN IO, DBStorageTID;

  Assert: PROC[condition: BOOLEAN] = {IF ~condition THEN ERROR};

  Print: PROC [] = {
    -- Maximum printing with minimum keystrokes
    DBSegment.CheckState[doPrinting: TRUE, printOnlyLockedPages: FALSE];
    DBStorageTuple.CheckState[doPrinting: TRUE];
    DBStorageGroupScan.CheckState[doPrinting: TRUE];
    DBStats.Print[heading: "DBStorageDebugImpl.Print", out: DBCommon.GetDebugStream[]];
    };

  PrintPage: PUBLIC PROC[
    dbPage: DBCommon.DBPage, p: LONG POINTER, fullPrint: BOOLEAN] = {
    SELECT DBStorageVec.TagOfPage[LOOPHOLE[p]] FROM
      DBStoragePagetags.Unused,
      DBStoragePagetags.Free,
      DBStoragePagetags.AMap,
      DBStoragePagetags.BTree => PrintPageType[dbPage, p, fullPrint];
      DBStoragePagetags.Tuple,
      DBStoragePagetags.SystemTuple => PrintVecPage[dbPage, LOOPHOLE[p], fullPrint];
      DBStoragePagetags.OverflowTuple => PrintOverflowPage[dbPage, LOOPHOLE[p], fullPrint];
    ENDCASE => PrintPageUnknown[dbPage, p, fullPrint];
    DBCommon.GetDebugStream[].PutF["*n"];
  };--PrintPage

  PrintPageType: PROC[dbPage: DBCommon.DBPage, p: LONG POINTER, fullPrint: BOOLEAN] = {
    DBCommon.GetDebugStream[].PutF["%bB: a %g", card[dbPage], IO.rope[PagetagToRope[DBStorageVec.TagOfPage[LOOPHOLE[p]]]]];
  };--PrintPageType

  PrintPageUnknown: PROC[dbPage: DBCommon.DBPage, p: LONG POINTER, fullPrint: BOOLEAN] = {
    DBCommon.GetDebugStream[].PutF["%bB: a page of type %g, an unrecognized type", card[dbPage],
           card[DBStorageVec.TagOfPage[LOOPHOLE[p]]]];
  };--PrintPageUnknown

  PrintVecPage: PROC[dbPage: DBCommon.DBPage, p: LONG POINTER, fullPrint: BOOLEAN] = {
    debugS: IO.STREAM← DBCommon.GetDebugStream[];
    debugS.PutF["%bB: a %g with %g words in free vec space", card[dbPage],
     IO.rope[PagetagToRope[DBStorageVec.TagOfPage[p]]], card[DBStorageVec.WordsLeftOnPage[p]]];
    IF ~fullPrint THEN RETURN;
    debugS.PutF["*n"];
    FOR slotIndex: CARDINAL IN [1 .. DBStorageVec.HighSlotIndexOfPage[p]] DO
      debugS.PutF["%g: ", card[slotIndex]];
      SELECT DBStorageVec.TypeOfSlot[p, slotIndex] FROM
        DBStorageVec.FreeType => PrintFreeSlot[];
        DBStorageVec.UnFreeType => PrintUnFreeSlot[];
        DBStorageVectags.TSDictType => PrintTSDict[p, slotIndex];
        DBStorageVectags.LString => PrintLString[p, slotIndex];
        IN [1..DBStorageVectags.MaxTuplesetPerPage] => PrintTupleVec[p, slotIndex];
      ENDCASE => PrintUnimplemented[DBStorageVec.TypeOfSlot[p, slotIndex]];
    ENDLOOP;
  };--PrintVecPage

  PrintOverflowPage: PROC[dbPage: DBCommon.DBPage, p: LONG POINTER, fullPrint: BOOLEAN] = {
    debugStream: IO.STREAM← DBCommon.GetDebugStream[];
    debugStream.PutF["%bB: a string overflow page with %g words in free vec space",
           card[dbPage], card[DBStorageVec.WordsLeftOnPage[p]]];
    IF ~fullPrint THEN RETURN;
    debugStream.PutF["*n"];
    FOR slotIndex: CARDINAL IN [1 .. DBStorageVec.HighSlotIndexOfPage[p]] DO
      debugStream.PutF["%g: ", card[slotIndex]];
      SELECT DBStorageVec.TypeOfSlot[p, slotIndex] FROM
        DBStorageVectags.EString => PrintEString[p, slotIndex];
      ENDCASE => PrintUnimplemented[DBStorageVec.TypeOfSlot[p, slotIndex]];
    ENDLOOP;
  };--PrintOverflowPage

  PrintEString: PROC[p: LONG POINTER TO DBStorageVec.VecPage, slotIndex: CARDINAL] = {
    eString: LONG POINTER TO DBStorageString.EString ←
     LOOPHOLE[DBStorageVec.VecOfSlot[p, slotIndex]];
    eStringID: TID ← eString.eStringID;
    nWordsForText: CARDINAL ← eString.header.length - DBStorageString.SizeOfNullEString;
    debugStream: IO.STREAM← DBCommon.GetDebugStream[];
    debugStream.PutF["External string descriptor, eStringID: %bB, nBytes: %g*n",
           card[eStringID], card[2*nWordsForText]];
    debugStream.PutF["     text: """];
    FOR i: CARDINAL IN [0..nWordsForText*2) DO
      debugStream.PutF["%g", char[eString.text[i]]];
      ENDLOOP;
    debugStream.PutF["""*n"];
  };--PrintEString

  PrintLString: PROC[p: LONG POINTER TO DBStorageVec.VecPage, slotIndex: CARDINAL] = {
    lString: LONG POINTER TO DBStorageString.LString ←
     LOOPHOLE[DBStorageVec.VecOfSlot[p, slotIndex]];
    eStringID: TID ← lString.eStringID;
    DBCommon.GetDebugStream[].PutF["Local string descriptor, bytesInRemString: %g, eStringID: %bB*n",
     card[lString.bytesInRemString], card[eStringID]];
  };--PrintLString

  PrintTSDict: PROC[p: LONG POINTER TO DBStorageVec.VecPage, slotIndex: CARDINAL] = {
    pDict: LONG POINTER TO DBStorageTSDict.TSDict ←
     LOOPHOLE[DBStorageVec.VecOfSlot[p, slotIndex]];
    nEntries: CARDINAL ← DBStorageTSDict.NEntries[pDict];
    entry: CARDINAL;
    debugStream: IO.STREAM← DBCommon.GetDebugStream[];
    Assert[slotIndex = DBStorageTSDict.TSDictSlotIndex];
    debugStream.PutF["Tupleset dictionary containing %g entry(s):*n", card[nEntries]];
    FOR entry IN [1..nEntries] DO
      debugStream.PutF["  %g:", card[entry]];
      debugStream.PutF["   TuplesetID: %bB,", card[pDict.seq[entry].tuplesetID]];
      debugStream.PutF[" NextPage: %bB,", card[pDict.seq[entry].next]];
      debugStream.PutF[" PrevPage: %bB*n", card[pDict.seq[entry].prev]];
    ENDLOOP;
  };--PrintTSDict

  PrintTupleVec: PROC[p: LONG POINTER TO DBStorageVec.VecPage, slotIndex: CARDINAL] = {
    pTuple: LONG POINTER TO DBStorageExtTuple.TupleBody ←
     LOOPHOLE[DBStorageVec.VecOfSlot[p, slotIndex]];
    nDataWords: CARDINAL ← pTuple.groupOffset - DBStorageExtTuple.SizeOfNullTuple;
    groupList: LONG DESCRIPTOR FOR DBStorageGroup.GroupList ←
     DBStorageGroup.GroupListFromTupleBase[pTuple];
    dataword, entry: CARDINAL;
    debugStream: IO.STREAM← DBCommon.GetDebugStream[];
    debugStream.PutF["Tuplevec of local type %g containing %g words fixed data, %g group list entry(s)*n",
           card[DBStorageVec.TypeOfSlot[p, slotIndex]], card[nDataWords], card[LENGTH[groupList]]];
    debugStream.PutF["  data:"];
    FOR dataword IN [0..nDataWords) DO debugStream.PutF["%b,", card[pTuple.fields[dataword]]]; ENDLOOP;
    debugStream.PutF["*n"];
    FOR entry IN [0..LENGTH[groupList]) DO
      debugStream.PutF["  %g:", card[entry]];
      debugStream.PutF["   GroupID: %bB,", card[groupList[entry].groupID]];
      debugStream.PutF[" FirstTuple: %bB,", card[groupList[entry].firstTID]];
      debugStream.PutF[" LastTuple: %bB*n", card[groupList[entry].lastTID]];
    ENDLOOP;
  };--PrintTupleVec

  PrintFreeSlot: PROC = {
    DBCommon.GetDebugStream[].PutF["Free slot*n"];
  };--PrintFreeSlot

  PrintUnFreeSlot: PROC = {
    DBCommon.GetDebugStream[].PutF["UnFree slot (this is an debugStream)*n"];
  };--PrintUnFreeSlot

  PrintUnimplemented: PROC[slotType: CARDINAL] = {
    DBCommon.GetDebugStream[].PutF["Slot type is %g, what does THAT mean?*n", card[slotType]];
  };--PrintUnimplemented

  PagetagToRope: PROC [tag: CARDINAL] RETURNS [ROPE] = {
    RETURN [SELECT tag FROM
      DBStoragePagetags.Unused => "unused page",
      DBStoragePagetags.Free => "free page",
      DBStoragePagetags.AMap => "AMap page",
      DBStoragePagetags.Tuple => "user tuple page",
      DBStoragePagetags.SystemTuple => "system tuple page",
      DBStoragePagetags.BTree => "BTree page",
      ENDCASE => ERROR];
  };--PagetagToString



END.--StoragePrintImpl


CHANGE LOG

Created by MBrown on May 27, 1980  11:04 AM

Changed by MBrown on May 28, 1980  9:59 AM
-- Still working on it...

Changed by MBrown on June 1, 1980  12:33 PM
-- Changed for new definition of GroupListOfTuple.

Changed by MBrown on June 18, 1980  8:19 PM
-- Changed for rearrangements in storage level.

Changed by MBrown on August 2, 1980  10:37 PM
-- Changed for change of interface: this module now is to print any type of page, not just vec
--pages.  Right now it does no more than print a string name for the page tag if the page has
--some other type (e.g. AMap, BTree).

Changed by MBrown on August 23, 1980  10:39 AM
-- Types that used to be in DBStoragePrivateB have now moved to separate defs.

Changed by MBrown on August 23, 1980  12:18 PM
-- Added printing of LStrings and EStrings.

Changed by Cattell on 30-Dec-81 10:35:01
-- Changed PF format codes to IOStream format codes.

Changed by Willie-Sue on June 24, 1982 12:07 pm
-- IOStream => IO

Changed by Willie-Sue on June 30, 1982 4:31 pm
-- debugStream => DBCommon.GetDebugStream[]