TurtleDB
A mini distributed database system
src/ca/uqac/dim/turtledb/Table.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 
00035 public class Table extends Relation
00036 {
00037   protected List<Tuple> m_tuples;
00038   protected Schema m_schema;
00039   protected int m_cursor;
00040   protected String m_name;
00041   
00045   /*package*/ Table()
00046   {
00047     super();
00048     m_tuples = new ArrayList<Tuple>();
00049     m_name = "";
00050   }
00051   
00052   /*package*/ Table(String s)
00053   {
00054     this();
00055     m_name = s;
00056   }
00057   
00062   public Table(Relation r)
00063   {
00064     this();
00065     copy(r);
00066   }
00067   
00075   public void setName(String name)
00076   {
00077     m_name = name;
00078     for (Tuple t : m_tuples)
00079       t.setTable(m_name);
00080   }
00081 
00086   /*package*/ Table(Schema sch)
00087   {
00088     this();
00089     m_schema = sch;
00090   }
00091   
00096   public String getName()
00097   {
00098     return m_name;
00099   }
00100   
00105   protected void setSchema(Schema sch)
00106   {
00107         Schema s = new Schema(sch);
00108         s.setTableName(m_name);
00109     m_schema = s;
00110   }
00111 
00112   @Override
00113   public Schema getSchema()
00114   {
00115     return m_schema;
00116   }
00117   
00130   public void put(Tuple t)
00131   {
00132     assert t != null;
00133     assert t.size() == m_schema.size();
00134     // Gives the current table's name to all the tuple's attributes
00135     if (m_name != null && !m_name.isEmpty())
00136       t.setTable(m_name);
00137     int index = Collections.binarySearch(m_tuples, t);
00138     if (index < 0) // We silently ignore tuples that are already present
00139       m_tuples.add(-index-1, t);
00140   }
00141   
00148   public void putAll(Collection<Tuple> tuples)
00149   {
00150     for (Tuple t : tuples)
00151     {
00152       put(t);
00153     }
00154   }
00155   
00156   @Override
00157   public void accept(QueryVisitor v) throws EmptyQueryVisitor.VisitorException
00158   {
00159     v.visit(this);
00160   }
00161   
00169   public void copy(Relation r)
00170   {
00171     assert r != null;
00172     m_schema = r.getSchema();
00173     Iterator<Tuple> i = r.iterator();
00174     while (i.hasNext())
00175     {
00176       Tuple t = i.next();
00177       this.put(t);
00178     }
00179   }
00180   
00181   public int getCardinality()
00182   {
00183     return m_tuples.size();
00184   }
00185   
00196   @Override
00197   public boolean contains(Tuple tup)
00198   {
00199     if (tup == null)
00200       return false;
00201     return m_tuples.contains(tup);
00202   }
00203   
00204   public int tupleCount()
00205   {
00206     return m_tuples.size();
00207   }
00208   
00209   @Override
00210   public final boolean isLeaf()
00211   {
00212     return true;
00213   }
00214 
00215   @Override
00216   public RelationStreamIterator streamIterator()
00217   {
00218     return new TableStreamIterator();
00219   }
00220   
00221   protected class TableStreamIterator extends RelationStreamIterator
00222   {
00223     protected Iterator<Tuple> m_iterator;
00224     
00225     public TableStreamIterator()
00226     {
00227       m_iterator = m_tuples.iterator();
00228     }
00229 
00230     @Override
00231     protected Tuple internalNext()
00232     {
00233       if (m_iterator.hasNext())
00234         return m_iterator.next();
00235       return null;
00236     }
00237     
00238     public void reset()
00239     {
00240       super.reset();
00241       m_iterator = m_tuples.iterator();
00242     }
00243   }
00244   
00245   protected Iterator<Tuple> tupleIterator()
00246   {
00247     return m_tuples.iterator();
00248   }
00249   
00250   protected class TableCacheIterator extends RelationCacheIterator
00251   {
00252 
00253     @Override
00254     protected void getIntermediateResult()
00255     {
00256       // The intermediate result is the table itself
00257       super.m_intermediateResult = Table.this; 
00258     }
00259     
00260   }
00261 
00262   @Override
00263   public RelationIterator cacheIterator()
00264   {
00265     return new TableStreamIterator();
00266   }
00267 
00268 }