TurtleDB
A mini distributed database system
src/ca/uqac/dim/turtledb/Relation.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 java.util.*;
00021 
00028 public abstract class Relation
00029 {
00030   
00035   public boolean m_streamingMode = false; 
00036   
00041   public abstract Schema getSchema();
00042   
00047   protected Relation()
00048   {
00049     super();
00050   }
00051 
00056   public final int getDegree()
00057   {
00058     return getSchema().size();
00059   }
00060   
00065   public void setStreamingMode(boolean b)
00066   {
00067     m_streamingMode = b;
00068   }
00069   
00070   public final RelationIterator iterator()
00071   {
00072     if (m_streamingMode)
00073       return streamIterator();
00074     return cacheIterator();
00075   }
00076   
00080   @Override
00081   public String toString()
00082   {
00083     StringBuilder out = new StringBuilder();
00084     Schema sch = this.getSchema();
00085     for (Attribute s : sch)
00086     {
00087       out.append(s).append("\t");
00088     }
00089     out.append("\n");
00090     for (int i = 0; i < sch.size(); i++)
00091     {
00092       out.append("--------");
00093     }
00094     out.append("\n");
00095     Iterator<Tuple> i = this.iterator();
00096     while (i.hasNext())
00097     {
00098       Tuple t = i.next();
00099       //for (Attribute s : t.keySet())
00100       for (Attribute s : sch)
00101       {
00102         Value v = t.get(s);
00103         assert v != null;
00104         out.append(v).append("\t");
00105       }
00106       out.append("\n");
00107     }
00108     return out.toString();
00109   }
00110   
00111   public abstract void accept(QueryVisitor v) throws QueryVisitor.VisitorException;
00112   
00120   public int getCardinality()
00121   {
00122     int size = 0;
00123     Iterator<Tuple> i = this.iterator();
00124     while (i.hasNext())
00125     {
00126       i.next();
00127       size++;
00128     }
00129     return size;
00130   }
00131   
00139   public abstract int tupleCount();  
00148   public boolean contains(Tuple tup)
00149   {
00150     if (tup == null)
00151       return false;
00152     assert tup != null;
00153     Iterator<Tuple> i = this.iterator();
00154     while (i.hasNext())
00155     {
00156       Tuple t = i.next();
00157       if (tup.equals(t))
00158         return true;
00159     }
00160     return false;
00161   }
00162 
00167   public abstract RelationIterator streamIterator();
00168   
00173   public abstract RelationIterator cacheIterator();
00174   
00180   public boolean isFragment()
00181   {
00182     return false;
00183   }
00184   
00191   public boolean isLeaf()
00192   {
00193     return false;
00194   }
00195 }