home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PASCAL / TBTREE16.ZIP / EXAM11.PAS < prev    next >
Pascal/Delphi Source File  |  1989-07-12  |  11KB  |  256 lines

  1. (* EXAM11.PAS *)
  2.  
  3. program driver;
  4. {$R+}
  5.  
  6. uses
  7.     exam0,
  8.     Btree,
  9.     Compare,
  10.     FileBuff,
  11.     FileDecs,
  12.     Files,
  13.     LRecList,
  14.     Numbers,
  15.     Page,
  16.     Logical;
  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.     lrLst : LrList;         (* variable which user declares and passes in to
  26.                                LRLIST unit.  LRLIST will build a list of
  27.                                logical records which fulfill a query criteria.
  28.                                (Actually the list will be built by a call
  29.                                to the BTREE unit which in turn uses the LRLIST
  30.                                unit.                                         *)
  31.  
  32.     nextLrNum,
  33.     lrNum : LrNumber;         (* Used to keep track of logical record number *)
  34.  
  35.     tempByte : Byte;        (* this variable is used to pass in the selection
  36.                                criteria for retrievals.  A variable or typed
  37.                                constants must be used.  Constants do not work
  38.                                since the call is by reference and not by
  39.                                value                                         *)
  40.  
  41.     exitSave : Pointer;     (* used as pointer to my terminiation procedure  *)
  42.  
  43.  
  44. (* This routine creates a random string and is used for creating strings to
  45. demonstrate the handling of strings in indexes                               *)
  46.  
  47. procedure CreateRandomString(var str : TestString);
  48.  
  49. var
  50.     chrCnt : 1 .. TESTSTRINGSIZE;
  51.     tss : Byte;
  52.  
  53.     begin
  54.     str := '';
  55.     for chrCnt := 1 to TESTSTRINGSIZE do
  56.         begin
  57.         str[chrCnt] := Chr(Random(25) + 65);
  58.         end;
  59.     tss := TESTSTRINGSIZE;
  60.     Move(tss,str,1);
  61.     end;
  62.  
  63.  
  64. (* This procedure will be called prior to termination of the program whether
  65.    there is an error or not.  This is a demonstration of a good technique to
  66.    use in conjunction with TBTREE.  Calls to write the buffer to disk and
  67.    close the files should be included.  This is also a place top handle runtime
  68.    error since I do not attempt to deal with errors or maintain global error
  69.    variables in TBTREE.                                                      *)
  70.  
  71. {$F+} procedure MyExit; {$F-}
  72.  
  73.     begin
  74.     ExitProc := ExitSave;           (* reinstall the saved value of ExitProc *)
  75.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  76.                                                       follow along           *)
  77.  
  78.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  79.                                         the program the buffer must be written
  80.                                         to disk or some changes will be lost.
  81.                                         This will cause major problems and lost
  82.                                         data!!!  This is not really required in
  83.                                         in this program since nothing is
  84.                                         modified.  A logical record list is
  85.                                         created, but it happens to be small
  86.                                         enough that a temporary file is not
  87.                                         needed.  If a file was needed, it would
  88.                                         have been destroyed on the call to
  89.                                         DestroyLrList anyway.                *)
  90.  
  91.     Writeln('Closing Files ...');                  (* just a note so you can
  92.                                                       follow along           *)
  93.  
  94.     CloseAllFiles;               (* Close the files to clean up.  It is
  95.                                     important to realize that CloseAllFiles
  96.                                     DOES NOT write the buffer to disk, it only
  97.                                     deals with the files open list.          *)
  98.  
  99.     end;
  100.  
  101.  
  102. (* This routine should be called before anything else happens.  It calls
  103.    the various routines to set up parameters according to values applicable
  104.    to your particular application.                                           *)
  105.  
  106. procedure SetUp;
  107.  
  108.     begin
  109.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  110.                                 of pages in the buffer.  If this is not done a
  111.                                 default of one page is used.  This will cause
  112.                                 poor performance but will not cause any
  113.                                 runtime errors                               *)
  114.  
  115.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  116.                                 of files the FILEBUFF unit can have open at
  117.                                 once.  See FILEBUFF unit for details.        *)
  118.  
  119.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  120.                                      buffered in the page buffer and will only
  121.                                      written to disk when they are swapped out
  122.                                      or explicitly written out by a user
  123.                                      request                                 *)
  124.  
  125.     end;
  126.  
  127.  
  128. (* This procedure initializes the file names as required.  Also, if will
  129.    create the files if they do not exist.  Therefore, you can use one
  130.    initialization routine whether the file exists or must be created.        *)
  131.  
  132. procedure InitFiles;
  133.  
  134.     begin
  135.  
  136.     dataFile := 'myFile1.dat';
  137.     if not FileExists(dataFile) then
  138.         begin
  139.         CreateDataFile(dataFile,SizeOf(TestRecord));
  140.         end;
  141.  
  142.     indexFile1 := 'testByte.idx';
  143.     if not FileExists(indexFile1) then
  144.         begin
  145.         CreateIndexFile(indexfile1,SizeOf(BYTE),BYTEVALUE);
  146.         end;
  147.  
  148.     indexFile2 := 'testStrg.idx';
  149.     if not FileExists(indexFile2) then
  150.         begin
  151.         CreateIndexFile(indexfile2,SizeOf(TestString),STRINGVALUE);
  152.         end;
  153.     end;
  154.  
  155.  
  156. (*****************************************************************************)
  157. (*                                                                           *)
  158. (*                         M A I N      P R O G R A M                        *)
  159. (*                                                                           *)
  160. (*****************************************************************************)
  161.  
  162. begin
  163.  
  164. Writeln('Setting up termination routine ...');     (* just a note so you can
  165.                                                       follow along           *)
  166.  
  167. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  168. ExitProc := @MyExit;
  169.  
  170. Writeln('Initializing Parameters ...');            (* just a note so you can
  171.                                                       follow along           *)
  172.  
  173. SetUp;              (* set file open buffer size and page buffer size limits *)
  174.                     (* set immediate disk write parameter as well            *)
  175.  
  176.  
  177. InitFiles;
  178.  
  179.  
  180.     (* The following shows the retrieval of all the records in the index using
  181.        the internal cursor and associated routines.  No logical record list
  182.        is created.                                                           *)
  183.  
  184. lrNum := UsingCursorGetFirstLr(indexFile1);
  185.  
  186. while lrNum <> 0 do
  187.     begin
  188.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  189.                       lrNum,              (* logical record number from list *)
  190.                       testRec);                     (* place to put the data *)
  191.  
  192.     Writeln(lrNum,'        ',testRec.randByte,'       ',testrec.randString);
  193.     lrNum := UsingCursorSkipAndGetNextLr(indexFile1);
  194.     end;
  195.  
  196. Writeln;
  197. writeln('now we are adding a new record ');
  198. Writeln;
  199.  
  200. testRec.randByte := 45;
  201. CreateRandomString(testRec.randString);       (* random string of 10 letters *)
  202. lrNum := StoreNewLogicalRecord(dataFile,testRec);   (* insert into data file *)
  203.  
  204. InsertValueInBTree(indexFile1,lrNum,testRec.randByte);
  205. InsertValueInBTree(indexFile2,lrNum,testRec.randString);
  206.  
  207. (* The following shows that the cursor works properly when a value is
  208.    inserted into the index                                                   *)
  209.  
  210. lrNum := UsingCursorGetCurrLr(indexFile1);
  211.  
  212. if lrNum <> 0 then
  213.     begin
  214.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  215.                       lrNum,              (* logical record number from list *)
  216.                       testRec);                     (* place to put the data *)
  217.  
  218.     Writeln(lrNum,'        ',testRec.randByte,'       ',testRec.randString);
  219.     end;
  220.  
  221. (* The following shows that the cursor works properly when a value is
  222.    deleted from the index.  It also shows how to do deletes using the cursor *)
  223.  
  224. Writeln;
  225. Writeln('now we are deleting all records with randByte >= 45');
  226. Writeln;
  227.  
  228. tempByte := 45;
  229. lrNum := UsingCursorAndValueGetLr(indexFile1,tempByte);
  230.  
  231. while lrNum <> 0 do
  232.     begin
  233.     GetALogicalRecord(dataFile,        (* variable holding name of data file *)
  234.                       lrNum,              (* logical record number from list *)
  235.                       testRec);                     (* place to put the data *)
  236.     Writeln(lrNum,'        ',testRec.randByte,'       ',testRec.randString);
  237.     nextLrNum := UsingCursorGetNextLr(indexFile1);  (* This step is necessary
  238.                                                        because if you delete
  239.                                                        the entry where the
  240.                                                        cursor is, the cursor
  241.                                                        will become invalid.
  242.                                                        You need to move the
  243.                                                        cursor to the next
  244.                                                        record number in the
  245.                                                        index prior to doing
  246.                                                        the delete.           *)
  247.     DeleteDataRecord(dataFile,lrNum);
  248.     DeleteValueFromBTree(indexFile1,lrNum,testRec.randByte);
  249.     DeleteValueFromBTree(indexFile2,lrNum,testRec.randString);
  250.     Writeln(lrNum,'        ',testRec.randByte,'       ',testRec.randString);
  251.     lrNum := nextLrNum;
  252.     end;
  253.  
  254. end.
  255.  
  256.