bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / inc / dbase / dindexnode.hxx
blobb32163511e6a82b15f979510c7b9ff8202664ce2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_CONNECTIVITY_SOURCE_INC_DBASE_DINDEXNODE_HXX
20 #define INCLUDED_CONNECTIVITY_SOURCE_INC_DBASE_DINDEXNODE_HXX
22 #include "file/fcode.hxx"
23 #include "file/FTable.hxx"
24 #include <connectivity/FValue.hxx>
25 #include <rtl/ref.hxx>
26 #include <tools/stream.hxx>
27 #include <vector>
29 #define NODE_NOTFOUND 0xFFFF
30 #define DINDEX_PAGE_SIZE 512
32 namespace connectivity
34 namespace dbase
37 class ONDXNode;
38 class ODbaseIndex;
40 // Index Key
42 typedef file::OOperand ONDXKey_BASE;
43 class ONDXKey : public ONDXKey_BASE
45 friend class ONDXNode;
46 sal_uInt32 nRecord; /* Record pointer */
47 ORowSetValue xValue; /* Key values */
49 public:
50 ONDXKey(sal_uInt32 nRec=0);
51 ONDXKey(const ORowSetValue& rVal, sal_Int32 eType, sal_uInt32 nRec);
52 ONDXKey(const OUString& aStr, sal_uInt32 nRec = 0);
53 ONDXKey(double aVal, sal_uInt32 nRec = 0);
55 inline ONDXKey(const ONDXKey& rKey);
57 inline ONDXKey& operator= (const ONDXKey& rKey);
58 virtual void setValue(const ORowSetValue& _rVal) SAL_OVERRIDE;
60 virtual const ORowSetValue& getValue() const SAL_OVERRIDE;
62 sal_uInt32 GetRecord() const { return nRecord; }
63 void setRecord(sal_uInt32 _nRec) { nRecord = _nRec; }
64 void ResetRecord() { nRecord = 0; }
66 bool operator == (const ONDXKey& rKey) const;
67 bool operator != (const ONDXKey& rKey) const;
68 bool operator < (const ONDXKey& rKey) const;
69 bool operator <= (const ONDXKey& rKey) const;
70 bool operator > (const ONDXKey& rKey) const;
71 bool operator >= (const ONDXKey& rKey) const;
73 bool Load (SvFileStream& rStream, bool bText);
74 bool Write(SvFileStream& rStream, bool bText);
76 static bool IsText(sal_Int32 eType);
78 private:
79 int Compare(const ONDXKey& rKey) const;
86 class ONDXPage;
88 // Index Page Pointer
89 // This is ref-count pointer class
90 class ONDXPagePtr
92 friend SvStream& WriteONDXPagePtr(SvStream &rStream, const ONDXPagePtr&);
93 friend SvStream& operator >> (SvStream &rStream, ONDXPagePtr&);
95 ONDXPage* mpPage;
96 sal_uInt32 nPagePos; // Position in the index file
98 public:
99 ONDXPagePtr(sal_uInt32 nPos = 0) : mpPage(0), nPagePos(nPos) {}
100 ONDXPagePtr(const ONDXPagePtr& rRef);
101 ONDXPagePtr(ONDXPage* pRefPage);
102 inline ~ONDXPagePtr();
104 sal_uInt32 GetPagePos() const {return nPagePos;}
105 bool HasPage() const {return nPagePos != 0;}
107 operator ONDXPage *() const { return mpPage; }
108 ONDXPage * operator ->() const { assert(mpPage != 0); return mpPage; }
109 bool Is() const { return mpPage != 0; }
110 inline void Clear();
111 ONDXPagePtr& operator=(const ONDXPagePtr& rRef);
114 // Index Page
115 // This is a ref-counted class, with re-cycling
116 class ONDXPage
118 friend class ODbaseIndex;
119 friend class ONDXPagePtr;
121 friend SvStream& WriteONDXPage(SvStream &rStream, const ONDXPage&);
122 friend SvStream& operator >> (SvStream &rStream, ONDXPage&);
124 // the only reason this is not bool is because MSVC cannot handle mixed type bitfields
125 unsigned int bNoDelete : 1;
126 unsigned int nRefCount : 31;
127 sal_uInt32 nPagePos; // Position in the index file
128 bool bModified : 1;
129 sal_uInt16 nCount;
131 ONDXPagePtr aParent, // Parent page
132 aChild; // Pointer to the right child page
133 ODbaseIndex& rIndex;
134 ONDXNode* ppNodes; // Array of nodes
136 public:
137 // Node operations
138 sal_uInt16 Count() const {return nCount;}
140 bool Insert(ONDXNode& rNode, sal_uInt32 nRowsLeft = 0);
141 bool Insert(sal_uInt16 nIndex, ONDXNode& rNode);
142 bool Append(ONDXNode& rNode);
143 bool Delete(sal_uInt16);
144 void Remove(sal_uInt16);
145 void Release(bool bSave = true);
146 void ReleaseFull(bool bSave = true);
148 // Split and merge
149 ONDXNode Split(ONDXPage& rPage);
150 void Merge(sal_uInt16 nParentNodePos, ONDXPagePtr xPage);
152 // Access operators
153 ONDXNode& operator[] (sal_uInt16 nPos);
154 const ONDXNode& operator[] (sal_uInt16 nPos) const;
156 bool IsRoot() const;
157 bool IsLeaf() const;
158 bool IsModified() const;
159 bool HasParent();
160 bool HasChild() const;
162 bool IsFull() const;
164 sal_uInt32 GetPagePos() const {return nPagePos;}
165 ONDXPagePtr& GetChild(ODbaseIndex* pIndex = 0);
167 // Parent does not need to be reloaded
168 ONDXPagePtr GetParent();
169 ODbaseIndex& GetIndex() {return rIndex;}
170 const ODbaseIndex& GetIndex() const {return rIndex;}
172 // Setting the child, via reference to retain the PagePos
173 void SetChild(ONDXPagePtr aCh);
174 void SetParent(ONDXPagePtr aPa);
176 sal_uInt16 Search(const ONDXKey& rSearch);
177 sal_uInt16 Search(const ONDXPage* pPage);
178 void SearchAndReplace(const ONDXKey& rSearch, ONDXKey& rReplace);
180 protected:
181 ONDXPage(ODbaseIndex& rIndex, sal_uInt32 nPos, ONDXPage* = NULL);
182 ~ONDXPage();
184 void ReleaseRef();
185 void QueryDelete();
186 void AddNextRef()
188 assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
189 ++nRefCount;
191 void AddFirstRef()
193 assert( nRefCount < (1 << 30) && "Do not add refs to dead objects" );
194 if( bNoDelete )
195 bNoDelete = 0;
196 ++nRefCount;
199 void SetModified(bool bMod) {bModified = bMod;}
200 void SetPagePos(sal_uInt32 nPage) {nPagePos = nPage;}
202 bool Find(const ONDXKey&); // Descend recursively
203 sal_uInt16 FindPos(const ONDXKey& rKey) const;
205 #if OSL_DEBUG_LEVEL > 1
206 void PrintPage();
207 #endif
210 inline ONDXPagePtr::~ONDXPagePtr() { if (mpPage != 0) mpPage->ReleaseRef(); }
211 inline void ONDXPagePtr::Clear()
213 if (mpPage != 0) {
214 ONDXPage * pRefObj = mpPage;
215 mpPage = 0;
216 pRefObj->ReleaseRef();
220 SvStream& WriteONDXPagePtr(SvStream &rStream, const ONDXPagePtr&);
221 SvStream& operator >> (SvStream &rStream, ONDXPagePtr&);
223 inline bool ONDXPage::IsRoot() const {return !aParent.Is();}
224 inline bool ONDXPage::IsLeaf() const {return !aChild.HasPage();}
225 inline bool ONDXPage::IsModified() const {return bModified;}
226 inline bool ONDXPage::HasParent() {return aParent.Is();}
227 inline bool ONDXPage::HasChild() const {return aChild.HasPage();}
228 inline ONDXPagePtr ONDXPage::GetParent() {return aParent;}
230 inline void ONDXPage::SetParent(ONDXPagePtr aPa = ONDXPagePtr())
232 aParent = aPa;
235 inline void ONDXPage::SetChild(ONDXPagePtr aCh = ONDXPagePtr())
237 aChild = aCh;
238 if (aChild.Is())
239 aChild->SetParent(this);
241 SvStream& operator >> (SvStream &rStream, ONDXPage& rPage);
242 SvStream& WriteONDXPage(SvStream &rStream, const ONDXPage& rPage);
245 typedef ::std::vector<ONDXPage*> ONDXPageList;
248 // Index Node
250 class ONDXNode
252 friend class ONDXPage;
253 ONDXPagePtr aChild; /* Next page reference */
254 ONDXKey aKey;
256 public:
257 ONDXNode(){}
258 ONDXNode(const ONDXKey& rKey,
259 ONDXPagePtr aPagePtr = ONDXPagePtr())
260 :aChild(aPagePtr),aKey(rKey) {}
262 // Does the node point to a page?
263 bool HasChild() const {return aChild.HasPage();}
264 // If an index is provided, we may be able to retrieve the page
265 ONDXPagePtr& GetChild(ODbaseIndex* pIndex = NULL, ONDXPage* = NULL);
267 const ONDXKey& GetKey() const { return aKey;}
268 ONDXKey& GetKey() { return aKey;}
270 // Setting the child, via reference to retain the PagePos
271 void SetChild(ONDXPagePtr aCh = ONDXPagePtr(), ONDXPage* = NULL);
272 void SetKey(ONDXKey& rKey) {aKey = rKey;}
274 void Write(SvStream &rStream, const ONDXPage& rPage) const;
275 void Read(SvStream &rStream, ODbaseIndex&);
278 // inline implementation
280 // inline ONDXKey::ONDXKey(const ORowSetValue& rVal, sal_Int32 eType, sal_uInt32 nRec)
281 // : ONDXKey_BASE(eType)
282 // , nRecord(nRec),xValue(rVal)
283 // {
284 // }
287 // inline ONDXKey::ONDXKey(const OUString& aStr, sal_uInt32 nRec)
288 // : ONDXKey_BASE(::com::sun::star::sdbc::DataType::VARCHAR)
289 // ,nRecord(nRec)
290 // {
291 // if (aStr.len())
292 // xValue = aStr;
293 // }
295 // inline ONDXKey::ONDXKey(double aVal, sal_uInt32 nRec)
296 // : ONDXKey_BASE(::com::sun::star::sdbc::DataType::DOUBLE)
297 // ,nRecord(nRec)
298 // ,xValue(aVal)
299 // {
300 // }
302 // inline ONDXKey::ONDXKey(sal_uInt32 nRec)
303 // :nRecord(nRec)
304 // {
305 // }
307 inline ONDXKey::ONDXKey(const ONDXKey& rKey)
308 : ONDXKey_BASE(rKey.getDBType())
309 ,nRecord(rKey.nRecord)
310 ,xValue(rKey.xValue)
314 inline ONDXKey& ONDXKey::operator=(const ONDXKey& rKey)
316 if(&rKey == this)
317 return *this;
319 xValue = rKey.xValue;
320 nRecord = rKey.nRecord;
321 m_eDBType = rKey.getDBType();
322 return *this;
325 inline bool ONDXKey::operator == (const ONDXKey& rKey) const
327 if(&rKey == this)
328 return true;
329 return Compare(rKey) == 0;
331 inline bool ONDXKey::operator != (const ONDXKey& rKey) const
333 return !operator== (rKey);
335 inline bool ONDXKey::operator < (const ONDXKey& rKey) const
337 return Compare(rKey) < 0;
339 inline bool ONDXKey::operator > (const ONDXKey& rKey) const
341 return Compare(rKey) > 0;
343 inline bool ONDXKey::operator <= (const ONDXKey& rKey) const
345 return !operator > (rKey);
347 inline bool ONDXKey::operator >= (const ONDXKey& rKey) const
349 return !operator< (rKey);
352 inline void ONDXNode::SetChild(ONDXPagePtr aCh, ONDXPage* pParent)
354 aChild = aCh;
355 if (aChild.Is())
356 aChild->SetParent(pParent);
366 #endif // INCLUDED_CONNECTIVITY_SOURCE_INC_DBASE_DINDEXNODE_HXX
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */