1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 #define NODE_NOTFOUND 0xFFFF
30 #define DINDEX_PAGE_SIZE 512
32 namespace connectivity
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 */
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
);
79 int Compare(const ONDXKey
& rKey
) const;
89 // This is ref-count pointer class
92 friend SvStream
& WriteONDXPagePtr(SvStream
&rStream
, const ONDXPagePtr
&);
93 friend SvStream
& operator >> (SvStream
&rStream
, ONDXPagePtr
&);
96 sal_uInt32 nPagePos
; // Position in the index file
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; }
111 ONDXPagePtr
& operator=(const ONDXPagePtr
& rRef
);
115 // This is a ref-counted class, with re-cycling
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
131 ONDXPagePtr aParent
, // Parent page
132 aChild
; // Pointer to the right child page
134 ONDXNode
* ppNodes
; // Array of nodes
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);
149 ONDXNode
Split(ONDXPage
& rPage
);
150 void Merge(sal_uInt16 nParentNodePos
, ONDXPagePtr xPage
);
153 ONDXNode
& operator[] (sal_uInt16 nPos
);
154 const ONDXNode
& operator[] (sal_uInt16 nPos
) const;
158 bool IsModified() const;
160 bool HasChild() 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
);
181 ONDXPage(ODbaseIndex
& rIndex
, sal_uInt32 nPos
, ONDXPage
* = NULL
);
188 assert( nRefCount
< (1 << 30) && "Do not add refs to dead objects" );
193 assert( nRefCount
< (1 << 30) && "Do not add refs to dead objects" );
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
210 inline ONDXPagePtr::~ONDXPagePtr() { if (mpPage
!= 0) mpPage
->ReleaseRef(); }
211 inline void ONDXPagePtr::Clear()
214 ONDXPage
* pRefObj
= mpPage
;
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())
235 inline void ONDXPage::SetChild(ONDXPagePtr aCh
= ONDXPagePtr())
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
;
252 friend class ONDXPage
;
253 ONDXPagePtr aChild
; /* Next page reference */
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)
287 // inline ONDXKey::ONDXKey(const OUString& aStr, sal_uInt32 nRec)
288 // : ONDXKey_BASE(::com::sun::star::sdbc::DataType::VARCHAR)
295 // inline ONDXKey::ONDXKey(double aVal, sal_uInt32 nRec)
296 // : ONDXKey_BASE(::com::sun::star::sdbc::DataType::DOUBLE)
302 // inline ONDXKey::ONDXKey(sal_uInt32 nRec)
307 inline ONDXKey::ONDXKey(const ONDXKey
& rKey
)
308 : ONDXKey_BASE(rKey
.getDBType())
309 ,nRecord(rKey
.nRecord
)
314 inline ONDXKey
& ONDXKey::operator=(const ONDXKey
& rKey
)
319 xValue
= rKey
.xValue
;
320 nRecord
= rKey
.nRecord
;
321 m_eDBType
= rKey
.getDBType();
325 inline bool ONDXKey::operator == (const ONDXKey
& rKey
) const
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
)
356 aChild
->SetParent(pParent
);
366 #endif // INCLUDED_CONNECTIVITY_SOURCE_INC_DBASE_DINDEXNODE_HXX
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */