home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / btree / tree / exam52.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-15  |  8.6 KB  |  196 lines

  1. (* EXAM52.PAS *)
  2.  
  3. program driver;
  4. {$R+}
  5.  
  6. uses
  7.     Exam0,
  8.     Btree,
  9.     Compare,
  10.     FileBuff,
  11.     FileDecs,
  12.     Files,
  13.     LRecList,
  14.     Numbers,
  15.     Page,
  16.     VLogical;
  17.  
  18. var
  19.     dataFile,
  20.     indexFile1 : FnString;                      (* holds file name strings *)
  21.  
  22.     testRec : TestRecord;                (* variable to hold a data record *)
  23.  
  24.     lrLst1,
  25.     lrLst2 : LrList;        (* variable which user declares and passes in to
  26.                                LRLIST unit.  LRLIST will build a list of
  27.                                logical records which fulfill a query criteria.
  28.                                (Actually the list will be built by a call
  29.                                to the BTREE unit which in turn uses the LRLIST
  30.                                unit.                                         *)
  31.  
  32.     lrNum : LrNumber;       (* a couple of variable used in this demo program
  33.                                to keep track of logical record number
  34.                                currently being processed.                    *)
  35.  
  36.     tempByte : Byte;        (* this variable is used to pass in the selection
  37.                                criteria for retrievals.  A variable or typed
  38.                                constants must be used.  Constants do not work
  39.                                since the call is by reference and not by
  40.                                value                                         *)
  41.  
  42.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  43.  
  44.  
  45. (* This procedure will be called prior to termination of the program whether
  46.    there is an error or not.  This is a demonstration of a good technique to
  47.    use in conjunction with TBTREE.  Calls to write the buffer to disk and
  48.    close the files should be included.  This is also a place top handle runtime
  49.    error since I do not attempt to deal with errors or maintain global error
  50.    variables in TBTREE.                                                      *)
  51.  
  52. {$F+} procedure MyExit; {$F-}
  53.  
  54.     begin
  55.     ExitProc := ExitSave;           (* reinstall the saved value of ExitProc *)
  56.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  57.                                                       follow along           *)
  58.  
  59.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  60.                                         the program the buffer must be written
  61.                                         to disk or some changes will be lost.
  62.                                         This will cause major problems and lost
  63.                                         data!!!  This is not really required in
  64.                                         in this program since nothing is
  65.                                         modified.  A logical record list is
  66.                                         created, but it happens to be small
  67.                                         enough that a temporary file is not
  68.                                         needed.  If a file was needed, it would
  69.                                         have been destroyed on the call to
  70.                                         DestroyLrList anyway.                *)
  71.  
  72.     Writeln('Closing Files ...');                  (* just a note so you can
  73.                                                       follow along           *)
  74.  
  75.     CloseAllFiles;               (* Close the files to clean up.  It is
  76.                                     important to realize that CloseAllFiles
  77.                                     DOES NOT write the buffer to disk, it only
  78.                                     deals with the files open list.          *)
  79.  
  80.     end;
  81.  
  82.  
  83. (* This routine should be called before anything else happens.  It calls
  84.    the various routines to set up parameters according to values applicable
  85.    to your particular application.                                           *)
  86.  
  87. procedure SetUp;
  88.  
  89.     begin
  90.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  91.                                 of pages in the buffer.  If this is not done a
  92.                                 default of one page is used.  This will cause
  93.                                 poor performance but will not cause any
  94.                                 runtime errors                               *)
  95.  
  96.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  97.                                 of files the FILEBUFF unit can have open at
  98.                                 once.  See FILEBUFF unit for details.        *)
  99.  
  100.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  101.                                      buffered in the page buffer and will only
  102.                                      written to disk when they are swapped out
  103.                                      or explicitly written out by a user
  104.                                      request                                 *)
  105.  
  106.     end;
  107.  
  108.  
  109. (* This procedure initializes the file names as required.  Also, if will
  110.    create the files if they do not exist.  Therefore, you can use one
  111.    initialization routine whether the file exists or must be created.        *)
  112.  
  113. procedure InitFiles;
  114.  
  115.     begin
  116.  
  117.     dataFile := 'myFile1.dat';
  118.     if not FileExists(dataFile) then
  119.         begin
  120.         VLRCreateDataFile(dataFile);
  121.         end;
  122.  
  123.     indexFile1 := 'testByte.idx';
  124.     if not FileExists(indexFile1) then
  125.         begin
  126.         CreateIndexFile(indexfile1,SizeOf(BYTE),BYTEVALUE);
  127.         end;
  128.  
  129.     end;
  130.  
  131. (*****************************************************************************)
  132. (*                                                                           *)
  133. (*                         M A I N      P R O G R A M                        *)
  134. (*                                                                           *)
  135. (*****************************************************************************)
  136.  
  137. begin
  138.  
  139. Writeln('Setting up termination routine ...');     (* just a note so you can
  140.                                                       follow along           *)
  141.  
  142. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  143. ExitProc := @MyExit;
  144.  
  145. Writeln('Initializing Parameters ...');            (* just a note so you can
  146.                                                       follow along           *)
  147.  
  148. SetUp;              (* set file open buffer size and page buffer size limits *)
  149.                     (* set immediate disk write parameter as well            *)
  150.  
  151.  
  152. InitFiles;
  153.  
  154. Writeln('Looking for logical records which match selection criterion and',
  155.          ' building logical record list ... this may take a minute ...' );
  156.                                                    (* just a note so you can
  157.                                                       follow along           *)
  158.  
  159. tempByte := 255;         (* since we are checking for existence, this is not
  160.                             required.  tempByte must still be included in the
  161.                             call however.  It will be ignored.               *)
  162.  
  163. GetValuesFromBTree(indexFile1,tempByte,EX,lrLst1); (* build list of logical
  164.                                                       record numbers which
  165.                                                       exist (ctriterion is
  166.                                                       EX which means exists *)
  167.  
  168. Writeln('Total records found matching criteria = ',GetCountLr(lrLst1));
  169.  
  170.     (* The following loop will fetch the data records associated with the
  171.        record numbers found in the newly created logical record list.  Each
  172.        of the records will be printed.  Since the list was built using
  173.        indexFile1 which is the index corresponding to the randByte field in
  174.        testRec, the records will be printed in ascending order by
  175.        testRec.randByte.  If the opposite order was desired the list should
  176.        be traversed in reverse by using GetLastLr and GetPrevLr.             *)
  177.  
  178.  
  179. lrNum := GetFirstLr(lrLst1);  (* get the first record number and set the cursor
  180.                                  the front of the list.                      *)
  181.  
  182. while lrNum <> 0 do
  183.     begin
  184.  
  185.     VLRGetALogicalRecord(dataFile,     (* variable holding name of data file *)
  186.                          lrNum,           (* logical record number from list *)
  187.                          testRec);                  (* place to put the data *)
  188.  
  189.     Write(VLRGetDataRecordSize(dataFile,lrNum):4,'    ');
  190.     Writeln(lrNum:5,'       ',testRec.randByte:3,'      ',testrec.randString);
  191.  
  192.     lrNum := GetNextLr(lrLst1);        (* advance the cursor to next in list *)
  193.     end;
  194.  
  195. end.
  196.