OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME |
This sample creates a file then adds indices
#include "oofile.h" // the general oofile library #include "oofios.h" // general stream io
First we declare our class. We will declare two types of index on two of the fields and leave the third without one.
DECLARE_CLASS(dbTest23) dbChar f1; dbUlong f2; dbReal f3;
dbTest23() : f1(20, "F1", kIndexCompress),
f2("F2", kIndexNoDups),
f3("F3")
{};
};
Here we use our own class to contain all the database . This common technique is quite useful as we can keep some methods attached to it, such as procedures that add test data. It is also convenient to be able to pass around a single pointer to our database and then refer to the tables within it. This way an object, procedure or method does not have to know where the information comes from (eg a window that edits data or one that displays a report).
class test23DB : public dbConnect_ctree
{
public:
test23DB();
These are some useful methods we wish to keep with our database.
void addTestData(unsigned long startNum, unsigned long endNum); void displaySubset();
This is our database's table:
dbTest23 mTable;
};
Now we define the methods. This is the constructor and merely defines the type of database we want to use. We ask it to use seperate files and force it to use exclusive access, despite filesharing library (FPUTFGET option), so we can rebuild the indices.
test23DB::test23DB()
{
useSeparateFiles();
useExclusiveAccess(); // force this despite filesharing library (FPUTFGET option)
}
Now we declare the method that will add test data.
void
test23DB::addTestData(unsigned long startNum, unsigned long endNum)
{
We create a set of names
const char* names[] = {
"Andy",
"Fred",
"Sally",
"Zach",
"William",
"Sue",
"Pamela",
"Tanith",
"Jane",
"Frederick"
};
Then create a record for each name and add a couple of numbers too.
const unsigned short kNumNames = 10; // sizeof(names);
for (unsigned long i=startNum; i<endNum; i++) {
mTable.newRecord();
const unsigned short iName = i%kNumNames;
mTable.f1 = names[iName]; // one of 10 names
mTable.f2 = i;
mTable.f3 = i+0.5;
mTable.saveRecord();
}
}
Here is the third method. It is merely a search for names of 'Andy' and a display of the records selected.
void
test23DB::displaySubset()
{
mTable.search(mTable.f1=="Andy");
cout << mTable << endl << endl;
}
int main()
{
cout << "OOFILE Validation Suite - Test 23\n"
<< "This tests the effect of adding indices to a database\n\n";
Here we declare our database. Note that the type is the database class we defined above.
test23DB theDB;
Now we must open or create our database.
if (dbConnect::fileExists("dbtest23.dat")) {
theDB.openConnection("");
theDB.deleteAll();
}
else {
cout << "Creating a new database\n";
theDB.newConnection("");
}
We now add test data to the database and out a subset using our displaySubset() method.
cout << "Adding 500 test records with indexing active\n"; theDB.addTestData(0, 51); theDB.displaySubset();
Now we try suppressing the indices declared in the original class by using the suppressIndices() function. Suppressing indices will make importing data much quicker.
Testing: function suppressIndices()
theDB.mTable.suppressIndices();
Then we try adding more tests records and print our the subset of those, seeing what sort of difference there is between our data.
Testing: adding data to indexed fields while indices are suppressed
cout << "Adding 500 test records with indexing disabled\n"; theDB.addTestData(51, 101);
We then use the function rebuild() to put the indices back again.
theDB.mTable.rebuild();
Then display again to see what effect that had.
cout << "Displaying records after rebuild\n"; theDB.displaySubset();
cout << endl <<"Test Completed" << endl;
return EXIT_SUCCESS; }
(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001