Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.99 KB | None | 0 0
  1.   protected def symmetricHashJoin(leftIter: Iterator[Row], rightIter: Iterator[Row]): Iterator[Row] = {
  2.     new Iterator[Row] {
  3.       /* Remember that Scala does not have any constructors. Whatever code you write here serves as a constructor. */
  4.       // IMPLEMENT ME
  5.       var inner: Iterator[Row] = leftIter
  6.       var outer: Iterator[Row] = rightIter
  7.       var leftHash = new HashMap[Row, ArrayList]
  8.       var rightHash = new HashMap[Row, ArrayList]
  9.       val resultQ = new Queue[Row]
  10.  
  11.       /**
  12.        * This method returns the next joined tuple.
  13.        *
  14.        * *** THIS MUST BE IMPLEMENTED FOR THE ITERATOR TRAIT ***
  15.        */
  16.       override def next() = {
  17.         // IMPLEMENT ME
  18.         if (!inner.hasNext && outer.hasNext) {
  19.           switchRelations()
  20.         }
  21.  
  22.         if (!resultQ.isEmpty || findNextMatch()) {
  23.           resultQ.dequeue
  24.         } else {
  25.           var matched = false
  26.           while (matched == false && hasNext()) {
  27.             switchRelations()
  28.             if (!inner.hasNext && outer.hasNext) {
  29.               switchRelations()
  30.             }
  31.             matched = findNextMatch()
  32.           }
  33.           if (!resultQ.isEmpty) {
  34.             resultQ.dequeue
  35.           } else {
  36.             null
  37.           }
  38.         }
  39.       }
  40.          
  41.       /**
  42.        * This method returns whether or not this iterator has any data left to return.
  43.        *
  44.        * *** THIS MUST BE IMPLEMENTED FOR THE ITERATOR TRAIT ***
  45.        */
  46.       override def hasNext() = {
  47.         // IMPLEMENT ME
  48.         if (!inner.hasNext && !outer.hasNext && resultQ.isEmpty) {
  49.           false
  50.         } else {
  51.           true
  52.         }
  53.       }
  54.  
  55.       /**
  56.        * This method is intended to switch the inner & outer relations.
  57.        */
  58.       private def switchRelations() = {
  59.         // IMPLEMENT ME
  60.         var temp = inner
  61.         inner = outer
  62.         outer = temp
  63.       }
  64.  
  65.       /**
  66.        * This method is intended to find the next match and return true if one such match exists.
  67.        *
  68.        * @return whether or not a match was found
  69.        */
  70.       def findNextMatch(): Boolean = {
  71.         // IMPLEMENT ME
  72.         // should fill up the queue with joined results
  73.         var currentRow: Row = inner.next
  74.         var rowKey: Row = null
  75.         var outerMatch: Set[Row] = null
  76.  
  77.         if (inner == leftIter) {
  78.           rowKey = leftKeyGenerator(currentRow)
  79.           leftHash.addBinding(rowKey, currentRow)
  80.           if (rightHash.contains(rowKey)) {
  81.             outerMatch = rightHash(rowKey)
  82.           }
  83.         } else {
  84.           rowKey = rightKeyGenerator(currentRow)
  85.           rightHash.put(rowKey, currentRow)
  86.           if (leftHash.contains(rowKey)) {
  87.             outerMatch = leftHash(rowKey)
  88.           }
  89.         }
  90.  
  91.         if (outerMatch != null) {
  92.           for (outerRow <- outerMatch.iterator) {
  93.             resultQ += new JoinedRow(currentRow, outerRow)
  94.           }
  95.           true
  96.         } else {
  97.           false
  98.         }
  99.       }
  100.  
  101.     }
  102.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement