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