home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / EXAM7.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-05-18  |  7.9 KB  |  193 lines

  1. (* EXAMPLE7.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.  
  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.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  47.  
  48.  
  49. (* This procedure will be called prior to termination of the program whether
  50.    there is an error or not.  This is a demonstration of a good technique to
  51.    use in conjunction with BTree10.  Calls to write the buffer to disk and
  52.    close the files should be included.  This is also a place top handle runtime
  53.    error since I do not attempt to deal with errors or maintain global error
  54.    variables in TBTree11.                                                    *)
  55.  
  56. {$F+} procedure MyExit; {$F-}
  57.  
  58.     begin
  59.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  60.                                                       follow along           *)
  61.  
  62.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  63.                                         the program the buffer must be written
  64.                                         to disk or some changes will be lost.
  65.                                         This will cause major problems and lost
  66.                                         data!!!  This is not really required in
  67.                                         in this program since nothing is
  68.                                         modified.  A logical record list is
  69.                                         created, but it happens to be small
  70.                                         enough that a temporary file is not
  71.                                         needed.  If a file was needed, it would
  72.                                         have been destroyed on the call to
  73.                                         DestroyLrList anyway.                *)
  74.  
  75.     Writeln('Closing Files ...');                  (* just a note so you can
  76.                                                       follow along           *)
  77.     CloseAllFiles;               (* Close the files to clean up.  It is
  78.                                     important to realize that CloseAllFiles
  79.                                     DOES NOT write the buffer to disk, it only
  80.                                     deals with the files open list.          *)
  81.  
  82.     end;
  83.  
  84.  
  85. (* This routine should be called before anything else happens.  It calls
  86.    the various routines to set up parameters according to values applicable
  87.    to your particular application.                                           *)
  88.  
  89. procedure SetUp;
  90.  
  91.     begin
  92.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  93.                                 of pages in the buffer.  If this is not done a
  94.                                 default of one page is used.  This will cause
  95.                                 poor performance but will not cause any
  96.                                 runtime errors                               *)
  97.  
  98.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  99.                                 of files the FILEBUFF unit can have open at
  100.                                 once.  See FILEBUFF unit for details.        *)
  101.  
  102.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  103.                                      buffered in the page buffer and will only
  104.                                      written to disk when they are swapped out
  105.                                      or explicitly written out by a user
  106.                                      request                                 *)
  107.  
  108.     end;
  109.  
  110.  
  111. (* This procedure adjusts the file names as required.  It appends the proper
  112.    extension to the name.                                                    *)
  113.  
  114. procedure InitFiles;
  115.  
  116.     begin
  117.  
  118.     dataFile := 'myFile1';
  119.     FixFileName(dataFile,DATA);
  120.  
  121.     indexFile1 := 'testByte';
  122.     FixFileName(indexFile1,INDEX);
  123.  
  124.     indexFile2 := 'testStrg';
  125.     FixFileName(indexFile2,INDEX);
  126.  
  127.     end;
  128.  
  129.  
  130.  
  131. begin
  132.  
  133. Writeln('Setting up termination routine ...');     (* just a note so you can
  134.                                                       follow along           *)
  135.  
  136. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  137. ExitProc := @MyExit;
  138.  
  139. Writeln('Initializing Parameters ...');            (* just a note so you can
  140.                                                       follow along           *)
  141.  
  142. SetUp;              (* set file open buffer size and page buffer size limits *)
  143.                     (* set immediate disk write parameter as well            *)
  144.  
  145.  
  146. InitFiles;                                       (* fix file and index names *)
  147.  
  148. CreateIndex(indexFile1,SizeOf(Byte),dataFile,BYTEVALUE);
  149.  
  150. CreateIndex(indexFile2,SizeOf(TestString),dataFile,STRINGVALUE);
  151.  
  152. Writeln('Looking for all logical records presently in use for our data file');
  153.                                                    (* just a note so you can
  154.                                                       follow along           *)
  155.  
  156. GetValidLogicalRecords(dataFile,lrLst);        (* get a list of all existing
  157.                                                    data records in this file *)
  158.  
  159. Writeln('Found them - rebuilding the indexes ...');
  160.  
  161.     (* The following loop will place the appropriate data in the two indexes.
  162.        This is an easy way to build an index from an existing data file.  This
  163.        same technique can be used to rebuild an index if it somehow becomes
  164.        damaged.                                                              *)
  165.  
  166.  
  167. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  168.                                  to the front of the list.                   *)
  169.  
  170. while lrNum <> 0 do
  171.     begin                   (* we need to fetch the record and print it out  *)
  172.  
  173.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  174.                       lrNum,                      (* logical record to fetch *)
  175.                       testRec,                      (* place to put the data *)
  176.                       SizeOf(testRec));       (* number of bytes to retrieve *)
  177.  
  178.     InsertValueInBTree(indexFile1,lrNum,testRec.randByte);
  179.     InsertValueInBTree(indexFile2,lrNum,testRec.randString);
  180.  
  181.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  182.  
  183.     end;
  184.  
  185. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  186.                            is important so that there aren't a bunch of
  187.                            temporary files upon termination.  Also, lists
  188.                            take up data space and also space in the page
  189.                            buffer                                            *)
  190.  
  191. end.
  192.  
  193.