Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "dp_misc.h"
31 : : #include "dp_ucb.h"
32 : : #include "dp_persmap.h"
33 : : #include "rtl/strbuf.hxx"
34 : : #include "rtl/ustrbuf.hxx"
35 : : #include "osl/file.hxx"
36 : : #include "osl/thread.h"
37 : :
38 : :
39 : : using namespace ::com::sun::star;
40 : : using namespace ::com::sun::star::uno;
41 : : using namespace ::rtl;
42 : : using ::osl::File;
43 : :
44 : : namespace dp_misc
45 : : {
46 : :
47 : : //______________________________________________________________________________
48 : 0 : void PersistentMap::throw_rtexc( int err, char const * pmsg ) const
49 : : {
50 : 0 : OUStringBuffer buf;
51 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("[") );
52 [ # # ]: 0 : buf.append( m_sysPath );
53 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("] Berkeley Db error (") );
54 [ # # ]: 0 : buf.append( static_cast<sal_Int32>(err) );
55 [ # # ]: 0 : buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("): ") );
56 [ # # ]: 0 : if (pmsg == 0)
57 [ # # ]: 0 : pmsg = DbEnv::strerror(err);
58 : 0 : const OString msg(pmsg);
59 : : buf.append( OUString( msg.getStr(), msg.getLength(),
60 [ # # ][ # # ]: 0 : osl_getThreadTextEncoding() ) );
[ # # ]
61 [ # # ]: 0 : const OUString msg_(buf.makeStringAndClear());
62 : : OSL_FAIL( rtl::OUStringToOString(
63 : : msg_, RTL_TEXTENCODING_UTF8 ).getStr() );
64 [ # # ]: 0 : throw RuntimeException( msg_, uno::Reference<XInterface>() );
65 : : }
66 : :
67 : : //______________________________________________________________________________
68 [ + - ]: 380 : PersistentMap::~PersistentMap()
69 : : {
70 : : try {
71 [ + - ]: 380 : m_db.close(0);
72 : : }
73 [ # # ]: 0 : catch (DbException & exc) {
74 : : (void) exc; // avoid warnings
75 : : OSL_FAIL( DbEnv::strerror( exc.get_errno() ) );
76 : : }
77 [ # # ]: 380 : }
78 : :
79 : : //______________________________________________________________________________
80 : 380 : PersistentMap::PersistentMap( OUString const & url )
81 [ + - ]: 380 : : m_db( 0 )
82 : : {
83 : : try
84 : : {
85 [ + - ]: 380 : rtl::OUString fileURL = expandUnoRcUrl(url);
86 [ + - ]: 380 : if ( File::getSystemPathFromFileURL( fileURL, m_sysPath ) != File::E_None )
87 : : OSL_ASSERT( false );
88 : :
89 : : OString cstr_sysPath(
90 [ + - ]: 380 : OUStringToOString( m_sysPath, RTL_TEXTENCODING_UTF8 ) );
91 : : int err = m_db.open(
92 : : // xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
93 : : 0, cstr_sysPath.getStr(), 0, DB_HASH,
94 [ + - ]: 380 : DB_CREATE/* | DB_THREAD*/, 0664 /* fs mode */ );
95 [ - + ]: 380 : if (err != 0)
96 [ # # ]: 380 : throw_rtexc(err);
97 : : }
98 [ # # # # ]: 0 : catch (const DbException & exc)
99 : : {
100 [ # # ]: 0 : throw_rtexc( exc.get_errno(), exc.what() );
101 : : }
102 : 380 : }
103 : :
104 : : //______________________________________________________________________________
105 : 0 : PersistentMap::PersistentMap()
106 [ # # ]: 0 : : m_db( 0 )
107 : : {
108 : : try {
109 : : // xxx todo: DB_THREAD, DB_DBT_MALLOC currently not used
110 [ # # ]: 0 : int err = m_db.open( 0, 0, 0, DB_HASH, DB_CREATE/* | DB_THREAD*/, 0 );
111 [ # # ]: 0 : if (err != 0)
112 [ # # ]: 0 : throw_rtexc(err);
113 : : }
114 [ # # # # ]: 0 : catch (DbException & exc) {
115 [ # # ]: 0 : throw_rtexc( exc.get_errno(), exc.what() );
116 : : }
117 : 0 : }
118 : :
119 : : //______________________________________________________________________________
120 : 0 : bool PersistentMap::has( OString const & key ) const
121 : : {
122 : 0 : return get( 0, key );
123 : : }
124 : :
125 : : //______________________________________________________________________________
126 : 70 : bool PersistentMap::get( OString * value, OString const & key ) const
127 : : {
128 : : try {
129 [ + - ]: 70 : Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
130 [ + - ]: 70 : Dbt dbData;
131 [ + - ]: 70 : int err = m_db.get( 0, &dbKey, &dbData, 0 );
132 [ + + ]: 70 : if (err == DB_NOTFOUND)
133 : 56 : return false;
134 [ + - ]: 14 : if (err == 0) {
135 [ + - ]: 14 : if (value != 0) {
136 : : *value = OString(
137 [ + - ]: 14 : static_cast< sal_Char const * >(dbData.get_data()),
138 [ + - ]: 28 : dbData.get_size() );
139 : : }
140 : 14 : return true;
141 : : }
142 [ # # ][ + - ]: 70 : throw_rtexc(err);
[ + - ][ + - ]
[ - + ]
143 : : }
144 [ # # ]: 0 : catch (DbException & exc) {
145 [ # # ]: 0 : throw_rtexc( exc.get_errno(), exc.what() );
146 : : }
147 : : #ifndef _MSC_VER
148 : 70 : return false; // avoiding warning
149 : : #endif
150 : : }
151 : :
152 : : //______________________________________________________________________________
153 : 502 : void PersistentMap::put( OString const & key, OString const & value )
154 : : {
155 : : try {
156 [ + - ]: 502 : Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
157 : : Dbt dbData( const_cast< sal_Char * >(
158 [ + - ]: 502 : value.getStr()), value.getLength() );
159 [ + - ]: 502 : int err = m_db.put( 0, &dbKey, &dbData, 0 );
160 [ + - ]: 502 : if (err == 0) {
161 : : #if OSL_DEBUG_LEVEL > 0
162 : : OString v;
163 : : OSL_ASSERT( get( &v, key ) );
164 : : OSL_ASSERT( v.equals( value ) );
165 : : #endif
166 [ + - ]: 502 : err = m_db.sync(0);
167 : : }
168 [ - + ]: 502 : if (err != 0)
169 [ # # ][ + - ]: 502 : throw_rtexc(err);
[ + - ]
170 : : }
171 [ # # ]: 0 : catch (DbException & exc) {
172 [ # # ]: 0 : throw_rtexc( exc.get_errno(), exc.what() );
173 : : }
174 : 502 : }
175 : :
176 : : //______________________________________________________________________________
177 : 6 : bool PersistentMap::erase( OString const & key, bool flush_immediately )
178 : : {
179 : : try {
180 [ + - ]: 6 : Dbt dbKey( const_cast< sal_Char * >(key.getStr()), key.getLength() );
181 [ + - ]: 6 : int err = m_db.del( &dbKey, 0 );
182 [ + - ]: 6 : if (err == 0) {
183 [ + - ]: 6 : if (flush_immediately) {
184 [ + - ]: 6 : err = m_db.sync(0);
185 [ - + ]: 6 : if (err != 0)
186 [ # # ]: 0 : throw_rtexc(err);
187 : : }
188 : 6 : return true;
189 : : }
190 [ # # ]: 0 : if (err == DB_NOTFOUND)
191 : 0 : return false;
192 [ # # ][ + - ]: 6 : throw_rtexc(err);
[ - + ]
193 : : }
194 [ # # ]: 0 : catch (DbException & exc) {
195 [ # # ]: 0 : throw_rtexc( exc.get_errno(), exc.what() );
196 : : }
197 : : #ifndef _MSC_VER
198 : 6 : return false; // avoiding warning
199 : : #endif
200 : : }
201 : :
202 : : //______________________________________________________________________________
203 : 1570 : t_string2string_map PersistentMap::getEntries() const
204 : : {
205 : : try {
206 : 1570 : Dbc * pcurs = 0;
207 [ + - ]: 1570 : int err = m_db.cursor( 0, &pcurs, 0 );
208 [ - + ]: 1570 : if (err != 0)
209 [ # # ]: 0 : throw_rtexc(err);
210 : :
211 [ + - ]: 1570 : t_string2string_map ret;
212 : 2852 : for (;;) {
213 [ + - ][ + - ]: 4422 : Dbt dbKey, dbData;
214 [ + - ]: 4422 : err = pcurs->get( &dbKey, &dbData, DB_NEXT );
215 [ + + ]: 4422 : if (err == DB_NOTFOUND)
216 : : break;
217 [ - + ]: 2852 : if (err != 0)
218 [ # # ]: 0 : throw_rtexc(err);
219 : :
220 : : #if OSL_DEBUG_LEVEL > 0
221 : : ::std::pair<t_string2string_map::iterator, bool> insertion =
222 : : #endif
223 : : ret.insert(
224 : : t_string2string_map::value_type(
225 [ + - ][ + - ]: 2852 : OString( static_cast<sal_Char const*>(dbKey.get_data()), dbKey.get_size() ),
226 [ + - ][ + - ]: 2852 : OString( static_cast<sal_Char const*>(dbData.get_data()), dbData.get_size() )
227 [ + - ][ + + ]: 10126 : ) );
228 : : OSL_ASSERT( insertion.second );
229 [ + - ][ + - ]: 8844 : }
[ + + ]
230 [ + - ]: 1570 : err = pcurs->close();
231 [ - + ]: 1570 : if (err != 0)
232 [ # # ]: 0 : throw_rtexc(err);
233 [ + - ][ + - ]: 1570 : return ret;
234 : : }
235 [ # # ]: 0 : catch (DbException & exc) {
236 [ # # ]: 0 : throw_rtexc( exc.get_errno(), exc.what() );
237 : : }
238 : : #ifndef _MSC_VER
239 [ # # ]: 1570 : return t_string2string_map(); // avoiding warning
240 : : #endif
241 : : }
242 : :
243 : : }
244 : :
245 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|