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

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