Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

TrDb Class Reference

opens and closes the primary and all secondary Berkeley dbs that are the underlying storage for a single GeneralData class. More...

#include <trdb.h>

List of all members.

Public Types

typedef std::map< std::string,
Index
IndexMap

Public Member Functions

 TrDb (DbEnv *, std::string, IndexMap i)
 ~TrDb ()
Index secondaryIndex (std::string index)
Dbc * createPrimaryCursor ()
Db * primaryDb ()
void open ()
void close ()
void sync ()
QString statistics ()

Private Attributes

std::string m_name
Db * db
QString lastErrPfxStr
QString lastErrMsgStr
IndexMap indexMap
DbEnv * dbenv

Classes

struct  Index
 this defines a secondary index. More...


Detailed Description

opens and closes the primary and all secondary Berkeley dbs that are the underlying storage for a single GeneralData class.

Definition at line 34 of file trdb.h.


Member Typedef Documentation

typedef std::map<std::string, Index> TrDb::IndexMap
 

Definition at line 84 of file trdb.h.


Constructor & Destructor Documentation

TrDb::TrDb DbEnv *  ,
std::string  ,
IndexMap  i
 

TrDb::~TrDb  ) 
 

Definition at line 24 of file trdb.cpp.

00025 {
00026 //   cerr<<"~TrDb() "<<endl;
00027 //   cerr<<"Bye ~TrDb() "<<endl;
00028 }


Member Function Documentation

void TrDb::close  ) 
 

Definition at line 36 of file trdb.cpp.

References db, and indexMap.

00037 {
00038     /* close secondary indeces first and then the primary.
00039        I beleive this order is correct, although I didn't find
00040        a clear statement about it in the berkeley db docs */
00041   
00042 //   cerr<<"closing trdb"<<endl;
00043   TrDb::IndexMap::iterator it;
00044   for ( it = indexMap.begin(); it != indexMap.end(); ++it) {
00045     Db * db_sec = it->second.db;
00046     Q_CHECK_PTR( db_sec );
00047     db_sec->close(0);
00048     delete db_sec;
00049   }
00050 
00051   Q_CHECK_PTR( db );
00052   db->close(0);
00053   delete db;
00054 }

Dbc * TrDb::createPrimaryCursor  ) 
 

Definition at line 126 of file trdb.cpp.

References db.

00127 {
00128     Dbc * cursor = NULL;
00129     if ( db )
00130     {
00131         cerr << "in cursor open" << endl;
00132         int ret;
00133         ret = db->cursor(NULL, &cursor, 0);
00134     }
00135     if ( cursor == NULL )
00136     {
00137         cerr << "cursor was null" << endl;
00138     }
00139     return cursor;
00140 }

void TrDb::open  ) 
 

Definition at line 56 of file trdb.cpp.

References db, dbenv, indexMap, and m_name.

Referenced by TrapperDoc::openDbs().

