home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / OOFILE / Buildable, limited OOFILE / samples / ooftst03.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-27  |  2.3 KB  |  83 lines  |  [TEXT/CWIE]

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST3
  4.  
  5. // this sample tries a number of random updates to test m/u
  6.  
  7. // Simple stream I/O is used to interact with the user.
  8. #include "oofile.hpp"
  9.  
  10. #include "ooftst02.inc"
  11.  
  12. #ifdef _Windows
  13. #include <windows.h>
  14. void yieldTime();
  15. void yieldTime() {
  16.     MSG msg;
  17.     PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
  18. }
  19. #endif
  20.  
  21. int main()
  22. {
  23.     cout << "OOFILE Validation Suite - Test 3\n"
  24.          << "Simple random updates testing multi-user" << endl
  25.          << "using a copy of the database from ooftest2" << endl;
  26.  
  27. // get probably remote file path
  28.     char remotePath[255];
  29.     cout << "Enter full file path, probably a remote disk: " << endl 
  30.          << "(eg: Mac Remote Disk:oofile:ooftst03.db  OR  E:\\OOFILE\\OOFTEST3.DB)" << endl;
  31.     cin.getline(remotePath, 255);
  32.     
  33.     cout << endl << "Are we running as a Writer or Reader (R/W)? ";
  34.     char writeORread;
  35.     cin >> writeORread;
  36.     bool asWriter = ( (writeORread=='W') || (writeORread=='w') );
  37.  
  38. //    if (dbConnect::fileExists("ooftest3.db"))
  39. // JUST ASSUME IT DOES - this function doesn't work if file already open!
  40.         theDB.openConnection(remotePath);
  41. //    else {
  42. //        dbConnect::raise("Please copy ooftest2.db (from running ooftest2) to ooftest3.db");
  43. //    }
  44.     
  45.     if (asWriter) {
  46.         cout << "Randomly updating records..." << endl;
  47.         unsigned long numRecs = People.count();
  48.         for (unsigned long i=0; i<100000 ; i++) {
  49.             unsigned long theRec = rand()%numRecs;
  50.             
  51.     // if turning on locking, uncomment here and at end of this block
  52.     // but remember 1 writer/many readers doesn't require you to do anything
  53.     //        theDB.enterWriteLocking();
  54.             People[theRec];    // jump to a random record
  55.             cout << "Writing rec: " << theRec << '\t' << People.LastName << " " << People.Salary << endl;
  56.             People.Salary = People.Salary + 1;
  57.             People.saveRecord();        
  58.     //        theDB.exitLocking();
  59. #ifdef _Windows
  60.             yieldTime();
  61. #endif
  62.         }
  63.     }
  64.     else {
  65.         cout << "Randomly reading records..." << endl;
  66.         unsigned long numRecs = People.count();
  67.         for (unsigned long i=0; i<100000 ; i++) {
  68.             unsigned long theRec = rand()%numRecs;
  69.             
  70.             People[theRec];    // jump to a random record
  71.             cout << "rec: " << theRec << '\t' << People.LastName << " " << People.Salary << endl;
  72. #ifdef _Windows
  73.             yieldTime();
  74. #endif
  75.         }
  76.     }
  77.  
  78.     cout << "Database after the random updates" << endl << People << endl;
  79.     cout << "done" << endl;
  80.  
  81.     return EXIT_SUCCESS;
  82. }
  83.