~juan457/+junk/zorba

« back to all changes in this revision

Viewing changes to src/store/naive/node_iterators.cpp

  • Committer: Markos Zaharioudakis
  • Date: 2012-07-11 15:38:39 UTC
  • mfrom: (10924 zorba)
  • mto: This revision was merged to the branch mainline in revision 10932.
  • Revision ID: markos_za@yahoo.com-20120711153839-0mkh15cg2ubknchd
work in progress

Show diffs side-by-side

added added

removed removed

Lines of Context:
251
251
 
252
252
void StoreNodeSortIterator::open()
253
253
{
254
 
  theCurrentNode = -1;
 
254
  theCurrentNode = 0;
255
255
}
256
256
 
257
257
 
258
258
bool StoreNodeSortIterator::next(store::Item_t& result)
259
259
{
260
 
  if (theCurrentNode < 0)
 
260
  if (theCurrentNode == 0)
261
261
  {
262
 
    theCurrentNode = 0;
 
262
    store::Item_t item;
263
263
 
264
 
    while (true)
 
264
    // We are not done yet with gathering all XML nodes.
 
265
    while (theInput->next(item))
265
266
    {
266
 
      store::Item_t contextNode;
267
 
      if (!theInput->next(contextNode))
268
 
        break;
269
 
 
270
 
      ZORBA_ASSERT(contextNode->isNode());
271
 
 
272
 
      theNodes.push_back(reinterpret_cast<XmlNode*>(contextNode.release()));
 
267
      if (!item->isNode())
 
268
      {
 
269
#ifdef ZORBA_WITH_JSON
 
270
        // If no JSON item should be found (like in a path expression), this
 
271
        // is handled by the consumer of this iterator.
 
272
        ZORBA_ASSERT(item->isJSONObject() || item->isJSONArray());
 
273
 
 
274
        json::JSONItem* jsonItem = static_cast<json::JSONItem*>(item.getp());
 
275
 
 
276
        if (theDistinct)
 
277
        {
 
278
          std::set<json::JSONItem*>::const_iterator ite = theJSONItems.find(jsonItem);
 
279
          if (ite == theJSONItems.end())
 
280
          {
 
281
            jsonItem = static_cast<json::JSONItem*>(item.release());
 
282
            theJSONItems.insert(jsonItem);
 
283
          }
 
284
          else
 
285
          {
 
286
            continue;
 
287
          }
 
288
        }
 
289
 
 
290
        result = jsonItem;
 
291
        return true;
 
292
#else
 
293
        ZORBA_ASSERT_WITH_MSG(false, "Non-node found in node sorting iterator.");
 
294
#endif
 
295
      }
 
296
 
 
297
      theNodes.push_back(reinterpret_cast<XmlNode*>(item.release()));
273
298
    }
274
 
 
 
299
    
 
300
    // We are out of items. We can now begin to output the nodes. In the next
 
301
    // iteration, this part of the code will be skipped.
275
302
    ComparisonFunction cmp(theAscending);
276
303
 
277
304
    std::sort(theNodes.begin(), theNodes.end(), cmp);
278
305
  }
279
306
 
280
 
  if (theCurrentNode < (long)theNodes.size())
 
307
  if (theCurrentNode < theNodes.size())
281
308
  {
282
309
    if (theDistinct)
283
310
    {
284
311
      result = theNodes[theCurrentNode++];
285
312
 
286
 
      while (theCurrentNode < (long)theNodes.size() &&
 
313
      while (theCurrentNode < theNodes.size() &&
287
314
             theNodes[theCurrentNode] == result)
288
315
      {
289
316
        theCurrentNode++;
319
346
  }
320
347
 
321
348
  theNodes.clear();
322
 
  theCurrentNode = -1;
 
349
  theCurrentNode = 0;
 
350
 
 
351
#ifdef ZORBA_WITH_JSON
 
352
  for (std::set<json::JSONItem*>::iterator ite = theJSONItems.begin();
 
353
      ite != theJSONItems.end();
 
354
      ++ite)
 
355
  {
 
356
    json::JSONItem* n = *ite;
 
357
    n->removeReference();
 
358
  }
 
359
  theJSONItems.clear();
 
360
#endif
323
361
}
324
362
 
325
363
 
329
367
  // which wraps this store iterator.
330
368
 
331
369
  csize numNodes = theNodes.size();
332
 
  for (csize i = 0; i < numNodes; i++)
 
370
  for (csize i = 0; i < numNodes; ++i)
333
371
  {
334
372
    XmlNode* n = theNodes[i];
335
373
    n->removeReference();
336
374
  }
337
375
 
338
376
  theNodes.clear();
339
 
  theCurrentNode = -1;
 
377
  theCurrentNode = 0;
 
378
 
 
379
#ifdef ZORBA_WITH_JSON
 
380
  for (std::set<json::JSONItem*>::iterator ite = theJSONItems.begin();
 
381
      ite != theJSONItems.end();
 
382
      ++ite)
 
383
  {
 
384
    json::JSONItem* n = *ite;
 
385
    n->removeReference();
 
386
  }
 
387
  theJSONItems.clear();
 
388
#endif
 
389
 
340
390
  theInput = NULL;
341
391
}
342
392
 
360
410
    return true;
361
411
  }
362
412
 
363
 
  if (theCurrentNode < 0)
 
413
  if (theCurrentNode == 0)
364
414
  {
365
 
    theCurrentNode = 0;
366
 
 
367
415
    while (true)
368
416
    {
369
417
      if (!theInput->next(result))
390
438
    std::sort(theNodes.begin(), theNodes.end(), cmp);
391
439
  }
392
440
 
393
 
  if (theCurrentNode < (long)theNodes.size())
 
441
  if (theCurrentNode < theNodes.size())
394
442
  {
395
443
    if (theDistinct)
396
444
    {