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

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