Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected def symmetricHashJoin(leftIter: Iterator[Row], rightIter: Iterator[Row]): Iterator[Row] = {
- new Iterator[Row] {
- /* Remember that Scala does not have any constructors. Whatever code you write here serves as a constructor. */
- // IMPLEMENT ME
- var inner: Iterator[Row] = leftIter
- var outer: Iterator[Row] = rightIter
- var leftHash = new HashMap[Row, ArrayList]
- var rightHash = new HashMap[Row, ArrayList]
- val resultQ = new Queue[Row]
- /**
- * This method returns the next joined tuple.
- *
- * *** THIS MUST BE IMPLEMENTED FOR THE ITERATOR TRAIT ***
- */
- override def next() = {
- // IMPLEMENT ME
- if (!inner.hasNext && outer.hasNext) {
- switchRelations()
- }
- if (!resultQ.isEmpty || findNextMatch()) {
- resultQ.dequeue
- } else {
- var matched = false
- while (matched == false && hasNext()) {
- switchRelations()
- if (!inner.hasNext && outer.hasNext) {
- switchRelations()
- }
- matched = findNextMatch()
- }
- if (!resultQ.isEmpty) {
- resultQ.dequeue
- } else {
- null
- }
- }
- }
- /**
- * This method returns whether or not this iterator has any data left to return.
- *
- * *** THIS MUST BE IMPLEMENTED FOR THE ITERATOR TRAIT ***
- */
- override def hasNext() = {
- // IMPLEMENT ME
- if (!inner.hasNext && !outer.hasNext && resultQ.isEmpty) {
- false
- } else {
- true
- }
- }
- /**
- * This method is intended to switch the inner & outer relations.
- */
- private def switchRelations() = {
- // IMPLEMENT ME
- var temp = inner
- inner = outer
- outer = temp
- }
- /**
- * This method is intended to find the next match and return true if one such match exists.
- *
- * @return whether or not a match was found
- */
- def findNextMatch(): Boolean = {
- // IMPLEMENT ME
- // should fill up the queue with joined results
- var currentRow: Row = inner.next
- var rowKey: Row = null
- var outerMatch: Set[Row] = null
- if (inner == leftIter) {
- rowKey = leftKeyGenerator(currentRow)
- leftHash.addBinding(rowKey, currentRow)
- if (rightHash.contains(rowKey)) {
- outerMatch = rightHash(rowKey)
- }
- } else {
- rowKey = rightKeyGenerator(currentRow)
- rightHash.put(rowKey, currentRow)
- if (leftHash.contains(rowKey)) {
- outerMatch = leftHash(rowKey)
- }
- }
- if (outerMatch != null) {
- for (outerRow <- outerMatch.iterator) {
- resultQ += new JoinedRow(currentRow, outerRow)
- }
- true
- } else {
- false
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement