2010-10-12 15:53:47 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2006-12-20 13:20:55 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-11 02:46:45 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2006-12-20 13:20:55 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2006-12-20 13:20:55 +00:00
|
|
|
*
|
2008-04-11 02:46:45 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2006-12-20 13:20:55 +00:00
|
|
|
*
|
2008-04-11 02:46:45 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2006-12-20 13:20:55 +00:00
|
|
|
*
|
2008-04-11 02:46:45 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2006-12-20 13:20:55 +00:00
|
|
|
*
|
2008-04-11 02:46:45 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2006-12-20 13:20:55 +00:00
|
|
|
*
|
2008-04-11 02:46:45 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2006-12-20 13:20:55 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "dp_misc.h"
|
|
|
|
#include "dp_ucb.h"
|
|
|
|
#include "dp_persmap.h"
|
|
|
|
#include "rtl/strbuf.hxx"
|
|
|
|
#include "rtl/ustrbuf.hxx"
|
|
|
|
#include "osl/file.hxx"
|
|
|
|
#include "osl/thread.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
using namespace ::rtl;
|
|
|
|
using ::osl::File;
|
|
|
|
|
|
|
|
namespace dp_misc
|
|
|
|
{
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
|
|
|
void PersistentMap::throw_rtexc( int err, char const * pmsg ) const
|
|
|
|
{
|
|
|
|
OUStringBuffer buf;
|
|
|
|
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("[") );
|
|
|
|
buf.append( m_sysPath );
|
|
|
|
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("] Berkeley Db error (") );
|
|
|
|
buf.append( static_cast<sal_Int32>(err) );
|
|
|
|
buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("): ") );
|
|
|
|
if (pmsg == 0)
|
|
|
|
pmsg = DbEnv::strerror(err);
|
|
|
|
const OString msg(pmsg);
|
|
|
|
buf.append( OUString( msg.getStr(), msg.getLength(),
|
|
|
|
osl_getThreadTextEncoding() ) );
|
|
|
|
const OUString msg_(buf.makeStringAndClear());
|
2011-03-12 12:08:22 +01:00
|
|
|
OSL_FAIL( rtl::OUStringToOString(
|
2006-12-20 13:20:55 +00:00
|
|
|
msg_, RTL_TEXTENCODING_UTF8 ).getStr() );
|
|
|
|
throw RuntimeException( msg_, Reference<XInterface>() );
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
|
|
|
PersistentMap::~PersistentMap()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
m_db.close(0);
|
|
|
|
}
|
|
|
|
catch (DbException & exc) {
|
|
|
|
(void) exc; // avoid warnings
|
2011-03-12 12:08:22 +01:00
|
|
|
OSL_FAIL( DbEnv::strerror( exc.get_errno() ) );
|
2006-12-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
2011-11-16 16:59:39 +00:00
|
|
|
PersistentMap::PersistentMap( OUString const & url )
|
2012-03-23 08:49:03 +00:00
|
|
|
: m_db( 0 )
|
2006-12-20 13:20:55 +00:00
|
|
|
{
|
|
|
|
try {
|
2011-11-16 16:59:39 +00:00
|
|
|
rtl::OUString fileURL = expandUnoRcUrl(url);
|
|
|
|
if ( File::getSystemPathFromFileURL( fileURL, m_sysPath ) != File::E_None )
|
2006-12-20 13:20:55 +00:00
|
|
|
OSL_ASSERT( false );
|
2011-11-16 16:59:39 +00:00
|
|
|
|
2006-12-20 13:20:55 +00:00
|
|
|
OString cstr_sysPath(
|
2009-12-08 14:11:19 +00:00
|
|
|
OUStringToOString( m_sysPath, RTL_TEXTENCODING_UTF8 ) );
|
2006-12-20 13:20:55 +00:00
|
|
|
int err = m_db.open(
|
|
|
|
// xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
|
2011-11-16 16:59:39 +00:00
|
|
|
0, cstr_sysPath.getStr(), 0, DB_HASH,
|
|
|
|
DB_CREATE/* | DB_THREAD*/, 0664 /* fs mode */ );
|
2006-12-20 13:20:55 +00:00
|
|
|
if (err != 0)
|
|
|
|
throw_rtexc(err);
|
|
|
|
}
|
|
|
|
catch (DbException & exc) {
|
|
|
|
throw_rtexc( exc.get_errno(), exc.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
|
|
|
PersistentMap::PersistentMap()
|
2012-03-23 08:49:03 +00:00
|
|
|
: m_db( 0 )
|
2006-12-20 13:20:55 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
// xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
|
|
|
|
int err = m_db.open( 0, 0, 0, DB_HASH, DB_CREATE/* | DB_THREAD*/, 0 );
|
|
|
|
if (err != 0)
|
|
|
|
throw_rtexc(err);
|
|
|
|
}
|
|
|
|
catch (DbException & exc) {
|
|
|
|
throw_rtexc( exc.get_errno(), exc.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
2007-01-18 13:52:43 +00:00
|
|
|
bool PersistentMap::has( OString const & key ) const
|
2006-12-20 13:20:55 +00:00
|
|
|
{
|
|
|
|
return get( 0, key );
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
2007-01-18 13:52:43 +00:00
|
|
|
bool PersistentMap::get( OString * value, OString const & key ) const
|
2006-12-20 13:20:55 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
|
|
|
|
Dbt dbData;
|
|
|
|
int err = m_db.get( 0, &dbKey, &dbData, 0 );
|
|
|
|
if (err == DB_NOTFOUND)
|
|
|
|
return false;
|
|
|
|
if (err == 0) {
|
|
|
|
if (value != 0) {
|
2007-01-18 13:52:43 +00:00
|
|
|
*value = OString(
|
2006-12-20 13:20:55 +00:00
|
|
|
static_cast< sal_Char const * >(dbData.get_data()),
|
2007-01-18 13:52:43 +00:00
|
|
|
dbData.get_size() );
|
2006-12-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw_rtexc(err);
|
|
|
|
}
|
|
|
|
catch (DbException & exc) {
|
|
|
|
throw_rtexc( exc.get_errno(), exc.what() );
|
|
|
|
}
|
|
|
|
return false; // avoiding warning
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
2007-01-18 13:52:43 +00:00
|
|
|
void PersistentMap::put( OString const & key, OString const & value )
|
2006-12-20 13:20:55 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
|
|
|
|
Dbt dbData( const_cast< sal_Char * >(
|
|
|
|
value.getStr()), value.getLength() );
|
2007-01-18 13:52:43 +00:00
|
|
|
int err = m_db.put( 0, &dbKey, &dbData, 0 );
|
2006-12-20 13:20:55 +00:00
|
|
|
if (err == 0) {
|
|
|
|
#if OSL_DEBUG_LEVEL > 0
|
2007-01-19 08:34:07 +00:00
|
|
|
OString v;
|
2007-01-18 13:52:43 +00:00
|
|
|
OSL_ASSERT( get( &v, key ) );
|
|
|
|
OSL_ASSERT( v.equals( value ) );
|
2006-12-20 13:20:55 +00:00
|
|
|
#endif
|
2007-01-18 13:52:43 +00:00
|
|
|
err = m_db.sync(0);
|
2006-12-20 13:20:55 +00:00
|
|
|
}
|
2007-01-18 13:52:43 +00:00
|
|
|
if (err != 0)
|
|
|
|
throw_rtexc(err);
|
2006-12-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
catch (DbException & exc) {
|
|
|
|
throw_rtexc( exc.get_errno(), exc.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
2007-01-18 13:52:43 +00:00
|
|
|
bool PersistentMap::erase( OString const & key, bool flush_immediately )
|
2006-12-20 13:20:55 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
|
|
|
|
int err = m_db.del( &dbKey, 0 );
|
|
|
|
if (err == 0) {
|
|
|
|
if (flush_immediately) {
|
|
|
|
err = m_db.sync(0);
|
|
|
|
if (err != 0)
|
|
|
|
throw_rtexc(err);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (err == DB_NOTFOUND)
|
|
|
|
return false;
|
|
|
|
throw_rtexc(err);
|
|
|
|
}
|
|
|
|
catch (DbException & exc) {
|
|
|
|
throw_rtexc( exc.get_errno(), exc.what() );
|
|
|
|
}
|
|
|
|
return false; // avoiding warning
|
|
|
|
}
|
|
|
|
|
|
|
|
//______________________________________________________________________________
|
|
|
|
t_string2string_map PersistentMap::getEntries() const
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Dbc * pcurs = 0;
|
|
|
|
int err = m_db.cursor( 0, &pcurs, 0 );
|
|
|
|
if (err != 0)
|
|
|
|
throw_rtexc(err);
|
|
|
|
|
|
|
|
t_string2string_map ret;
|
|
|
|
for (;;) {
|
|
|
|
Dbt dbKey, dbData;
|
|
|
|
err = pcurs->get( &dbKey, &dbData, DB_NEXT );
|
|
|
|
if (err == DB_NOTFOUND)
|
|
|
|
break;
|
|
|
|
if (err != 0)
|
|
|
|
throw_rtexc(err);
|
|
|
|
|
2011-02-09 08:55:10 +01:00
|
|
|
#if OSL_DEBUG_LEVEL > 0
|
|
|
|
::std::pair<t_string2string_map::iterator, bool> insertion =
|
|
|
|
#endif
|
|
|
|
ret.insert(
|
|
|
|
t_string2string_map::value_type(
|
|
|
|
OString( static_cast<sal_Char const*>(dbKey.get_data()), dbKey.get_size() ),
|
|
|
|
OString( static_cast<sal_Char const*>(dbData.get_data()), dbData.get_size() )
|
|
|
|
) );
|
2006-12-20 13:20:55 +00:00
|
|
|
OSL_ASSERT( insertion.second );
|
|
|
|
}
|
|
|
|
err = pcurs->close();
|
|
|
|
if (err != 0)
|
|
|
|
throw_rtexc(err);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch (DbException & exc) {
|
|
|
|
throw_rtexc( exc.get_errno(), exc.what() );
|
|
|
|
}
|
|
|
|
return t_string2string_map(); // avoiding warning
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-12 15:53:47 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|