Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. package edu.berkeley.cs186.database.query;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.NoSuchElementException;
  10.  
  11. import edu.berkeley.cs186.database.Database;
  12. import edu.berkeley.cs186.database.DatabaseException;
  13. import edu.berkeley.cs186.database.databox.DataBox;
  14. import edu.berkeley.cs186.database.io.Page;
  15. import edu.berkeley.cs186.database.table.Record;
  16. import edu.berkeley.cs186.database.table.RecordIterator;
  17. import edu.berkeley.cs186.database.table.Schema;
  18.  
  19. import javax.xml.crypto.Data;
  20.  
  21. public class SortMergeOperator extends JoinOperator {
  22.  
  23. public SortMergeOperator(QueryOperator leftSource,
  24. QueryOperator rightSource,
  25. String leftColumnName,
  26. String rightColumnName,
  27. Database.Transaction transaction) throws QueryPlanException, DatabaseException {
  28. super(leftSource, rightSource, leftColumnName, rightColumnName, transaction, JoinType.SORTMERGE);
  29.  
  30. }
  31.  
  32. public Iterator<Record> iterator() throws QueryPlanException, DatabaseException {
  33. return new SortMergeOperator.SortMergeIterator();
  34. }
  35.  
  36. /**
  37. * An implementation of Iterator that provides an iterator interface for this operator.
  38. *
  39. * Before proceeding, you should read and understand SNLJOperator.java
  40. * You can find it in the same directory as this file.
  41. *
  42. * Word of advice: try to decompose the problem into distinguishable sub-problems.
  43. * This means you'll probably want to add more methods than those given (Once again,
  44. * SNLJOperator.java might be a useful reference).
  45. *
  46. * Also, see discussion slides for week 7.
  47. */
  48. private class SortMergeIterator extends JoinIterator {
  49. /**
  50. * Some member variables are provided for guidance, but there are many possible solutions.
  51. * You should implement the solution that's best for you, using any member variables you need.
  52. * You're free to use these member variables, but you're not obligated to.
  53. */
  54.  
  55. private String leftTableName;
  56. private String rightTableName;
  57. private RecordIterator leftIterator;
  58. private RecordIterator rightIterator;
  59. private Record leftRecord;
  60. private Record nextRecord;
  61. private Record rightRecord;
  62. private int marked;
  63. private Comparator<Record> comparator=new LR_RecordComparator();
  64. public SortMergeIterator() throws QueryPlanException, DatabaseException {
  65. super();
  66. SortOperator left=new SortOperator(getTransaction(),getLeftTableName(),new LeftRecordComparator());
  67. SortOperator right= new SortOperator(getTransaction(),getLeftTableName(),new RightRecordComparator());
  68. leftIterator=getRecordIterator(left.sort());
  69. rightIterator=getRecordIterator(right.sort());
  70. marked=2;
  71. }
  72.  
  73. /**
  74. * Checks if there are more record(s) to yield
  75. *
  76. * @return true if this iterator has another record to yield, otherwise false
  77. */
  78. public boolean hasNext() {
  79. if(nextRecord != null){return true;}
  80. if(!((leftRecord !=null || leftIterator.hasNext()))){return false;}
  81. Record left,right;
  82. if(leftRecord==null){
  83. left=leftIterator.next();
  84. }
  85. else{
  86. left=leftRecord;
  87. }
  88. if(marked==0){
  89. rightIterator.mark();
  90. marked=1;
  91. }
  92. if(!rightIterator.hasNext()){
  93. if(leftIterator.hasNext()){
  94. leftRecord=leftIterator.next();
  95. }
  96. else{
  97. return false;
  98. }
  99. rightIterator.reset();
  100. return hasNext();
  101. }
  102. else{
  103. right=rightIterator.next();
  104. }
  105. if(marked==2){
  106. marked=1;
  107. rightIterator.mark();
  108. }
  109. if(leftRecord==null){
  110. leftRecord=left;
  111. }
  112. while (comparator.compare(left,right)!=0){
  113. if(comparator.compare(left,right)>=0){
  114. if(rightIterator.hasNext()){
  115. right=rightIterator.next();
  116. marked=0;
  117. }
  118. else{return false;}
  119. }
  120. else{
  121. if(leftIterator.hasNext()){
  122. left=leftIterator.next();
  123. leftRecord=left;
  124. rightIterator.reset();
  125. right=rightIterator.next();
  126. }
  127. else{return false;}
  128. }
  129. }
  130. ArrayList<DataBox> leftvals=new ArrayList<>(left.getValues());
  131. ArrayList<DataBox> rightvals=new ArrayList<>(right.getValues());
  132. leftvals.addAll(rightvals);
  133. nextRecord=new Record(leftvals);
  134. return true;
  135. }
  136.  
  137.  
  138. /**
  139. * Yields the next record of this iterator.
  140. *
  141. * @return the next Record
  142. * @throws NoSuchElementException if there are no more Records to yield
  143. */
  144. public Record next() {
  145. if (nextRecord != null) {
  146. Record ret = nextRecord;
  147. nextRecord = null;
  148. return ret;
  149. }
  150. throw new NoSuchElementException();
  151. }
  152.  
  153. public void remove() {
  154. throw new UnsupportedOperationException();
  155. }
  156.  
  157.  
  158. private class LeftRecordComparator implements Comparator<Record> {
  159. public int compare(Record o1, Record o2) {
  160. return o1.getValues().get(SortMergeOperator.this.getLeftColumnIndex()).compareTo(
  161. o2.getValues().get(SortMergeOperator.this.getLeftColumnIndex()));
  162. }
  163. }
  164.  
  165. private class RightRecordComparator implements Comparator<Record> {
  166. public int compare(Record o1, Record o2) {
  167. return o1.getValues().get(SortMergeOperator.this.getRightColumnIndex()).compareTo(
  168. o2.getValues().get(SortMergeOperator.this.getRightColumnIndex()));
  169. }
  170. }
  171.  
  172. /**
  173. * Left-Right Record comparator
  174. * o1 : leftRecord
  175. * o2: rightRecord
  176. */
  177. private class LR_RecordComparator implements Comparator<Record> {
  178. public int compare(Record o1, Record o2) {
  179. return o1.getValues().get(SortMergeOperator.this.getLeftColumnIndex()).compareTo(
  180. o2.getValues().get(SortMergeOperator.this.getRightColumnIndex()));
  181. }
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement