LibreOffice Module connectivity (master) 1
dindexnode.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <dbase/dindexnode.hxx>
21#include <dbase/DIndex.hxx>
22#include <o3tl/safeint.hxx>
23#include <tools/debug.hxx>
24#include <tools/stream.hxx>
25#include <sal/log.hxx>
26
27#include <algorithm>
28#include <memory>
29#include <utility>
30
31
32using namespace connectivity;
33using namespace connectivity::dbase;
34using namespace connectivity::file;
35using namespace com::sun::star::sdbc;
36
37ONDXKey::ONDXKey()
38 :nRecord(0)
39{
40}
41
42ONDXKey::ONDXKey(ORowSetValue aVal, sal_Int32 eType, sal_uInt32 nRec)
44 , nRecord(nRec)
45 , xValue(std::move(aVal))
46{
47}
48
49ONDXKey::ONDXKey(const OUString& aStr, sal_uInt32 nRec)
50 : ONDXKey_BASE(css::sdbc::DataType::VARCHAR)
51 ,nRecord(nRec)
52{
53 if (!aStr.isEmpty())
54 {
55 xValue = aStr;
56 xValue.setBound(true);
57 }
58}
59
60ONDXKey::ONDXKey(double aVal, sal_uInt32 nRec)
61 : ONDXKey_BASE(css::sdbc::DataType::DOUBLE)
62 ,nRecord(nRec)
63 ,xValue(aVal)
64{
65}
66
67// index page
68ONDXPage::ONDXPage(ODbaseIndex& rInd, sal_uInt32 nPos, ONDXPage* pParent)
69 : nRefCount(0)
70 , bNoDelete(1)
71 , nPagePos(nPos)
72 , bModified(false)
73 , nCount(0)
74 , aParent(pParent)
75 , rIndex(rInd)
76{
77 sal_uInt16 nT = rIndex.getHeader().db_maxkeys;
78 ppNodes.reset( new ONDXNode[nT] );
79}
80
82{
83}
84
86{
87 assert( nRefCount >= 1);
88 if(--nRefCount == 0 && !bNoDelete)
89 {
91 }
92}
93
95{
96 // Store in GarbageCollector
99
100 bModified = false;
101 if (rIndex.UseCollector())
102 {
103 if (aChild.Is())
104 aChild->Release(false);
105
106 for (sal_uInt16 i = 0; i < rIndex.getHeader().db_maxkeys;i++)
107 {
108 if (ppNodes[i].GetChild().Is())
109 ppNodes[i].GetChild()->Release(false);
110
111 ppNodes[i] = ONDXNode();
112 }
113 bNoDelete = 1;
114
115 nCount = 0;
116 aParent.Clear();
117 rIndex.Collect(this);
118 }
119 else
120 {
121 // I'm not sure about the original purpose of this line, but right now
122 // it serves the purpose that anything that attempts to do an AddFirstRef()
123 // after an object is deleted will trip an assert.
124 nRefCount = 1 << 30;
125 delete this;
126 }
127}
128
130{
131 if (!aChild.Is() && pIndex)
132 {
134 }
135 return aChild;
136}
137
138
139sal_uInt16 ONDXPage::FindPos(const ONDXKey& rKey) const
140{
141 // searches the position for the given key in a page
142 sal_uInt16 i = 0;
143 while (i < nCount && rKey > ((*this)[i]).GetKey())
144 i++;
145
146 return i;
147}
148
149
150bool ONDXPage::Find(const ONDXKey& rKey)
151{
152 // searches the given key
153 // Speciality: At the end of the method
154 // the actual page and the position of the node, fulfilling the '<=' condition, are saved
155 // This is considered at insert.
156 sal_uInt16 i = 0;
157 while (i < nCount && rKey > ((*this)[i]).GetKey())
158 i++;
159
160 bool bResult = false;
161
162 if (!IsLeaf())
163 {
164 // descend further
165 ONDXPagePtr aPage = (i==0) ? GetChild(&rIndex) : ((*this)[i-1]).GetChild(&rIndex, this);
166 bResult = aPage.Is() && aPage->Find(rKey);
167 }
168 else if (i == nCount)
169 {
170 rIndex.m_aCurLeaf = this;
171 rIndex.m_nCurNode = i - 1;
172 bResult = false;
173 }
174 else
175 {
176 bResult = rKey == ((*this)[i]).GetKey();
177 rIndex.m_aCurLeaf = this;
178 rIndex.m_nCurNode = bResult ? i : i - 1;
179 }
180 return bResult;
181}
182
183
184bool ONDXPage::Insert(ONDXNode& rNode, sal_uInt32 nRowsLeft)
185{
186 // When creating an index there can be multiple nodes added,
187 // these are sorted ascending
188 bool bAppend = nRowsLeft > 0;
189 if (IsFull())
190 {
191 ONDXNode aSplitNode;
192 if (bAppend)
193 aSplitNode = rNode;
194 else
195 {
196 // Save the last node
197 aSplitNode = (*this)[nCount-1];
198 if(rNode.GetKey() <= aSplitNode.GetKey())
199 {
200 bool bResult = true;
201 // this practically reduces the number of nodes by 1
202 if (IsLeaf() && this == rIndex.m_aCurLeaf)
203 {
204 // assumes, that the node, for which the condition (<=) holds, is stored in m_nCurNode
205 --nCount; // (otherwise we might get Assertions and GPFs - 60593)
206 bResult = Insert(rIndex.m_nCurNode + 1, rNode);
207 }
208 else // position unknown
209 {
210 sal_uInt16 nPos = NODE_NOTFOUND;
211 while (++nPos < nCount && rNode.GetKey() > ((*this)[nPos]).GetKey()) ;
212
213 --nCount; // (otherwise we might get Assertions and GPFs - 60593)
214 bResult = Insert(nPos, rNode);
215 }
216
217 // can the new node be inserted
218 if (!bResult)
219 {
220 nCount++;
221 aSplitNode = rNode;
222 }
223 }
224 else
225 aSplitNode = rNode;
226 }
227
228 sal_uInt32 nNewPagePos = rIndex.GetPageCount();
229 sal_uInt32 nNewPageCount = nNewPagePos + 1;
230
231 // insert extracted node into parent node
232 if (!HasParent())
233 {
234 // No parent, then new root
235 ONDXPagePtr aNewRoot = rIndex.CreatePage(nNewPagePos + 1);
236 aNewRoot->SetChild(this);
237
238 rIndex.m_aRoot = aNewRoot;
239 rIndex.SetRootPos(nNewPagePos + 1);
240 rIndex.SetPageCount(++nNewPageCount);
241 }
242
243 // create new leaf and divide page
244 ONDXPagePtr aNewPage = rIndex.CreatePage(nNewPagePos,aParent);
245 rIndex.SetPageCount(nNewPageCount);
246
247 // How many nodes are being inserted?
248 // Enough, then we can fill the page to the brim
249 ONDXNode aInnerNode;
250 if (!IsLeaf() || nRowsLeft < o3tl::make_unsigned(rIndex.GetMaxNodes() / 2))
251 aInnerNode = Split(*aNewPage);
252 else
253 {
254 aInnerNode = (*this)[nCount - 1];
255
256 // Node points to the new page
257 aInnerNode.SetChild(aNewPage);
258
259 // Inner nodes have no record number
260 if (rIndex.isUnique())
261 aInnerNode.GetKey().ResetRecord();
262
263 // new page points to the page of the extracted node
264 if (!IsLeaf())
265 aNewPage->SetChild(aInnerNode.GetChild());
266 }
267
268 aNewPage->Append(aSplitNode);
269 ONDXPagePtr aTempParent = aParent;
270 if (IsLeaf())
271 {
272 rIndex.m_aCurLeaf = aNewPage;
274
275 // free not needed pages, there are no references to those on the page
276 // afterwards 'this' can't be valid anymore!!!
277 ReleaseFull();
278 }
279
280 // Insert extracted node
281 return aTempParent->Insert(aInnerNode);
282 }
283 else // Fill the page up
284 {
285 if (bAppend)
286 {
287 if (IsLeaf())
289 return Append(rNode);
290 }
291 else
292 {
293 sal_uInt16 nNodePos = FindPos(rNode.GetKey());
294 if (IsLeaf())
295 rIndex.m_nCurNode = nNodePos;
296
297 return Insert(nNodePos, rNode);
298 }
299 }
300}
301
302
303bool ONDXPage::Insert(sal_uInt16 nPos, ONDXNode& rNode)
304{
305 sal_uInt16 nMaxCount = rIndex.getHeader().db_maxkeys;
306 if (nPos >= nMaxCount)
307 return false;
308
309 if (nCount)
310 {
311 ++nCount;
312 // shift right
313 for (sal_uInt16 i = std::min(static_cast<sal_uInt16>(nMaxCount-1), static_cast<sal_uInt16>(nCount-1)); nPos < i; --i)
314 (*this)[i] = (*this)[i-1];
315 }
316 else
317 if (nCount < nMaxCount)
318 nCount++;
319
320 // insert at the position
321 ONDXNode& rInsertNode = (*this)[nPos];
322 rInsertNode = rNode;
323 if (rInsertNode.GetChild().Is())
324 {
325 rInsertNode.GetChild()->SetParent(this);
326 rNode.GetChild()->SetParent(this);
327 }
328
329 bModified = true;
330
331 return true;
332}
333
334
336{
337 DBG_ASSERT(!IsFull(), "no Append possible");
338 return Insert(nCount, rNode);
339}
340
341void ONDXPage::Release(bool bSave)
342{
343 // free pages
344 if (aChild.Is())
345 aChild->Release(bSave);
346
347 // free pointer
348 aChild.Clear();
349
350 for (sal_uInt16 i = 0; i < rIndex.getHeader().db_maxkeys;i++)
351 {
352 if (ppNodes[i].GetChild())
353 ppNodes[i].GetChild()->Release(bSave);
354
355 ppNodes[i].GetChild().Clear();
356 }
357 aParent.Clear();
358}
359
361{
362 ONDXPagePtr aTempParent = aParent;
363 Release();
364
365 if (aTempParent.Is())
366 {
367 // Free pages not needed, there will be no reference anymore to the pages
368 // afterwards 'this' can't be valid anymore!!!
369 sal_uInt16 nParentPos = aTempParent->Search(this);
370 if (nParentPos != NODE_NOTFOUND)
371 (*aTempParent)[nParentPos].GetChild().Clear();
372 else
373 aTempParent->GetChild().Clear();
374 }
375}
376
377void ONDXPage::Delete(sal_uInt16 nNodePos)
378{
379 if (IsLeaf())
380 {
381 // The last element will not be deleted
382 if (nNodePos == (nCount - 1))
383 {
384 ONDXNode aNode = (*this)[nNodePos];
385
386 // parent's KeyValue has to be replaced
387 if (HasParent())
389 (*this)[nNodePos-1].GetKey());
390 }
391 }
392
393 // Delete the node
394 Remove(nNodePos);
395
396 // Underflow
397 if (HasParent() && nCount < (rIndex.GetMaxNodes() / 2))
398 {
399 // determine, which node points to the page
400 sal_uInt16 nParentNodePos = aParent->Search(this);
401 // last element on parent-page -> merge with secondlast page
402 if (nParentNodePos == (aParent->Count() - 1))
403 {
404 if (!nParentNodePos)
405 // merge with left neighbour
406 Merge(nParentNodePos,aParent->GetChild(&rIndex));
407 else
408 Merge(nParentNodePos,(*aParent)[nParentNodePos-1].GetChild(&rIndex,aParent));
409 }
410 // otherwise merge page with next page
411 else
412 {
413 // merge with right neighbour
414 Merge(nParentNodePos + 1,((*aParent)[nParentNodePos + 1].GetChild(&rIndex,aParent)));
415 nParentNodePos++;
416 }
417 if (HasParent() && !(*aParent)[nParentNodePos].HasChild())
418 aParent->Delete(nParentNodePos);
419 }
420 else if (IsRoot())
421 // make sure that the position of the root is kept
423}
424
425
427{
428 DBG_ASSERT(IsFull(), "Incorrect Splitting");
429 /* divide one page into two
430 leaf:
431 Page 1 is (n - (n/2))
432 Page 2 is (n/2)
433 Node n/2 will be duplicated
434 inner node:
435 Page 1 is (n+1)/2
436 Page 2 is (n/2-1)
437 Node ((n+1)/2 + 1) : will be taken out
438 */
439 ONDXNode aResultNode;
440 if (IsLeaf())
441 {
442 for (sal_uInt16 i = nCount - (nCount / 2), j = 0 ; i < nCount; i++)
443 rPage.Insert(j++,(*this)[i]);
444
445 // this node contains a key that already exists in the tree and must be replaced
446 ONDXNode aLastNode = (*this)[nCount - 1];
447 nCount = nCount - (nCount / 2);
448 aResultNode = (*this)[nCount - 1];
449
450 if (HasParent())
451 aParent->SearchAndReplace(aLastNode.GetKey(),
452 aResultNode.GetKey());
453 }
454 else
455 {
456 for (sal_uInt16 i = (nCount + 1) / 2 + 1, j = 0 ; i < nCount; i++)
457 rPage.Insert(j++,(*this)[i]);
458
459 aResultNode = (*this)[(nCount + 1) / 2];
460 nCount = (nCount + 1) / 2;
461
462 // new page points to page with extracted node
463 rPage.SetChild(aResultNode.GetChild());
464 }
465 // node points to new page
466 aResultNode.SetChild(&rPage);
467
468 // inner nodes have no record number
469 if (rIndex.isUnique())
470 aResultNode.GetKey().ResetRecord();
471 bModified = true;
472 return aResultNode;
473}
474
475
476void ONDXPage::Merge(sal_uInt16 nParentNodePos, const ONDXPagePtr& xPage)
477{
478 DBG_ASSERT(HasParent(), "no parent existing");
479 DBG_ASSERT(nParentNodePos != NODE_NOTFOUND, "Wrong index setup");
480
481 /* Merge 2 pages */
482 sal_uInt16 nMaxNodes = rIndex.GetMaxNodes(),
483 nMaxNodes_2 = nMaxNodes / 2;
484
485 // Determine if page is right or left neighbour
486 bool bRight = ((*xPage)[0].GetKey() > (*this)[0].GetKey()); // true when xPage is at the right side
487 sal_uInt16 nNewCount = (*xPage).Count() + Count();
488
489 if (IsLeaf())
490 {
491 // Condition for merge
492 if (nNewCount < (nMaxNodes_2 * 2))
493 {
494 sal_uInt16 nLastNode = bRight ? Count() - 1 : xPage->Count() - 1;
495 if (bRight)
496 {
497 DBG_ASSERT(xPage != this,"xPage and THIS must not be the same: infinite loop");
498 // shift all nodes from xPage to the left node (append)
499 while (xPage->Count())
500 {
501 Append((*xPage)[0]);
502 xPage->Remove(0);
503 }
504 }
505 else
506 {
507 DBG_ASSERT(xPage != this,"xPage and THIS must not be the same: infinite loop");
508 // xPage is the left page and THIS the right one
509 while (xPage->Count())
510 {
511 Insert(0,(*xPage)[xPage->Count()-1]);
512 xPage->Remove(xPage->Count()-1);
513 }
514 // replace old position of xPage in parent with this
515 if (nParentNodePos)
516 (*aParent)[nParentNodePos-1].SetChild(this,aParent);
517 else // or set as right node
518 aParent->SetChild(this);
519 aParent->SetModified(true);
520
521 }
522
523 // cancel Child-relationship at parent node
524 (*aParent)[nParentNodePos].SetChild();
525 // replace the Node-value, only if changed page is the left one, otherwise become
526 if(aParent->IsRoot() && aParent->Count() == 1)
527 {
528 (*aParent)[0].SetChild();
530 aParent.Clear();
532 rIndex.m_aRoot = this;
533 SetModified(true);
534 }
535 else
536 aParent->SearchAndReplace((*this)[nLastNode].GetKey(),(*this)[nCount-1].GetKey());
537
538 xPage->SetModified(false);
539 xPage->ReleaseFull(); // is not needed anymore
540 }
541 // balance the elements nNewCount >= (nMaxNodes_2 * 2)
542 else
543 {
544 if (bRight)
545 {
546 // shift all nodes from xPage to the left node (append)
547 ONDXNode aReplaceNode = (*this)[nCount - 1];
548 while (nCount < nMaxNodes_2)
549 {
550 Append((*xPage)[0]);
551 xPage->Remove(0);
552 }
553 // Replace the node values: replace old last value by the last of xPage
554 aParent->SearchAndReplace(aReplaceNode.GetKey(),(*this)[nCount-1].GetKey());
555 }
556 else
557 {
558 // insert all nodes from this in front of the xPage nodes
559 ONDXNode aReplaceNode = (*this)[nCount - 1];
560 while (xPage->Count() < nMaxNodes_2)
561 {
562 xPage->Insert(0,(*this)[nCount-1]);
563 Remove(nCount-1);
564 }
565 // Replace the node value
566 aParent->SearchAndReplace(aReplaceNode.GetKey(),(*this)[Count()-1].GetKey());
567 }
568 }
569 }
570 else // !IsLeaf()
571 {
572 // Condition for merge
573 if (nNewCount < nMaxNodes_2 * 2)
574 {
575 if (bRight)
576 {
577 DBG_ASSERT(xPage != this,"xPage and THIS must not be the same: infinite loop");
578 // Parent node will be integrated; is initialized with Child from xPage
579 (*aParent)[nParentNodePos].SetChild(xPage->GetChild(),aParent);
580 Append((*aParent)[nParentNodePos]);
581 for (sal_uInt16 i = 0 ; i < xPage->Count(); i++)
582 Append((*xPage)[i]);
583 }
584 else
585 {
586 DBG_ASSERT(xPage != this,"xPage and THIS must not be the same: infinite loop");
587 // Parent-node will be integrated; is initialized with child
588 (*aParent)[nParentNodePos].SetChild(GetChild(),aParent); // Parent memorizes my child
589 Insert(0,(*aParent)[nParentNodePos]); // insert parent node into myself
590 while (xPage->Count())
591 {
592 Insert(0,(*xPage)[xPage->Count()-1]);
593 xPage->Remove(xPage->Count()-1);
594 }
595 SetChild(xPage->GetChild());
596
597 if (nParentNodePos)
598 (*aParent)[nParentNodePos-1].SetChild(this,aParent);
599 else
600 aParent->SetChild(this);
601 }
602
603 // afterwards parent node will be reset
604 (*aParent)[nParentNodePos].SetChild();
605 aParent->SetModified(true);
606
607 if(aParent->IsRoot() && aParent->Count() == 1)
608 {
609 (*aParent).SetChild();
611 aParent.Clear();
613 rIndex.m_aRoot = this;
614 SetModified(true);
615 }
616 else if(nParentNodePos)
617 // replace the node value
618 // for Append the range will be enlarged, for Insert the old node from xPage will reference to this
619 // that's why the node must be updated here
620 aParent->SearchAndReplace((*aParent)[nParentNodePos-1].GetKey(),(*aParent)[nParentNodePos].GetKey());
621
622 xPage->SetModified(false);
623 xPage->ReleaseFull();
624 }
625 // balance the elements
626 else
627 {
628 if (bRight)
629 {
630 while (nCount < nMaxNodes_2)
631 {
632 (*aParent)[nParentNodePos].SetChild(xPage->GetChild(),aParent);
633 Append((*aParent)[nParentNodePos]);
634 (*aParent)[nParentNodePos] = (*xPage)[0];
635 xPage->Remove(0);
636 }
637 xPage->SetChild((*aParent)[nParentNodePos].GetChild());
638 (*aParent)[nParentNodePos].SetChild(xPage,aParent);
639 }
640 else
641 {
642 while (nCount < nMaxNodes_2)
643 {
644 (*aParent)[nParentNodePos].SetChild(GetChild(),aParent);
645 Insert(0,(*aParent)[nParentNodePos]);
646 (*aParent)[nParentNodePos] = (*xPage)[xPage->Count()-1];
647 xPage->Remove(xPage->Count()-1);
648 }
649 SetChild((*aParent)[nParentNodePos].GetChild());
650 (*aParent)[nParentNodePos].SetChild(this,aParent);
651
652 }
653 aParent->SetModified(true);
654 }
655 }
656}
657
658// ONDXNode
659
660
661void ONDXNode::Read(SvStream &rStream, ODbaseIndex const & rIndex)
662{
663 rStream.ReadUInt32( aKey.nRecord ); // key
664
665 if (rIndex.getHeader().db_keytype)
666 {
667 double aDbl;
668 rStream.ReadDouble( aDbl );
669 aKey = ONDXKey(aDbl,aKey.nRecord);
670 }
671 else
672 {
673 sal_uInt16 nLen = rIndex.getHeader().db_keylen;
674 OString aBuf = read_uInt8s_ToOString(rStream, nLen);
675 //get length minus trailing whitespace
676 sal_Int32 nContentLen = aBuf.getLength();
677 while (nContentLen && aBuf[nContentLen-1] == ' ')
678 --nContentLen;
679 aKey = ONDXKey(OUString(aBuf.getStr(), nContentLen, rIndex.m_pTable->getConnection()->getTextEncoding()) ,aKey.nRecord);
680 }
681 rStream >> aChild;
682}
683
684
685void ONDXNode::Write(SvStream &rStream, const ONDXPage& rPage) const
686{
687 const ODbaseIndex& rIndex = rPage.GetIndex();
688 if (!rIndex.isUnique() || rPage.IsLeaf())
689 rStream.WriteUInt32( aKey.nRecord ); // key
690 else
691 rStream.WriteUInt32( 0 ); // key
692
693 if (rIndex.getHeader().db_keytype) // double
694 {
695 if (sizeof(double) != rIndex.getHeader().db_keylen)
696 {
697 SAL_WARN("connectivity.dbase", "this key length cannot possibly be right?");
698 }
699 if (aKey.getValue().isNull())
700 {
701 sal_uInt8 buf[sizeof(double)] = {};
702 rStream.WriteBytes(&buf[0], sizeof(double));
703 }
704 else
705 rStream.WriteDouble( aKey.getValue().getDouble() );
706 }
707 else
708 {
709 sal_uInt16 const nLen(rIndex.getHeader().db_keylen);
710 std::unique_ptr<sal_uInt8[]> pBuf(new sal_uInt8[nLen]);
711 memset(&pBuf[0], 0x20, nLen);
712 if (!aKey.getValue().isNull())
713 {
714 OUString sValue = aKey.getValue().getString();
715 OString aText(OUStringToOString(sValue, rIndex.m_pTable->getConnection()->getTextEncoding()));
716 strncpy(reinterpret_cast<char *>(&pBuf[0]), aText.getStr(),
717 std::min<size_t>(nLen, aText.getLength()));
718 }
719 rStream.WriteBytes(&pBuf[0], nLen);
720 }
721 WriteONDXPagePtr( rStream, aChild );
722}
723
724
726{
727 if (!aChild.Is() && pIndex)
728 {
729 aChild = pIndex->CreatePage(aChild.GetPagePos(),pParent,aChild.HasPage());
730 }
731 return aChild;
732}
733
734
735// ONDXKey
736
737
738bool ONDXKey::IsText(sal_Int32 eType)
739{
740 return eType == DataType::VARCHAR || eType == DataType::CHAR;
741}
742
743
744int ONDXKey::Compare(const ONDXKey& rKey) const
745{
746 sal_Int32 nRes;
747
748 if (getValue().isNull())
749 {
750 if (rKey.getValue().isNull() || (IsText(getDBType()) && rKey.getValue().getString().isEmpty()))
751 nRes = 0;
752 else
753 nRes = -1;
754 }
755 else if (rKey.getValue().isNull())
756 {
757 if (getValue().isNull() || (IsText(getDBType()) && getValue().getString().isEmpty()))
758 nRes = 0;
759 else
760 nRes = 1;
761 }
762 else if (IsText(getDBType()))
763 {
764 nRes = getValue().getString().compareTo(rKey.getValue().getString());
765 }
766 else
767 {
768 double m = getValue().getDouble();
769 double n = rKey.getValue().getDouble();
770 nRes = (m > n) ? 1 : ( m < n) ? -1 : 0;
771 }
772
773 // compare record, if index !Unique
774 if (nRes == 0 && nRecord && rKey.nRecord)
775 {
776 nRes = (nRecord > rKey.nRecord) ? 1 :
777 (nRecord == rKey.nRecord) ? 0 : -1;
778 }
779 return nRes;
780}
781
783{
784 xValue = _rVal;
785}
786
788{
789 return xValue;
790}
791
793{
794 rStream.ReadUInt32( rPage.nPagePos );
795 return rStream;
796}
797
799{
800 rStream.WriteUInt32( rPage.nPagePos );
801 return rStream;
802}
803
804// ONDXPagePtr
806 : mpPage(nullptr)
807 , nPagePos(0)
808{
809}
810
812{
813 mpPage = rRef.mpPage;
814 rRef.mpPage = nullptr;
815 nPagePos = rRef.nPagePos;
816}
817
819 : mpPage(rRef.mpPage)
820 , nPagePos(rRef.nPagePos)
821{
822 if (mpPage != nullptr)
824}
825
827 : mpPage(pRefPage)
828 , nPagePos(0)
829{
830 if (mpPage != nullptr)
832 if (pRefPage)
833 nPagePos = pRefPage->GetPagePos();
834}
835
837{
838 if (mpPage != nullptr) mpPage->ReleaseRef();
839}
840
842{
843 if (mpPage != nullptr) {
844 ONDXPage * pRefObj = mpPage;
845 mpPage = nullptr;
846 pRefObj->ReleaseRef();
847 }
848}
849
851{
852 ONDXPagePtr aTemp(rOther);
853 *this = std::move(aTemp);
854 return *this;
855}
856
858{
859 if (mpPage != nullptr) {
861 }
862 mpPage = rOther.mpPage;
863 nPagePos = rOther.nPagePos;
864 rOther.mpPage = nullptr;
865 return *this;
866}
867
868static sal_uInt32 nValue;
869
871{
872 rStream.Seek(rPage.GetPagePos() * DINDEX_PAGE_SIZE);
873 rStream.ReadUInt32( nValue ) >> rPage.aChild;
874 rPage.nCount = sal_uInt16(nValue);
875
876 for (sal_uInt16 i = 0; i < rPage.nCount; i++)
877 rPage[i].Read(rStream, rPage.GetIndex());
878 return rStream;
879}
880
881
883{
884 // Page doesn't exist yet
885 std::size_t nSize = rPage.GetPagePos() + 1;
886 nSize *= DINDEX_PAGE_SIZE;
887 if (nSize > rStream.TellEnd())
888 {
889 rStream.SetStreamSize(nSize);
890 rStream.Seek(rPage.GetPagePos() * DINDEX_PAGE_SIZE);
891
892 char aEmptyData[DINDEX_PAGE_SIZE] = {};
893 rStream.WriteBytes(aEmptyData, DINDEX_PAGE_SIZE);
894 }
895 rStream.Seek(rPage.GetPagePos() * DINDEX_PAGE_SIZE);
896
897 nValue = rPage.nCount;
898 rStream.WriteUInt32( nValue );
899 WriteONDXPagePtr( rStream, rPage.aChild );
900
901 sal_uInt16 i = 0;
902 for (; i < rPage.nCount; i++)
903 rPage[i].Write(rStream, rPage);
904
905 // check if we have to fill the stream with '\0'
906 if(i < rPage.rIndex.getHeader().db_maxkeys)
907 {
908 std::size_t nTell = rStream.Tell() % DINDEX_PAGE_SIZE;
909 sal_uInt16 nBufferSize = rStream.GetBufferSize();
910 std::size_t nRemainSize = nBufferSize - nTell;
911 if ( nRemainSize <= nBufferSize )
912 {
913 std::unique_ptr<char[]> pEmptyData( new char[nRemainSize] );
914 memset(pEmptyData.get(), 0x00, nRemainSize);
915 rStream.WriteBytes(pEmptyData.get(), nRemainSize);
916 rStream.Seek(nTell);
917 }
918 }
919 return rStream;
920}
921
922#if OSL_DEBUG_LEVEL > 1
923
925{
926 SAL_WARN("connectivity.dbase", "SDB: -----------Page: " << nPagePos << " Parent: " << (HasParent() ? aParent->GetPagePos() : 0)
927 << " Count: " << nCount << " Child: " << aChild.GetPagePos() << "-----");
928
929 for (sal_uInt16 i = 0; i < nCount; i++)
930 {
931 ONDXNode rNode = (*this)[i];
932 ONDXKey& rKey = rNode.GetKey();
933 if (!IsLeaf())
934 rNode.GetChild(&rIndex, this);
935
936 if (rKey.getValue().isNull())
937 {
938 SAL_WARN("connectivity.dbase", "SDB: [" << rKey.GetRecord() << ",NULL," << rNode.GetChild().GetPagePos() << "]");
939 }
940 else if (rIndex.getHeader().db_keytype)
941 {
942 SAL_WARN("connectivity.dbase", "SDB: [" << rKey.GetRecord() << "," << rKey.getValue().getDouble()
943 << "," << rNode.GetChild().GetPagePos() << "]");
944 }
945 else
946 {
947 SAL_WARN("connectivity.dbase", "SDB: [" << rKey.GetRecord() << "," << rKey.getValue().getString()
948 << "," << rNode.GetChild().GetPagePos() << "]" );
949 }
950 }
951 SAL_WARN("connectivity.dbase", "SDB: -----------------------------------------------");
952 if (!IsLeaf())
953 {
954#if OSL_DEBUG_LEVEL > 1
956 for (sal_uInt16 i = 0; i < nCount; i++)
957 {
958 ONDXNode rNode = (*this)[i];
959 rNode.GetChild(&rIndex,this)->PrintPage();
960 }
961#endif
962 }
963 SAL_WARN("connectivity.dbase", "SDB: ===============================================");
964}
965#endif
966
968{
969 return Count() == rIndex.getHeader().db_maxkeys;
970}
971
972
973sal_uInt16 ONDXPage::Search(const ONDXKey& rSearch)
974{
975 // binary search later
976 sal_uInt16 i = NODE_NOTFOUND;
977 while (++i < Count())
978 if ((*this)[i].GetKey() == rSearch)
979 break;
980
981 return (i < Count()) ? i : NODE_NOTFOUND;
982}
983
984
985sal_uInt16 ONDXPage::Search(const ONDXPage* pPage)
986{
987 sal_uInt16 i = NODE_NOTFOUND;
988 while (++i < Count())
989 if (((*this)[i]).GetChild() == pPage)
990 break;
991
992 // if not found, then we assume, that the page itself points to the page
993 return (i < Count()) ? i : NODE_NOTFOUND;
994}
995
996// runs recursively
998 ONDXKey const & rReplace)
999{
1000 OSL_ENSURE(rSearch != rReplace,"Invalid here:rSearch == rReplace");
1001 if (rSearch == rReplace)
1002 return;
1003
1004 sal_uInt16 nPos = NODE_NOTFOUND;
1005 ONDXPage* pPage = this;
1006
1007 while (pPage)
1008 {
1009 nPos = pPage->Search(rSearch);
1010 if (nPos != NODE_NOTFOUND)
1011 break;
1012 pPage = pPage->aParent;
1013 }
1014
1015 if (pPage)
1016 {
1017 (*pPage)[nPos].GetKey() = rReplace;
1018 pPage->SetModified(true);
1019 }
1020}
1021
1023{
1024 DBG_ASSERT(nCount > nPos, "incorrect index access");
1025 return ppNodes[nPos];
1026}
1027
1028
1029const ONDXNode& ONDXPage::operator[] (sal_uInt16 nPos) const
1030{
1031 DBG_ASSERT(nCount > nPos, "incorrect index access");
1032 return ppNodes[nPos];
1033}
1034
1035void ONDXPage::Remove(sal_uInt16 nPos)
1036{
1037 DBG_ASSERT(nCount > nPos, "incorrect index access");
1038
1039 for (sal_uInt16 i = nPos; i < (nCount-1); i++)
1040 (*this)[i] = (*this)[i+1];
1041
1042 nCount--;
1043 bModified = true;
1044}
1045
1046
1047/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FPDF_PAGE mpPage
#define DOUBLE
SvStream & WriteDouble(const double &rDouble)
sal_uInt64 Tell() const
virtual sal_uInt64 TellEnd()
SvStream & ReadDouble(double &rDouble)
sal_uInt16 GetBufferSize() const
std::size_t WriteBytes(const void *pData, std::size_t nSize)
bool SetStreamSize(sal_uInt64 nSize)
SvStream & WriteUInt32(sal_uInt32 nUInt32)
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
sal_uInt64 Seek(sal_uInt64 nPos)
rtl_TextEncoding getTextEncoding() const
Definition: TConnection.hxx:61
OUString getString() const
Definition: FValue.cxx:933
void setBound(bool _bBound)
Definition: FValue.hxx:332
double getDouble() const
Definition: FValue.cxx:1745
void SetPageCount(sal_uInt32 nCount)
Definition: DIndex.hxx:99
void SetRootPos(sal_uInt32 nPos)
Definition: DIndex.hxx:98
const NDXHeader & getHeader() const
Definition: DIndex.hxx:95
ONDXPage * CreatePage(sal_uInt32 nPagePos, ONDXPage *pParent=nullptr, bool bLoad=false)
Definition: DIndex.cxx:293
sal_uInt32 GetPageCount() const
Definition: DIndex.hxx:101
std::unique_ptr< SvStream > m_pFileStream
Definition: DIndex.hxx:67
sal_uInt16 GetMaxNodes() const
Definition: DIndex.hxx:103
virtual const ORowSetValue & getValue() const override
Definition: dindexnode.cxx:787
virtual void setValue(const ORowSetValue &_rVal) override
Definition: dindexnode.cxx:782
int Compare(const ONDXKey &rKey) const
Definition: dindexnode.cxx:744
static bool IsText(sal_Int32 eType)
Definition: dindexnode.cxx:738
sal_uInt32 GetRecord() const
Definition: dindexnode.hxx:58
const ONDXKey & GetKey() const
Definition: dindexnode.hxx:245
void SetChild(ONDXPagePtr aCh=ONDXPagePtr(), ONDXPage *=nullptr)
Definition: dindexnode.hxx:296
void Read(SvStream &rStream, ODbaseIndex const &)
Definition: dindexnode.cxx:661
ONDXPagePtr & GetChild(ODbaseIndex *pIndex=nullptr, ONDXPage *=nullptr)
Definition: dindexnode.cxx:725
void Write(SvStream &rStream, const ONDXPage &rPage) const
Definition: dindexnode.cxx:685
ONDXPagePtr & operator=(ONDXPagePtr const &rRef)
Definition: dindexnode.cxx:850
bool Append(ONDXNode &rNode)
Definition: dindexnode.cxx:335
ONDXPagePtr & GetChild(ODbaseIndex const *pIndex=nullptr)
Definition: dindexnode.cxx:129
void SetChild(ONDXPagePtr aCh)
Definition: dindexnode.hxx:217
sal_uInt32 GetPagePos() const
Definition: dindexnode.hxx:157
sal_uInt16 Search(const ONDXKey &rSearch)
Definition: dindexnode.cxx:973
sal_uInt16 Count() const
Definition: dindexnode.hxx:132
ONDXNode Split(ONDXPage &rPage)
Definition: dindexnode.cxx:426
ONDXPage(ODbaseIndex &rIndex, sal_uInt32 nPos, ONDXPage *)
Definition: dindexnode.cxx:68
void SetParent(ONDXPagePtr aPa)
Definition: dindexnode.hxx:212
ONDXNode & operator[](sal_uInt16 nPos)
bool Find(const ONDXKey &)
Definition: dindexnode.cxx:150
sal_uInt16 FindPos(const ONDXKey &rKey) const
Definition: dindexnode.cxx:139
void SearchAndReplace(const ONDXKey &rSearch, ONDXKey const &rReplace)
Definition: dindexnode.cxx:997
void Merge(sal_uInt16 nParentNodePos, const ONDXPagePtr &xPage)
Definition: dindexnode.cxx:476
bool Insert(ONDXNode &rNode, sal_uInt32 nRowsLeft=0)
Definition: dindexnode.cxx:184
std::unique_ptr< ONDXNode[]> ppNodes
Definition: dindexnode.hxx:128
void Release(bool bSave=true)
Definition: dindexnode.cxx:341
friend SvStream & WriteONDXPage(SvStream &rStream, const ONDXPage &)
OConnection * getConnection() const
Definition: FTable.hxx:66
sal_Int32 getDBType() const
Definition: fcode.hxx:68
sal_Int32 nRefCount
int nCount
#define DBG_ASSERT(sCon, aError)
static sal_uInt32 nValue
Definition: dindexnode.cxx:868
#define DINDEX_PAGE_SIZE
Definition: dindexnode.hxx:26
#define NODE_NOTFOUND
Definition: dindexnode.hxx:25
DocumentType eType
sal_Int64 n
sal_uInt16 nPos
#define SAL_WARN(area, stream)
aStr
aBuf
OUString getString(const Any &_rAny)
SvStream & operator>>(SvStream &rStream, ODbaseIndex &)
Definition: DIndex.cxx:333
SvStream & WriteONDXPage(SvStream &rStream, const ONDXPage &rPage)
Definition: dindexnode.cxx:882
SvStream & WriteONDXPagePtr(SvStream &rStream, const ONDXPagePtr &)
Definition: dindexnode.cxx:798
int i
m
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
DataType
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Definition: pq_tools.cxx:100
TOOLS_DLLPUBLIC OString read_uInt8s_ToOString(SvStream &rStrm, std::size_t nUnits)
unsigned char sal_uInt8