00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "trdb.h"
00015
00016 #include <qstring.h>
00017 #include <iostream>
00018 #include <qdir.h>
00019
00020 using namespace std;
00021
00022
00023
00024 TrDb::~TrDb()
00025 {
00026
00027
00028 }
00029
00030 TrDb::TrDb( DbEnv * dbenv_, string _name, IndexMap i ) : m_name(_name), indexMap( i ), dbenv( dbenv_ )
00031 {
00032 Q_CHECK_PTR( dbenv );
00033 db = NULL;
00034 }
00035
00036 void TrDb::close()
00037 {
00038
00039
00040
00041
00042
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 }
00055
00056 void TrDb::open()
00057 {
00058
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
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
00084 idb->set_error_stream(&std::cerr);
00085
00086 idb->set_flags(DB_DUP);
00087
00088 idb->set_bt_compare( it->second.bt_compare_func );
00089
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() );
00094 }
00095
00096
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");
00099
00100 }
00101
00102 }
00103 return;
00104 }
00105
00106 void TrDb::sync()
00107 {
00108 int ret;
00109 if ( ( ret = db->sync(0) ) != 0 )
00110 db->err(ret, "TrDb::sync(): Unable to flush db" );
00111
00112 }
00113
00114
00115 TrDb::Index TrDb::secondaryIndex( std::string str )
00116 {
00117 Index index = indexMap[str];
00118 return index;
00119 }
00120
00121 Db * TrDb::primaryDb()
00122 {
00123 return db;
00124 }
00125
00126 Dbc * TrDb::createPrimaryCursor( )
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 }
00141
00142
00143 QString TrDb::statistics()
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
00153
00154
00155 free(statp);
00156
00157 return str;
00158 }