1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: stortree.hxx,v $
*
* $Revision: 1.5 $
*
* last change: $Author: hr $ $Date: 2006-06-20 00:34:40 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
#ifndef _STORE_STORTREE_HXX
#define _STORE_STORTREE_HXX "$Revision: 1.5 $"
#ifndef _SAL_TYPES_H_
#include <sal/types.h>
#endif
#ifndef _RTL_MEMORY_H_
#include <rtl/memory.h>
#endif
#ifndef _OSL_ENDIAN_H_
#include <osl/endian.h>
#endif
#ifndef _OSL_MUTEX_HXX_
#include <osl/mutex.hxx>
#endif
#ifndef _STORE_TYPES_H_
#include <store/types.h>
#endif
#ifndef _STORE_STORBASE_HXX
#include <storbase.hxx>
#endif
namespace store
{
/*========================================================================
*
* OStoreBTreeEntry.
*
*======================================================================*/
struct OStoreBTreeEntry
{
typedef OStorePageKey K;
typedef OStorePageLink L;
/** Representation.
*/
K m_aKey;
L m_aLink;
sal_uInt32 m_nAttrib;
/** Construction.
*/
OStoreBTreeEntry (void)
: m_nAttrib (0)
{}
OStoreBTreeEntry (const OStoreBTreeEntry& rOther)
: m_aKey (rOther.m_aKey),
m_aLink (rOther.m_aLink),
m_nAttrib (rOther.m_nAttrib)
{}
OStoreBTreeEntry& operator= (const OStoreBTreeEntry& rOther)
{
m_aKey = rOther.m_aKey;
m_aLink = rOther.m_aLink;
m_nAttrib = rOther.m_nAttrib;
return *this;
}
/** Comparison.
*/
enum CompareResult
{
COMPARE_LESS = -1,
COMPARE_EQUAL = 0,
COMPARE_GREATER = 1
};
CompareResult compare (const OStoreBTreeEntry& rOther) const
{
if (m_aKey < rOther.m_aKey)
return COMPARE_LESS;
else if (m_aKey == rOther.m_aKey)
return COMPARE_EQUAL;
else
return COMPARE_GREATER;
}
/** swap (internal and external representation).
*/
void swap (void)
{
#ifdef OSL_BIGENDIAN
m_aKey.swap();
m_aLink.swap();
m_nAttrib = OSL_SWAPDWORD(m_nAttrib);
#endif /* OSL_BIGENDIAN */
}
};
/*========================================================================
*
* OStoreBTreeNodeData.
*
*======================================================================*/
#define STORE_MAGIC_BTREENODE sal_uInt32(0x58190322)
struct OStoreBTreeNodeData : public store::OStorePageData
{
typedef OStorePageData base;
typedef OStoreBTreeNodeData self;
typedef OStorePageGuard G;
typedef OStoreBTreeEntry T;
/** Representation.
*/
G m_aGuard;
T m_pData[1];
/** size.
*/
static sal_uInt16 size (void)
{
return sal_uInt16(sizeof(G));
}
/** capacity.
*/
static sal_uInt16 capacity (const D& rDescr)
{
return (rDescr.m_nSize - (base::size() + self::size()));
}
sal_uInt16 capacity (void) const
{
return self::capacity (base::m_aDescr);
}
/** capacityCount (must be even).
*/
sal_uInt16 capacityCount (void) const
{
return sal_uInt16(capacity() / sizeof(T));
}
/** usage.
*/
static sal_uInt16 usage (const D& rDescr)
{
return (rDescr.m_nUsed - (base::size() + self::size()));
}
sal_uInt16 usage (void) const
{
return self::usage (base::m_aDescr);
}
/** usageCount.
*/
sal_uInt16 usageCount (void) const
{
return sal_uInt16(usage() / sizeof(T));
}
void usageCount (sal_uInt16 nCount)
{
base::m_aDescr.m_nUsed = base::size() + self::size() +
sal_uInt16(nCount * sizeof(T));
}
/** Construction.
*/
OStoreBTreeNodeData (sal_uInt16 nPageSize);
void initialize (void);
self& operator= (const self& rOther)
{
if (this != &rOther)
{
base::operator= (rOther);
m_aGuard = rOther.m_aGuard;
rtl_copyMemory (m_pData, rOther.m_pData, capacity());
}
return *this;
}
/** Comparison.
*/
sal_Bool operator== (const self& rOther) const
{
return (base::operator==(rOther) && (m_aGuard == rOther.m_aGuard));
}
/** swap (external and internal representation).
*/
void swap (const D& rDescr);
/** guard (external representation).
*/
void guard (const D& rDescr)
{
sal_uInt32 nCRC32 = 0;
nCRC32 = G::crc32 (nCRC32, &m_aGuard.m_nMagic, sizeof(sal_uInt32));
nCRC32 = G::crc32 (nCRC32, m_pData, capacity(rDescr));
#ifdef OSL_BIGENDIAN
nCRC32 = OSL_SWAPDWORD(nCRC32);
#endif /* OSL_BIGENDIAN */
m_aGuard.m_nCRC32 = nCRC32;
}
/** verify (external representation).
*/
storeError verify (const D& rDescr)
{
sal_uInt32 nCRC32 = 0;
nCRC32 = G::crc32 (nCRC32, &m_aGuard.m_nMagic, sizeof(sal_uInt32));
nCRC32 = G::crc32 (nCRC32, m_pData, capacity(rDescr));
#ifdef OSL_BIGENDIAN
nCRC32 = OSL_SWAPDWORD(nCRC32);
#endif /* OSL_BIGENDIAN */
if (m_aGuard.m_nCRC32 != nCRC32)
return store_E_InvalidChecksum;
else
return store_E_None;
}
/** depth.
*/
sal_uInt32 depth (void) const
{
return self::m_aGuard.m_nMagic;
}
void depth (sal_uInt32 nDepth)
{
self::m_aGuard.m_nMagic = nDepth;
}
/** queryMerge.
*/
sal_Bool queryMerge (const self &rPageR) const
{
return ((usageCount() + rPageR.usageCount()) <= capacityCount());
}
/** querySplit.
*/
sal_Bool querySplit (void) const
{
return (!(usageCount() < capacityCount()));
}
/** Operation.
*/
sal_uInt16 find (const T& t) const;
void insert (sal_uInt16 i, const T& t);
void remove (sal_uInt16 i);
/** merge (with right page).
*/
void merge (const self& rPageR);
/** split (left half copied from right half of left page).
*/
void split (const self& rPageL);
/** truncate (to n elements).
*/
void truncate (sal_uInt16 n);
};
/*========================================================================
*
* OStoreBTreeNodeObject.
*
*======================================================================*/
class OStoreBTreeNodeObject : public store::OStorePageObject
{
typedef OStorePageObject base;
typedef OStoreBTreeNodeObject self;
typedef OStoreBTreeNodeData page;
typedef OStorePageDescriptor D;
typedef OStoreBTreeEntry T;
public:
/** Construction.
*/
inline OStoreBTreeNodeObject (page& rPage);
/** External representation.
*/
virtual void swap (const D& rDescr);
virtual void guard (const D& rDescr);
virtual storeError verify (const D& rDescr);
/** Query split.
*/
inline sal_Bool querySplit (void) const;
/** split.
*/
virtual storeError split (
sal_uInt16 nIndexL,
OStoreBTreeNodeData &rPageL,
OStoreBTreeNodeData &rPageR,
OStorePageBIOS &rBIOS,
osl::Mutex *pMutex = NULL);
/** remove (down to leaf node, recursive).
*/
storeError remove (
sal_uInt16 nIndexL,
OStoreBTreeEntry &rEntryL,
OStoreBTreeNodeData &rPageL,
#if 0 /* NYI */
OStoreBTreeNodeData &rPageR,
#endif /* NYI */
OStorePageBIOS &rBIOS,
osl::Mutex *pMutex = NULL);
private:
/** Representation.
*/
page& m_rPage;
};
inline OStoreBTreeNodeObject::OStoreBTreeNodeObject (page& rPage)
: OStorePageObject (rPage), m_rPage (rPage)
{
}
inline sal_Bool OStoreBTreeNodeObject::querySplit (void) const
{
return m_rPage.querySplit();
}
/*========================================================================
*
* OStoreBTreeRootObject.
*
*======================================================================*/
class OStoreBTreeRootObject : public store::OStoreBTreeNodeObject
{
typedef OStoreBTreeNodeObject base;
typedef OStoreBTreeNodeData page;
public:
/** Construction.
*/
inline OStoreBTreeRootObject (page& rPage);
/** split.
*/
virtual storeError split (
sal_uInt16 nIndexL,
OStoreBTreeNodeData &rPageL,
OStoreBTreeNodeData &rPageR,
OStorePageBIOS &rBIOS,
osl::Mutex *pMutex = NULL);
private:
/** Representation.
*/
page& m_rPage;
/** change (Root).
*/
storeError change (
OStoreBTreeNodeData &rPageL,
OStorePageBIOS &rBIOS,
osl::Mutex *pMutex = NULL);
};
inline OStoreBTreeRootObject::OStoreBTreeRootObject (page& rPage)
: OStoreBTreeNodeObject (rPage), m_rPage (rPage)
{
}
/*========================================================================
*
* The End.
*
*======================================================================*/
} // namespace store
#endif /* !_STORE_STORTREE_HXX */
|