Line data Source code
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/DIndexIter.hxx"
21 : #include <com/sun/star/sdb/SQLFilterOperator.hpp>
22 :
23 : using namespace ::com::sun::star::sdb;
24 : using namespace connectivity;
25 : using namespace connectivity::dbase;
26 : using namespace connectivity::file;
27 :
28 : // OIndexIterator
29 :
30 :
31 0 : OIndexIterator::~OIndexIterator()
32 : {
33 0 : m_pIndex->release();
34 0 : }
35 :
36 :
37 0 : sal_uIntPtr OIndexIterator::First()
38 : {
39 0 : return Find(sal_True);
40 : }
41 :
42 :
43 0 : sal_uIntPtr OIndexIterator::Next()
44 : {
45 0 : return Find(sal_False);
46 : }
47 :
48 0 : sal_uIntPtr OIndexIterator::Find(sal_Bool bFirst)
49 : {
50 0 : sal_uIntPtr nRes = NODE_NOTFOUND;
51 :
52 0 : if (bFirst)
53 : {
54 0 : m_aRoot = m_pIndex->getRoot();
55 0 : m_aCurLeaf = NULL;
56 : }
57 :
58 0 : if (!m_pOperator)
59 : {
60 : // Preparation, position on the smallest element
61 0 : if (bFirst)
62 : {
63 0 : ONDXPage* pPage = m_aRoot;
64 0 : while (pPage && !pPage->IsLeaf())
65 0 : pPage = pPage->GetChild(m_pIndex);
66 :
67 0 : m_aCurLeaf = pPage;
68 0 : m_nCurNode = NODE_NOTFOUND;
69 : }
70 0 : ONDXKey* pKey = GetNextKey();
71 0 : nRes = pKey ? pKey->GetRecord() : NODE_NOTFOUND;
72 : }
73 0 : else if (m_pOperator->IsA(TYPE(OOp_ISNOTNULL)))
74 0 : nRes = GetNotNull(bFirst);
75 0 : else if (m_pOperator->IsA(TYPE(OOp_ISNULL)))
76 0 : nRes = GetNull(bFirst);
77 0 : else if (m_pOperator->IsA(TYPE(OOp_LIKE)))
78 0 : nRes = GetLike(bFirst);
79 0 : else if (m_pOperator->IsA(TYPE(OOp_COMPARE)))
80 0 : nRes = GetCompare(bFirst);
81 :
82 0 : return nRes;
83 : }
84 :
85 :
86 0 : ONDXKey* OIndexIterator::GetFirstKey(ONDXPage* pPage, const OOperand& rKey)
87 : {
88 : // searches a given key
89 : // Speciality: At the end of the algorithm
90 : // the actual page and the position of the node which fulfil the
91 : // '<='-condition are saved. this is considered for inserts.
92 : // ONDXIndex* m_pIndex = GetNDXIndex();
93 0 : OOp_COMPARE aTempOp(SQLFilterOperator::GREATER);
94 0 : sal_uInt16 i = 0;
95 :
96 0 : if (pPage->IsLeaf())
97 : {
98 : // in the leaf the actual operation is run, otherwise temp. (>)
99 0 : while (i < pPage->Count() && !m_pOperator->operate(&((*pPage)[i]).GetKey(),&rKey))
100 0 : i++;
101 : }
102 : else
103 0 : while (i < pPage->Count() && !aTempOp.operate(&((*pPage)[i]).GetKey(),&rKey))
104 0 : i++;
105 :
106 :
107 0 : ONDXKey* pFoundKey = NULL;
108 0 : if (!pPage->IsLeaf())
109 : {
110 : // descend further
111 0 : ONDXPagePtr aPage = (i==0) ? pPage->GetChild(m_pIndex)
112 0 : : ((*pPage)[i-1]).GetChild(m_pIndex, pPage);
113 0 : pFoundKey = aPage.Is() ? GetFirstKey(aPage, rKey) : NULL;
114 : }
115 0 : else if (i == pPage->Count())
116 : {
117 0 : pFoundKey = NULL;
118 : }
119 : else
120 : {
121 0 : pFoundKey = &(*pPage)[i].GetKey();
122 0 : if (!m_pOperator->operate(pFoundKey,&rKey))
123 0 : pFoundKey = NULL;
124 :
125 0 : m_aCurLeaf = pPage;
126 0 : m_nCurNode = pFoundKey ? i : i - 1;
127 : }
128 0 : return pFoundKey;
129 : }
130 :
131 :
132 0 : sal_uIntPtr OIndexIterator::GetCompare(sal_Bool bFirst)
133 : {
134 0 : ONDXKey* pKey = NULL;
135 0 : sal_Int32 ePredicateType = PTR_CAST(file::OOp_COMPARE,m_pOperator)->getPredicateType();
136 :
137 0 : if (bFirst)
138 : {
139 : // Preparation, position on the smallest element
140 0 : ONDXPage* pPage = m_aRoot;
141 0 : switch (ePredicateType)
142 : {
143 : case SQLFilterOperator::NOT_EQUAL:
144 : case SQLFilterOperator::LESS:
145 : case SQLFilterOperator::LESS_EQUAL:
146 0 : while (pPage && !pPage->IsLeaf())
147 0 : pPage = pPage->GetChild(m_pIndex);
148 :
149 0 : m_aCurLeaf = pPage;
150 0 : m_nCurNode = NODE_NOTFOUND;
151 : }
152 :
153 :
154 0 : switch (ePredicateType)
155 : {
156 : case SQLFilterOperator::NOT_EQUAL:
157 0 : while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand)) ;
158 0 : break;
159 : case SQLFilterOperator::LESS:
160 0 : while ( ( ( pKey = GetNextKey() ) != NULL ) && pKey->getValue().isNull()) ;
161 0 : break;
162 : case SQLFilterOperator::LESS_EQUAL:
163 0 : while ( ( pKey = GetNextKey() ) != NULL ) ;
164 0 : break;
165 : case SQLFilterOperator::GREATER_EQUAL:
166 : case SQLFilterOperator::EQUAL:
167 0 : pKey = GetFirstKey(m_aRoot,*m_pOperand);
168 0 : break;
169 : case SQLFilterOperator::GREATER:
170 0 : pKey = GetFirstKey(m_aRoot,*m_pOperand);
171 0 : if ( !pKey )
172 0 : while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand)) ;
173 : }
174 : }
175 : else
176 : {
177 0 : switch (ePredicateType)
178 : {
179 : case SQLFilterOperator::NOT_EQUAL:
180 0 : while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand))
181 : ;
182 0 : break;
183 : case SQLFilterOperator::LESS:
184 : case SQLFilterOperator::LESS_EQUAL:
185 : case SQLFilterOperator::EQUAL:
186 0 : if ( ( ( pKey = GetNextKey() ) == NULL ) || !m_pOperator->operate(pKey,m_pOperand))
187 : {
188 0 : pKey = NULL;
189 0 : m_aCurLeaf = NULL;
190 : }
191 0 : break;
192 : case SQLFilterOperator::GREATER_EQUAL:
193 : case SQLFilterOperator::GREATER:
194 0 : pKey = GetNextKey();
195 : }
196 : }
197 :
198 0 : return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
199 : }
200 :
201 :
202 0 : sal_uIntPtr OIndexIterator::GetLike(sal_Bool bFirst)
203 : {
204 0 : if (bFirst)
205 : {
206 0 : ONDXPage* pPage = m_aRoot;
207 :
208 0 : while (pPage && !pPage->IsLeaf())
209 0 : pPage = pPage->GetChild(m_pIndex);
210 :
211 0 : m_aCurLeaf = pPage;
212 0 : m_nCurNode = NODE_NOTFOUND;
213 : }
214 :
215 : ONDXKey* pKey;
216 0 : while ( ( ( pKey = GetNextKey() ) != NULL ) && !m_pOperator->operate(pKey,m_pOperand))
217 : ;
218 0 : return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
219 : }
220 :
221 :
222 0 : sal_uIntPtr OIndexIterator::GetNull(sal_Bool bFirst)
223 : {
224 0 : if (bFirst)
225 : {
226 0 : ONDXPage* pPage = m_aRoot;
227 0 : while (pPage && !pPage->IsLeaf())
228 0 : pPage = pPage->GetChild(m_pIndex);
229 :
230 0 : m_aCurLeaf = pPage;
231 0 : m_nCurNode = NODE_NOTFOUND;
232 : }
233 :
234 : ONDXKey* pKey;
235 0 : if ( ( ( pKey = GetNextKey() ) == NULL ) || !pKey->getValue().isNull())
236 : {
237 0 : pKey = NULL;
238 0 : m_aCurLeaf = NULL;
239 : }
240 0 : return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
241 : }
242 :
243 :
244 0 : sal_uIntPtr OIndexIterator::GetNotNull(sal_Bool bFirst)
245 : {
246 : ONDXKey* pKey;
247 0 : if (bFirst)
248 : {
249 : // go through all NULL values first
250 0 : for (sal_uIntPtr nRec = GetNull(bFirst);
251 : nRec != NODE_NOTFOUND;
252 : nRec = GetNull(sal_False))
253 : ;
254 0 : pKey = m_aCurLeaf.Is() ? &(*m_aCurLeaf)[m_nCurNode].GetKey() : NULL;
255 : }
256 : else
257 0 : pKey = GetNextKey();
258 :
259 0 : return pKey ? pKey->GetRecord() : NODE_NOTFOUND;
260 : }
261 :
262 :
263 0 : ONDXKey* OIndexIterator::GetNextKey()
264 : {
265 0 : if (m_aCurLeaf.Is() && ((++m_nCurNode) >= m_aCurLeaf->Count()))
266 : {
267 0 : ONDXPage* pPage = m_aCurLeaf;
268 : // search next page
269 0 : while (pPage)
270 : {
271 0 : ONDXPage* pParentPage = pPage->GetParent();
272 0 : if (pParentPage)
273 : {
274 0 : sal_uInt16 nPos = pParentPage->Search(pPage);
275 0 : if (nPos != pParentPage->Count() - 1)
276 : { // page found
277 0 : pPage = (*pParentPage)[nPos+1].GetChild(m_pIndex,pParentPage);
278 0 : break;
279 : }
280 : }
281 0 : pPage = pParentPage;
282 : }
283 :
284 : // now go on with leaf
285 0 : while (pPage && !pPage->IsLeaf())
286 0 : pPage = pPage->GetChild(m_pIndex);
287 :
288 0 : m_aCurLeaf = pPage;
289 0 : m_nCurNode = 0;
290 : }
291 0 : return m_aCurLeaf.Is() ? &(*m_aCurLeaf)[m_nCurNode].GetKey() : NULL;
292 : }
293 :
294 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|