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

trdb.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *                                                                             *
00003  *   Copyright (C) 2003  Erik Sjolund,  (<html>Erik Sj&ouml;lund</html>)       *
00004  *                       Center for Genomics and Bioinformatics,               *
00005  *                       Karolinska Institutet,                                *
00006  *                       Stockholm,                                            *
00007  *                       Sweden                                                *
00008  *                                                                             *
00009  *  Author: Erik Sjolund                                                       *
00010  *  Email: erik.sjolund@adivo.se                                               *
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 //   cerr<<"~TrDb() "<<endl;
00027 //   cerr<<"Bye ~TrDb() "<<endl;
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     /* 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 }
00055 
00056 void TrDb::open()
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 }
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     // Note: must use free, not delete.
00153     // This struct is allocated by C.
00154     //
00155     free(statp);
00156 
00157     return str;
00158 }

Generated on Fri Mar 17 17:44:24 2006 for trapper by  doxygen 1.4.4