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