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 ca.uqac.dim.turtledb.QueryVisitor.VisitorException; 00021 00022 public class Join extends BinaryRelation 00023 { 00024 protected Product m_product; 00025 protected Condition m_condition; 00026 00027 public Join() 00028 { 00029 super(); 00030 m_product = new Product(); 00031 } 00032 00033 public Join(Condition c) 00034 { 00035 this(); 00036 m_condition = c; 00037 } 00038 00039 public void setCondition(Condition c) 00040 { 00041 m_condition = c; 00042 } 00043 00044 @Override 00045 public Schema getSchema() 00046 { 00047 return m_product.getSchema(); 00048 } 00049 00050 public void addOperand(Relation r) 00051 { 00052 m_product.addOperand(r); 00053 } 00054 00055 public int tupleCount() 00056 { 00057 return m_product.tupleCount(); 00058 } 00059 00060 00061 00062 @Override 00063 public void accept(QueryVisitor v) throws VisitorException 00064 { 00065 super.acceptBinary(v); 00066 v.visit(this); 00067 } 00068 00069 protected class JoinStreamIterator extends BinaryRelationStreamIterator 00070 { 00071 protected RelationStreamIterator m_childIterator; 00072 00073 public JoinStreamIterator() 00074 { 00075 super(); 00076 m_childIterator = m_product.streamIterator(); 00077 reset(); 00078 } 00079 00080 00084 @Override 00085 protected Tuple internalNext() 00086 { 00087 while (m_childIterator.hasNext()) 00088 { 00089 Tuple t = m_childIterator.next(); 00090 if (m_condition.evaluate(t)) 00091 return t; 00092 } 00093 return null; 00094 } 00095 00096 public void reset() 00097 { 00098 super.reset(); 00099 m_childIterator.reset(); 00100 } 00101 } 00102 00103 @Override 00104 public RelationIterator streamIterator() 00105 { 00106 return new JoinStreamIterator(); 00107 } 00108 00109 @Override 00110 public RelationIterator cacheIterator() 00111 { 00112 return new JoinCacheIterator(); 00113 } 00114 00115 protected class JoinCacheIterator extends RelationCacheIterator 00116 { 00117 public JoinCacheIterator() 00118 { 00119 super(); 00120 m_intermediateResult = null; 00121 } 00122 00123 protected void getIntermediateResult() 00124 { 00125 m_intermediateResult = new Table(m_product.getSchema()); 00126 RelationIterator it = m_product.cacheIterator(); 00127 while (it.hasNext()) 00128 { 00129 Tuple t = it.next(); 00130 if (m_condition.evaluate(t)) 00131 m_intermediateResult.put(t); 00132 } 00133 } 00134 00135 } 00136 00137 }