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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <dbase/DIndexIter.hxx>
#include <com/sun/star/sdb/SQLFilterOperator.hpp>
using namespace ::com::sun::star::sdb;
using namespace connectivity;
using namespace connectivity::dbase;
using namespace connectivity::file;
// OIndexIterator
OIndexIterator::~OIndexIterator()
{
}
sal_uInt32 OIndexIterator::First()
{
return Find(true);
}
sal_uInt32 OIndexIterator::Next()
{
return Find(false);
}
sal_uInt32 OIndexIterator::Find(bool bFirst)
{
sal_uInt32 nRes = NODE_NOTFOUND;
if (bFirst)
{
m_aRoot = m_xIndex->getRoot();
m_aCurLeaf.Clear();
}
if (!m_pOperator)
{
// Preparation, position on the smallest element
if (bFirst)
{
ONDXPage* pPage = m_aRoot;
while (pPage && !pPage->IsLeaf())
pPage = pPage->GetChild(m_xIndex.get());
m_aCurLeaf = pPage;
m_nCurNode = NODE_NOTFOUND;
}
ONDXKey* pKey = GetNextKey();
nRes = pKey ? pKey->GetRecord() : NODE_NOTFOUND;
}
else if (dynamic_cast<const OOp_ISNOTNULL *>(m_pOperator) != nullptr)
nRes = GetNotNull(bFirst);
else if (dynamic_cast<const OOp_ISNULL *>(m_pOperator) != nullptr)
nRes = GetNull(bFirst);
else if (dynamic_cast<const OOp_LIKE *>(m_pOperator) != nullptr)
nRes = GetLike(bFirst);
else if (dynamic_cast<const OOp_COMPARE *>(m_pOperator) != nullptr)
nRes = GetCompare(bFirst);
return nRes;
}
ONDXKey* OIndexIterator::GetFirstKey(ONDXPage* pPage, const OOperand& rKey)
{
// searches a given key
// Speciality: At the end of the algorithm
// the actual page and the position of the node which fulfil the
// '<='-condition are saved. this is considered for inserts.
// ONDXIndex* m_pIndex = GetNDXIndex();
OOp_COMPARE aTempOp(SQLFilterOperator::GREATER);
sal_uInt16 i = 0;
if (pPage->IsLeaf())
{
// in the leaf the actual operation is run, otherwise temp. (>)
while (i < pPage->Count() && !m_pOperator->operate(&((*pPage)[i]).GetKey(),&rKey))
i++;
}
else
while (i < pPage->Count() && !aTempOp.operate(&((*pPage)[i]).GetKey(),&rKey))
i++;
ONDXKey* pFoundKey = nullptr;
if (!pPage->IsLeaf())
{
// descend further
ONDXPagePtr aPage = (i==0) ? pPage->GetChild(m_xIndex.get())
: ((*pPage)[i-1]).GetChild(m_xIndex.get(), pPage);
pFoundKey = aPage.Is() ? GetFirstKey(aPage, rKey) : nullptr;
}
else if (i == pPage->Count())
{
pFoundKey = nullptr;
}
else
{
pFoundKey = &(*pPage)[i].GetKey();
if (!m_pOperator->operate(pFoundKey,&rKey))
pFoundKey = nullptr;
m_aCurLeaf = pPage;
m_nCurNode = pFoundKey ? i : i - 1;
}
return pFoundKey;
}
sal_uInt32 OIndexIterator::GetCompare(bool bFirst)
{
ONDXKey* pKey = nullptr;
sal_Int32 ePredicateType = dynamic_cast<file::OOp_COMPARE&>(*m_pOperator).getPredicateType();
if (bFirst)
{
// Preparation, position on the smallest element
ONDXPage* pPage = m_aRoot;
switch (ePredicateType)
{
case SQLFilterOperator::NOT_EQUAL:
case SQLFilterOperator::LESS:
case SQLFilterOperator::LESS_EQUAL:
while (pPage && !pPage->IsLeaf())
pPage = pPage->GetChild(m_xIndex.get());
m_aCurLeaf = pPage;
m_nCurNode = NODE_NOTFOUND;
}
switch (ePredicateType)
{
case SQLFilterOperator::NOT_EQUAL:
while ( ( ( pKey = GetNextKey() ) != nullptr ) && !m_pOperator->operate(pKey,m_pOperand)) ;
break;
case SQLFilterOperator::LESS:
while ( ( ( pKey = GetNextKey() ) != nullptr ) && pKey->getValue().isNull()) ;
break;
case SQLFilterOperator::LESS_EQUAL:
while ( ( pKey = GetNextKey() ) != nullptr ) ;
break;
case SQLFilterOperator::GREATER_EQUAL:
case SQLFilterOperator::EQUAL:
pKey = GetFirstKey(m_aRoot,*m_pOperand);
break;
case SQLFilterOperator::GREATER:
pKey = GetFirstKey(m_aRoot,*m_pOperand);
if ( !pKey )
while ( ( ( pKey = GetNextKey() ) != nullptr ) && !m_pOperator->operate(pKey,m_pOperand)) ;
}
}
else
{
switch (ePredicateType)
{
case SQLFilterOperator::NOT_EQUAL:
while ( ( ( pKey = GetNextKey() ) != nullptr ) && !m_pOperator->operate(pKey,m_pOperand))
;
break;
case SQLFilterOperator::LESS:
case SQLFilterOperator::LESS_EQUAL:
case SQLFilterOperator::EQUAL:
if ( ( ( pKey = GetNextKey() ) == nullptr ) || !m_pOperator->operate(pKey,m_pOperand))
{
pKey = nullptr;
m_aCurLeaf.Clear();
}
break;
case SQLFilterOperator::GREATER_EQUAL:
case SQLFilterOperator::GREATER:
pKey = GetNextKey();
}
}
return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
}
sal_uInt32 OIndexIterator::GetLike(bool bFirst)
{
if (bFirst)
{
ONDXPage* pPage = m_aRoot;
while (pPage && !pPage->IsLeaf())
pPage = pPage->GetChild(m_xIndex.get());
m_aCurLeaf = pPage;
m_nCurNode = NODE_NOTFOUND;
}
ONDXKey* pKey;
while ( ( ( pKey = GetNextKey() ) != nullptr ) && !m_pOperator->operate(pKey,m_pOperand))
;
return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
}
sal_uInt32 OIndexIterator::GetNull(bool bFirst)
{
if (bFirst)
{
ONDXPage* pPage = m_aRoot;
while (pPage && !pPage->IsLeaf())
pPage = pPage->GetChild(m_xIndex.get());
m_aCurLeaf = pPage;
m_nCurNode = NODE_NOTFOUND;
}
ONDXKey* pKey;
if ( ( ( pKey = GetNextKey() ) == nullptr ) || !pKey->getValue().isNull())
{
pKey = nullptr;
m_aCurLeaf.Clear();
}
return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
}
sal_uInt32 OIndexIterator::GetNotNull(bool bFirst)
{
ONDXKey* pKey;
if (bFirst)
{
// go through all NULL values first
for (sal_uInt32 nRec = GetNull(bFirst);
nRec != NODE_NOTFOUND;
nRec = GetNull(false))
;
pKey = m_aCurLeaf.Is() ? &(*m_aCurLeaf)[m_nCurNode].GetKey() : nullptr;
}
else
pKey = GetNextKey();
return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
}
ONDXKey* OIndexIterator::GetNextKey()
{
if (m_aCurLeaf.Is() && ((++m_nCurNode) >= m_aCurLeaf->Count()))
{
ONDXPage* pPage = m_aCurLeaf;
// search next page
while (pPage)
{
ONDXPage* pParentPage = pPage->GetParent();
if (pParentPage)
{
sal_uInt16 nPos = pParentPage->Search(pPage);
if (nPos != pParentPage->Count() - 1)
{ // page found
pPage = (*pParentPage)[nPos+1].GetChild(m_xIndex.get(),pParentPage);
break;
}
}
pPage = pParentPage;
}
// now go on with leaf
while (pPage && !pPage->IsLeaf())
pPage = pPage->GetChild(m_xIndex.get());
m_aCurLeaf = pPage;
m_nCurNode = 0;
}
return m_aCurLeaf.Is() ? &(*m_aCurLeaf)[m_nCurNode].GetKey() : nullptr;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|