00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "OW_config.h"
00031 #include "OW_dbDatabase.hpp"
00032 #include "OW_Exception.hpp"
00033 #include "OW_Format.hpp"
00034 #include "OW_UTF8Utils.hpp"
00035 #include "OW_dbUtils.hpp"
00036
00037 #include <db.h>
00038
00039 extern "C" {
00040 static int recCompare(const DBT* key1, const DBT* key2);
00041 }
00042
00043 namespace OW_NAMESPACE
00044 {
00045
00047 dbDatabase::dbDatabase()
00048 : m_db(0)
00049 {
00050 }
00051
00053 dbDatabase::~dbDatabase()
00054 {
00055 try
00056 {
00057 close();
00058 }
00059 catch (...)
00060 {
00061 }
00062 }
00063
00065 void dbDatabase::open(const char* name, ::DB_ENV* env, ::DB_TXN* txn, EDuplicateKeysFlag allowDuplicates)
00066 {
00067
00068 int ret = ::db_create(&m_db, env, 0);
00069 dbUtils::checkReturn(ret, "db_create");
00070
00071 if (allowDuplicates == E_DUPLICATES)
00072 {
00073 int ret = m_db->set_flags(m_db, DB_DUP);
00074 dbUtils::checkReturn(ret, "m_db->set_flags: DB_DUP");
00075 }
00076
00077
00078
00079
00080
00081 ret = m_db->open(m_db, txn, name, NULL, DB_BTREE, DB_CREATE | DB_THREAD, 0600);
00082 dbUtils::checkReturn(ret, "m_db->open");
00083
00084 }
00085
00087 void dbDatabase::close()
00088 {
00089 if (m_db != 0)
00090 {
00091 int ret = m_db->close(m_db, 0);
00092 dbUtils::checkReturn(ret, "m_db->close");
00093 m_db = 0;
00094 }
00095 }
00096
00098 bool dbDatabase::get(const String& key, std::vector<unsigned char>& data, ::DB_TXN* txn)
00099 {
00100 DBT dbtkey = {(void*)key.c_str(), key.length(), key.length(), 0, 0, 0 };
00101 DBT dbtdata = { 0, 0, 0, 0, 0, DB_DBT_USERMEM };
00102 int ret = m_db->get(m_db, txn, &dbtkey, &dbtdata, 0);
00103 if (ret == DB_NOTFOUND)
00104 {
00105 return false;
00106 }
00107 dbUtils::checkReturn(ret, "m_db->get");
00108 data.resize(dbtdata.size);
00109 dbtdata.data = &data[0];
00110 ret = m_db->get(m_db, txn, &dbtkey, &dbtdata, 0);
00111 dbUtils::checkReturn(ret, "m_db->get");
00112 return true;
00113 }
00114
00116 void dbDatabase::put(const String& key, const std::vector<unsigned char>& data, ::DB_TXN* txn)
00117 {
00118 DBT dbtkey = {(void*)key.c_str(), key.length(), key.length(), 0, 0, 0 };
00119 DBT dbtdata = { (void*)&data[0], data.size(), 0, 0, 0, 0 };
00120 int ret = m_db->put(m_db, txn, &dbtkey, &dbtdata, 0);
00121 dbUtils::checkReturn(ret, "m_db->put");
00122 }
00123
00124 }
00125
00127 extern "C" {
00128 static int
00129 OW_dbrecCompare(const ::DBT* key1, const ::DBT* key2)
00130 {
00131 if (key1->data && key2->data)
00132 {
00133 return OpenWBEM::UTF8Utils::compareToIgnoreCase(reinterpret_cast<const char*>(key1->data),
00134 reinterpret_cast<const char*>(key2->data));
00135 }
00136 else if (key1->data && !key2->data)
00137 {
00138 return 1;
00139 }
00140 else if (!key1->data && key2->data)
00141 {
00142 return -1;
00143 }
00144 else
00145 {
00146 return 0;
00147 }
00148 }
00149 }