OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME

OOFTST 3 - tests multi-user capabilities   

This sample tries a number of random updates to test multi-user capabilities. Simple stream I/O is used to interact with the user.


Include files:

#include "oofile.h"	// the general oofile library  
#include "ooftst02.h"	// declarations for multi-table classes for use in tests  

These are the global variables that define the database using the ooftst02 classes

	dbConnect_ctree    theDB;
	dbPatients     Patients;
	dbVisits	Visits;  

Here we declare the relationship. Basic info and how to declare classes

	dbRelationship PatientVisits(Patients.Visits, Visits.Patient);
	  

* I don't know what this does

#ifdef _Windows
#include <windows.h>
void yieldTime();
void yieldTime() {
	MSG msg;
	PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
}
#endif    

 

int main()
{
	cout << "OOFILE Validation Suite - Test 3\n"
		 << "Simple random updates testing multi-user" << endl
		 << "using a copy of the database from ooftest2" << endl;    

First, we must get the file path, which is quite probably remote.

	char remotePath[255];
	cout << "Enter full file path, probably a remote disk: " << endl 
	     << "(eg: Mac Remote Disk:oofile:ooftst03.db  OR  E:\\OOFILE\\OOFTEST3.DB)" 
        << endl;
	cin.getline(remotePath, 255);
  

Then ask the user whether we want to test reading or writing.

	cout << endl << "Are we running as a Writer or Reader (R/W)? ";
	char writeORread;
	cin >> writeORread;
	bool asWriter = ( (writeORread=='W') || (writeORread=='w') );    

Now, we try and open the database. At the moment, this code assumes that the file exists as the function to check doesn't work if the file is already open so it is commented out, as is the error message if the check didn't work.

Testing: opening a file given a filename from the user

//	if (dbConnect::fileExists("ooftest3.db"))
  
		theDB.openConnection(remotePath);
  
//	else {
//		dbConnect::raise("Please copy ooftest2.db (from running ooftest2) to ooftest3.db");
//	}
  

Now,  if the user wanted write mode, we start to write to the database randomly.

	if (asWriter) {
		cout << "Repeatedly updating the first record..." << endl;
		unsigned long numRecs = Patients.count();
		for (unsigned long i=0; i<100000 ; i++) {
			unsigned long theRec = rand()%numRecs;
			    

? Here we turn on locking and modify the fields of the record we have chosen to change.

	// if turning on locking, uncomment here and at end of this block
	// but remember 1 writer/many readers doesn't require you to do anything
			theDB.enterWriteLocking();
			Patients.start(); // comment out this line if you want random updates
// IF YOU WANT RANDOM	Patients[theRec];	// jump to a random record
			cout << "Writing rec: " << theRec << '\t' 
               << Patients.LastName << " " << Patients.Salary;
			Patients.Salary = Patients.Salary + 1;
			Patients.saveRecord();	
			Patients.unloadRecord();  // force read from disk
			cout << " ... " << Patients.Salary << endl;
			theDB.exitLocking();
#ifdef _Windows
			yieldTime();
#endif
		}
	}  

 

Otherwise, the user must have chosen to read from the database so we randomly jump to a record and read it.

	else {
		cout << "Randomly reading records..." << endl;
		unsigned long numRecs = Patients.count();
		for (unsigned long i=0; i<100000 ; i++) {
			unsigned long theRec = rand()%numRecs;
			    
			Patients[theRec];	// jump to a random record
			cout << "rec: " << theRec << '\t' 
               << Patients.LastName << " " << Patients.Salary << endl;
#ifdef _Windows
			yieldTime();
#endif
		}
	}    
	cout << "Database after the random updates" << endl 
            << Patients << endl;
	cout << "done" << endl;    
	return EXIT_SUCCESS;
}  

 

Feature index

(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001