home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / EXAM5.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-04-29  |  8.4 KB  |  197 lines

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