home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / btree / tree / exam51.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-15  |  7.4 KB  |  189 lines

  1. (* EXAM51.PAS *)
  2.  
  3. program driver;
  4. {$R+}
  5.  
  6. uses
  7.     Exam0,
  8.     Btree,
  9.     Compare,
  10.     FileBuff,
  11.     FileDecs,
  12.     Files,
  13.     VLogical,
  14.     LRecList,
  15.     Numbers,
  16.     Page;
  17.  
  18. var
  19.     dataFile,
  20.     indexFile1 : FnString;                      (* holds file name strings *)
  21.  
  22.     testRec : TestRecord;                (* variable to hold a data record *)
  23.  
  24.     recCnt : 1 .. TOTALRECORDS;
  25.  
  26.     lrNum : LrNumber;
  27.  
  28.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  29.  
  30.  
  31. (* This procedure will be called prior to termination of the program whether
  32.    there is an error or not.  This is a demonstration of a good technique to
  33.    use in conjunction with TBTREE.  Calls to write the buffer to disk and
  34.    close the files should be included.  This is also a place top handle runtime
  35.    error since I do not attempt to deal with errors or maintain global error
  36.    variables in TBTREE.                                                      *)
  37.  
  38. {$F+} procedure MyExit; {$F-}
  39.  
  40.     begin
  41.     ExitProc := ExitSave;           (* reinstall the saved value of ExitProc *)
  42.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  43.                                                       follow along           *)
  44.  
  45.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  46.                                         the program the buffer must be written
  47.                                         to disk or some changes will be lost.
  48.                                         This will cause major problems and lost
  49.                                         data!!!  This is not really required in
  50.                                         in this program since nothing is
  51.                                         modified.  A logical record list is
  52.                                         created, but it happens to be small
  53.                                         enough that a temporary file is not
  54.                                         needed.  If a file was needed, it would
  55.                                         have been destroyed on the call to
  56.                                         DestroyLrList anyway.                *)
  57.  
  58.     Writeln('Closing Files ...');                  (* just a note so you can
  59.                                                       follow along           *)
  60.  
  61.     CloseAllFiles;               (* Close the files to clean up.  It is
  62.                                     important to realize that CloseAllFiles
  63.                                     DOES NOT write the buffer to disk, it only
  64.                                     deals with the files open list.          *)
  65.  
  66.     end;
  67.  
  68.  
  69. (* This routine should be called before anything else happens.  It calls
  70.    the various routines to set up parameters according to values applicable
  71.    to your particular application.                                           *)
  72.  
  73. procedure SetUp;
  74.  
  75.     begin
  76.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  77.                                 of pages in the buffer.  If this is not done a
  78.                                 default of one page is used.  This will cause
  79.                                 poor performance but will not cause any
  80.                                 runtime errors                               *)
  81.  
  82.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  83.                                 of files the FILEBUFF unit can have open at
  84.                                 once.  See FILEBUFF unit for details.        *)
  85.  
  86.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  87.                                      buffered in the page buffer and will only
  88.                                      written to disk when they are swapped out
  89.                                      or explicitly written out by a user
  90.                                      request                                 *)
  91.  
  92.     end;
  93.  
  94.  
  95. (* This procedure creates the one data file and the two corresponding
  96.    index files.  It will overwrite the files if they happen to already
  97.    exist.                                                                    *)
  98.  
  99. procedure InitFiles;
  100.  
  101.     begin
  102.  
  103.     dataFile := 'myFile1.dat';
  104.     VLRCreateDataFile(dataFile);
  105.  
  106.     indexFile1 := 'testByte.idx';
  107.     CreateIndexFile(indexFile1,SizeOf(Byte),BYTEVALUE);
  108.  
  109.     end;
  110.  
  111.  
  112. (* This routine creates a random string of randon length and is used for
  113.    creating strings to demonstrate the handling of variable length records   *)
  114.  
  115. procedure CreateRandomString(var str : TestString);
  116.  
  117. var
  118.     chrCnt : 1 .. TESTSTRINGSIZE;
  119.     tss : Byte;
  120.  
  121.     begin
  122.     str := '';
  123.     tss := 0;
  124.     for chrCnt := 1 to Random(TESTSTRINGSIZE) + 1 do
  125.         begin
  126.         str[chrCnt] := Chr(Random(25) + 65);
  127.         Inc(tss);
  128.         end;
  129.     Move(tss,str,1);
  130.     end;
  131.  
  132.  
  133. (*****************************************************************************)
  134. (*                                                                           *)
  135. (*                         M A I N      P R O G R A M                        *)
  136. (*                                                                           *)
  137. (*****************************************************************************)
  138.  
  139. begin
  140.  
  141. Writeln('Setting up termination routine ...');     (* just a note so you can
  142.                                                       follow along           *)
  143.  
  144. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  145. ExitProc := @MyExit;
  146.  
  147. Writeln('Initializing Parameters ...');            (* just a note so you can
  148.                                                       follow along           *)
  149.  
  150. SetUp;              (* set file open buffer size and page buffer size limits *)
  151.                     (* set immediate disk write parameter as well            *)
  152.  
  153. Writeln('Creating Files ...');                     (* just a note so you can
  154.                                                       follow along           *)
  155.  
  156.  
  157. InitFiles;                                   (* create the files and indexes *)
  158.  
  159. Randomize;                       (* needed only for the random string creation
  160.                                     routine and has nothing to do with
  161.                                     TBTree                                   *)
  162.  
  163. Writeln;
  164.  
  165. Writeln('Creating and storing data ... this may take a minute ...');
  166.                                                    (* just a note so you can
  167.                                                       follow along           *)
  168.  
  169.     (* the following loop will create records which will be inserted into
  170.        the data file.  The appropriate values are also inserted into the
  171.        two index files.                                                      *)
  172.  
  173. for recCnt := 1 to TOTALRECORDS do
  174.     begin
  175.     testRec.randByte := Random(MAXBYTE + 1);    (* random byte from 0 .. 255 *)
  176.     CreateRandomString(testRec.randString);
  177.  
  178.     lrNum := VLRStoreNewLogicalRecord(dataFile,testRec,
  179.                                       SizeOf(testRec.randByte) +
  180.                                       Length(testRec.randString) + 1);
  181.                                (* insert in variable length record data file
  182.                                (* notice how each record will be different
  183.                                   size                                       *)
  184.     InsertValueInBTree(indexFile1,lrNum,testRec.randByte);
  185.  
  186.     end;
  187.  
  188. end.
  189.