Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. package simpledb;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6. * The Join operator implements the relational join operation.
  7. */
  8. public class Join extends Operator {
  9.  
  10. public JoinPredicate _predicate;
  11. public DbIterator _child1;
  12. public DbIterator _child2;
  13.  
  14. boolean hasNext = true;
  15. boolean needToRead = true;
  16. Tuple outerTuple = null;
  17.  
  18. private static final long serialVersionUID = 1L;
  19.  
  20. /**
  21. * Constructor. Accepts two children to join and the predicate to join them
  22. * on
  23. *
  24. * @param p
  25. * The predicate to use to join the children
  26. * @param child1
  27. * Iterator for the left(outer) relation to join
  28. * @param child2
  29. * Iterator for the right(inner) relation to join
  30. */
  31. public Join(JoinPredicate p, DbIterator child1, DbIterator child2) {
  32. System.out.println("in join constructor");
  33. this._predicate = p;
  34. this._child1 = child1;
  35. this._child2 = child2;
  36. }
  37.  
  38. public JoinPredicate getJoinPredicate() {
  39. return this._predicate;
  40. }
  41.  
  42. /**
  43. * @return
  44. * the field name of join field1. Should be quantified by
  45. * alias or table name.
  46. * */
  47. public String getJoinField1Name() {
  48. return this._child1.getTupleDesc().getFieldName(_predicate.getField1());
  49. }
  50.  
  51. /**
  52. * @return
  53. * the field name of join field2. Should be quantified by
  54. * alias or table name.
  55. * */
  56. public String getJoinField2Name() {
  57. return this._child2.getTupleDesc().getFieldName(_predicate.getField1());
  58. }
  59.  
  60. /**
  61. * @see simpledb.TupleDesc#merge(TupleDesc, TupleDesc) for possible
  62. * implementation logic.
  63. */
  64. public TupleDesc getTupleDesc() {
  65. TupleDesc tupleDescChild1 = this._child1.getTupleDesc();
  66. TupleDesc tupleDescChild2 = this._child2.getTupleDesc();
  67. TupleDesc merged = TupleDesc.merge(tupleDescChild1, tupleDescChild2);
  68. return merged;
  69. }
  70.  
  71. public void open() throws DbException, NoSuchElementException,
  72. TransactionAbortedException {
  73. System.out.println("in join open");
  74. this._child1.open();
  75. this._child2.open();
  76. }
  77.  
  78. public void close() {
  79. this._child1.close();
  80. this._child2.close();
  81. }
  82.  
  83. public void rewind() throws DbException, TransactionAbortedException {
  84. this._child1.rewind();
  85. this._child2.rewind();
  86. }
  87.  
  88. /**
  89. * Returns the next tuple generated by the join, or null if there are no
  90. * more tuples. Logically, this is the next tuple in r1 cross r2 that
  91. * satisfies the join predicate. There are many possible implementations;
  92. * the simplest is a nested loops join.
  93. * <p>
  94. * Note that the tuples returned from this particular implementation of Join
  95. * are simply the concatenation of joining tuples from the left and right
  96. * relation. Therefore, if an equality predicate is used there will be two
  97. * copies of the join attribute in the results. (Removing such duplicate
  98. * columns can be done with an additional projection operator if needed.)
  99. * <p>
  100. * For example, if one tuple is {1,2,3} and the other tuple is {1,5,6},
  101. * joined on equality of the first column, then this returns {1,2,3,1,5,6}.
  102. * f
  103. * @return The next matching tuple.
  104. * @see JoinPredicate#filter
  105. */
  106. protected Tuple fetchNext() throws TransactionAbortedException, DbException {
  107.  
  108. // First time we run the program, we need to get the first tuple
  109. if (needToRead = true) {
  110. // so we check if child1 has a next
  111. if (this._child1.hasNext()) {
  112. outerTuple = this._child1.next();
  113. needToRead = false;
  114. }
  115. }
  116.  
  117. // while there are more tuples in the outer relation
  118. while (hasNext) {
  119.  
  120. // if we need to read this outer tuple
  121. if (needToRead) {
  122. // we call next to get the actual first tuple
  123. outerTuple = this._child1.next();
  124. }
  125.  
  126. // while there are more tuples in the inner relation
  127. while (this._child2.hasNext()) {
  128.  
  129. // grab the next inner tuple
  130. Tuple innerTuple = this._child2.next();
  131.  
  132. // check if the tuples satisfy the join condition
  133. if (this._predicate.filter(outerTuple, innerTuple)) {
  134.  
  135. Tuple newTuple = new Tuple(this.getTupleDesc());
  136.  
  137. int index = 0;
  138. for (int i = 0; i < outerTuple.getTupleDesc().numFields(); i ++) {
  139. newTuple.setField(i, outerTuple.getField(i));
  140. index++;
  141. }
  142. for (int j = 0; j < innerTuple.getTupleDesc().numFields(); j ++ ) {
  143. newTuple.setField(index, innerTuple.getField(j));
  144. index++;
  145. }
  146.  
  147. // we just read the current tuple combination, no need to do it again
  148. needToRead = false;
  149.  
  150. return newTuple;
  151. }
  152. }
  153.  
  154. this._child2.rewind();
  155. hasNext = this._child1.hasNext();
  156. needToRead = true;
  157. }
  158.  
  159. return null;
  160. }
  161.  
  162. @Override
  163. public DbIterator[] getChildren() {
  164. // some code goes here
  165. return null;
  166. }
  167.  
  168. @Override
  169. public void setChildren(DbIterator[] children) {
  170. // some code goes here
  171. }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement