xref: /aoo42x/main/store/source/stortree.hxx (revision cdf0e10c)
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 #ifndef _STORE_STORTREE_HXX
29*cdf0e10cSrcweir #define _STORE_STORTREE_HXX "$Revision: 1.6.8.2 $"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "sal/types.h"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "store/types.h"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "storbase.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace store
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir class OStorePageBIOS;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /*========================================================================
43*cdf0e10cSrcweir  *
44*cdf0e10cSrcweir  * OStoreBTreeEntry.
45*cdf0e10cSrcweir  *
46*cdf0e10cSrcweir  *======================================================================*/
47*cdf0e10cSrcweir struct OStoreBTreeEntry
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 	typedef OStorePageKey  K;
50*cdf0e10cSrcweir 	typedef OStorePageLink L;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 	/** Representation.
53*cdf0e10cSrcweir 	*/
54*cdf0e10cSrcweir 	K          m_aKey;
55*cdf0e10cSrcweir 	L          m_aLink;
56*cdf0e10cSrcweir 	sal_uInt32 m_nAttrib;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	/** Construction.
59*cdf0e10cSrcweir 	*/
60*cdf0e10cSrcweir 	explicit OStoreBTreeEntry (
61*cdf0e10cSrcweir         K const &  rKey    = K(),
62*cdf0e10cSrcweir         L const &  rLink   = L(),
63*cdf0e10cSrcweir         sal_uInt32 nAttrib = 0)
64*cdf0e10cSrcweir         : m_aKey    (rKey),
65*cdf0e10cSrcweir           m_aLink   (rLink),
66*cdf0e10cSrcweir           m_nAttrib (store::htonl(nAttrib))
67*cdf0e10cSrcweir 	{}
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	OStoreBTreeEntry (const OStoreBTreeEntry & rhs)
70*cdf0e10cSrcweir 		: m_aKey    (rhs.m_aKey),
71*cdf0e10cSrcweir 		  m_aLink   (rhs.m_aLink),
72*cdf0e10cSrcweir 		  m_nAttrib (rhs.m_nAttrib)
73*cdf0e10cSrcweir 	{}
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	OStoreBTreeEntry& operator= (const OStoreBTreeEntry & rhs)
76*cdf0e10cSrcweir 	{
77*cdf0e10cSrcweir 		m_aKey    = rhs.m_aKey;
78*cdf0e10cSrcweir 		m_aLink   = rhs.m_aLink;
79*cdf0e10cSrcweir 		m_nAttrib = rhs.m_nAttrib;
80*cdf0e10cSrcweir 		return *this;
81*cdf0e10cSrcweir 	}
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	/** Comparison.
84*cdf0e10cSrcweir 	*/
85*cdf0e10cSrcweir 	enum CompareResult
86*cdf0e10cSrcweir 	{
87*cdf0e10cSrcweir 		COMPARE_LESS    = -1,
88*cdf0e10cSrcweir 		COMPARE_EQUAL   =  0,
89*cdf0e10cSrcweir 		COMPARE_GREATER =  1
90*cdf0e10cSrcweir 	};
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	CompareResult compare (const OStoreBTreeEntry& rOther) const
93*cdf0e10cSrcweir 	{
94*cdf0e10cSrcweir 		if (m_aKey < rOther.m_aKey)
95*cdf0e10cSrcweir 			return COMPARE_LESS;
96*cdf0e10cSrcweir 		else if (m_aKey == rOther.m_aKey)
97*cdf0e10cSrcweir 			return COMPARE_EQUAL;
98*cdf0e10cSrcweir 		else
99*cdf0e10cSrcweir 			return COMPARE_GREATER;
100*cdf0e10cSrcweir 	}
101*cdf0e10cSrcweir };
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir /*========================================================================
104*cdf0e10cSrcweir  *
105*cdf0e10cSrcweir  * OStoreBTreeNodeData.
106*cdf0e10cSrcweir  *
107*cdf0e10cSrcweir  *======================================================================*/
108*cdf0e10cSrcweir #define STORE_MAGIC_BTREENODE sal_uInt32(0x58190322)
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir struct OStoreBTreeNodeData : public store::OStorePageData
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	typedef OStorePageData      base;
113*cdf0e10cSrcweir 	typedef OStoreBTreeNodeData self;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	typedef OStorePageGuard     G;
116*cdf0e10cSrcweir 	typedef OStoreBTreeEntry    T;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	/** Representation.
119*cdf0e10cSrcweir      */
120*cdf0e10cSrcweir 	G m_aGuard;
121*cdf0e10cSrcweir 	T m_pData[1];
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     /** type.
124*cdf0e10cSrcweir      */
125*cdf0e10cSrcweir     static const sal_uInt32 theTypeId = STORE_MAGIC_BTREENODE;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	/** theSize.
128*cdf0e10cSrcweir      */
129*cdf0e10cSrcweir     static const size_t     theSize     = sizeof(G);
130*cdf0e10cSrcweir     static const sal_uInt16 thePageSize = base::theSize + self::theSize;
131*cdf0e10cSrcweir     STORE_STATIC_ASSERT(STORE_MINIMUM_PAGESIZE >= self::thePageSize);
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	/** capacity.
134*cdf0e10cSrcweir 	*/
135*cdf0e10cSrcweir 	sal_uInt16 capacity (void) const
136*cdf0e10cSrcweir 	{
137*cdf0e10cSrcweir 		return (store::ntohs(base::m_aDescr.m_nSize) - self::thePageSize);
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	/** capacityCount (must be even).
141*cdf0e10cSrcweir 	*/
142*cdf0e10cSrcweir 	sal_uInt16 capacityCount (void) const
143*cdf0e10cSrcweir 	{
144*cdf0e10cSrcweir 		return sal_uInt16(capacity() / sizeof(T));
145*cdf0e10cSrcweir 	}
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	/** usage.
148*cdf0e10cSrcweir 	*/
149*cdf0e10cSrcweir 	sal_uInt16 usage (void) const
150*cdf0e10cSrcweir 	{
151*cdf0e10cSrcweir 		return (store::ntohs(base::m_aDescr.m_nUsed) - self::thePageSize);
152*cdf0e10cSrcweir 	}
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	/** usageCount.
155*cdf0e10cSrcweir 	*/
156*cdf0e10cSrcweir 	sal_uInt16 usageCount (void) const
157*cdf0e10cSrcweir 	{
158*cdf0e10cSrcweir 		return sal_uInt16(usage() / sizeof(T));
159*cdf0e10cSrcweir 	}
160*cdf0e10cSrcweir 	void usageCount (sal_uInt16 nCount)
161*cdf0e10cSrcweir 	{
162*cdf0e10cSrcweir         size_t const nBytes = self::thePageSize + nCount * sizeof(T);
163*cdf0e10cSrcweir 		base::m_aDescr.m_nUsed = store::htons(sal::static_int_cast< sal_uInt16 >(nBytes));
164*cdf0e10cSrcweir 	}
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	/** Construction.
167*cdf0e10cSrcweir 	*/
168*cdf0e10cSrcweir 	explicit OStoreBTreeNodeData (sal_uInt16 nPageSize = self::thePageSize);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	/** guard (external representation).
171*cdf0e10cSrcweir 	*/
172*cdf0e10cSrcweir 	void guard()
173*cdf0e10cSrcweir 	{
174*cdf0e10cSrcweir 		sal_uInt32 nCRC32 = 0;
175*cdf0e10cSrcweir 		nCRC32 = rtl_crc32 (nCRC32, &m_aGuard.m_nMagic, sizeof(sal_uInt32));
176*cdf0e10cSrcweir 		nCRC32 = rtl_crc32 (nCRC32, m_pData, capacity());
177*cdf0e10cSrcweir 		m_aGuard.m_nCRC32 = store::htonl(nCRC32);
178*cdf0e10cSrcweir 	}
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	/** verify (external representation).
181*cdf0e10cSrcweir 	*/
182*cdf0e10cSrcweir 	storeError verify() const
183*cdf0e10cSrcweir 	{
184*cdf0e10cSrcweir 		sal_uInt32 nCRC32 = 0;
185*cdf0e10cSrcweir 		nCRC32 = rtl_crc32 (nCRC32, &m_aGuard.m_nMagic, sizeof(sal_uInt32));
186*cdf0e10cSrcweir 		nCRC32 = rtl_crc32 (nCRC32, m_pData, capacity());
187*cdf0e10cSrcweir 		if (m_aGuard.m_nCRC32 != store::htonl(nCRC32))
188*cdf0e10cSrcweir 			return store_E_InvalidChecksum;
189*cdf0e10cSrcweir 		else
190*cdf0e10cSrcweir 			return store_E_None;
191*cdf0e10cSrcweir 	}
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	/** depth.
194*cdf0e10cSrcweir 	*/
195*cdf0e10cSrcweir 	sal_uInt32 depth (void) const
196*cdf0e10cSrcweir 	{
197*cdf0e10cSrcweir 		return store::ntohl(self::m_aGuard.m_nMagic);
198*cdf0e10cSrcweir 	}
199*cdf0e10cSrcweir 	void depth (sal_uInt32 nDepth)
200*cdf0e10cSrcweir 	{
201*cdf0e10cSrcweir 		self::m_aGuard.m_nMagic = store::htonl(nDepth);
202*cdf0e10cSrcweir 	}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 	/** queryMerge.
205*cdf0e10cSrcweir 	*/
206*cdf0e10cSrcweir 	sal_Bool queryMerge (const self &rPageR) const
207*cdf0e10cSrcweir 	{
208*cdf0e10cSrcweir 		return ((usageCount() + rPageR.usageCount()) <= capacityCount());
209*cdf0e10cSrcweir 	}
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	/** querySplit.
212*cdf0e10cSrcweir 	*/
213*cdf0e10cSrcweir 	sal_Bool querySplit (void) const
214*cdf0e10cSrcweir 	{
215*cdf0e10cSrcweir 		return (!(usageCount() < capacityCount()));
216*cdf0e10cSrcweir 	}
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	/** Operation.
219*cdf0e10cSrcweir 	*/
220*cdf0e10cSrcweir 	sal_uInt16 find   (const T& t) const;
221*cdf0e10cSrcweir 	void       insert (sal_uInt16 i, const T& t);
222*cdf0e10cSrcweir 	void       remove (sal_uInt16 i);
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir #if 0  /* NYI */
225*cdf0e10cSrcweir 	/** merge (with right page).
226*cdf0e10cSrcweir 	 */
227*cdf0e10cSrcweir 	void merge (const self& rPageR);
228*cdf0e10cSrcweir #endif
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 	/** split (left half copied from right half of left page).
231*cdf0e10cSrcweir 	*/
232*cdf0e10cSrcweir 	void split (const self& rPageL);
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	/** truncate (to n elements).
235*cdf0e10cSrcweir 	*/
236*cdf0e10cSrcweir 	void truncate (sal_uInt16 n);
237*cdf0e10cSrcweir };
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir /*========================================================================
240*cdf0e10cSrcweir  *
241*cdf0e10cSrcweir  * OStoreBTreeNodeObject.
242*cdf0e10cSrcweir  *
243*cdf0e10cSrcweir  *======================================================================*/
244*cdf0e10cSrcweir class OStoreBTreeNodeObject : public store::OStorePageObject
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir 	typedef OStorePageObject      base;
247*cdf0e10cSrcweir 	typedef OStoreBTreeNodeObject self;
248*cdf0e10cSrcweir 	typedef OStoreBTreeNodeData   page;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 	typedef OStoreBTreeEntry      T;
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir public:
253*cdf0e10cSrcweir 	/** Construction.
254*cdf0e10cSrcweir 	*/
255*cdf0e10cSrcweir     explicit OStoreBTreeNodeObject (PageHolder const & rxPage = PageHolder())
256*cdf0e10cSrcweir         : OStorePageObject (rxPage)
257*cdf0e10cSrcweir     {}
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 	/** External representation.
260*cdf0e10cSrcweir 	*/
261*cdf0e10cSrcweir 	virtual storeError guard  (sal_uInt32 nAddr);
262*cdf0e10cSrcweir 	virtual storeError verify (sal_uInt32 nAddr) const;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 	/** split.
265*cdf0e10cSrcweir      *
266*cdf0e10cSrcweir      *  @param rxPageL [inout] left child to be split
267*cdf0e10cSrcweir      */
268*cdf0e10cSrcweir 	storeError split (
269*cdf0e10cSrcweir 		sal_uInt16                 nIndexL,
270*cdf0e10cSrcweir 		PageHolderObject< page > & rxPageL,
271*cdf0e10cSrcweir 		OStorePageBIOS &           rBIOS);
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 	/** remove (down to leaf node, recursive).
274*cdf0e10cSrcweir 	*/
275*cdf0e10cSrcweir 	storeError remove (
276*cdf0e10cSrcweir 		sal_uInt16         nIndexL,
277*cdf0e10cSrcweir 		OStoreBTreeEntry & rEntryL,
278*cdf0e10cSrcweir 		OStorePageBIOS &   rBIOS);
279*cdf0e10cSrcweir };
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir /*========================================================================
282*cdf0e10cSrcweir  *
283*cdf0e10cSrcweir  * OStoreBTreeRootObject.
284*cdf0e10cSrcweir  *
285*cdf0e10cSrcweir  *======================================================================*/
286*cdf0e10cSrcweir class OStoreBTreeRootObject : public store::OStoreBTreeNodeObject
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir 	typedef OStoreBTreeNodeObject base;
289*cdf0e10cSrcweir 	typedef OStoreBTreeNodeData   page;
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 	typedef OStoreBTreeEntry      T;
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir public:
294*cdf0e10cSrcweir 	/** Construction.
295*cdf0e10cSrcweir      */
296*cdf0e10cSrcweir     explicit OStoreBTreeRootObject (PageHolder const & rxPage = PageHolder())
297*cdf0e10cSrcweir         : OStoreBTreeNodeObject (rxPage)
298*cdf0e10cSrcweir     {}
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     storeError loadOrCreate (
301*cdf0e10cSrcweir         sal_uInt32       nAddr,
302*cdf0e10cSrcweir         OStorePageBIOS & rBIOS);
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir     /** find_lookup (w/o split()).
305*cdf0e10cSrcweir      *  Precond: root node page loaded.
306*cdf0e10cSrcweir      */
307*cdf0e10cSrcweir     storeError find_lookup (
308*cdf0e10cSrcweir         OStoreBTreeNodeObject & rNode,  // [out]
309*cdf0e10cSrcweir         sal_uInt16 &            rIndex, // [out]
310*cdf0e10cSrcweir         OStorePageKey const &   rKey,
311*cdf0e10cSrcweir         OStorePageBIOS &        rBIOS);
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir     /** find_insert (possibly with split()).
314*cdf0e10cSrcweir      *  Precond: root node page loaded.
315*cdf0e10cSrcweir      */
316*cdf0e10cSrcweir     storeError find_insert (
317*cdf0e10cSrcweir         OStoreBTreeNodeObject & rNode,
318*cdf0e10cSrcweir         sal_uInt16 &            rIndex,
319*cdf0e10cSrcweir         OStorePageKey const &   rKey,
320*cdf0e10cSrcweir         OStorePageBIOS &        rBIOS);
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir private:
323*cdf0e10cSrcweir     /** testInvariant.
324*cdf0e10cSrcweir      *  Precond: root node page loaded.
325*cdf0e10cSrcweir      */
326*cdf0e10cSrcweir     bool testInvariant (char const * message);
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir 	/** change (Root).
329*cdf0e10cSrcweir      *
330*cdf0e10cSrcweir      *  @param rxPageL [out] prev. root (needs split)
331*cdf0e10cSrcweir      */
332*cdf0e10cSrcweir 	storeError change (
333*cdf0e10cSrcweir 		PageHolderObject< page > & rxPageL,
334*cdf0e10cSrcweir 		OStorePageBIOS &           rBIOS);
335*cdf0e10cSrcweir };
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir /*========================================================================
338*cdf0e10cSrcweir  *
339*cdf0e10cSrcweir  * The End.
340*cdf0e10cSrcweir  *
341*cdf0e10cSrcweir  *======================================================================*/
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir } // namespace store
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir #endif /* !_STORE_STORTREE_HXX */
346