TurtleDB
A mini distributed database system
|
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 00022 public class Projection extends UnaryRelation 00023 { 00024 protected Schema m_schema; 00025 00026 public Projection(Schema sch, Relation rel) 00027 { 00028 super(); 00029 m_schema = sch; 00030 m_relation = rel; 00031 } 00032 00033 @Override 00034 public Schema getSchema() 00035 { 00036 return m_schema; 00037 } 00038 00044 private Tuple project(Tuple t) 00045 { 00046 if (t == null) 00047 return null; 00048 Value[] parts = new Value[m_schema.size()]; 00049 int i = 0; 00050 for (Attribute a : m_schema) 00051 { 00052 parts[i++] = t.get(a); 00053 } 00054 return new Tuple(m_schema, parts); 00055 } 00056 00057 public void setSchema(Schema sch) 00058 { 00059 assert sch != null; 00060 m_schema = sch; 00061 } 00062 00063 @Override 00064 public void accept(QueryVisitor v) throws EmptyQueryVisitor.VisitorException 00065 { 00066 m_relation.accept(v); 00067 v.visit(this); 00068 } 00069 00070 protected class ProjectionStreamIterator extends UnaryRelationStreamIterator 00071 { 00072 public ProjectionStreamIterator() 00073 { 00074 super(); 00075 m_outputTuples = new LinkedList<Tuple>(); 00076 } 00077 00078 protected Tuple internalNext() 00079 { 00080 Tuple t = m_childIterator.next(); 00081 return project(t); 00082 } 00083 } 00084 00085 protected class ProjectionCacheIterator extends UnaryRelationCacheIterator 00086 { 00087 public void getIntermediateResult() 00088 { 00089 Table tab_out = new Table(getSchema()); 00090 super.getIntermediateResult(); 00091 Iterator<Tuple> it = m_intermediateResult.tupleIterator(); 00092 while (it.hasNext()) 00093 { 00094 Tuple t = it.next(); 00095 Tuple t2 = project(t); 00096 tab_out.put(t2); 00097 } 00098 m_intermediateResult = tab_out; 00099 } 00100 } 00101 00102 @Override 00103 public RelationStreamIterator streamIterator() 00104 { 00105 return new ProjectionStreamIterator(); 00106 } 00107 00108 @Override 00109 public RelationIterator cacheIterator() 00110 { 00111 return new ProjectionCacheIterator(); 00112 } 00113 00114 }