1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_desktop.hxx"
33 #include "dp_persmap.h"
34 #include "rtl/strbuf.hxx"
35 #include "rtl/ustrbuf.hxx"
36 #include "osl/file.hxx"
37 #include "osl/thread.h"
40 using namespace ::com::sun::star
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::rtl
;
48 //______________________________________________________________________________
49 void PersistentMap::throw_rtexc( int err
, char const * pmsg
) const
52 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("[") );
53 buf
.append( m_sysPath
);
54 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("] Berkeley Db error (") );
55 buf
.append( static_cast<sal_Int32
>(err
) );
56 buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("): ") );
58 pmsg
= DbEnv::strerror(err
);
59 const OString
msg(pmsg
);
60 buf
.append( OUString( msg
.getStr(), msg
.getLength(),
61 osl_getThreadTextEncoding() ) );
62 const OUString
msg_(buf
.makeStringAndClear());
63 OSL_ENSURE( 0, rtl::OUStringToOString(
64 msg_
, RTL_TEXTENCODING_UTF8
).getStr() );
65 throw RuntimeException( msg_
, Reference
<XInterface
>() );
68 //______________________________________________________________________________
69 PersistentMap::~PersistentMap()
74 catch (DbException
& exc
) {
75 (void) exc
; // avoid warnings
76 OSL_ENSURE( 0, DbEnv::strerror( exc
.get_errno() ) );
80 //______________________________________________________________________________
81 PersistentMap::PersistentMap( OUString
const & url_
, bool readOnly
)
85 OUString
url( expandUnoRcUrl(url_
) );
86 if ( File::getSystemPathFromFileURL( url
, m_sysPath
) != File::E_None
)
91 OUStringToOString( m_sysPath
, RTL_TEXTENCODING_UTF8
) );
92 char const * pcstr_sysPath
= cstr_sysPath
.getStr();
94 u_int32_t flags
= DB_CREATE
;
97 if (! create_ucb_content(
99 Reference
<com::sun::star::ucb::XCommandEnvironment
>(),
100 false /* no throw */ )) {
101 // ignore non-existent file in read-only mode: simulate empty db
108 // xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
109 0, pcstr_sysPath
, 0, DB_HASH
, flags
/* | DB_THREAD*/, 0664 /* fs mode */ );
113 catch (DbException
& exc
) {
114 throw_rtexc( exc
.get_errno(), exc
.what() );
118 //______________________________________________________________________________
119 PersistentMap::PersistentMap()
123 // xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
124 int err
= m_db
.open( 0, 0, 0, DB_HASH
, DB_CREATE
/* | DB_THREAD*/, 0 );
128 catch (DbException
& exc
) {
129 throw_rtexc( exc
.get_errno(), exc
.what() );
133 //______________________________________________________________________________
134 bool PersistentMap::has( OString
const & key
) const
136 return get( 0, key
);
139 //______________________________________________________________________________
140 bool PersistentMap::get( OString
* value
, OString
const & key
) const
143 Dbt
dbKey( const_cast< sal_Char
* >(key
.getStr()), key
.getLength() );
145 int err
= m_db
.get( 0, &dbKey
, &dbData
, 0 );
146 if (err
== DB_NOTFOUND
)
151 static_cast< sal_Char
const * >(dbData
.get_data()),
158 catch (DbException
& exc
) {
159 throw_rtexc( exc
.get_errno(), exc
.what() );
161 return false; // avoiding warning
164 //______________________________________________________________________________
165 void PersistentMap::put( OString
const & key
, OString
const & value
)
168 Dbt
dbKey( const_cast< sal_Char
* >(key
.getStr()), key
.getLength() );
169 Dbt
dbData( const_cast< sal_Char
* >(
170 value
.getStr()), value
.getLength() );
171 int err
= m_db
.put( 0, &dbKey
, &dbData
, 0 );
173 #if OSL_DEBUG_LEVEL > 0
175 OSL_ASSERT( get( &v
, key
) );
176 OSL_ASSERT( v
.equals( value
) );
183 catch (DbException
& exc
) {
184 throw_rtexc( exc
.get_errno(), exc
.what() );
188 //______________________________________________________________________________
189 bool PersistentMap::erase( OString
const & key
, bool flush_immediately
)
192 Dbt
dbKey( const_cast< sal_Char
* >(key
.getStr()), key
.getLength() );
193 int err
= m_db
.del( &dbKey
, 0 );
195 if (flush_immediately
) {
202 if (err
== DB_NOTFOUND
)
206 catch (DbException
& exc
) {
207 throw_rtexc( exc
.get_errno(), exc
.what() );
209 return false; // avoiding warning
212 //______________________________________________________________________________
213 t_string2string_map
PersistentMap::getEntries() const
217 int err
= m_db
.cursor( 0, &pcurs
, 0 );
221 t_string2string_map ret
;
224 err
= pcurs
->get( &dbKey
, &dbData
, DB_NEXT
);
225 if (err
== DB_NOTFOUND
)
230 ::std::pair
<t_string2string_map::iterator
, bool > insertion(
231 ret
.insert( t_string2string_map::value_type(
232 t_string2string_map::value_type(
233 OString( static_cast< sal_Char
const * >(
236 OString( static_cast< sal_Char
const * >(
238 dbData
.get_size() ) ) ) ) );
239 OSL_ASSERT( insertion
.second
);
241 err
= pcurs
->close();
246 catch (DbException
& exc
) {
247 throw_rtexc( exc
.get_errno(), exc
.what() );
249 return t_string2string_map(); // avoiding warning