Advertisement
Guest User

Untitled

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