#include <trdb.h>
Collaboration diagram for TrDb:
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... |
Definition at line 34 of file trdb.h.
typedef std::map<std::string, Index> TrDb::IndexMap |
TrDb::TrDb | ( | DbEnv * | , | |
std::string | , | |||
IndexMap | i | |||
) |
TrDb::~TrDb | ( | ) |
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 }
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 }
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 }
void TrDb::open | ( | ) |
Definition at line 56 of file trdb.cpp.
References db, dbenv, indexMap, m_name, and QDir::mkdir().
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 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 }
void TrDb::close | ( | ) |
Definition at line 36 of file trdb.cpp.
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 }
void TrDb::sync | ( | ) |
QString TrDb::statistics | ( | ) |
Definition at line 143 of file trdb.cpp.
References QString::arg(), db, and m_name.
00144 { 00145 Q_CHECK_PTR( db ); 00146 DB_BTREE_STAT *statp; 00147 db->stat(NULL, &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 }
std::string TrDb::m_name [private] |
Db* TrDb::db [private] |
Definition at line 96 of file trdb.h.
Referenced by close(), createPrimaryCursor(), open(), primaryDb(), statistics(), and sync().
QString TrDb::lastErrPfxStr [private] |
QString TrDb::lastErrMsgStr [private] |
IndexMap TrDb::indexMap [private] |
DbEnv* TrDb::dbenv [private] |