1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
36 #include <rtl/alloc.h>
40 namespace berkeleydbproxy
{
42 //----------------------------------------------------------------------------
45 static void raise_error(int dberr
, const char * where
);
47 static inline int check_error(int dberr
, const char * where
)
49 if (dberr
) raise_error(dberr
,where
);
54 //----------------------------------------------------------------------------
56 char *DbEnv::strerror(int error
) {
57 return (db_strerror(error
));
60 //----------------------------------------------------------------------------
62 Db::Db(DbEnv
* pDbenv
,u_int32_t flags
)
65 db_internal::check_error( db_create(&m_pDBP
,pDbenv
? pDbenv
->m_pDBENV
:0,flags
),"Db::Db" );
80 int Db::close(u_int32_t flags
)
82 int error
= m_pDBP
->close(m_pDBP
,flags
);
84 return db_internal::check_error(error
,"Db::close");
87 int Db::open(DB_TXN
*txnid
,
94 int err
= m_pDBP
->open(m_pDBP
,txnid
,file
,database
,type
,flags
,mode
);
95 return db_internal::check_error( err
,"Db::open" );
99 int Db::get(DB_TXN
*txnid
, Dbt
*key
, Dbt
*data
, u_int32_t flags
)
101 int err
= m_pDBP
->get(m_pDBP
,txnid
,key
,data
,flags
);
103 // these are non-exceptional outcomes
104 if (err
!= DB_NOTFOUND
&& err
!= DB_KEYEMPTY
)
105 db_internal::check_error( err
,"Db::get" );
110 int Db::put(DB_TXN
* txnid
, Dbt
*key
, Dbt
*data
, u_int32_t flags
)
112 int err
= m_pDBP
->put(m_pDBP
,txnid
,key
,data
,flags
);
114 if (err
!= DB_KEYEXIST
) // this is a non-exceptional outcome
115 db_internal::check_error( err
,"Db::put" );
119 int Db::cursor(DB_TXN
*txnid
, Dbc
**cursorp
, u_int32_t flags
)
122 int error
= m_pDBP
->cursor(m_pDBP
,txnid
,&dbc
,flags
);
124 if (!db_internal::check_error(error
,"Db::cursor"))
125 *cursorp
= new Dbc(dbc
);
131 #define DB_INCOMPLETE (-30999)/* Sync didn't finish. */
133 int Db::sync(u_int32_t flags
)
139 db_internal::check_error(EINVAL
,"Db::sync");
142 if ((err
= db
->sync(db
, flags
)) != 0 && err
!= DB_INCOMPLETE
) {
143 db_internal::check_error(err
, "Db::sync");
149 int Db::del(Dbt
*key
, u_int32_t flags
)
154 if ((err
= db
->del(db
, 0, key
, flags
)) != 0) {
155 // DB_NOTFOUND is a "normal" return, so should not be
156 // thrown as an error
158 if (err
!= DB_NOTFOUND
) {
159 db_internal::check_error(err
, "Db::del");
166 //----------------------------------------------------------------------------
179 int err
= m_pDBC
->c_close(m_pDBC
);
181 return db_internal::check_error( err
,"Dbcursor::close" );
184 int Dbc::get(Dbt
*key
, Dbt
*data
, u_int32_t flags
)
186 int err
= m_pDBC
->c_get(m_pDBC
,key
,data
,flags
);
188 // these are non-exceptional outcomes
189 if (err
!= DB_NOTFOUND
&& err
!= DB_KEYEMPTY
)
190 db_internal::check_error( err
, "Dbcursor::get" );
195 //----------------------------------------------------------------------------
200 DBT
* thispod
= this;
201 memset(thispod
, 0, sizeof *thispod
);
205 Dbt::Dbt(void *data_arg
, u_int32_t size_arg
)
208 DBT
* thispod
= this;
209 memset(thispod
, 0, sizeof *thispod
);
210 this->set_data(data_arg
);
211 this->set_size(size_arg
);
214 Dbt::Dbt(const Dbt
& other
)
217 const DBT
*otherpod
= &other
;
219 memcpy(thispod
, otherpod
, sizeof *thispod
);
222 Dbt
& Dbt::operator = (const Dbt
& other
)
227 const DBT
*otherpod
= &other
;
229 memcpy(thispod
, otherpod
, sizeof *thispod
);
238 void * Dbt::get_data() const
243 void Dbt::set_data(void *value
)
248 u_int32_t
Dbt::get_size() const
253 void Dbt::set_size(u_int32_t value
)
258 //----------------------------------------------------------------------------
259 void db_internal::raise_error(int dberr
, const char * where
)
261 if (!where
) where
= "<unknown>";
263 const char * dberrmsg
= db_strerror(dberr
);
264 if (!dberrmsg
|| !*dberrmsg
) dberrmsg
= "<unknown DB error>";
266 rtl::OString msg
= where
;
270 throw DbException(msg
);
273 //----------------------------------------------------------------------------