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

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