OW_dbDatabase.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2003 Center 7, Inc All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Center 7 nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Center 7, Inc OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
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    // Create the database handle.
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    // Open a database in the environment:
00078    // create if it doesn't exist
00079    // free-threaded handle
00080    // read/write owner only
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 } // end namespace OW_NAMESPACE
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 // key1->data == 0 && key2->data == 0
00145    {
00146       return 0;
00147    }
00148 }
00149 } // extern "C"

Generated on Thu Feb 9 08:47:58 2006 for openwbem by  doxygen 1.4.6