Update ooo320-m1
[ooovba.git] / xmlhelp / source / cxxhelp / provider / db.hxx
blob82b63743211614bd09145fee43e0690971a0eb91
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: db.hxx,v $
10 * $Revision: 1.9 $
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 ************************************************************************/
30 #ifndef BERKELEYDBPROXY_DB_HXX_
31 #define BERKELEYDBPROXY_DB_HXX_
33 #ifdef SYSTEM_DB
34 #include <db.h>
35 #else
36 #include <berkeleydb/db.h>
37 #endif
39 #include "com/sun/star/ucb/XSimpleFileAccess.hpp"
41 #include <hash_map>
42 #include <rtl/string.hxx>
44 extern "C" {
45 typedef void *(*db_malloc_fcn_type)(size_t);
46 typedef void *(*db_realloc_fcn_type)(void *, size_t);
47 typedef void (*db_free_fcn_type)(void *);
51 namespace berkeleydbproxy {
53 class Dbc;
54 class Dbt;
56 namespace db_internal
58 class Noncopyable
60 // not implemented
61 Noncopyable(const Noncopyable&);
62 void operator=(const Noncopyable&);
63 protected:
64 Noncopyable() {}
65 ~Noncopyable() {}
69 class DbException
71 rtl::OString what_;
72 public:
73 explicit DbException(rtl::OString const & whatparam)
74 : what_(whatparam)
77 const char *what() const
78 { return what_.getStr(); }
81 struct eq
83 bool operator()( const rtl::OString& rKey1, const rtl::OString& rKey2 ) const
84 { return rKey1.compareTo( rKey2 ) == 0; }
87 struct ha
89 size_t operator()( const rtl::OString& rName ) const
90 { return rName.hashCode(); }
94 //#define TEST_DBHELP
96 class DBData
98 friend class DBHelp;
100 int m_nSize;
101 char* m_pBuffer;
103 void copyToBuffer( const char* pSrcData, int nSize );
105 public:
106 DBData( void )
107 : m_nSize( 0 )
108 , m_pBuffer( NULL )
110 ~DBData()
111 { delete [] m_pBuffer; }
113 int getSize() const
114 { return m_nSize; }
115 const char* getData() const
116 { return m_pBuffer; }
119 typedef std::hash_map< rtl::OString,std::pair<int,int>,ha,eq > StringToValPosMap;
120 typedef std::hash_map< rtl::OString,rtl::OString,ha,eq > StringToDataMap;
122 class DBHelp
124 rtl::OUString m_aFileName;
125 StringToDataMap* m_pStringToDataMap;
126 StringToValPosMap* m_pStringToValPosMap;
127 com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess >
128 m_xSFA;
130 com::sun::star::uno::Sequence< sal_Int8 >
131 m_aItData;
132 const char* m_pItData;
133 int m_nItRead;
134 int m_iItPos;
136 bool implReadLenAndData( const char* pData, int& riPos, DBData& rValue );
138 public:
139 DBHelp( const rtl::OUString& rFileName,
140 com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess > xSFA )
141 : m_aFileName( rFileName )
142 , m_pStringToDataMap( NULL )
143 , m_pStringToValPosMap( NULL )
144 , m_xSFA( xSFA )
145 , m_pItData( NULL )
146 , m_nItRead( -1 )
147 , m_iItPos( -1 )
149 ~DBHelp()
150 { releaseHashMap(); }
152 void createHashMap( bool bOptimizeForPerformance = false );
153 void releaseHashMap( void );
155 #ifdef TEST_DBHELP
156 bool testAgainstDb( const rtl::OString& fileName, bool bOldDbAccess );
157 #endif
159 bool getValueForKey( const rtl::OString& rKey, DBData& rValue );
161 bool startIteration( void );
162 bool getNextKeyAndValue( DBData& rKey, DBData& rValue );
163 void stopIteration( void );
166 class Db : db_internal::Noncopyable
168 private:
169 DB* m_pDBP;
170 DBHelp* m_pDBHelp;
172 public:
173 Db();
174 ~Db();
176 void setDBHelp( DBHelp* pDBHelp )
177 { m_pDBHelp = pDBHelp; }
178 DBHelp* getDBHelp( void )
179 { return m_pDBHelp; }
181 int close(u_int32_t flags);
183 int open(DB_TXN *txnid,
184 const char *file,
185 const char *database,
186 DBTYPE type,
187 u_int32_t flags,
188 int mode);
191 int get(DB_TXN* txnid, Dbt *key, Dbt *data, u_int32_t flags);
193 int cursor(DB_TXN *txnid, Dbc **cursorp, u_int32_t flags);
196 class Dbc : db_internal::Noncopyable
198 friend class Db;
199 friend class Dbt;
201 private:
202 DBC* m_pDBC;
204 explicit Dbc(DBC* pDBC);
205 ~Dbc();
207 public:
208 int close();
210 int get(Dbt *key, Dbt *data, u_int32_t flags);
213 class Dbt: private DBT
215 friend class Db;
216 friend class Dbc;
218 public:
219 Dbt(void *data_arg, u_int32_t size_arg);
221 Dbt();
222 //Dbt(const Dbt & other);
223 //Dbt & operator=(const Dbt & other);
225 ~Dbt();
227 void *get_data() const;
228 void set_data(void *value);
230 u_int32_t get_size() const;
231 void set_size(u_int32_t value);
233 void set_flags(u_int32_t);
237 #endif