xref: /trunk/main/store/source/stortree.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_store.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "stortree.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "sal/types.h"
34*cdf0e10cSrcweir #include "osl/diagnose.h"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "store/types.h"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "storbase.hxx"
39*cdf0e10cSrcweir #include "storbios.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace store;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir /*========================================================================
44*cdf0e10cSrcweir  *
45*cdf0e10cSrcweir  * OStoreBTreeNodeData implementation.
46*cdf0e10cSrcweir  *
47*cdf0e10cSrcweir  *======================================================================*/
48*cdf0e10cSrcweir /*
49*cdf0e10cSrcweir  * OStoreBTreeNodeData.
50*cdf0e10cSrcweir  */
51*cdf0e10cSrcweir OStoreBTreeNodeData::OStoreBTreeNodeData (sal_uInt16 nPageSize)
52*cdf0e10cSrcweir     : OStorePageData (nPageSize)
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     base::m_aGuard.m_nMagic = store::htonl(self::theTypeId);
55*cdf0e10cSrcweir     base::m_aDescr.m_nUsed  = store::htons(self::thePageSize); // usageCount(0)
56*cdf0e10cSrcweir     self::m_aGuard.m_nMagic = store::htonl(0); // depth(0)
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     sal_uInt16 const n = capacityCount();
59*cdf0e10cSrcweir     T const          t;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     for (sal_uInt16 i = 1; i < n; i++)
62*cdf0e10cSrcweir         m_pData[i] = t;
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir /*
66*cdf0e10cSrcweir  * find.
67*cdf0e10cSrcweir  */
68*cdf0e10cSrcweir sal_uInt16 OStoreBTreeNodeData::find (const T& t) const
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir     register sal_Int32 l = 0;
71*cdf0e10cSrcweir     register sal_Int32 r = usageCount() - 1;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     while (l < r)
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         register sal_Int32 const m = ((l + r) >> 1);
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         if (t.m_aKey == m_pData[m].m_aKey)
78*cdf0e10cSrcweir             return ((sal_uInt16)(m));
79*cdf0e10cSrcweir         if (t.m_aKey < m_pData[m].m_aKey)
80*cdf0e10cSrcweir             r = m - 1;
81*cdf0e10cSrcweir         else
82*cdf0e10cSrcweir             l = m + 1;
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     sal_uInt16 const k = ((sal_uInt16)(r));
86*cdf0e10cSrcweir     if ((k < capacityCount()) && (t.m_aKey < m_pData[k].m_aKey))
87*cdf0e10cSrcweir         return(k - 1);
88*cdf0e10cSrcweir     else
89*cdf0e10cSrcweir         return(k);
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir /*
93*cdf0e10cSrcweir  * insert.
94*cdf0e10cSrcweir  */
95*cdf0e10cSrcweir void OStoreBTreeNodeData::insert (sal_uInt16 i, const T& t)
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir     sal_uInt16 const n = usageCount();
98*cdf0e10cSrcweir     sal_uInt16 const m = capacityCount();
99*cdf0e10cSrcweir     if ((n < m) && (i < m))
100*cdf0e10cSrcweir     {
101*cdf0e10cSrcweir         // shift right.
102*cdf0e10cSrcweir         memmove (&(m_pData[i + 1]), &(m_pData[i]), (n - i) * sizeof(T));
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         // insert.
105*cdf0e10cSrcweir         m_pData[i] = t;
106*cdf0e10cSrcweir         usageCount (n + 1);
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir /*
111*cdf0e10cSrcweir  * remove.
112*cdf0e10cSrcweir  */
113*cdf0e10cSrcweir void OStoreBTreeNodeData::remove (sal_uInt16 i)
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir     sal_uInt16 const n = usageCount();
116*cdf0e10cSrcweir     if (i < n)
117*cdf0e10cSrcweir     {
118*cdf0e10cSrcweir         // shift left.
119*cdf0e10cSrcweir         memmove (&(m_pData[i]), &(m_pData[i + 1]), (n - i - 1) * sizeof(T));
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         // truncate.
122*cdf0e10cSrcweir         m_pData[n - 1] = T();
123*cdf0e10cSrcweir         usageCount (n - 1);
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir #if 0  /* NYI */
128*cdf0e10cSrcweir /*
129*cdf0e10cSrcweir  * merge (with right page).
130*cdf0e10cSrcweir  */
131*cdf0e10cSrcweir void OStoreBTreeNodeData::merge (const self& rPageR)
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir     sal_uInt16 const n = usageCount();
134*cdf0e10cSrcweir     sal_uInt16 const m = rPageR.usageCount();
135*cdf0e10cSrcweir     if ((n + m) <= capacityCount())
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         memcpy (&(m_pData[n]), &(rPageR.m_pData[0]), m * sizeof(T));
138*cdf0e10cSrcweir         usageCount (n + m);
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir #endif
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir /*
144*cdf0e10cSrcweir  * split (left half copied from right half of left page).
145*cdf0e10cSrcweir  */
146*cdf0e10cSrcweir void OStoreBTreeNodeData::split (const self& rPageL)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir     sal_uInt16 const h = capacityCount() / 2;
149*cdf0e10cSrcweir     memcpy (&(m_pData[0]), &(rPageL.m_pData[h]), h * sizeof(T));
150*cdf0e10cSrcweir     truncate (h);
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir /*
154*cdf0e10cSrcweir  * truncate.
155*cdf0e10cSrcweir  */
156*cdf0e10cSrcweir void OStoreBTreeNodeData::truncate (sal_uInt16 n)
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir     sal_uInt16 const m = capacityCount();
159*cdf0e10cSrcweir     T const          t;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     for (sal_uInt16 i = n; i < m; i++)
162*cdf0e10cSrcweir         m_pData[i] = t;
163*cdf0e10cSrcweir     usageCount (n);
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir /*========================================================================
167*cdf0e10cSrcweir  *
168*cdf0e10cSrcweir  * OStoreBTreeNodeObject implementation.
169*cdf0e10cSrcweir  *
170*cdf0e10cSrcweir  *======================================================================*/
171*cdf0e10cSrcweir /*
172*cdf0e10cSrcweir  * guard.
173*cdf0e10cSrcweir  */
174*cdf0e10cSrcweir storeError OStoreBTreeNodeObject::guard (sal_uInt32 nAddr)
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir     return PageHolderObject< page >::guard (m_xPage, nAddr);
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir /*
180*cdf0e10cSrcweir  * verify.
181*cdf0e10cSrcweir  */
182*cdf0e10cSrcweir storeError OStoreBTreeNodeObject::verify (sal_uInt32 nAddr) const
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir     return PageHolderObject< page >::verify (m_xPage, nAddr);
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir /*
188*cdf0e10cSrcweir  * split.
189*cdf0e10cSrcweir  */
190*cdf0e10cSrcweir storeError OStoreBTreeNodeObject::split (
191*cdf0e10cSrcweir     sal_uInt16                 nIndexL,
192*cdf0e10cSrcweir     PageHolderObject< page > & rxPageL,
193*cdf0e10cSrcweir     OStorePageBIOS           & rBIOS)
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     PageHolderObject< page > xPage (m_xPage);
196*cdf0e10cSrcweir     if (!xPage.is())
197*cdf0e10cSrcweir         return store_E_InvalidAccess;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     // Check left page usage.
200*cdf0e10cSrcweir     if (!rxPageL.is())
201*cdf0e10cSrcweir         return store_E_InvalidAccess;
202*cdf0e10cSrcweir     if (!rxPageL->querySplit())
203*cdf0e10cSrcweir         return store_E_None;
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     // Construct right page.
206*cdf0e10cSrcweir     PageHolderObject< page > xPageR;
207*cdf0e10cSrcweir     if (!xPageR.construct (rBIOS.allocator()))
208*cdf0e10cSrcweir         return store_E_OutOfMemory;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     // Split right page off left page.
211*cdf0e10cSrcweir     xPageR->split (*rxPageL);
212*cdf0e10cSrcweir     xPageR->depth (rxPageL->depth());
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     // Allocate right page.
215*cdf0e10cSrcweir     self aNodeR (xPageR.get());
216*cdf0e10cSrcweir     storeError eErrCode = rBIOS.allocate (aNodeR);
217*cdf0e10cSrcweir     if (eErrCode != store_E_None)
218*cdf0e10cSrcweir         return eErrCode;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     // Truncate left page.
221*cdf0e10cSrcweir     rxPageL->truncate (rxPageL->capacityCount() / 2);
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     // Save left page.
224*cdf0e10cSrcweir     self aNodeL (rxPageL.get());
225*cdf0e10cSrcweir     eErrCode = rBIOS.saveObjectAt (aNodeL, aNodeL.location());
226*cdf0e10cSrcweir     if (eErrCode != store_E_None)
227*cdf0e10cSrcweir         return eErrCode;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     // Insert right page.
230*cdf0e10cSrcweir     OStorePageLink aLink (xPageR->location());
231*cdf0e10cSrcweir     xPage->insert (nIndexL + 1, T(xPageR->m_pData[0].m_aKey, aLink));
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     // Save this page and leave.
234*cdf0e10cSrcweir     return rBIOS.saveObjectAt (*this, location());
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir /*
238*cdf0e10cSrcweir  * remove (down to leaf node, recursive).
239*cdf0e10cSrcweir  */
240*cdf0e10cSrcweir storeError OStoreBTreeNodeObject::remove (
241*cdf0e10cSrcweir     sal_uInt16         nIndexL,
242*cdf0e10cSrcweir     OStoreBTreeEntry & rEntryL,
243*cdf0e10cSrcweir     OStorePageBIOS &   rBIOS)
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir     PageHolderObject< page > xImpl (m_xPage);
246*cdf0e10cSrcweir     page & rPage = (*xImpl);
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir     // Check depth.
249*cdf0e10cSrcweir     storeError eErrCode = store_E_None;
250*cdf0e10cSrcweir     if (rPage.depth())
251*cdf0e10cSrcweir     {
252*cdf0e10cSrcweir         // Check link entry.
253*cdf0e10cSrcweir         T const aEntryL (rPage.m_pData[nIndexL]);
254*cdf0e10cSrcweir         if (!(rEntryL.compare (aEntryL) == T::COMPARE_EQUAL))
255*cdf0e10cSrcweir             return store_E_InvalidAccess;
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         // Load link node.
258*cdf0e10cSrcweir         self aNodeL;
259*cdf0e10cSrcweir         eErrCode = rBIOS.loadObjectAt (aNodeL, aEntryL.m_aLink.location());
260*cdf0e10cSrcweir         if (eErrCode != store_E_None)
261*cdf0e10cSrcweir             return eErrCode;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir         // Recurse: remove from link node.
264*cdf0e10cSrcweir         eErrCode = aNodeL.remove (0, rEntryL, rBIOS);
265*cdf0e10cSrcweir         if (eErrCode != store_E_None)
266*cdf0e10cSrcweir             return eErrCode;
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir         // Check resulting link node usage.
269*cdf0e10cSrcweir         PageHolderObject< page > xPageL (aNodeL.get());
270*cdf0e10cSrcweir         if (xPageL->usageCount() == 0)
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir             // Free empty link node.
273*cdf0e10cSrcweir             eErrCode = rBIOS.free (xPageL->location());
274*cdf0e10cSrcweir             if (eErrCode != store_E_None)
275*cdf0e10cSrcweir                 return eErrCode;
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir             // Remove index.
278*cdf0e10cSrcweir             rPage.remove (nIndexL);
279*cdf0e10cSrcweir             touch();
280*cdf0e10cSrcweir         }
281*cdf0e10cSrcweir         else
282*cdf0e10cSrcweir         {
283*cdf0e10cSrcweir #if 0   /* NYI */
284*cdf0e10cSrcweir             // Check for right sibling.
285*cdf0e10cSrcweir             sal_uInt16 const nIndexR = nIndexL + 1;
286*cdf0e10cSrcweir             if (nIndexR < rPage.usageCount())
287*cdf0e10cSrcweir             {
288*cdf0e10cSrcweir                 // Load right link node.
289*cdf0e10cSrcweir                 self aNodeR;
290*cdf0e10cSrcweir                 eErrCode = rBIOS.loadObjectAt (aNodeR, rPage.m_pData[nIndexR].m_aLink.location());
291*cdf0e10cSrcweir                 if (eErrCode == store_E_None)
292*cdf0e10cSrcweir                 {
293*cdf0e10cSrcweir                     if (rPageL.queryMerge (rPageR))
294*cdf0e10cSrcweir                     {
295*cdf0e10cSrcweir                         rPageL.merge (rPageR);
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir                         eErrCode = rBIOS.free (rPageR.location());
298*cdf0e10cSrcweir                     }
299*cdf0e10cSrcweir                 }
300*cdf0e10cSrcweir             }
301*cdf0e10cSrcweir #endif  /* NYI */
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir             // Relink.
304*cdf0e10cSrcweir             rPage.m_pData[nIndexL].m_aKey = xPageL->m_pData[0].m_aKey;
305*cdf0e10cSrcweir             touch();
306*cdf0e10cSrcweir         }
307*cdf0e10cSrcweir     }
308*cdf0e10cSrcweir     else
309*cdf0e10cSrcweir     {
310*cdf0e10cSrcweir         // Check leaf entry.
311*cdf0e10cSrcweir         if (!(rEntryL.compare (rPage.m_pData[nIndexL]) == T::COMPARE_EQUAL))
312*cdf0e10cSrcweir             return store_E_NotExists;
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir         // Save leaf entry.
315*cdf0e10cSrcweir         rEntryL = rPage.m_pData[nIndexL];
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir         // Remove leaf index.
318*cdf0e10cSrcweir         rPage.remove (nIndexL);
319*cdf0e10cSrcweir         touch();
320*cdf0e10cSrcweir     }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir     // Check for modified node.
323*cdf0e10cSrcweir     if (dirty())
324*cdf0e10cSrcweir     {
325*cdf0e10cSrcweir         // Save this page.
326*cdf0e10cSrcweir         eErrCode = rBIOS.saveObjectAt (*this, location());
327*cdf0e10cSrcweir     }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir     // Done.
330*cdf0e10cSrcweir     return eErrCode;
331*cdf0e10cSrcweir }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir /*========================================================================
334*cdf0e10cSrcweir  *
335*cdf0e10cSrcweir  * OStoreBTreeRootObject implementation.
336*cdf0e10cSrcweir  *
337*cdf0e10cSrcweir  *======================================================================*/
338*cdf0e10cSrcweir /*
339*cdf0e10cSrcweir  * testInvariant.
340*cdf0e10cSrcweir  * Precond: root node page loaded.
341*cdf0e10cSrcweir  */
342*cdf0e10cSrcweir bool OStoreBTreeRootObject::testInvariant (char const * message)
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir     OSL_PRECOND(m_xPage.get() != 0, "OStoreBTreeRootObject::testInvariant(): Null pointer");
345*cdf0e10cSrcweir     bool result = ((m_xPage->location() - m_xPage->size()) == 0);
346*cdf0e10cSrcweir     OSL_POSTCOND(result, message); (void) message;
347*cdf0e10cSrcweir     return result;
348*cdf0e10cSrcweir }
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir /*
351*cdf0e10cSrcweir  * loadOrCreate.
352*cdf0e10cSrcweir  */
353*cdf0e10cSrcweir storeError OStoreBTreeRootObject::loadOrCreate (
354*cdf0e10cSrcweir     sal_uInt32       nAddr,
355*cdf0e10cSrcweir     OStorePageBIOS & rBIOS)
356*cdf0e10cSrcweir {
357*cdf0e10cSrcweir     storeError eErrCode = rBIOS.loadObjectAt (*this, nAddr);
358*cdf0e10cSrcweir     if (eErrCode != store_E_NotExists)
359*cdf0e10cSrcweir         return eErrCode;
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir     eErrCode = construct<page>(rBIOS.allocator());
362*cdf0e10cSrcweir     if (eErrCode != store_E_None)
363*cdf0e10cSrcweir         return eErrCode;
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir     eErrCode = rBIOS.allocate (*this);
366*cdf0e10cSrcweir     if (eErrCode != store_E_None)
367*cdf0e10cSrcweir         return eErrCode;
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir     // Notify caller of the creation.
370*cdf0e10cSrcweir     (void) testInvariant("OStoreBTreeRootObject::loadOrCreate(): leave");
371*cdf0e10cSrcweir     return store_E_Pending;
372*cdf0e10cSrcweir }
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir /*
375*cdf0e10cSrcweir  * change.
376*cdf0e10cSrcweir  */
377*cdf0e10cSrcweir storeError OStoreBTreeRootObject::change (
378*cdf0e10cSrcweir     PageHolderObject< page > & rxPageL,
379*cdf0e10cSrcweir     OStorePageBIOS &           rBIOS)
380*cdf0e10cSrcweir {
381*cdf0e10cSrcweir     PageHolderObject< page > xPage (m_xPage);
382*cdf0e10cSrcweir     (void) testInvariant("OStoreBTreeRootObject::change(): enter");
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir     // Save root location.
385*cdf0e10cSrcweir     sal_uInt32 const nRootAddr = xPage->location();
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir     // Construct new root.
388*cdf0e10cSrcweir     if (!rxPageL.construct (rBIOS.allocator()))
389*cdf0e10cSrcweir         return store_E_OutOfMemory;
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir     // Save this as prev root.
392*cdf0e10cSrcweir     storeError eErrCode = rBIOS.allocate (*this);
393*cdf0e10cSrcweir     if (eErrCode != store_E_None)
394*cdf0e10cSrcweir         return store_E_OutOfMemory;
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir     // Setup new root.
397*cdf0e10cSrcweir     rxPageL->depth (xPage->depth() + 1);
398*cdf0e10cSrcweir     rxPageL->m_pData[0] = xPage->m_pData[0];
399*cdf0e10cSrcweir     rxPageL->m_pData[0].m_aLink = xPage->location();
400*cdf0e10cSrcweir     rxPageL->usageCount(1);
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir     // Change root.
403*cdf0e10cSrcweir     rxPageL.swap (xPage);
404*cdf0e10cSrcweir     {
405*cdf0e10cSrcweir         PageHolder tmp (xPage.get());
406*cdf0e10cSrcweir         tmp.swap (m_xPage);
407*cdf0e10cSrcweir     }
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir     // Save this as new root and finish.
410*cdf0e10cSrcweir     eErrCode = rBIOS.saveObjectAt (*this, nRootAddr);
411*cdf0e10cSrcweir     (void) testInvariant("OStoreBTreeRootObject::change(): leave");
412*cdf0e10cSrcweir     return eErrCode;
413*cdf0e10cSrcweir }
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir /*
416*cdf0e10cSrcweir  * find_lookup (w/o split()).
417*cdf0e10cSrcweir  * Precond: root node page loaded.
418*cdf0e10cSrcweir  */
419*cdf0e10cSrcweir storeError OStoreBTreeRootObject::find_lookup (
420*cdf0e10cSrcweir     OStoreBTreeNodeObject & rNode,  // [out]
421*cdf0e10cSrcweir     sal_uInt16 &            rIndex, // [out]
422*cdf0e10cSrcweir     OStorePageKey const &   rKey,
423*cdf0e10cSrcweir     OStorePageBIOS &        rBIOS)
424*cdf0e10cSrcweir {
425*cdf0e10cSrcweir     // Init node w/ root page.
426*cdf0e10cSrcweir     (void) testInvariant("OStoreBTreeRootObject::find_lookup(): enter");
427*cdf0e10cSrcweir     {
428*cdf0e10cSrcweir         PageHolder tmp (m_xPage);
429*cdf0e10cSrcweir         tmp.swap (rNode.get());
430*cdf0e10cSrcweir     }
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir     // Setup BTree entry.
433*cdf0e10cSrcweir     T const entry (rKey);
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir     // Check current page.
436*cdf0e10cSrcweir     PageHolderObject< page > xPage (rNode.get());
437*cdf0e10cSrcweir     for (; xPage->depth() > 0; xPage = rNode.makeHolder< page >())
438*cdf0e10cSrcweir     {
439*cdf0e10cSrcweir         // Find next page.
440*cdf0e10cSrcweir         page const & rPage = (*xPage);
441*cdf0e10cSrcweir         sal_uInt16 const i = rPage.find(entry);
442*cdf0e10cSrcweir         sal_uInt16 const n = rPage.usageCount();
443*cdf0e10cSrcweir         if (!(i < n))
444*cdf0e10cSrcweir         {
445*cdf0e10cSrcweir             // Path to entry not exists (Must not happen(?)).
446*cdf0e10cSrcweir             return store_E_NotExists;
447*cdf0e10cSrcweir         }
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir         // Check address.
450*cdf0e10cSrcweir         sal_uInt32 const nAddr = rPage.m_pData[i].m_aLink.location();
451*cdf0e10cSrcweir         if (nAddr == STORE_PAGE_NULL)
452*cdf0e10cSrcweir         {
453*cdf0e10cSrcweir             // Path to entry not exists (Must not happen(?)).
454*cdf0e10cSrcweir             return store_E_NotExists;
455*cdf0e10cSrcweir         }
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir         // Load next page.
458*cdf0e10cSrcweir         storeError eErrCode = rBIOS.loadObjectAt (rNode, nAddr);
459*cdf0e10cSrcweir         if (eErrCode != store_E_None)
460*cdf0e10cSrcweir             return eErrCode;
461*cdf0e10cSrcweir     }
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir     // Find index.
464*cdf0e10cSrcweir     page const & rPage = (*xPage);
465*cdf0e10cSrcweir     rIndex = rPage.find(entry);
466*cdf0e10cSrcweir     if (!(rIndex < rPage.usageCount()))
467*cdf0e10cSrcweir         return store_E_NotExists;
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir     // Compare entry.
470*cdf0e10cSrcweir     T::CompareResult eResult = entry.compare(rPage.m_pData[rIndex]);
471*cdf0e10cSrcweir     OSL_POSTCOND(eResult != T::COMPARE_LESS, "store::BTreeRoot::find_lookup(): sort error");
472*cdf0e10cSrcweir     if (eResult == T::COMPARE_LESS)
473*cdf0e10cSrcweir         return store_E_Unknown;
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir     // Greater or Equal.
476*cdf0e10cSrcweir     (void) testInvariant("OStoreBTreeRootObject::find_lookup(): leave");
477*cdf0e10cSrcweir     return store_E_None;
478*cdf0e10cSrcweir }
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir /*
481*cdf0e10cSrcweir  * find_insert (possibly with split()).
482*cdf0e10cSrcweir  * Precond: root node page loaded.
483*cdf0e10cSrcweir  */
484*cdf0e10cSrcweir storeError OStoreBTreeRootObject::find_insert (
485*cdf0e10cSrcweir     OStoreBTreeNodeObject & rNode,  // [out]
486*cdf0e10cSrcweir     sal_uInt16 &            rIndex, // [out]
487*cdf0e10cSrcweir     OStorePageKey const &   rKey,
488*cdf0e10cSrcweir     OStorePageBIOS &        rBIOS)
489*cdf0e10cSrcweir {
490*cdf0e10cSrcweir     (void) testInvariant("OStoreBTreeRootObject::find_insert(): enter");
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir     // Check for RootNode split.
493*cdf0e10cSrcweir     PageHolderObject< page > xRoot (m_xPage);
494*cdf0e10cSrcweir     if (xRoot->querySplit())
495*cdf0e10cSrcweir     {
496*cdf0e10cSrcweir         PageHolderObject< page > xPageL;
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir         // Change root.
499*cdf0e10cSrcweir         storeError eErrCode = change (xPageL, rBIOS);
500*cdf0e10cSrcweir         if (eErrCode != store_E_None)
501*cdf0e10cSrcweir             return eErrCode;
502*cdf0e10cSrcweir 
503*cdf0e10cSrcweir         // Split left page (prev root).
504*cdf0e10cSrcweir         eErrCode = split (0, xPageL, rBIOS);
505*cdf0e10cSrcweir         if (eErrCode != store_E_None)
506*cdf0e10cSrcweir             return eErrCode;
507*cdf0e10cSrcweir     }
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir     // Init node w/ root page.
510*cdf0e10cSrcweir     {
511*cdf0e10cSrcweir         PageHolder tmp (m_xPage);
512*cdf0e10cSrcweir         tmp.swap (rNode.get());
513*cdf0e10cSrcweir     }
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir     // Setup BTree entry.
516*cdf0e10cSrcweir     T const entry (rKey);
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir     // Check current Page.
519*cdf0e10cSrcweir     PageHolderObject< page > xPage (rNode.get());
520*cdf0e10cSrcweir     for (; xPage->depth() > 0; xPage = rNode.makeHolder< page >())
521*cdf0e10cSrcweir     {
522*cdf0e10cSrcweir         // Find next page.
523*cdf0e10cSrcweir         page const & rPage = (*xPage);
524*cdf0e10cSrcweir         sal_uInt16 const i = rPage.find (entry);
525*cdf0e10cSrcweir         sal_uInt16 const n = rPage.usageCount();
526*cdf0e10cSrcweir         if (!(i < n))
527*cdf0e10cSrcweir         {
528*cdf0e10cSrcweir             // Path to entry not exists (Must not happen(?)).
529*cdf0e10cSrcweir             return store_E_NotExists;
530*cdf0e10cSrcweir         }
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir         // Check address.
533*cdf0e10cSrcweir         sal_uInt32 const nAddr = rPage.m_pData[i].m_aLink.location();
534*cdf0e10cSrcweir         if (nAddr == STORE_PAGE_NULL)
535*cdf0e10cSrcweir         {
536*cdf0e10cSrcweir             // Path to entry not exists (Must not happen(?)).
537*cdf0e10cSrcweir             return store_E_NotExists;
538*cdf0e10cSrcweir         }
539*cdf0e10cSrcweir 
540*cdf0e10cSrcweir         // Load next page.
541*cdf0e10cSrcweir         OStoreBTreeNodeObject aNext;
542*cdf0e10cSrcweir         storeError eErrCode = rBIOS.loadObjectAt (aNext, nAddr);
543*cdf0e10cSrcweir         if (eErrCode != store_E_None)
544*cdf0e10cSrcweir             return eErrCode;
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir         // Check for next node split.
547*cdf0e10cSrcweir         PageHolderObject< page > xNext (aNext.get());
548*cdf0e10cSrcweir         if (xNext->querySplit())
549*cdf0e10cSrcweir         {
550*cdf0e10cSrcweir             // Split next node.
551*cdf0e10cSrcweir             eErrCode = rNode.split (i, xNext, rBIOS);
552*cdf0e10cSrcweir             if (eErrCode != store_E_None)
553*cdf0e10cSrcweir                 return eErrCode;
554*cdf0e10cSrcweir 
555*cdf0e10cSrcweir             // Restart.
556*cdf0e10cSrcweir             continue;
557*cdf0e10cSrcweir         }
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir         // Let next page be current.
560*cdf0e10cSrcweir         PageHolder tmp (aNext.get());
561*cdf0e10cSrcweir         tmp.swap (rNode.get());
562*cdf0e10cSrcweir     }
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir     // Find index.
565*cdf0e10cSrcweir     page const & rPage = (*xPage);
566*cdf0e10cSrcweir     rIndex = rPage.find(entry);
567*cdf0e10cSrcweir     if (rIndex < rPage.usageCount())
568*cdf0e10cSrcweir     {
569*cdf0e10cSrcweir         // Compare entry.
570*cdf0e10cSrcweir         T::CompareResult result = entry.compare (rPage.m_pData[rIndex]);
571*cdf0e10cSrcweir         OSL_POSTCOND(result != T::COMPARE_LESS, "store::BTreeRoot::find_insert(): sort error");
572*cdf0e10cSrcweir         if (result == T::COMPARE_LESS)
573*cdf0e10cSrcweir             return store_E_Unknown;
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir         if (result == T::COMPARE_EQUAL)
576*cdf0e10cSrcweir             return store_E_AlreadyExists;
577*cdf0e10cSrcweir     }
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir     // Greater or not (yet) existing.
580*cdf0e10cSrcweir     (void) testInvariant("OStoreBTreeRootObject::find_insert(): leave");
581*cdf0e10cSrcweir     return store_E_None;
582*cdf0e10cSrcweir }
583