00001 /******************************************************************************* 00002 * * 00003 * Copyright (C) 2003 Erik Sjolund, (<html>Erik Sjö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 00015 #ifndef TRDB_H 00016 #define TRDB_H 00017 00018 #include <db_cxx.h> 00019 #include <qstring.h> 00020 #include <map> 00021 00022 class Dbc; 00023 class Db; 00024 class Dbt; 00025 00026 typedef int (*bt_compare_func_t)( DB * , const DBT *, const DBT *); 00027 typedef int (*associate_func_t)(Db *, const Dbt *, const Dbt *, Dbt *); 00028 00029 class DbEnv; 00030 00031 /** \brief opens and closes the primary and all secondary Berkeley dbs that are 00032 the underlying storage for a single GeneralData class. */ 00033 00034 class TrDb 00035 { 00036 public: 00037 /** \brief this defines a secondary index. 00038 * 00039 * read: 00040 * http://www.sleepycat.com/docs/api_cxx/db_associate.html 00041 * 00042 * http://www.sleepycat.com/docs/api_cxx/db_set_bt_compare.html 00043 * 00044 * Right now only in FeatureData and in ReadData is indexing done. 00045 * Right now the serializing there is done with structs. 00046 * But if we want to index in both in a class and also in its child classes 00047 * we should probably use an associate() function where we don't unserialize 00048 * with a struct but instead use the GeneralData::readStream(). 00049 * For pulling out an integer value from a GeneralData we do something like 00050 (note the DB_DBT_APPMALLOC flag): 00051 00052 int SubClassOfGeneralData::associateFunc( Db * dbp , const Dbt * , const Dbt *pdata, Dbt *skey) 00053 00054 { 00055 00056 memset(skey, 0, sizeof(Dbt)); 00057 00058 SubClassOfGeneralData d; 00059 00060 Database::setFromDbt( pdata, &d ) 00061 00062 int answer = d.getInterestingValue(); 00063 00064 int * allocatedInt = malloc(sizeof( int )); 00065 00066 *allocatedInt = answer; 00067 00068 skey->set_data( ( u_int8_t * ) allocatedInt; 00069 00070 skey->set_size(sizeof( int )); 00071 00072 skey->set_flags( DB_DBT_APPMALLOC ); 00073 00074 return 0; 00075 } 00076 */ 00077 struct Index 00078 { 00079 bt_compare_func_t bt_compare_func; /** http://www.sleepycat.com/docs/api_cxx/db_set_bt_compare.html */ 00080 associate_func_t associate_func; /** http://www.sleepycat.com/docs/api_cxx/db_associate.html */ 00081 QString name; /** lookup string */ 00082 Db * db; 00083 }; 00084 typedef std::map<std::string, Index> IndexMap; 00085 TrDb( DbEnv *, std::string, IndexMap i ); 00086 ~TrDb(); 00087 Index secondaryIndex( std::string index ); 00088 Dbc * createPrimaryCursor( ); 00089 Db * primaryDb(); 00090 void open(); 00091 void close(); 00092 void sync(); 00093 QString statistics(); 00094 private: 00095 std::string m_name; 00096 Db *db; 00097 QString lastErrPfxStr; 00098 QString lastErrMsgStr; 00099 IndexMap indexMap; 00100 DbEnv * dbenv; 00101 }; 00102 00103 00104 #endif