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

  1. (* EXAMPLE1.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.  
  17. const
  18.     TOTALRECORDS = 100;               (* number of records in this example *)
  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.     recCnt : 1 .. TOTALRECORDS;
  37.  
  38.     lrNum : LrNumber;
  39.  
  40.  
  41. (* This routine should be called before anything else happens.  It calls
  42.    the various routines to set up parameters according to values applicable
  43.    to your particular application.                                           *)
  44.  
  45. procedure SetUp;
  46.  
  47.     begin
  48.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  49.                                 of pages in the buffer.  If this is not done a
  50.                                 default of one page is used.  This will cause
  51.                                 poor performance but will not cause any
  52.                                 runtime errors                               *)
  53.  
  54.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  55.                                 of files the FILEBUFF unit can have open at
  56.                                 once.  See FILEBUFF unit for details.        *)
  57.  
  58.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  59.                                      buffered in the page buffer and will only
  60.                                      written to disk when they are swapped out
  61.                                      or explicitly written out by a user
  62.                                      request                                 *)
  63.  
  64.     end;
  65.  
  66.  
  67. (* This procedure creates the one data file and the two corresponding
  68.    index files.  A couple of things should be noted.  First, the files
  69.    have no extensions!  The extensions will be appended during creation
  70.    and the file name variable will be modified.  This will ensure that the
  71.    correct extensions are used.  The extensions are necessary to later
  72.    determine file type and therefore should not be altered.  Secondly,
  73.    the data file needs to be created before the index file because the
  74.    data file name is stored as part of the index.  Presently this feature
  75.    is not used, but I may need it later and decided to include it now.  If
  76.    you declare the data file after the index file, the name stored will not
  77.    have the appropriate extension.  Thirdly, the user does not create the
  78.    bitmap files for the data and index files.  As a matter of fact, the
  79.    user NEVER gets involved with bitmap files directly.  Ladtly, notice the
  80.    use of SizeOf to pass in the size of TestString.  This will alleviate
  81.    problems with incorrectly calculation size of data types, especially
  82.    strings.                                                                  *)
  83.  
  84. procedure InitFiles;
  85.  
  86.     begin
  87.  
  88.     dataFile := 'myFile1';
  89.     CreateFile(dataFile,DATA);
  90.  
  91.     indexFile1 := 'testByte';
  92.     CreateIndex(indexFile1,SizeOf(Byte),dataFile,BYTEVALUE);
  93.  
  94.     indexfile2 := 'testStrg';
  95.     CreateIndex(indexFile2,SizeOf(TestString),dataFile,STRINGVALUE);
  96.  
  97.     end;
  98.  
  99.  
  100. (* This routine creates a random string and is used for creating strings to
  101. demonstrate the handling of strings in indexes                               *)
  102.  
  103. procedure CreateRandomString(var str : TestString);
  104.  
  105. var
  106.     chrCnt : 1 .. TESTSTRINGSIZE;
  107.     tss : Byte;
  108.  
  109.     begin
  110.     str := '';
  111.     for chrCnt := 1 to TESTSTRINGSIZE do
  112.         begin
  113.         str[chrCnt] := Chr(Random(25) + 65);
  114.         end;
  115.     tss := TESTSTRINGSIZE;
  116.     Move(tss,str,1);
  117.     end;
  118.  
  119.  
  120.  
  121. begin
  122.  
  123. Writeln('Initializing Parameters ...');            (* just a note so you can
  124.                                                       follow along           *)
  125.  
  126. SetUp;              (* set file open buffer size and page buffer size limits *)
  127.                     (* set immediate disk write parameter as well            *)
  128.  
  129. Writeln('Creating Files ...');                     (* just a note so you can
  130.                                                       follow along           *)
  131.  
  132.  
  133. InitFiles;                                   (* create the files and indexes *)
  134.  
  135. Randomize;                       (* needed only for the random string creation
  136.                                     routine and has nothing to do with
  137.                                     BTree10                                  *)
  138.  
  139. Writeln('The number of levels in the string index is -> ',
  140.         NumberOfBTreeLevels(indexFile2));
  141. Writeln;
  142.  
  143. Writeln('Creating and storing data ... this may take a minute ...');
  144.                                                    (* just a note so you can
  145.                                                       follow along           *)
  146.  
  147.     (* the following loop will create 100 records which will be inserted into
  148.        the data file.  The appropriate values are also inserted into the
  149.        two index files.                                                      *)
  150.  
  151. for recCnt := 1 to TOTALRECORDS do
  152.     begin
  153.  
  154.     testRec.randByte := Random(MAXBYTE + 1);    (* random byte from 1 .. 255 *)
  155.     CreateRandomString(testRec.randString);   (* random string of 10 letters *)
  156.  
  157.     lrNum := StoreNewLogicalRecord(dataFile,testRec,SizeOf(testRec));
  158.                                                                    (* insert
  159.                                                                       in data
  160.                                                                       file   *)
  161.  
  162.     InsertValueInBTree(indexFile1,lrNum,testRec.randByte);
  163.     InsertValueInBTree(indexFile2,lrNum,testRec.randString);
  164.  
  165.     end;
  166.  
  167. Writeln;
  168. Writeln('The number of levels in the string index is -> ',
  169.         NumberOfBTreeLevels(indexFile2));
  170. Writeln;
  171.  
  172. Writeln('Writing Records To Disk ...');            (* just a note so you can
  173.                                                       follow along           *)
  174.  
  175. WriteEntireBufferToDisk;         (* very important step!!  Before leaving the
  176.                                     program the buffer must be written to disk
  177.                                     or some changes will be lost.  This will
  178.                                     cause major problems and lost data!!!    *)
  179.  
  180. Writeln('Closing Files ...');                      (* just a note so you can
  181.                                                       follow along           *)
  182.  
  183. CloseAllFiles;                   (* Close the files to clean up.  It is
  184.                                     important to realize that CloseAllFiles
  185.                                     DOES NOT write the buffer to disk, it only
  186.                                     deals with the files open list.          *)
  187.  
  188. end.
  189.