xref: /AOO41X/main/xmlhelp/source/cxxhelp/provider/db.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef BERKELEYDBPROXY_DB_HXX_
28*cdf0e10cSrcweir #define BERKELEYDBPROXY_DB_HXX_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #ifdef SYSTEM_DB
31*cdf0e10cSrcweir #include <db.h>
32*cdf0e10cSrcweir #else
33*cdf0e10cSrcweir #include <berkeleydb/db.h>
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "com/sun/star/ucb/XSimpleFileAccess.hpp"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <hash_map>
39*cdf0e10cSrcweir #include <rtl/string.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir extern "C" {
42*cdf0e10cSrcweir   typedef void *(*db_malloc_fcn_type)(size_t);
43*cdf0e10cSrcweir   typedef void *(*db_realloc_fcn_type)(void *, size_t);
44*cdf0e10cSrcweir   typedef void (*db_free_fcn_type)(void *);
45*cdf0e10cSrcweir }
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace berkeleydbproxy {
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     class Dbc;
51*cdf0e10cSrcweir     class Dbt;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     namespace db_internal
54*cdf0e10cSrcweir     {
55*cdf0e10cSrcweir         class Noncopyable
56*cdf0e10cSrcweir         {
57*cdf0e10cSrcweir             // not implemented
58*cdf0e10cSrcweir             Noncopyable(const Noncopyable&);
59*cdf0e10cSrcweir             void operator=(const Noncopyable&);
60*cdf0e10cSrcweir         protected:
61*cdf0e10cSrcweir             Noncopyable() {}
62*cdf0e10cSrcweir             ~Noncopyable() {}
63*cdf0e10cSrcweir         };
64*cdf0e10cSrcweir     }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     class DbException
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         rtl::OString what_;
69*cdf0e10cSrcweir     public:
70*cdf0e10cSrcweir         explicit DbException(rtl::OString const & whatparam)
71*cdf0e10cSrcweir         : what_(whatparam)
72*cdf0e10cSrcweir         {}
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	    const char *what() const
75*cdf0e10cSrcweir         { return what_.getStr(); }
76*cdf0e10cSrcweir     };
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	struct eq
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		bool operator()( const rtl::OString& rKey1, const rtl::OString& rKey2 ) const
81*cdf0e10cSrcweir 			{ return rKey1.compareTo( rKey2 ) == 0; }
82*cdf0e10cSrcweir 	};
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	struct ha
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		size_t operator()( const rtl::OString& rName ) const
87*cdf0e10cSrcweir 			{ return rName.hashCode(); }
88*cdf0e10cSrcweir 	};
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir //#define TEST_DBHELP
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	class DBData
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir 		friend class		DBHelp;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 		int					m_nSize;
98*cdf0e10cSrcweir 		char*				m_pBuffer;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 		void copyToBuffer( const char* pSrcData, int nSize );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	public:
103*cdf0e10cSrcweir 		DBData( void )
104*cdf0e10cSrcweir 			: m_nSize( 0 )
105*cdf0e10cSrcweir 			, m_pBuffer( NULL )
106*cdf0e10cSrcweir 		{}
107*cdf0e10cSrcweir 		~DBData()
108*cdf0e10cSrcweir 			{ delete [] m_pBuffer; }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir   	    int getSize() const
111*cdf0e10cSrcweir 			{ return m_nSize; }
112*cdf0e10cSrcweir   	    const char* getData() const
113*cdf0e10cSrcweir 			{ return m_pBuffer; }
114*cdf0e10cSrcweir 	};
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	typedef std::hash_map< rtl::OString,std::pair<int,int>,ha,eq >	StringToValPosMap;
117*cdf0e10cSrcweir 	typedef std::hash_map< rtl::OString,rtl::OString,ha,eq >		StringToDataMap;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	class DBHelp
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		rtl::OUString		m_aFileURL;
122*cdf0e10cSrcweir 		StringToDataMap*	m_pStringToDataMap;
123*cdf0e10cSrcweir 		StringToValPosMap*	m_pStringToValPosMap;
124*cdf0e10cSrcweir 		com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess >
125*cdf0e10cSrcweir 							m_xSFA;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 		com::sun::star::uno::Sequence< sal_Int8 >
128*cdf0e10cSrcweir 							m_aItData;
129*cdf0e10cSrcweir 		const char*			m_pItData;
130*cdf0e10cSrcweir 		int					m_nItRead;
131*cdf0e10cSrcweir 		int					m_iItPos;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		bool implReadLenAndData( const char* pData, int& riPos, DBData& rValue );
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	public:
136*cdf0e10cSrcweir         //DBHelp must get a fileURL which can then directly be used by simple file access.
137*cdf0e10cSrcweir         //SimpleFileAccess requires file URLs as arguments. Passing file path may work but fails
138*cdf0e10cSrcweir         //for example when using long file paths on Windows, which start with "\\?\"
139*cdf0e10cSrcweir 		DBHelp( const rtl::OUString& rFileURL,
140*cdf0e10cSrcweir 			com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess > xSFA )
141*cdf0e10cSrcweir 				: m_aFileURL( rFileURL )
142*cdf0e10cSrcweir 				, m_pStringToDataMap( NULL )
143*cdf0e10cSrcweir 				, m_pStringToValPosMap( NULL )
144*cdf0e10cSrcweir 				, m_xSFA( xSFA )
145*cdf0e10cSrcweir 				, m_pItData( NULL )
146*cdf0e10cSrcweir 				, m_nItRead( -1 )
147*cdf0e10cSrcweir 				, m_iItPos( -1 )
148*cdf0e10cSrcweir 		{
149*cdf0e10cSrcweir             OSL_ASSERT(!rFileURL.compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:")), 5));
150*cdf0e10cSrcweir         }
151*cdf0e10cSrcweir 		~DBHelp()
152*cdf0e10cSrcweir 			{ releaseHashMap(); }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 		void createHashMap( bool bOptimizeForPerformance = false );
155*cdf0e10cSrcweir 		void releaseHashMap( void );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir #ifdef TEST_DBHELP
158*cdf0e10cSrcweir 		bool testAgainstDb( const rtl::OUString& fileURL, bool bOldDbAccess );
159*cdf0e10cSrcweir #endif
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		bool getValueForKey( const rtl::OString& rKey, DBData& rValue );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 		bool startIteration( void );
164*cdf0e10cSrcweir 		bool getNextKeyAndValue( DBData& rKey, DBData& rValue );
165*cdf0e10cSrcweir 		void stopIteration( void );
166*cdf0e10cSrcweir 	};
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     class Db : db_internal::Noncopyable
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir     private:
171*cdf0e10cSrcweir 	    DB* m_pDBP;
172*cdf0e10cSrcweir 	    DBHelp* m_pDBHelp;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     public:
175*cdf0e10cSrcweir 	    Db();
176*cdf0e10cSrcweir 	    ~Db();
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 	    void setDBHelp( DBHelp* pDBHelp )
179*cdf0e10cSrcweir 			{ m_pDBHelp = pDBHelp; }
180*cdf0e10cSrcweir 	    DBHelp* getDBHelp( void )
181*cdf0e10cSrcweir 			{ return m_pDBHelp; }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	    int close(u_int32_t flags);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	    int open(DB_TXN *txnid,
186*cdf0e10cSrcweir 			     const char *file,
187*cdf0e10cSrcweir 			     const char *database,
188*cdf0e10cSrcweir 			     DBTYPE type,
189*cdf0e10cSrcweir 			     u_int32_t flags,
190*cdf0e10cSrcweir 			     int mode);
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         int open(DB_TXN *txnid,
193*cdf0e10cSrcweir 			     ::rtl::OUString const & fileURL,
194*cdf0e10cSrcweir 			     DBTYPE type,
195*cdf0e10cSrcweir 			     u_int32_t flags,
196*cdf0e10cSrcweir 			     int mode);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     	int get(DB_TXN* txnid, Dbt *key, Dbt *data, u_int32_t flags);
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	    int cursor(DB_TXN *txnid, Dbc **cursorp, u_int32_t flags);
202*cdf0e10cSrcweir     };
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     class Dbc : db_internal::Noncopyable
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir     	friend class Db;
207*cdf0e10cSrcweir 	    friend class Dbt;
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     private:
210*cdf0e10cSrcweir 	    DBC* m_pDBC;
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	    explicit Dbc(DBC* pDBC);
213*cdf0e10cSrcweir 	    ~Dbc();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     public:
216*cdf0e10cSrcweir 	    int close();
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	    int get(Dbt *key, Dbt *data, u_int32_t flags);
219*cdf0e10cSrcweir     };
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     class Dbt: private DBT
222*cdf0e10cSrcweir     {
223*cdf0e10cSrcweir 	    friend class Db;
224*cdf0e10cSrcweir 	    friend class Dbc;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     public:
227*cdf0e10cSrcweir 	    Dbt(void *data_arg, u_int32_t size_arg);
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 	    Dbt();
230*cdf0e10cSrcweir         //Dbt(const Dbt & other);
231*cdf0e10cSrcweir         //Dbt & operator=(const Dbt & other);
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 	    ~Dbt();
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir   	    void *get_data() const;
236*cdf0e10cSrcweir 	    void set_data(void *value);
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir   	    u_int32_t get_size() const;
239*cdf0e10cSrcweir 	    void set_size(u_int32_t value);
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 	    void set_flags(u_int32_t);
242*cdf0e10cSrcweir     };
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir #endif
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 
262