summaryrefslogtreecommitdiffstats
path: root/qlparser/qtjoiniterator.cc
blob: 12277ab026f572f30f39fe515441cdbf1133302c (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 *
 * PURPOSE:
 *
 *
 * COMMENTS:
 *
 ************************************************************/


#include "raslib/rmdebug.hh"

#include "qlparser/qtjoiniterator.hh"
#include "qlparser/qtmdd.hh"

#include <iostream>
#ifndef CPPSTDLIB
#include <ospace/string.h> // STL<ToolKit>
#else
#include <string>
using namespace std;
#endif

const QtNode::QtNodeType QtJoinIterator::nodeType = QtNode::QT_JOIN_ITERATOR;


QtJoinIterator::QtJoinIterator()
  : QtIterator(),
    actualTupel(NULL),
    outputStreamIsEmpty(false)
{
}


QtJoinIterator::QtJoinIterator( QtNode* node )
  : QtIterator( node ),
    actualTupel(NULL),
    outputStreamIsEmpty(false)
{
}


QtJoinIterator::~QtJoinIterator()
{
  vector<QtData*>::iterator i; //default

  if( actualTupel )
  {
    // first delete still existing data carriers
    for( QtDataList::iterator iter=actualTupel->begin(); iter!=actualTupel->end(); iter++ )
      if( *iter ) (*iter)->deleteRef();

    delete actualTupel;
    actualTupel=NULL;
  }
}


void
QtJoinIterator::printTree( int tab, ostream& s, QtChildType mode )
{
  s << SPACE_STR(tab).c_str() << "QtJoinIterator Object: type " << flush;
  dataStreamType.printStatus( s );
  s << endl;

  QtIterator::printTree( tab, s, mode );
}



void
QtJoinIterator::printAlgebraicExpression( ostream& s )
{
  s << "join";

  QtIterator::printAlgebraicExpression( s );
}


void
QtJoinIterator::open()
{
  RMDBCLASS( "QtJoinIterator", "open()", "qlparser", __FILE__, __LINE__ )

  QtIterator::open();

  outputStreamIsEmpty = false;  // initialization

  if( inputs )
  {
    // The idea of actualTupel initialization:
    // 
    //    tupel[0]  tupel[1]  tupel[2]  ...  |
    //    -----------------------------------------------------
    //       0         0         0           |  initial phase
    //       0        b1        c1           |  open 
    //      a1        b1        c1           |  next invocation
    //      a2        b1        c1           |        " 
    //      a1        b2        c1           |        "
    //       :         :         :           |        "

    // allocate an empty tupel, right now each input stream provides one data element
    actualTupel = new QtDataList( inputs->size() );

    // set the first element of the tupel to NULL
    (*actualTupel)[0] = NULL;

    // fill the tupel, except of the first element, with the first elements of the input streams
	//the first element is filled in the ::next() method
    for( int tupelPos=1; tupelPos<actualTupel->size(); tupelPos++ )
    {
      QtDataList* resultList = (*inputs)[tupelPos]->next();

      if( resultList )
      {
        // take the first data element of the input stream result
        (*actualTupel)[tupelPos] = (*resultList)[0];

        // delete the result vector (only the first data carriers is taken, the others are never deleted)
        delete resultList;
	resultList = NULL;
      }
      else
      {
	// In that case, one of the input streams is empty. Therefore, the output stream of
	// the self object is empty either.
	(*actualTupel)[tupelPos] = NULL;
	outputStreamIsEmpty = true;
      }
    }

    // Reset the first stream again, because the first tupel element is catched when next() is
    // called for the first time.
    // (*inputs)[0]->reset();
  }
}


QtNode::QtDataList*
QtJoinIterator::next()
{
  RMDBCLASS( "QtJoinIterator", "next()", "qlparser", __FILE__, __LINE__ )


  QtDataList* returnValue = NULL;

  if( inputs && actualTupel && !outputStreamIsEmpty )
  {
    bool        nextTupelAvailable = true;
    bool        nextTupelValid = false;
    int         tupelPos;
    QtDataList* resultList = NULL;
    QtONCStreamList::iterator iter;

    while( !nextTupelValid && nextTupelAvailable && !outputStreamIsEmpty )
    {
      // switch to the next tupel which means

      nextTupelAvailable = false;
      tupelPos           = 0;
      iter               = inputs->begin();

      while( !nextTupelAvailable && iter!=inputs->end() )
      {
        resultList = (*iter)->next();

        // Test, if the first input stream is empty, because this is not tested in open()
        if( resultList == NULL && tupelPos==0 && (*actualTupel)[0] == 0 )
          outputStreamIsEmpty = true;          

        if( resultList == NULL )
        {
          (*iter)->reset();              // reset the stream ...
	//this causes the first element of the list to be deleted - not the others
        resultList = (*iter)->next();  // ... and read the first element again
	// this was commented out because it will cause problems when the stream is closed
	// if it is commented out it will break join queries
        }
        else
          nextTupelAvailable = true;

        //
        // exchange the actual element in the tupel
        //
  
        //  delete the data carrier
        if( (*actualTupel)[tupelPos] )
        {
          (*actualTupel)[tupelPos]->deleteRef();
          (*actualTupel)[tupelPos] = NULL;
        }

        if( resultList )
        {
          // take the first data element of the input stream result - copy the data carrier pointer
          (*actualTupel)[tupelPos] = (*resultList)[0];

          // delete the result vector (only the first data carrier is taken, the others are never deleted)
          delete resultList;
          resultList = NULL;
        }

        iter++;
        tupelPos++;
      }

      if( nextTupelAvailable )
        nextTupelValid = true;
   }

   if( nextTupelAvailable )
   {
     // Copy the actual tupel in order to pass it as the next stream element
     // which means increase references to data elements.
     returnValue = new QtDataList( actualTupel->size() );

     for( tupelPos=0; tupelPos < actualTupel->size(); tupelPos++ )
       if( (*actualTupel)[tupelPos] )
       {
   	 (*returnValue)[tupelPos] = (*actualTupel)[tupelPos];
         (*actualTupel)[tupelPos]->incRef();
       }
       else
       {
	 // should not come here, because now the next tupel isn't valid

         // delete return value again
         for( tupelPos=0; tupelPos < returnValue->size(); tupelPos++ )
           if( (*returnValue)[tupelPos] )
             (*returnValue)[tupelPos]->deleteRef();

         delete returnValue;
         returnValue = NULL;

	 RMInit::logOut << "Internal Error in QtJoinIterator::next()" << endl;
       }
   }
  }

  return returnValue;
}



void
QtJoinIterator::close()
{
  RMDBCLASS( "QtJoinIterator", "close()", "qlparser", __FILE__, __LINE__ )

  if( actualTupel )
  {
    // first delete still existing data carriers
    for( QtDataList::iterator iter=actualTupel->begin(); iter!=actualTupel->end(); iter++ )
      if( *iter ) (*iter)->deleteRef();

    delete actualTupel;
    actualTupel = NULL;
  }

  QtIterator::close();
}


void
QtJoinIterator::reset()
{
  RMDBCLASS( "QtJoinIterator", "reset()", "qlparser", __FILE__, __LINE__ )

  // reset the input streams
  QtIterator::reset();

  if( inputs )
  {
    // first delete still existing data carriers
    for( QtDataList::iterator iter=actualTupel->begin(); iter!=actualTupel->end(); iter++ )
      if( *iter )
      {
        (*iter)->deleteRef();
	(*iter) = NULL;
      }

    // fill the tupel with the first elements of the input streams except of the first element
    for( int tupelPos=1; tupelPos<actualTupel->size(); tupelPos++ )
    {
      QtDataList* resultList = (*inputs)[tupelPos]->next();

      if( resultList )
      {
        // take the first data element of the input stream result
        (*actualTupel)[tupelPos] = (*resultList)[0];

        // delete the result vector (only the first data carriers is taken, the others are never deleted)
        delete resultList;
	resultList = NULL;
      }
      else
	(*actualTupel)[tupelPos] = NULL;

    }

    (*actualTupel)[0] = NULL;  // fist tupel element is catched when next() is called for the first time
  }
}



const QtTypeTuple&
QtJoinIterator::checkType()
{
  RMDBCLASS( "QtJoinIterator", "checkType()", "qlparser", __FILE__, __LINE__ )

  getInputTypeTuple( dataStreamType );

  return dataStreamType;
}