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

  1. (* EXAM53.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.     lrLst : LrList;         (* variable which user declares and passes in to
  25.                                LRLIST unit.  LRLIST will build a list of
  26.                                logical records which fulfill a query criteria.
  27.                                (Actually the list will be built by a call
  28.                                to the BTREE unit which in turn uses the LRLIST
  29.                                unit.                                         *)
  30.  
  31.     lrNum : LrNumber;       (* a variable used in this demo program
  32.                                to keep track of logical record number
  33.                                currently being processed.                    *)
  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 procedure will be called prior to termination of the program whether
  45.    there is an error or not.  This is a demonstration of a good technique to
  46.    use in conjunction with TBTREE.  Calls to write the buffer to disk and
  47.    close the files should be included.  This is also a place top handle runtime
  48.    error since I do not attempt to deal with errors or maintain global error
  49.    variables in TBTREE.                                                      *)
  50.  
  51. {$F+} procedure MyExit; {$F-}
  52.  
  53.     begin
  54.     ExitProc := ExitSave;           (* reinstall the saved value of ExitProc *)
  55.     Writeln('Writing Records To Disk ...');        (* just a note so you can
  56.                                                       follow along           *)
  57.  
  58.     WriteEntireBufferToDisk;         (* very important step!!  Before leaving
  59.                                         the program the buffer must be written
  60.                                         to disk or some changes will be lost.
  61.                                         This will cause major problems and lost
  62.                                         data!!!  This is not really required in
  63.                                         in this program since nothing is
  64.                                         modified.  A logical record list is
  65.                                         created, but it happens to be small
  66.                                         enough that a temporary file is not
  67.                                         needed.  If a file was needed, it would
  68.                                         have been destroyed on the call to
  69.                                         DestroyLrList anyway.                *)
  70.  
  71.     Writeln('Closing Files ...');                  (* just a note so you can
  72.                                                       follow along           *)
  73.     CloseAllFiles;               (* Close the files to clean up.  It is
  74.                                     important to realize that CloseAllFiles
  75.                                     DOES NOT write the buffer to disk, it only
  76.                                     deals with the files open list.          *)
  77.  
  78.     end;
  79.  
  80.  
  81. (* This routine should be called before anything else happens.  It calls
  82.    the various routines to set up parameters according to values applicable
  83.    to your particular application.                                           *)
  84.  
  85. procedure SetUp;
  86.  
  87.     begin
  88.     SetMaxBufferPages(100);  (* a call to the PAGE unit to set up the number
  89.                                 of pages in the buffer.  If this is not done a
  90.                                 default of one page is used.  This will cause
  91.                                 poor performance but will not cause any
  92.                                 runtime errors                               *)
  93.  
  94.     SetMaxOpenFiles(10);     (* a call to the FILEBUFF unit to set the number
  95.                                 of files the FILEBUFF unit can have open at
  96.                                 once.  See FILEBUFF unit for details.        *)
  97.  
  98.     SetImmediateDiskWrite(FALSE); (* changed and newly created pages will be
  99.                                      buffered in the page buffer and will only
  100.                                      written to disk when they are swapped out
  101.                                      or explicitly written out by a user
  102.                                      request                                 *)
  103.  
  104.     end;
  105.  
  106.  
  107. (* This procedure initializes the file names as required.  Also, if will
  108.    create the files if they do not exist.  Therefore, you can use one
  109.    initialization routine whether the file exists or must be created.        *)
  110.  
  111. procedure InitFiles;
  112.  
  113.     begin
  114.  
  115.     dataFile := 'myFile1.dat';
  116.     if not FileExists(dataFile) then
  117.         begin
  118.         VLRCreateDataFile(dataFile);
  119.         end;
  120.  
  121.     indexFile1 := 'testByte.idx';
  122.     if not FileExists(indexFile1) then
  123.         begin
  124.         CreateIndexFile(indexfile1,SizeOf(BYTE),BYTEVALUE);
  125.         end;
  126.  
  127.      end;
  128.  
  129. (*****************************************************************************)
  130. (*                                                                           *)
  131. (*                         M A I N      P R O G R A M                        *)
  132. (*                                                                           *)
  133. (*****************************************************************************)
  134.  
  135. begin
  136.  
  137. Writeln('Setting up termination routine ...');     (* just a note so you can
  138.                                                       follow along           *)
  139.  
  140. exitSave := ExitProc;       (* see page 376 - 377 of Turbo Pascal 4.0 manual *)
  141. ExitProc := @MyExit;
  142.  
  143. Writeln('Initializing Parameters ...');            (* just a note so you can
  144.                                                       follow along           *)
  145.  
  146. SetUp;              (* set file open buffer size and page buffer size limits *)
  147.                     (* set immediate disk write parameter as well            *)
  148.  
  149.  
  150. InitFiles;
  151.  
  152. Writeln('Looking for logical records which match selection criterion and',
  153.          ' building logical record list ... this may take a minute ...' );
  154.                                                    (* just a note so you can
  155.                                                       follow along           *)
  156.  
  157. tempByte := 50;         (* We want to find and delete all records for which
  158.                            tempByte is less than or equal to 50.  We need to
  159.                            set tempByte to the search value (50).            *)
  160.  
  161. GetValuesFromBTree(indexFile1,tempByte,LE,lrLst);  (* build list of logical
  162.                                                       record numbers which
  163.                                                       are less than or equal
  164.                                                       to 50                  *)
  165.  
  166.     (* The following loop will delete data records associated with the
  167.        record numbers found in the newly created logical record list.        *)
  168.  
  169. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  170.                                  the front of the list.                      *)
  171.  
  172. while lrNum <> 0 do
  173.     begin
  174.  
  175.     (* we need to fetch the record so we can use the values for
  176.        testRec.randByte to make deletions from the the index                 *)
  177.  
  178.     VLRGetALogicalRecord(dataFile,     (* variable holding name of data file *)
  179.                          lrNum,                   (* logical record to fetch *)
  180.                          testRec);                  (* place to put the data *)
  181.  
  182.     VLRDeleteDataRecord(dataFile,lrNum);(* delete the logical record specified
  183.                                            from the data file.  We still need
  184.                                            to delete it from the index.      *)
  185.  
  186.     (* The next statement deletes the appropriate entry from the index       *)
  187.  
  188.     DeleteValueFromBTree(indexFile1,lrNum,testRec.randByte);
  189.  
  190.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  191.  
  192.     end;
  193.  
  194. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  195.                            is important so that there aren't a bunch of
  196.                            temporary files upon termination.  Also, lists
  197.                            take up data space and also space in the page
  198.                            buffer                                            *)
  199.  
  200.     (* now that that is done, let's do an update.  Let's get all the records
  201.        for which the randByte value is greater than 75 and change randString
  202.        for those records to 'testtest'.                                      *)
  203.  
  204. tempByte := 75;                      (* We want to find all records for which
  205.                                                    tempByte is greater
  206.                                                    75.                       *)
  207.  
  208. GetValuesFromBTree(indexFile1,tempByte,GT,lrLst);  (* build list of logical
  209.                                                       record numbers which
  210.                                                       have values > 75       *)
  211.  
  212.     (* The following loop will update data records associated with the
  213.        record numbers found in the newly created logical record list.  It
  214.        won't make any difference, but we might as well traverse the list
  215.        in reverse order this time.                                           *)
  216.  
  217. lrNum := GetLastLr(lrLst);   (* get the last record number and set the cursor
  218.                                 the end of the list.                         *)
  219.  
  220. while lrNum <> 0 do
  221.     begin
  222.  
  223.     (* we need to fetch the record so we can use the value for
  224.        testRec.randString to make the appropriate deletion from indexFile2   *)
  225.  
  226.     VLRGetALogicalRecord(dataFile,     (* variable holding name of data file *)
  227.                       lrNum,                      (* logical record to fetch *)
  228.                       testRec);                     (* place to put the data *)
  229.  
  230.     testRec.randString := 'testtest';
  231.  
  232.     (* the next statement stores the updated record *)
  233.  
  234.     VLRStoreALogicalRecord(dataFile,   (* variable holding name of data file *)
  235.                         lrNum,                    (* logical record to store *)
  236.                         testRec,                   (* place to get data from *)
  237.                         SizeOf(TestRec));      (* size of the logical record *)
  238.  
  239.     lrNum := GetPrevLr(lrLst);         (* advance the cursor to next in list *)
  240. writeln(lrNum);
  241.     end;
  242.  
  243. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  244.                            is important so that there aren't a bunch of
  245.                            temporary files upon termination.  Also, lists
  246.                            take up data space and also space in the page
  247.                            buffer                                            *)
  248.  
  249.     (* now let's get all the records and see what we've done *)
  250.  
  251. tempByte := 255;         (* since we are checking for existence, this is not
  252.                             required.  tempByte must still be included in the
  253.                             call however.  It will be ignored.               *)
  254.  
  255.  
  256. GetValuesFromBTree(indexFile1,tempByte,EX,lrLst);  (* build list of logical
  257.                                                       record numbers which
  258.                                                       exist (criterion is
  259.                                                       EX which means exists *)
  260.  
  261.     (* The following loop will fetch the data records associated with the
  262.        record numbers found in the newly created logical record list.  Each
  263.        of the records will be printed.  Since the list was built using
  264.        indexFile1 which is the index corresponding to the randByte field in
  265.        testRec, the records will be printed in ascending order by
  266.        testRec.randByte.  If the opposite order was desired the list should
  267.        be traversed in reverse by using GetLastLr and GetPrevLr.             *)
  268.  
  269.  
  270. lrNum := GetFirstLr(lrLst);   (* get the first record number and set the cursor
  271.                                  the front of the list.                      *)
  272.  
  273. while lrNum <> 0 do
  274.     begin
  275.  
  276.     VLRGetALogicalRecord(dataFile,     (* variable holding name of data file *)
  277.                       lrNum,              (* logical record number from list *)
  278.                       testRec);                     (* place to put the data *)
  279.  
  280.     Writeln(lrNum,'        ',testRec.randByte,'       ',testrec.randString);
  281.  
  282.     lrNum := GetNextLr(lrLst);         (* advance the cursor to next in list *)
  283.     end;
  284.  
  285. Writeln('Total records found matching criteria = ',GetCountLr(lrLst));
  286.  
  287. DestroyLrList(lrLst);   (* we're done with list so let's get rid of it.  This
  288.                            is important so that there aren't a bunch of
  289.                            temporary files upon termination.  Also, lists
  290.                            take up data space and also space in the page
  291.                            buffer                                            *)
  292.  
  293. end.
  294.