home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / EXAM2.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-03-07  |  9.0 KB  |  215 lines

  1. (* EXAMPLE2.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.     tempByte : Byte;        (* this variable is used to pass in the selection
  47.                                criteria for retrievals.  A variable or typed
  48.                                constants must be used.  Constants do not work
  49.                                since the call is by reference and not by
  50.                                value                                         *)
  51.  
  52.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  53.  
  54.  
  55. (* This procedure will be called prior to termination of the program whether
  56.    there is an error or not.  This is a demonstration of a good technique to
  57.    use in conjunction with BTree10.  Calls to write the buffer to disk and
  58.    close the files should be included.  This is also a place top handle runtime
  59.    error since I do not attempt to deal with errors or maintain global error
  60.    variables in BTree10.                                                     *)
  61.  
  62. {$F+} procedure MyExit; {$F-}
  63.  
  64.     begin
  65.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  66.                                                       follow along           *)
  67.  
  68.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  69.                                         the program the buffer must be written
  70.                                         to disk or some changes will be lost.
  71.                                         This will cause major problems and lost
  72.                                         data!!!  This is not really required in
  73.                                         in this program since nothing is
  74.                                         modified.  A logical record list is
  75.                                         created, but it happens to be small
  76.                                         enough that a temporary file is not
  77.                                         needed.  If a file was needed, it would
  78.                                         have been destroyed on the call to
  79.                                         DestroyLrList anyway.                *)
  80.  
  81.     Writeln('Closing Files ...');                  (* just a note so you can
  82.                                                       follow along           *)
  83.  
  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.  
  139.  
  140. begin
  141.  
  142. Writeln('Setting up termination routine ...');     (* just a note so you can
  143.                                                       follow along           *)
  144.  
  145. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  146. ExitProc := @MyExit;
  147.  
  148. Writeln('Initializing Parameters ...');            (* just a note so you can
  149.                                                       follow along           *)
  150.  
  151. SetUp;              (* set file open buffer size and page buffer size limits *)
  152.                     (* set immediate disk write parameter as well            *)
  153.  
  154.  
  155. InitFiles;                                   (* create the files and indexes *)
  156.  
  157. Writeln('Looking for logical records which match selection criterion and',
  158.          ' building logical record list ... this may take a minute ...' );
  159.                                                    (* just a note so you can
  160.                                                       follow along           *)
  161.  
  162. tempByte := 255;         (* since we are checking for existence, this is not
  163.                             required.  tempByte must still be included in the
  164.                             call however.  It will be ignored.               *)
  165.  
  166.  
  167. GetValuesFromBTree(indexFile1,tempByte,EX,lrLst);  (* build list of logical
  168.                                                       record numbers which
  169.                                                       exist (ctriterion is
  170.                                                       EX which means exists *)
  171.  
  172.     (* The following loop will fetch the data records associated with the
  173.        record numbers found in the newly created logical record list.  Each
  174.        of the records will be printed.  Since the list was built using
  175.        indexFile1 which is the index corresponding to the randByte field in
  176.        testRec, the records will be printed in ascending order by
  177.        testRec.randByte.  If the opposite order was desired the list should
  178.        be traversed in reverse by using GetLastLr and GetPrevLr.             *)
  179.  
  180.  
  181. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  182.                                  the front of the list.                      *)
  183.  
  184. while lrNum <> 0 do
  185.     begin
  186.  
  187.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  188.                       lrNum,              (* logical record number from list *)
  189.                       testRec,                      (* place to put the data *)
  190.                       SizeOf(testRec));       (* number of bytes to retrieve *)
  191.  
  192.     Writeln(lrNum,'        ',testRec.randByte,'       ',testrec.randString);
  193.  
  194.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  195.     end;
  196.  
  197.     (* The following three calls are for demo purposes to show a couple of
  198.        utilities available.                                                  *)
  199.  
  200. Writeln('Total records found matching criteria = ',GetCountLr(lrLst));
  201.  
  202. Writeln('Last record used = ',LastUsedRecord(dataFile));
  203.  
  204. Writeln('Number of files presently open = ',GetNumberOpenFiles);
  205.  
  206.  
  207. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  208.                            is important so that there aren't a bunch of
  209.                            temporary files upon termination.  Also, lists
  210.                            take up data space and also space in the page
  211.                            buffer                                            *)
  212.  
  213.  
  214. end.
  215.