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

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