TurtleDB
A mini distributed database system
src/ca/uqac/dim/turtledb/Union.java
Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002     Simple distributed database engine
00003     Copyright (C) 2012  Sylvain Hallé
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  -------------------------------------------------------------------------*/
00018 package ca.uqac.dim.turtledb;
00019 
00020 import ca.uqac.dim.turtledb.QueryVisitor.VisitorException;
00021 
00028 public class Union extends NAryRelation
00029 {   
00030 
00031   @Override
00032   public void accept(QueryVisitor v) throws VisitorException
00033   {
00034     super.acceptNAry(v);
00035     v.visit(this);
00036   }
00037 
00038   protected class UnionStreamIterator extends NAryRelationStreamIterator
00039   {
00040     public UnionStreamIterator()
00041     {
00042       super();
00043     }
00044     
00045     @Override
00046     protected Tuple internalNext()
00047     {
00048       super.initializeIteration();
00049       Relation r = m_relations.get(0);
00050       Schema sch = r.getSchema();
00051       Tuple t = super.incrementSmallestTuple();
00052       if (t == null)
00053         return null;
00054       Tuple t2 = new Tuple(t);
00055       t2.setSchema(sch);
00056       return t2;
00057     }
00058   }
00059 
00060   @Override
00061   public RelationStreamIterator streamIterator()
00062   {
00063     return new UnionStreamIterator();
00064   }
00065 
00066   @Override
00067   public RelationIterator cacheIterator()
00068   {
00069     return new UnionCacheIterator();
00070   }
00071   
00072   protected class UnionCacheIterator extends RelationCacheIterator
00073   {
00074     @Override
00075     protected void getIntermediateResult()
00076     {
00077       Schema sch = getSchema();
00078       Table tab = new Table(getSchema());
00079       for (Relation r : m_relations)
00080       {
00081         RelationIterator i = r.cacheIterator();
00082         while (i.hasNext())
00083         {
00084           Tuple t = i.next();
00085           // Set schema of t to that of the current table
00086           Tuple t2 = new Tuple(t);
00087           t2.setSchema(sch);
00088           tab.put(t2);
00089         }
00090       }
00091       m_intermediateResult = tab;
00092     }
00093   }
00094 }