00057 {
00058 //   cerr<<"opening trdb"<<endl;
00059   string fileName = m_name + "/primary";
00060   int ret;
00061   db = new Db(dbenv, 0);
00062   db->set_error_stream(&std::cerr);
00063   db->set_errpfx( fileName.c_str() );
00064   db->set_pagesize(1024);
00065   if (( ret = db->open( NULL, fileName.c_str() , NULL, DB_RECNO, DB_CREATE , 0664) ) != 0 ) {
00066     string message = "in function TrDb::open() calling db->open with filename = \"" + fileName + "\"";
00067     db->err(ret, message.c_str());
00068     exit(1);
00069     /*  err( ret , NULL );*/
00070   }
00071   string secondaryDir = m_name + "/secondary";
00072   QDir dir;
00073   dir.mkdir( secondaryDir.c_str(), true);
00074   
00075   TrDb::IndexMap::iterator it;
00076   
00077   for ( it = indexMap.begin(); it != indexMap.end(); ++it) {
00078     Db * idb = new Db(dbenv, DB_CXX_NO_EXCEPTIONS );
00079     
00080     it->second.db = idb;
00081     string indexDbFileName = secondaryDir + "/"+  it->first;
00082     idb->set_errpfx( indexDbFileName.c_str() );
00083     //         cerr << "setting error prefix for file" <<  indexDbFileName.c_str() << endl;
00084     idb->set_error_stream(&std::cerr);
00085     //         cerr << "a1" << endl;
00086     idb->set_flags(DB_DUP);
00087     //         cerr << "a2" << endl;
00088     idb->set_bt_compare( it->second.bt_compare_func );
00089     //         cerr << "a3" << endl;
00090     if (( ret = idb->open( NULL, indexDbFileName.c_str(), NULL, DB_BTREE,
00091                            DB_CREATE, 0664) ) != 0 ) {
00092       string message = "in function TrDb::open() calling idb->open with filename = \"" + indexDbFileName + "\"";
00093       idb->err(ret, message.c_str() );  /*  err( ret , NULL );*/
00094     }
00095     //         cerr << "a4" << endl;
00096     /* Associate the secondary with the primary. */
00097     if ((ret = db->associate(NULL,idb, it->second.associate_func , DB_CREATE)) != 0) {
00098       db->err(ret, "in function TrDb::open() calling db->associate");  /*  err( ret , NULL );*/
00099       /*  err( ret , NULL );*/
00100     }
00101     //         cerr << "a5" << endl;
00102   }
00103   return;
00104 }

Db * TrDb::primaryDb  ) 
 

Definition at line 121 of file trdb.cpp.

References db.

Referenced by Database::Creator< T >::Creator(), and Database::PrimaryIterator< T >::PrimaryIterator().

00122 {
00123     return db;
00124 }

TrDb::Index TrDb::secondaryIndex std::string  index  ) 
 

Definition at line 115 of file trdb.cpp.

References indexMap.

Referenced by Database::SecondaryIterator< T >::SecondaryIterator().

00116 {
00117     Index index = indexMap[str];
00118     return index;
00119 }

QString TrDb::statistics  ) 
 

Definition at line 143 of file trdb.cpp.

References db, and m_name.

00144 {
00145     Q_CHECK_PTR( db );
00146     DB_BTREE_STAT *statp;
00147     db->stat(&statp, 0);
00148     QString str;
00149     QString database( m_name.c_str() );
00150     str = QString("database \"%1\" contains %2 records\n").arg(database).arg( (u_long)statp->bt_ndata );
00151 
00152     // Note: must use free, not delete.
00153     // This struct is allocated by C.
00154     //
00155     free(statp);
00156 
00157     return str;
00158 }

void TrDb::sync  ) 
 

Definition at line 106 of file trdb.cpp.

References db.

Referenced by TrapperApp::slotFileFlush().

00107 {
00108   int ret;
00109   if ( ( ret = db->sync(0) ) != 0 )
00110     db->err(ret, "TrDb::sync(): Unable to flush db" );
00111 
00112 }


Member Data Documentation

Db* TrDb::db [private]
 

Definition at line 96 of file trdb.h.

Referenced by close(), createPrimaryCursor(), open(), primaryDb(), statistics(), and sync().

DbEnv* TrDb::dbenv [private]
 

Definition at line 100 of file trdb.h.

Referenced by open().

IndexMap TrDb::indexMap [private]
 

Definition at line 99 of file trdb.h.

Referenced by close(), open(), and secondaryIndex().

QString TrDb::lastErrMsgStr [private]
 

Definition at line 98 of file trdb.h.

QString TrDb::lastErrPfxStr [private]
 

Definition at line 97 of file trdb.h.

std::string TrDb::m_name [private]
 

Definition at line 95 of file trdb.h.

Referenced by open(), and statistics().


The documentation for this class was generated from the following files:
Generated on Fri Mar 17 17:45:02 2006 for trapper by  doxygen 1.4.4