TurtleDB
A mini distributed database system
src/ca/uqac/dim/turtledb/Selection.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 public class Selection extends UnaryRelation
00021 {
00022   protected Condition m_condition;
00023    
00024   public Selection(Condition c, Relation r)
00025   {
00026     super();
00027     m_condition = c;
00028     m_relation = r;
00029   }
00030 
00031   @Override
00032   public Schema getSchema()
00033   {
00034     // The schema of a selection is the same as the schema of
00035     // its underlying relation
00036     return m_relation.getSchema();
00037   }
00038 
00039   public void setCondition(Condition c)
00040   {
00041     assert c != null;
00042     m_condition = c;
00043   }
00044   
00045   @Override
00046   public void accept(QueryVisitor v) throws EmptyQueryVisitor.VisitorException
00047   {
00048     m_relation.accept(v);
00049     v.visit(this);
00050   }
00051   
00052   protected class SelectionStreamIterator extends UnaryRelationStreamIterator
00053   { 
00054     public SelectionStreamIterator()
00055     {
00056       super();
00057     }
00058     
00059     protected Tuple internalNext()
00060     {
00061       m_nextTuple = null;
00062       while (m_childIterator.hasNext())
00063       {
00064         Tuple t = m_childIterator.next();
00065         if (m_condition.evaluate(t))
00066         {
00067           return t;
00068         }
00069       }
00070       return null;
00071     }
00072   }
00073   
00074   protected class SelectionCacheIterator extends RelationCacheIterator
00075   {
00076     @Override
00077     protected void getIntermediateResult()
00078     {
00079       Table tab = new Table(getSchema());
00080       RelationIterator i = m_relation.cacheIterator();
00081       while (i.hasNext())
00082       {
00083         Tuple t = i.next();
00084         if (m_condition.evaluate(t))
00085           tab.put(t);
00086       }
00087       m_intermediateResult = tab;
00088     }
00089   }
00090 
00091   @Override
00092   public RelationStreamIterator streamIterator()
00093   {
00094     return new SelectionStreamIterator();
00095   }
00096 
00097   @Override
00098   public RelationIterator cacheIterator()
00099   {
00100     return new SelectionCacheIterator();
00101   }
00102 
00103 }