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 .
20 #ifndef INCLUDED_STORE_SOURCE_STORTREE_HXX
21 #define INCLUDED_STORE_SOURCE_STORTREE_HXX
23 #include "sal/config.h"
25 #include "sal/types.h"
27 #include "store/types.h"
29 #include "storbase.hxx"
36 /*========================================================================
40 *======================================================================*/
41 struct OStoreBTreeEntry
43 typedef OStorePageKey K
;
44 typedef OStorePageLink L
;
54 explicit OStoreBTreeEntry (
56 L
const & rLink
= L(),
57 sal_uInt32 nAttrib
= 0)
60 m_nAttrib (store::htonl(nAttrib
))
63 OStoreBTreeEntry (const OStoreBTreeEntry
& rhs
)
64 : m_aKey (rhs
.m_aKey
),
65 m_aLink (rhs
.m_aLink
),
66 m_nAttrib (rhs
.m_nAttrib
)
69 OStoreBTreeEntry
& operator= (const OStoreBTreeEntry
& rhs
)
72 m_aLink
= rhs
.m_aLink
;
73 m_nAttrib
= rhs
.m_nAttrib
;
86 CompareResult
compare (const OStoreBTreeEntry
& rOther
) const
88 if (m_aKey
< rOther
.m_aKey
)
90 else if (m_aKey
== rOther
.m_aKey
)
93 return COMPARE_GREATER
;
97 /*========================================================================
99 * OStoreBTreeNodeData.
101 *======================================================================*/
102 #define STORE_MAGIC_BTREENODE sal_uInt32(0x58190322)
104 struct OStoreBTreeNodeData
: public store::OStorePageData
106 typedef OStorePageData base
;
107 typedef OStoreBTreeNodeData self
;
109 typedef OStorePageGuard G
;
110 typedef OStoreBTreeEntry T
;
119 static const sal_uInt32 theTypeId
= STORE_MAGIC_BTREENODE
;
123 static const size_t theSize
= sizeof(G
);
124 static const sal_uInt16 thePageSize
= base::theSize
+ self::theSize
;
125 static_assert(STORE_MINIMUM_PAGESIZE
>= self::thePageSize
, "got to be at least equal in size");
129 sal_uInt16
capacity() const
131 return (store::ntohs(base::m_aDescr
.m_nSize
) - self::thePageSize
);
134 /** capacityCount (must be even).
136 sal_uInt16
capacityCount() const
138 return sal_uInt16(capacity() / sizeof(T
));
143 sal_uInt16
usage() const
145 return (store::ntohs(base::m_aDescr
.m_nUsed
) - self::thePageSize
);
150 sal_uInt16
usageCount() const
152 return sal_uInt16(usage() / sizeof(T
));
154 void usageCount (sal_uInt16 nCount
)
156 size_t const nBytes
= self::thePageSize
+ nCount
* sizeof(T
);
157 base::m_aDescr
.m_nUsed
= store::htons(sal::static_int_cast
< sal_uInt16
>(nBytes
));
162 explicit OStoreBTreeNodeData (sal_uInt16 nPageSize
= self::thePageSize
);
164 /** guard (external representation).
168 sal_uInt32 nCRC32
= 0;
169 nCRC32
= rtl_crc32 (nCRC32
, &m_aGuard
.m_nMagic
, sizeof(sal_uInt32
));
170 nCRC32
= rtl_crc32 (nCRC32
, m_pData
, capacity());
171 m_aGuard
.m_nCRC32
= store::htonl(nCRC32
);
174 /** verify (external representation).
176 storeError
verify() const
178 sal_uInt32 nCRC32
= 0;
179 nCRC32
= rtl_crc32 (nCRC32
, &m_aGuard
.m_nMagic
, sizeof(sal_uInt32
));
180 nCRC32
= rtl_crc32 (nCRC32
, m_pData
, capacity());
181 if (m_aGuard
.m_nCRC32
!= store::htonl(nCRC32
))
182 return store_E_InvalidChecksum
;
189 sal_uInt32
depth() const
191 return store::ntohl(self::m_aGuard
.m_nMagic
);
193 void depth (sal_uInt32 nDepth
)
195 self::m_aGuard
.m_nMagic
= store::htonl(nDepth
);
200 bool queryMerge (const self
&rPageR
) const
202 return ((usageCount() + rPageR
.usageCount()) <= capacityCount());
207 bool querySplit() const
209 return (!(usageCount() < capacityCount()));
214 sal_uInt16
find (const T
& t
) const;
215 void insert (sal_uInt16 i
, const T
& t
);
216 void remove (sal_uInt16 i
);
218 /** split (left half copied from right half of left page).
220 void split (const self
& rPageL
);
222 /** truncate (to n elements).
224 void truncate (sal_uInt16 n
);
227 /*========================================================================
229 * OStoreBTreeNodeObject.
231 *======================================================================*/
232 class OStoreBTreeNodeObject
: public store::OStorePageObject
234 typedef OStorePageObject base
;
235 typedef OStoreBTreeNodeObject self
;
236 typedef OStoreBTreeNodeData page
;
238 typedef OStoreBTreeEntry T
;
243 explicit OStoreBTreeNodeObject (PageHolder
const & rxPage
= PageHolder())
244 : OStorePageObject (rxPage
)
247 /** External representation.
249 virtual storeError
guard (sal_uInt32 nAddr
) SAL_OVERRIDE
;
250 virtual storeError
verify (sal_uInt32 nAddr
) const SAL_OVERRIDE
;
254 * @param rxPageL [inout] left child to be split
258 PageHolderObject
< page
> & rxPageL
,
259 OStorePageBIOS
& rBIOS
);
261 /** remove (down to leaf node, recursive).
265 OStoreBTreeEntry
& rEntryL
,
266 OStorePageBIOS
& rBIOS
);
269 /*========================================================================
271 * OStoreBTreeRootObject.
273 *======================================================================*/
274 class OStoreBTreeRootObject
: public store::OStoreBTreeNodeObject
276 typedef OStoreBTreeNodeObject base
;
277 typedef OStoreBTreeNodeData page
;
279 typedef OStoreBTreeEntry T
;
284 explicit OStoreBTreeRootObject (PageHolder
const & rxPage
= PageHolder())
285 : OStoreBTreeNodeObject (rxPage
)
288 storeError
loadOrCreate (
290 OStorePageBIOS
& rBIOS
);
292 /** find_lookup (w/o split()).
293 * Precond: root node page loaded.
295 storeError
find_lookup (
296 OStoreBTreeNodeObject
& rNode
, // [out]
297 sal_uInt16
& rIndex
, // [out]
298 OStorePageKey
const & rKey
,
299 OStorePageBIOS
& rBIOS
);
301 /** find_insert (possibly with split()).
302 * Precond: root node page loaded.
304 storeError
find_insert (
305 OStoreBTreeNodeObject
& rNode
,
307 OStorePageKey
const & rKey
,
308 OStorePageBIOS
& rBIOS
);
312 * Precond: root node page loaded.
314 void testInvariant (char const * message
);
318 * @param rxPageL [out] prev. root (needs split)
321 PageHolderObject
< page
> & rxPageL
,
322 OStorePageBIOS
& rBIOS
);
325 /*========================================================================
329 *======================================================================*/
333 #endif // INCLUDED_STORE_SOURCE_STORTREE_HXX
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */