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 _CONNECTIVITY_DBASE_INDEXNODE_HXX_ 28*cdf0e10cSrcweir #define _CONNECTIVITY_DBASE_INDEXNODE_HXX_ 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include "file/fcode.hxx" 31*cdf0e10cSrcweir #include "file/FTable.hxx" 32*cdf0e10cSrcweir #include "dbase/DIndexPage.hxx" 33*cdf0e10cSrcweir #include "connectivity/FValue.hxx" 34*cdf0e10cSrcweir #include <tools/ref.hxx> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #define NODE_NOTFOUND 0xFFFF 37*cdf0e10cSrcweir #define PAGE_SIZE 512 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir namespace connectivity 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir namespace dbase 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir class ONDXNode; 45*cdf0e10cSrcweir class ODbaseIndex; 46*cdf0e10cSrcweir //================================================================== 47*cdf0e10cSrcweir // Index Key 48*cdf0e10cSrcweir //================================================================== 49*cdf0e10cSrcweir typedef file::OOperand ONDXKey_BASE; 50*cdf0e10cSrcweir class ONDXKey : public ONDXKey_BASE 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir friend class ONDXNode; 53*cdf0e10cSrcweir sal_uInt32 nRecord; /* Satzzeiger */ 54*cdf0e10cSrcweir ORowSetValue xValue; /* Schluesselwert */ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir public: 57*cdf0e10cSrcweir ONDXKey(sal_uInt32 nRec=0); 58*cdf0e10cSrcweir ONDXKey(const ORowSetValue& rVal, sal_Int32 eType, sal_uInt32 nRec); 59*cdf0e10cSrcweir ONDXKey(const rtl::OUString& aStr, sal_uInt32 nRec = 0); 60*cdf0e10cSrcweir ONDXKey(double aVal, sal_uInt32 nRec = 0); 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir inline ONDXKey(const ONDXKey& rKey); 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir inline ONDXKey& operator= (const ONDXKey& rKey); 65*cdf0e10cSrcweir virtual void setValue(const ORowSetValue& _rVal); 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir virtual const ORowSetValue& getValue() const; 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir sal_uInt32 GetRecord() const { return nRecord; } 70*cdf0e10cSrcweir void setRecord(sal_uInt32 _nRec) { nRecord = _nRec; } 71*cdf0e10cSrcweir void ResetRecord() { nRecord = 0; } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir sal_Bool operator == (const ONDXKey& rKey) const; 74*cdf0e10cSrcweir sal_Bool operator != (const ONDXKey& rKey) const; 75*cdf0e10cSrcweir sal_Bool operator < (const ONDXKey& rKey) const; 76*cdf0e10cSrcweir sal_Bool operator <= (const ONDXKey& rKey) const; 77*cdf0e10cSrcweir sal_Bool operator > (const ONDXKey& rKey) const; 78*cdf0e10cSrcweir sal_Bool operator >= (const ONDXKey& rKey) const; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir sal_Bool Load (SvFileStream& rStream, sal_Bool bText); 81*cdf0e10cSrcweir sal_Bool Write(SvFileStream& rStream, sal_Bool bText); 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir static sal_Bool IsText(sal_Int32 eType); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir private: 86*cdf0e10cSrcweir StringCompare Compare(const ONDXKey& rKey) const; 87*cdf0e10cSrcweir }; 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir //================================================================== 93*cdf0e10cSrcweir // Index Seitenverweis 94*cdf0e10cSrcweir //================================================================== 95*cdf0e10cSrcweir SV_DECL_REF(ONDXPage) // Basisklasse da weitere Informationen gehalten werden muessen 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir class ONDXPagePtr : public ONDXPageRef 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir friend SvStream& operator << (SvStream &rStream, const ONDXPagePtr&); 101*cdf0e10cSrcweir friend SvStream& operator >> (SvStream &rStream, ONDXPagePtr&); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir sal_uInt32 nPagePos; // Position in der Indexdatei 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir public: 106*cdf0e10cSrcweir ONDXPagePtr(sal_uInt32 nPos = 0):nPagePos(nPos){} 107*cdf0e10cSrcweir ONDXPagePtr(const ONDXPagePtr& rRef); 108*cdf0e10cSrcweir ONDXPagePtr(ONDXPage* pRefPage); 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir ONDXPagePtr& operator=(const ONDXPagePtr& rRef); 111*cdf0e10cSrcweir ONDXPagePtr& operator=(ONDXPage* pPageRef); 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir sal_uInt32 GetPagePos() const {return nPagePos;} 114*cdf0e10cSrcweir sal_Bool HasPage() const {return nPagePos != 0;} 115*cdf0e10cSrcweir // sal_Bool Is() const { return isValid(); } 116*cdf0e10cSrcweir }; 117*cdf0e10cSrcweir //================================================================== 118*cdf0e10cSrcweir // Index Seite 119*cdf0e10cSrcweir //================================================================== 120*cdf0e10cSrcweir class ONDXPage : public SvRefBase 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir friend class ODbaseIndex; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir friend SvStream& operator << (SvStream &rStream, const ONDXPage&); 125*cdf0e10cSrcweir friend SvStream& operator >> (SvStream &rStream, ONDXPage&); 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir sal_uInt32 nPagePos; // Position in der Indexdatei 128*cdf0e10cSrcweir sal_Bool bModified : 1; 129*cdf0e10cSrcweir sal_uInt16 nCount; 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir ONDXPagePtr aParent, // VaterSeite 132*cdf0e10cSrcweir aChild; // Zeiger auf rechte ChildPage 133*cdf0e10cSrcweir ODbaseIndex& rIndex; 134*cdf0e10cSrcweir ONDXNode* ppNodes; // array von Knoten 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir public: 137*cdf0e10cSrcweir // Knoten Operationen 138*cdf0e10cSrcweir sal_uInt16 Count() const {return nCount;} 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir sal_Bool Insert(ONDXNode& rNode, sal_uInt32 nRowsLeft = 0); 141*cdf0e10cSrcweir sal_Bool Insert(sal_uInt16 nIndex, ONDXNode& rNode); 142*cdf0e10cSrcweir sal_Bool Append(ONDXNode& rNode); 143*cdf0e10cSrcweir sal_Bool Delete(sal_uInt16); 144*cdf0e10cSrcweir void Remove(sal_uInt16); 145*cdf0e10cSrcweir void Release(sal_Bool bSave = sal_True); 146*cdf0e10cSrcweir void ReleaseFull(sal_Bool bSave = sal_True); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir // Aufteilen und Zerlegen 149*cdf0e10cSrcweir ONDXNode Split(ONDXPage& rPage); 150*cdf0e10cSrcweir void Merge(sal_uInt16 nParentNodePos, ONDXPagePtr xPage); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir // Zugriffsoperationen 153*cdf0e10cSrcweir ONDXNode& operator[] (sal_uInt16 nPos); 154*cdf0e10cSrcweir const ONDXNode& operator[] (sal_uInt16 nPos) const; 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir sal_Bool IsRoot() const; 157*cdf0e10cSrcweir sal_Bool IsLeaf() const; 158*cdf0e10cSrcweir sal_Bool IsModified() const; 159*cdf0e10cSrcweir sal_Bool HasParent(); 160*cdf0e10cSrcweir sal_Bool HasChild() const; 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir sal_Bool IsFull() const; 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir sal_uInt32 GetPagePos() const {return nPagePos;} 165*cdf0e10cSrcweir ONDXPagePtr& GetChild(ODbaseIndex* pIndex = 0); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir // Parent braucht nicht nachgeladen zu werden 168*cdf0e10cSrcweir ONDXPagePtr GetParent(); 169*cdf0e10cSrcweir ODbaseIndex& GetIndex() {return rIndex;} 170*cdf0e10cSrcweir const ODbaseIndex& GetIndex() const {return rIndex;} 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir // Setzen des Childs, ueber Referenz, um die PagePos zu erhalten 173*cdf0e10cSrcweir void SetChild(ONDXPagePtr aCh); 174*cdf0e10cSrcweir void SetParent(ONDXPagePtr aPa); 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir sal_uInt16 Search(const ONDXKey& rSearch); 177*cdf0e10cSrcweir sal_uInt16 Search(const ONDXPage* pPage); 178*cdf0e10cSrcweir void SearchAndReplace(const ONDXKey& rSearch, ONDXKey& rReplace); 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir protected: 181*cdf0e10cSrcweir ONDXPage(ODbaseIndex& rIndex, sal_uInt32 nPos, ONDXPage* = NULL); 182*cdf0e10cSrcweir ~ONDXPage(); 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir virtual void QueryDelete(); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir void SetModified(sal_Bool bMod) {bModified = bMod;} 187*cdf0e10cSrcweir void SetPagePos(sal_uInt32 nPage) {nPagePos = nPage;} 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir sal_Bool Find(const ONDXKey&); // rek. Abstieg 190*cdf0e10cSrcweir sal_uInt16 FindPos(const ONDXKey& rKey) const; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 193*cdf0e10cSrcweir void PrintPage(); 194*cdf0e10cSrcweir #endif 195*cdf0e10cSrcweir }; 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir SV_IMPL_REF(ONDXPage); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir SvStream& operator << (SvStream &rStream, const ONDXPagePtr&); 200*cdf0e10cSrcweir SvStream& operator >> (SvStream &rStream, ONDXPagePtr&); 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir inline sal_Bool ONDXPage::IsRoot() const {return !aParent.Is();} 203*cdf0e10cSrcweir inline sal_Bool ONDXPage::IsLeaf() const {return !aChild.HasPage();} 204*cdf0e10cSrcweir inline sal_Bool ONDXPage::IsModified() const {return bModified;} 205*cdf0e10cSrcweir inline sal_Bool ONDXPage::HasParent() {return aParent.Is();} 206*cdf0e10cSrcweir inline sal_Bool ONDXPage::HasChild() const {return aChild.HasPage();} 207*cdf0e10cSrcweir inline ONDXPagePtr ONDXPage::GetParent() {return aParent;} 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir inline void ONDXPage::SetParent(ONDXPagePtr aPa = ONDXPagePtr()) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir aParent = aPa; 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir inline void ONDXPage::SetChild(ONDXPagePtr aCh = ONDXPagePtr()) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir aChild = aCh; 217*cdf0e10cSrcweir if (aChild.Is()) 218*cdf0e10cSrcweir aChild->SetParent(this); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir SvStream& operator >> (SvStream &rStream, ONDXPage& rPage); 221*cdf0e10cSrcweir SvStream& operator << (SvStream &rStream, const ONDXPage& rPage); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir typedef ::std::vector<ONDXPage*> ONDXPageList; 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir //================================================================== 227*cdf0e10cSrcweir // Index Knoten 228*cdf0e10cSrcweir //================================================================== 229*cdf0e10cSrcweir class ONDXNode 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir friend class ONDXPage; 232*cdf0e10cSrcweir ONDXPagePtr aChild; /* naechster Seitenverweis */ 233*cdf0e10cSrcweir ONDXKey aKey; 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir public: 236*cdf0e10cSrcweir ONDXNode(){} 237*cdf0e10cSrcweir ONDXNode(const ONDXKey& rKey, 238*cdf0e10cSrcweir ONDXPagePtr aPagePtr = ONDXPagePtr()) 239*cdf0e10cSrcweir :aChild(aPagePtr),aKey(rKey) {} 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir // verweist der Knoten auf eine Seite 242*cdf0e10cSrcweir sal_Bool HasChild() const {return aChild.HasPage();} 243*cdf0e10cSrcweir // Ist ein Index angegeben, kann gegebenfalls die Seite nachgeladen werden 244*cdf0e10cSrcweir ONDXPagePtr& GetChild(ODbaseIndex* pIndex = NULL, ONDXPage* = NULL); 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir const ONDXKey& GetKey() const { return aKey;} 247*cdf0e10cSrcweir ONDXKey& GetKey() { return aKey;} 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir // Setzen des Childs, ueber Referenz, um die PagePos zu erhalten 250*cdf0e10cSrcweir void SetChild(ONDXPagePtr aCh = ONDXPagePtr(), ONDXPage* = NULL); 251*cdf0e10cSrcweir void SetKey(ONDXKey& rKey) {aKey = rKey;} 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir void Write(SvStream &rStream, const ONDXPage& rPage) const; 254*cdf0e10cSrcweir void Read(SvStream &rStream, ODbaseIndex&); 255*cdf0e10cSrcweir }; 256*cdf0e10cSrcweir //================================================================== 257*cdf0e10cSrcweir // inline implementation 258*cdf0e10cSrcweir //================================================================== 259*cdf0e10cSrcweir // inline ONDXKey::ONDXKey(const ORowSetValue& rVal, sal_Int32 eType, sal_uInt32 nRec) 260*cdf0e10cSrcweir // : ONDXKey_BASE(eType) 261*cdf0e10cSrcweir // , nRecord(nRec),xValue(rVal) 262*cdf0e10cSrcweir // { 263*cdf0e10cSrcweir // } 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir // inline ONDXKey::ONDXKey(const rtl::OUString& aStr, sal_uInt32 nRec) 267*cdf0e10cSrcweir // : ONDXKey_BASE(::com::sun::star::sdbc::DataType::VARCHAR) 268*cdf0e10cSrcweir // ,nRecord(nRec) 269*cdf0e10cSrcweir // { 270*cdf0e10cSrcweir // if (aStr.len()) 271*cdf0e10cSrcweir // xValue = aStr; 272*cdf0e10cSrcweir // } 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir // inline ONDXKey::ONDXKey(double aVal, sal_uInt32 nRec) 275*cdf0e10cSrcweir // : ONDXKey_BASE(::com::sun::star::sdbc::DataType::DOUBLE) 276*cdf0e10cSrcweir // ,nRecord(nRec) 277*cdf0e10cSrcweir // ,xValue(aVal) 278*cdf0e10cSrcweir // { 279*cdf0e10cSrcweir // } 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir // inline ONDXKey::ONDXKey(sal_uInt32 nRec) 282*cdf0e10cSrcweir // :nRecord(nRec) 283*cdf0e10cSrcweir // { 284*cdf0e10cSrcweir // } 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir inline ONDXKey::ONDXKey(const ONDXKey& rKey) 287*cdf0e10cSrcweir : ONDXKey_BASE(rKey.getDBType()) 288*cdf0e10cSrcweir ,nRecord(rKey.nRecord) 289*cdf0e10cSrcweir ,xValue(rKey.xValue) 290*cdf0e10cSrcweir { 291*cdf0e10cSrcweir } 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir inline ONDXKey& ONDXKey::operator=(const ONDXKey& rKey) 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir if(&rKey == this) 296*cdf0e10cSrcweir return *this; 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir xValue = rKey.xValue; 299*cdf0e10cSrcweir nRecord = rKey.nRecord; 300*cdf0e10cSrcweir m_eDBType = rKey.getDBType(); 301*cdf0e10cSrcweir return *this; 302*cdf0e10cSrcweir } 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir inline sal_Bool ONDXKey::operator == (const ONDXKey& rKey) const 305*cdf0e10cSrcweir { 306*cdf0e10cSrcweir if(&rKey == this) 307*cdf0e10cSrcweir return sal_True; 308*cdf0e10cSrcweir return Compare(rKey) == COMPARE_EQUAL; 309*cdf0e10cSrcweir } 310*cdf0e10cSrcweir inline sal_Bool ONDXKey::operator != (const ONDXKey& rKey) const 311*cdf0e10cSrcweir { 312*cdf0e10cSrcweir return !operator== (rKey); 313*cdf0e10cSrcweir } 314*cdf0e10cSrcweir inline sal_Bool ONDXKey::operator < (const ONDXKey& rKey) const 315*cdf0e10cSrcweir { 316*cdf0e10cSrcweir return Compare(rKey) == COMPARE_LESS; 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir inline sal_Bool ONDXKey::operator > (const ONDXKey& rKey) const 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir return Compare(rKey) == COMPARE_GREATER; 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir inline sal_Bool ONDXKey::operator <= (const ONDXKey& rKey) const 323*cdf0e10cSrcweir { 324*cdf0e10cSrcweir return !operator > (rKey); 325*cdf0e10cSrcweir } 326*cdf0e10cSrcweir inline sal_Bool ONDXKey::operator >= (const ONDXKey& rKey) const 327*cdf0e10cSrcweir { 328*cdf0e10cSrcweir return !operator< (rKey); 329*cdf0e10cSrcweir } 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir inline void ONDXNode::SetChild(ONDXPagePtr aCh, ONDXPage* pParent) 332*cdf0e10cSrcweir { 333*cdf0e10cSrcweir aChild = aCh; 334*cdf0e10cSrcweir if (aChild.Is()) 335*cdf0e10cSrcweir aChild->SetParent(pParent); 336*cdf0e10cSrcweir } 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir } 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir } 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir 343*cdf0e10cSrcweir 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir #endif // _CONNECTIVITY_DBASE_INDEXNODE_HXX_ 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir 348