33
33
/*******************************************************************************
35
35
********************************************************************************/
36
SimpleLazyTempSeq::SimpleLazyTempSeq(const store::Iterator_t& aIter)
36
SimpleLazyTempSeq::SimpleLazyTempSeq(const store::Iterator_t& iter)
44
44
********************************************************************************/
45
45
SimpleLazyTempSeq::~SimpleLazyTempSeq()
50
51
/*******************************************************************************
52
53
********************************************************************************/
53
void SimpleLazyTempSeq::init(const store::Iterator_t& aIter)
54
void SimpleLazyTempSeq::clear()
56
theMatFinished = false;
56
std::vector<store::Item*>::iterator ite = theItems.begin();
57
std::vector<store::Item*>::iterator end = theItems.end();
58
for (; ite != end; ++ite)
60
(*ite)->removeReference();
62
67
/*******************************************************************************
64
********************************************************************************/
65
void SimpleLazyTempSeq::append(const store::Iterator_t& iter)
67
while (!theMatFinished)
72
theMatFinished = false;
77
/*******************************************************************************
79
********************************************************************************/
80
void SimpleLazyTempSeq::purge()
86
/*******************************************************************************
88
********************************************************************************/
89
void SimpleLazyTempSeq::purgeUpTo(xs_integer upTo)
94
lUpTo = to_xs_long(upTo);
96
catch (std::range_error& e)
98
throw ZORBA_EXCEPTION(zerr::ZSTR0060_RANGE_EXCEPTION,
99
ERROR_PARAMS(BUILD_STRING("sequence too big (" << e.what() << ")")));
102
ZORBA_ASSERT(lUpTo >= thePurgedUpTo);
103
ZORBA_ASSERT(lUpTo - thePurgedUpTo <= theItems.size());
105
theItems.erase(theItems.begin(), (theItems.begin() + (lUpTo - thePurgedUpTo)));
106
thePurgedUpTo = lUpTo;
68
Get the next item (if any) from the input iterator and put it at the end of
70
********************************************************************************/
71
void SimpleLazyTempSeq::matNextItem()
75
if (! theIterator->next(item))
77
theMatFinished = true;
81
theItems.push_back(item.release());
134
110
/*******************************************************************************
136
112
********************************************************************************/
113
xs_integer SimpleLazyTempSeq::getSize() const
116
return xs_integer(thePurgedUpTo + theItems.size());
120
/*******************************************************************************
122
********************************************************************************/
123
void SimpleLazyTempSeq::init(const store::Iterator_t& iter)
127
theMatFinished = false;
134
/*******************************************************************************
136
********************************************************************************/
137
void SimpleLazyTempSeq::append(const store::Iterator_t& iter)
139
while (!theMatFinished)
144
theMatFinished = false;
149
/*******************************************************************************
151
********************************************************************************/
152
void SimpleLazyTempSeq::purge()
156
theMatFinished = false;
163
/*******************************************************************************
165
********************************************************************************/
166
void SimpleLazyTempSeq::purgeUpTo(xs_integer position)
171
pos = to_xs_long(position);
173
catch (std::range_error& e)
175
throw ZORBA_EXCEPTION(zerr::ZSTR0060_RANGE_EXCEPTION,
176
ERROR_PARAMS(BUILD_STRING("sequence too big (" << e.what() << ")")));
179
ZORBA_ASSERT(pos >= thePurgedUpTo);
180
ZORBA_ASSERT(pos - thePurgedUpTo <= theItems.size());
182
std::vector<store::Item*>::iterator ite = theItems.begin();
183
std::vector<store::Item*>::iterator end = theItems.begin() + (pos - thePurgedUpTo);
184
for (; ite != end; ++ite)
186
(*ite)->removeReference();
189
theItems.erase(theItems.begin(), end);
195
/*******************************************************************************
197
********************************************************************************/
137
198
void SimpleLazyTempSeq::getItem(xs_integer position, store::Item_t& result)
142
lPos = to_xs_long(position);
203
pos = to_xs_long(position);
144
205
catch (std::range_error& e)
147
208
ERROR_PARAMS(BUILD_STRING("access out of bounds " << e.what() << ")")));
150
if (this->containsItem(position))
152
result = theItems[lPos - thePurgedUpTo - 1];
211
ZORBA_ASSERT(pos > thePurgedUpTo);
213
xs_long numItemsToBuffer = pos - thePurgedUpTo;
215
while (!theMatFinished && theItems.size() < numItemsToBuffer)
220
if (theItems.size() >= numItemsToBuffer)
222
result = theItems[pos - thePurgedUpTo - 1];
180
250
********************************************************************************/
181
251
inline bool SimpleLazyTempSeq::containsItem(xs_integer position)
186
lPos = to_xs_long(position);
256
pos = to_xs_long(position);
188
258
catch (std::range_error& e)
191
261
ERROR_PARAMS(BUILD_STRING("access out of bounds " << e.what() << ")")));
194
assert(lPos > thePurgedUpTo);
264
ZORBA_ASSERT(pos > thePurgedUpTo);
196
xs_long numItemsToBuffer = lPos - thePurgedUpTo;
266
xs_long numItemsToBuffer = pos - thePurgedUpTo;
198
268
while (!theMatFinished && theItems.size() < numItemsToBuffer)
207
277
/*******************************************************************************
208
Get the next item (if any) from the input iterator and put it at the end of
210
********************************************************************************/
211
void SimpleLazyTempSeq::matNextItem()
213
theItems.push_back(NULL);
215
store::Item_t& lLocation = theItems.back();
217
if (theIterator->next(lLocation))
219
if (theCopy && lLocation->isNode())
221
store::CopyMode lCopyMode;
222
lLocation = lLocation->copy(NULL, lCopyMode);
227
// We do not want to have an empty item materialized.
229
theMatFinished = true;
230
theIterator->close();
235
/*******************************************************************************
237
********************************************************************************/
238
xs_integer SimpleLazyTempSeq::getSize() const
241
return xs_integer(0);
245
/*******************************************************************************
246
278
Reads the whole Sequence from beginning to end; it is allowed to have several
247
279
concurrent iterators on the same TempSeq.
250
282
********************************************************************************/
251
283
store::Iterator_t SimpleLazyTempSeq::getIterator() const
253
return new SimpleLazyTempSeqIter(this, xs_integer(1), xs_integer(std::numeric_limits<long>::max()));
285
return new SimpleLazyTempSeqIter(this,
287
xs_integer(std::numeric_limits<long>::max()));
291
/////////////////////////////////////////////////////////////////////////////////
293
// SimpleLazyTempSeqIter //
295
/////////////////////////////////////////////////////////////////////////////////
257
298
/*******************************************************************************
258
Returns an iterator which reads just a part of the underlying TempSeq
260
@param startPos The first item which the iterator returns
261
@param endPos The last item which the iterator returns
263
300
********************************************************************************/
264
store::Iterator_t SimpleLazyTempSeq::getIterator(
267
bool streaming) const
301
SimpleLazyTempSeqIter::SimpleLazyTempSeqIter()
269
return new SimpleLazyTempSeqIter(this, startPos, endPos);
275
312
********************************************************************************/
276
313
SimpleLazyTempSeqIter::SimpleLazyTempSeqIter(
277
const SimpleLazyTempSeq* aTempSeq,
278
xs_integer aStartPos,
314
const SimpleLazyTempSeq* tempSeq,
281
theTempSeq(const_cast<SimpleLazyTempSeq*>(aTempSeq))
318
theTempSeq(const_cast<SimpleLazyTempSeq*>(tempSeq))
285
theCurPos = to_xs_long(aStartPos) - 1;
286
theStartPos = to_xs_long(aStartPos);
287
theEndPos = to_xs_long(aEndPos);
322
theStartPos = to_xs_long(startPos);
323
theEndPos = to_xs_long(endPos);
324
theCurPos = theStartPos - 1;
289
326
catch (std::range_error& e)
334
/*******************************************************************************
336
********************************************************************************/
297
337
SimpleLazyTempSeqIter::~SimpleLazyTempSeqIter()
342
/*******************************************************************************
344
********************************************************************************/
345
void SimpleLazyTempSeqIter::init(const store::TempSeq_t& seq)
351
/*******************************************************************************
353
********************************************************************************/
354
void SimpleLazyTempSeqIter::init(
355
const store::TempSeq_t& seq,
359
assert(seq->isLazy());
361
theTempSeq = static_cast<SimpleLazyTempSeq*>(seq.getp());
365
theStartPos = to_xs_long(startPos);
366
theEndPos = to_xs_long(endPos);
367
theCurPos = theStartPos - 1;
369
catch (std::range_error& e)
371
RAISE_ERROR_NO_LOC(zerr::ZSTR0060_RANGE_EXCEPTION,
372
ERROR_PARAMS(BUILD_STRING("sequence too big (" << e.what() << ")")));
377
/*******************************************************************************
379
********************************************************************************/
302
380
void SimpleLazyTempSeqIter::open()
304
382
theCurPos = theStartPos - 1;
386
/*******************************************************************************
388
********************************************************************************/
308
389
bool SimpleLazyTempSeqIter::next(store::Item_t& result)
310
391
if (theCurPos < theEndPos && theTempSeq->containsItem(xs_integer(theCurPos+1)))
404
/*******************************************************************************
406
********************************************************************************/
323
407
void SimpleLazyTempSeqIter::reset()
325
409
theCurPos = theStartPos - 1;
413
/*******************************************************************************
415
********************************************************************************/
329
416
void SimpleLazyTempSeqIter::close()