From 4b0f36a85dd91f239d4e7eaf2deb4285c85c5da0 Mon Sep 17 00:00:00 2001 From: Max O'Cull Date: Fri, 29 Mar 2019 00:37:22 -0400 Subject: [PATCH] Formatting, ready for part 2 --- p3/src/relop/FileScan.java | 182 +++++++++++++++---------------- p3/src/relop/HashJoin.java | 91 ++++++++-------- p3/src/relop/IndexScan.java | 169 +++++++++++++++-------------- p3/src/relop/KeyScan.java | 142 ++++++++++++------------ p3/src/relop/Projection.java | 125 ++++++++++++++-------- p3/src/relop/Selection.java | 143 +++++++++++++++++-------- p3/src/relop/SimpleJoin.java | 202 +++++++++++++++++------------------ p3/src/tests/ROTest.java | 36 +++---- 8 files changed, 587 insertions(+), 503 deletions(-) diff --git a/p3/src/relop/FileScan.java b/p3/src/relop/FileScan.java index 1b51ef9..130e4a0 100644 --- a/p3/src/relop/FileScan.java +++ b/p3/src/relop/FileScan.java @@ -1,91 +1,91 @@ -package relop; - -import global.RID; -import heap.HeapFile; -import heap.HeapScan; - -/** - * Wrapper for heap file scan, the most basic access method. This "iterator" - * version takes schema into consideration and generates real tuples. - */ -public class FileScan extends Iterator { - private HeapFile file = null; // needed for restart(), getFile(); - private HeapScan scan = null; - private RID rid = null; - private boolean isOpen = false; - - /** - * Constructs a file scan, given the schema and heap file. - */ - public FileScan(Schema aSchema, HeapFile aFile) { - this.schema = aSchema; - this.file = aFile; - this.scan = file.openScan(); - this.rid = new RID(); - isOpen = true; - } - - /** - * Gives a one-line explanation of the iterator, repeats the call on any - * child iterators, and increases the indent depth along the way. - */ - public void explain(int depth) { - for(int i = 0; i < depth; i++){ - System.out.printf(" "); - } - System.out.printf("FileScan\n"); - } - - /** - * Restarts the iterator, i.e. as if it were just constructed. - */ - public void restart() { - scan.close(); - scan = file.openScan(); - //rid = new RID(); - } - - /** - * Returns true if the iterator is open; false otherwise. - */ - public boolean isOpen() { - return isOpen; - } - - /** - * Closes the iterator, releasing any resources (i.e. pinned pages). - */ - public void close() { - scan.close(); - isOpen = false; - } - - /** - * Returns true if there are more tuples, false otherwise. - */ - public boolean hasNext() { - return scan.hasNext(); - } - - /** - * Gets the next tuple in the iteration. - * - * @throws IllegalStateException if no more tuples - */ - public Tuple getNext() { - return new Tuple(schema, scan.getNext(rid)); - } - - /** - * Gets the RID of the last tuple returned. - */ - public RID getLastRID() { - return rid; - } - - // getter; added so HashJoin doesn't have to copy the file; - public HeapFile getFile(){ - return file; - } - -} // public class FileScan extends Iterator +package relop; + +import global.RID; +import heap.HeapFile; +import heap.HeapScan; + +/** + * Wrapper for heap file scan, the most basic access method. This "iterator" + * version takes schema into consideration and generates real tuples. + */ +public class FileScan extends Iterator { + private HeapFile file = null; // needed for restart(), getFile(); + private HeapScan scan = null; + private RID rid = null; + private boolean isOpen = false; + + /** + * Constructs a file scan, given the schema and heap file. + */ + public FileScan(Schema aSchema, HeapFile aFile) { + this.schema = aSchema; + this.file = aFile; + this.scan = file.openScan(); + this.rid = new RID(); + isOpen = true; + } + + /** + * Gives a one-line explanation of the iterator, repeats the call on any child + * iterators, and increases the indent depth along the way. + */ + public void explain(int depth) { + for (int i = 0; i < depth; i++) { + System.out.printf(" "); + } + System.out.printf("FileScan\n"); + } + + /** + * Restarts the iterator, i.e. as if it were just constructed. + */ + public void restart() { + scan.close(); + scan = file.openScan(); + // rid = new RID(); + } + + /** + * Returns true if the iterator is open; false otherwise. + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Closes the iterator, releasing any resources (i.e. pinned pages). + */ + public void close() { + scan.close(); + isOpen = false; + } + + /** + * Returns true if there are more tuples, false otherwise. + */ + public boolean hasNext() { + return scan.hasNext(); + } + + /** + * Gets the next tuple in the iteration. + * + * @throws IllegalStateException if no more tuples + */ + public Tuple getNext() { + return new Tuple(schema, scan.getNext(rid)); + } + + /** + * Gets the RID of the last tuple returned. + */ + public RID getLastRID() { + return rid; + } + + // getter; added so HashJoin doesn't have to copy the file; + public HeapFile getFile() { + return file; + } + +} // public class FileScan extends Iterator diff --git a/p3/src/relop/HashJoin.java b/p3/src/relop/HashJoin.java index 9875366..3207ad1 100644 --- a/p3/src/relop/HashJoin.java +++ b/p3/src/relop/HashJoin.java @@ -8,53 +8,58 @@ import global.AttrOperator; import global.AttrType; public class HashJoin extends Iterator { - - public HashJoin(Iterator aIter1, Iterator aIter2, int aJoinCol1, int aJoinCol2){ - throw new UnsupportedOperationException("Not implemented"); - } + private Iterator outer; + private Iterator inner; + private Predicate[] preds; - /** - * Gives a one-line explanation of the iterator, repeats the call on any - * child iterators, and increases the indent depth along the way. - */ - public void explain(int depth) { - throw new UnsupportedOperationException("Not implemented"); - } + public HashJoin(Iterator aIter1, Iterator aIter2, int aJoinCol1, int aJoinCol2) { + aIter1.getSchema().getLength(); + this.outer = aIter1; + this.inner = aIter2; + } - /** - * Restarts the iterator, i.e. as if it were just constructed. - */ - public void restart() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Gives a one-line explanation of the iterator, repeats the call on any child + * iterators, and increases the indent depth along the way. + */ + public void explain(int depth) { + throw new UnsupportedOperationException("Not implemented"); + } - /** - * Returns true if the iterator is open; false otherwise. - */ - public boolean isOpen() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Restarts the iterator, i.e. as if it were just constructed. + */ + public void restart() { + throw new UnsupportedOperationException("Not implemented"); + } - /** - * Closes the iterator, releasing any resources (i.e. pinned pages). - */ - public void close() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Returns true if the iterator is open; false otherwise. + */ + public boolean isOpen() { + throw new UnsupportedOperationException("Not implemented"); + } - /** - * Returns true if there are more tuples, false otherwise. - */ - public boolean hasNext() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Closes the iterator, releasing any resources (i.e. pinned pages). + */ + public void close() { + throw new UnsupportedOperationException("Not implemented"); + } - /** - * Gets the next tuple in the iteration. - * - * @throws IllegalStateException if no more tuples - */ - public Tuple getNext() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Returns true if there are more tuples, false otherwise. + */ + public boolean hasNext() { + throw new UnsupportedOperationException("Not implemented"); + } + + /** + * Gets the next tuple in the iteration. + * + * @throws IllegalStateException if no more tuples + */ + public Tuple getNext() { + throw new UnsupportedOperationException("Not implemented"); + } } // end class HashJoin; diff --git a/p3/src/relop/IndexScan.java b/p3/src/relop/IndexScan.java index 468e05e..02d1037 100644 --- a/p3/src/relop/IndexScan.java +++ b/p3/src/relop/IndexScan.java @@ -10,102 +10,101 @@ import index.HashIndex; * Wrapper for bucket scan, an index access method. */ public class IndexScan extends Iterator { - private HeapFile file = null; - private BucketScan scan = null; - private HashIndex index = null; - private boolean isOpen = false; + private HeapFile file = null; + private BucketScan scan = null; + private HashIndex index = null; + private boolean isOpen = false; + /** + * Constructs an index scan, given the hash index and schema. + */ + public IndexScan(Schema schema, HashIndex index, HeapFile file) { + this.file = file; + this.schema = schema; + this.index = index; + this.scan = this.index.openScan(); + this.isOpen = true; + } - /** - * Constructs an index scan, given the hash index and schema. - */ - public IndexScan(Schema schema, HashIndex index, HeapFile file) { - this.file = file; - this.schema = schema; - this.index = index; - this.scan = this.index.openScan(); - this.isOpen = true; - } + /** + * Gives a one-line explaination of the iterator, repeats the call on any child + * iterators, and increases the indent depth along the way. + */ + public void explain(int depth) { + throw new UnsupportedOperationException("Not implemented"); + } - /** - * Gives a one-line explaination of the iterator, repeats the call on any - * child iterators, and increases the indent depth along the way. - */ - public void explain(int depth) { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Restarts the iterator, i.e. as if it were just constructed. + */ + public void restart() { + if (this.isOpen()) { + scan.close(); + this.isOpen = false; + } - /** - * Restarts the iterator, i.e. as if it were just constructed. - */ - public void restart() { - if (this.isOpen()) { - scan.close(); - this.isOpen = false; - } + this.scan = this.index.openScan(); + this.isOpen = true; + } - this.scan = this.index.openScan(); - this.isOpen = true; - } + /** + * Returns true if the iterator is open; false otherwise. + */ + public boolean isOpen() { + return this.isOpen; + } - /** - * Returns true if the iterator is open; false otherwise. - */ - public boolean isOpen() { - return this.isOpen; - } + /** + * Closes the iterator, releasing any resources (i.e. pinned pages). + */ + public void close() { + if (this.isOpen()) { + this.scan.close(); + this.scan = null; + this.isOpen = false; + } + } - /** - * Closes the iterator, releasing any resources (i.e. pinned pages). - */ - public void close() { - if (this.isOpen()) { - this.scan.close(); - this.scan = null; - this.isOpen = false; - } - } + /** + * Returns true if there are more tuples, false otherwise. + */ + public boolean hasNext() { + if (this.isOpen()) { + return this.scan.hasNext(); + } - /** - * Returns true if there are more tuples, false otherwise. - */ - public boolean hasNext() { - if (this.isOpen()) { - return this.scan.hasNext(); - } + return false; + } - return false; - } + /** + * Gets the next tuple in the iteration. + * + * @throws IllegalStateException if no more tuples + */ + public Tuple getNext() { + if (this.isOpen()) { + RID rid = this.scan.getNext(); + byte[] data = this.file.selectRecord(rid); + Tuple tuple = new Tuple(this.getSchema(), data); + return tuple; + } - /** - * Gets the next tuple in the iteration. - * - * @throws IllegalStateException if no more tuples - */ - public Tuple getNext() { - if (this.isOpen()) { - RID rid = this.scan.getNext(); - byte[] data = this.file.selectRecord(rid); - Tuple tuple = new Tuple(this.getSchema(), data); - return tuple; - } + return null; + } - return null; - } + /** + * Gets the key of the last tuple returned. + */ + public SearchKey getLastKey() { + return this.scan.getLastKey(); + } - /** - * Gets the key of the last tuple returned. - */ - public SearchKey getLastKey() { - return this.scan.getLastKey(); - } - - /** - * Returns the hash value for the bucket containing the next tuple, or maximum - * number of buckets if none. - */ - public int getNextHash() { - return this.scan.getNextHash(); - } + /** + * Returns the hash value for the bucket containing the next tuple, or maximum + * number of buckets if none. + */ + public int getNextHash() { + return this.scan.getNextHash(); + } } // public class IndexScan extends Iterator diff --git a/p3/src/relop/KeyScan.java b/p3/src/relop/KeyScan.java index 6751bc4..5f43e84 100644 --- a/p3/src/relop/KeyScan.java +++ b/p3/src/relop/KeyScan.java @@ -10,86 +10,86 @@ import index.HashScan; * Wrapper for hash scan, an index access method. */ public class KeyScan extends Iterator { - private HeapFile file = null; // needed for restart(), getFile(); - private HashScan scan = null; - private HashIndex index = null; - private SearchKey key = null; - private boolean isOpen; + private HeapFile file = null; // needed for restart(), getFile(); + private HashScan scan = null; + private HashIndex index = null; + private SearchKey key = null; + private boolean isOpen; - /** - * Constructs an index scan, given the hash index and schema. - */ - public KeyScan(Schema aSchema, HashIndex aIndex, SearchKey aKey, HeapFile aFile) { - this.schema = aSchema; - this.index = aIndex; - this.key = aKey; - this.file = aFile; - this.scan = this.index.openScan(this.key); - this.isOpen = true; - } + /** + * Constructs an index scan, given the hash index and schema. + */ + public KeyScan(Schema aSchema, HashIndex aIndex, SearchKey aKey, HeapFile aFile) { + this.schema = aSchema; + this.index = aIndex; + this.key = aKey; + this.file = aFile; + this.scan = this.index.openScan(this.key); + this.isOpen = true; + } - /** - * Gives a one-line explanation of the iterator, repeats the call on any - * child iterators, and increases the indent depth along the way. - */ - public void explain(int depth) { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Gives a one-line explanation of the iterator, repeats the call on any child + * iterators, and increases the indent depth along the way. + */ + public void explain(int depth) { + throw new UnsupportedOperationException("Not implemented"); + } - /** - * Restarts the iterator, i.e. as if it were just constructed. - */ - public void restart() { - this.isOpen = false; // In case of errors maybe? - this.close(); - this.scan = this.index.openScan(this.key); - this.isOpen = true; - } + /** + * Restarts the iterator, i.e. as if it were just constructed. + */ + public void restart() { + this.isOpen = false; // In case of errors maybe? + this.close(); + this.scan = this.index.openScan(this.key); + this.isOpen = true; + } - /** - * Returns true if the iterator is open; false otherwise. - */ - public boolean isOpen() { - return this.isOpen; - } + /** + * Returns true if the iterator is open; false otherwise. + */ + public boolean isOpen() { + return this.isOpen; + } - /** - * Closes the iterator, releasing any resources (i.e. pinned pages). - */ - public void close() { - if (this.isOpen()) { - this.scan.close(); - this.scan = null; - this.isOpen = false; - } - } + /** + * Closes the iterator, releasing any resources (i.e. pinned pages). + */ + public void close() { + if (this.isOpen()) { + this.scan.close(); + this.scan = null; + this.isOpen = false; + } + } - /** - * Returns true if there are more tuples, false otherwise. - */ - public boolean hasNext() { - if (this.isOpen()) { - return this.scan.hasNext(); - } + /** + * Returns true if there are more tuples, false otherwise. + */ + public boolean hasNext() { + if (this.isOpen()) { + return this.scan.hasNext(); + } - return false; - } + return false; + } - /** - * Gets the next tuple in the iteration. - * - * @throws IllegalStateException if no more tuples - */ - public Tuple getNext() { - if (this.isOpen()) { - RID rid = scan.getNext(); - byte[] data = this.file.selectRecord(rid); - Tuple tuple = new Tuple(this.getSchema(), data); + /** + * Gets the next tuple in the iteration. + * + * @throws IllegalStateException if no more tuples + */ + public Tuple getNext() { + if (this.isOpen()) { + RID rid = scan.getNext(); + byte[] data = this.file.selectRecord(rid); + Tuple tuple = new Tuple(this.getSchema(), data); - return tuple; - } + return tuple; + } - return null; - } + return null; + } } // public class KeyScan extends Iterator diff --git a/p3/src/relop/Projection.java b/p3/src/relop/Projection.java index 245de20..5bade98 100644 --- a/p3/src/relop/Projection.java +++ b/p3/src/relop/Projection.java @@ -1,62 +1,93 @@ package relop; - /** * The projection operator extracts columns from a relation; unlike in * relational algebra, this operator does NOT eliminate duplicate tuples. */ public class Projection extends Iterator { - - /** - * Constructs a projection, given the underlying iterator and field numbers. - */ - public Projection(Iterator aIter, Integer... aFields) { - throw new UnsupportedOperationException("Not implemented"); - } + private Iterator iterator; + private Integer[] fields; + private Schema projectingSchema; + private boolean isOpen; - /** - * Gives a one-line explanation of the iterator, repeats the call on any - * child iterators, and increases the indent depth along the way. - */ - public void explain(int depth) { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Constructs a projection, given the underlying iterator and field numbers. + */ + public Projection(Iterator aIter, Integer... aFields) { + this.iterator = aIter; + this.fields = aFields; - /** - * Restarts the iterator, i.e. as if it were just constructed. - */ - public void restart() { - throw new UnsupportedOperationException("Not implemented"); - } + // We need to make a deep copy instead of referencing Iterator's. + // this.projectingSchema = this.iterator.getSchema(); - /** - * Returns true if the iterator is open; false otherwise. - */ - public boolean isOpen() { - throw new UnsupportedOperationException("Not implemented"); - } + Schema originalSchema = this.iterator.getSchema(); + this.projectingSchema = new Schema(fields.length); - /** - * Closes the iterator, releasing any resources (i.e. pinned pages). - */ - public void close() { - throw new UnsupportedOperationException("Not implemented"); - } + for (int i = 0; i < fields.length; i++) { + this.projectingSchema.initField(i, originalSchema.fieldType(fields[i]), + originalSchema.fieldLength(fields[i]), originalSchema.fieldName(fields[i])); + } - /** - * Returns true if there are more tuples, false otherwise. - */ - public boolean hasNext() { - throw new UnsupportedOperationException("Not implemented"); - } + this.isOpen = true; + } - /** - * Gets the next tuple in the iteration. - * - * @throws IllegalStateException if no more tuples - */ - public Tuple getNext() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Gives a one-line explanation of the iterator, repeats the call on any child + * iterators, and increases the indent depth along the way. + */ + public void explain(int depth) { + throw new UnsupportedOperationException("Not implemented"); + } + + /** + * Restarts the iterator, i.e. as if it were just constructed. + */ + public void restart() { + this.isOpen = false; + this.iterator.restart(); + this.isOpen = true; + } + + /** + * Returns true if the iterator is open; false otherwise. + */ + public boolean isOpen() { + return this.isOpen; + } + + /** + * Closes the iterator, releasing any resources (i.e. pinned pages). + */ + public void close() { + this.iterator.close(); + this.isOpen = false; + } + + /** + * Returns true if there are more tuples, false otherwise. + */ + public boolean hasNext() { + return this.iterator.hasNext(); + } + + /** + * Gets the next tuple in the iteration. + * + * @throws IllegalStateException if no more tuples + */ + public Tuple getNext() { + if (this.iterator.hasNext()) { + Tuple original = this.iterator.getNext(); + Tuple projecting = new Tuple(this.projectingSchema); + + for (int i = 0; i < this.fields.length; i++) { + projecting.setField(i, original.getField(this.fields[i])); + } + + return projecting; + } + + return new Tuple(new Schema(1)); + } } // public class Projection extends Iterator diff --git a/p3/src/relop/Selection.java b/p3/src/relop/Selection.java index 7967fb4..61ce7ab 100644 --- a/p3/src/relop/Selection.java +++ b/p3/src/relop/Selection.java @@ -6,57 +6,108 @@ package relop; * connected by OR operators. */ public class Selection extends Iterator { - - /** - * Constructs a selection, given the underlying iterator and predicates. - */ - public Selection(Iterator aIter, Predicate... aPreds) { - throw new UnsupportedOperationException("Not implemented"); - } + private Iterator iterator; + private Predicate[] predicates; + private Tuple currentTuple; + private boolean isOpen; - /** - * Gives a one-line explanation of the iterator, repeats the call on any - * child iterators, and increases the indent depth along the way. - */ - public void explain(int depth) { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Constructs a selection, given the underlying iterator and predicates. + */ + public Selection(Iterator aIter, Predicate... aPreds) { + this.iterator = aIter; + this.predicates = aPreds; + this.schema = this.iterator.schema; + this.isOpen = true; + this.currentTuple = null; + } - /** - * Restarts the iterator, i.e. as if it were just constructed. - */ - public void restart() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Gives a one-line explanation of the iterator, repeats the call on any child + * iterators, and increases the indent depth along the way. + */ + public void explain(int depth) { + throw new UnsupportedOperationException("Not implemented"); + } - /** - * Returns true if the iterator is open; false otherwise. - */ - public boolean isOpen() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Restarts the iterator, i.e. as if it were just constructed. + */ + public void restart() { + this.isOpen = false; + this.iterator.restart(); + this.isOpen = true; + } - /** - * Closes the iterator, releasing any resources (i.e. pinned pages). - */ - public void close() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Returns true if the iterator is open; false otherwise. + */ + public boolean isOpen() { + return this.isOpen; + } - /** - * Returns true if there are more tuples, false otherwise. - */ - public boolean hasNext() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Closes the iterator, releasing any resources (i.e. pinned pages). + */ + public void close() { + this.iterator.close(); + this.isOpen = false; + } - /** - * Gets the next tuple in the iteration. - * - * @throws IllegalStateException if no more tuples - */ - public Tuple getNext() { - throw new UnsupportedOperationException("Not implemented"); - } + /** + * Returns true if there are more tuples, false otherwise. + */ + public boolean hasNext() { + if (this.currentTuple != null) { + return true; + } + + while (this.iterator.hasNext()) { + if (this.predicates.length == 0) { + return true; + } + + this.currentTuple = iterator.getNext(); + + for (int i = 0; i < this.predicates.length; i++) { + if (this.predicates[i].evaluate(this.currentTuple)) { + return true; + } + } + } + + this.currentTuple = null; + return false; + } + + /** + * Gets the next tuple in the iteration. + * + * @throws IllegalStateException if no more tuples + */ + public Tuple getNext() { + if (this.currentTuple != null) { + Tuple tuple = this.currentTuple; + this.currentTuple = null; + return tuple; + } + + while (this.iterator.hasNext()) { + Tuple tuple = this.iterator.getNext(); + if (this.predicates.length == 0) { + return tuple; + } + + this.currentTuple = iterator.getNext(); + + for (int i = 0; i < this.predicates.length; i++) { + if (this.predicates[i].evaluate(this.currentTuple)) { + return tuple; + } + } + } + + return null; + } } // public class Selection extends Iterator diff --git a/p3/src/relop/SimpleJoin.java b/p3/src/relop/SimpleJoin.java index b9abfeb..e788679 100644 --- a/p3/src/relop/SimpleJoin.java +++ b/p3/src/relop/SimpleJoin.java @@ -6,123 +6,121 @@ package relop; */ public class SimpleJoin extends Iterator { - private Iterator outer; - private Iterator inner; - private Predicate[] preds; - - private boolean startJoin = true; - Tuple leftTuple; - - // boolean variable to indicate whether the pre-fetched tuple is consumed or not - private boolean nextTupleIsConsumed; - - // pre-fetched tuple - private Tuple nextTuple; + private Iterator outer; + private Iterator inner; + private Predicate[] preds; - /** - * Constructs a join, given the left and right iterators and join predicates - * (relative to the combined schema). - */ - public SimpleJoin(Iterator left, Iterator right, Predicate... preds) { + private boolean startJoin = true; + Tuple leftTuple; - this.outer = left; - this.inner = right; - this.preds = preds; - this.schema = Schema.join(left.schema, right.schema); - - nextTupleIsConsumed = true; - } + // boolean variable to indicate whether the pre-fetched tuple is consumed or not + private boolean nextTupleIsConsumed; - /** - * Gives a one-line explanation of the iterator, repeats the call on any - * child iterators, and increases the indent depth along the way. - */ - public void explain(int depth) { - - throw new UnsupportedOperationException("Not implemented"); - } + // pre-fetched tuple + private Tuple nextTuple; - /** - * Restarts the iterator, i.e. as if it were just constructed. - */ - public void restart() { + /** + * Constructs a join, given the left and right iterators and join predicates + * (relative to the combined schema). + */ + public SimpleJoin(Iterator left, Iterator right, Predicate... preds) { - outer.restart(); - nextTupleIsConsumed = true; - } + this.outer = left; + this.inner = right; + this.preds = preds; + this.schema = Schema.join(left.schema, right.schema); - /** - * Returns true if the iterator is open; false otherwise. - */ - public boolean isOpen() { - - if (outer.isOpen()) - return true; + nextTupleIsConsumed = true; + } - return false; - } + /** + * Gives a one-line explanation of the iterator, repeats the call on any child + * iterators, and increases the indent depth along the way. + */ + public void explain(int depth) { - /** - * Closes the iterator, releasing any resources (i.e. pinned pages). - */ - public void close() { + throw new UnsupportedOperationException("Not implemented"); + } - outer.close(); - inner.close(); - } + /** + * Restarts the iterator, i.e. as if it were just constructed. + */ + public void restart() { - /** - * Returns true if there are more tuples, false otherwise. - * - */ - public boolean hasNext() { + outer.restart(); + nextTupleIsConsumed = true; + } - if (!nextTupleIsConsumed) - return true; - - if (!outer.hasNext()) - //if(!inner.hasNext() && !outer.hasNext()) // Piazza post 116 - return false; + /** + * Returns true if the iterator is open; false otherwise. + */ + public boolean isOpen() { - Tuple rightTuple; - - if (startJoin) { - leftTuple = outer.getNext(); - startJoin = false; - } + if (outer.isOpen()) + return true; - while (true) { + return false; + } - while (inner.hasNext()) { - - rightTuple = inner.getNext(); + /** + * Closes the iterator, releasing any resources (i.e. pinned pages). + */ + public void close() { - // try to match - nextTuple = Tuple.join(leftTuple, rightTuple, this.schema); - for (int i = 0; i < preds.length; i++) - if (preds[i].evaluate(nextTuple)) { - nextTupleIsConsumed = false; - return true; - } - } + outer.close(); + inner.close(); + } - if (outer.hasNext()) { - leftTuple = outer.getNext(); - inner.restart(); - } - else - return false; - } - } + /** + * Returns true if there are more tuples, false otherwise. + * + */ + public boolean hasNext() { - /** - * Gets the next tuple in the iteration. - * - * @throws IllegalStateException if no more tuples - */ - public Tuple getNext() { - - nextTupleIsConsumed = true; - return nextTuple; - } + if (!nextTupleIsConsumed) + return true; + + if (!outer.hasNext()) + // if(!inner.hasNext() && !outer.hasNext()) // Piazza post 116 + return false; + + Tuple rightTuple; + + if (startJoin) { + leftTuple = outer.getNext(); + startJoin = false; + } + + while (true) { + + while (inner.hasNext()) { + + rightTuple = inner.getNext(); + + // try to match + nextTuple = Tuple.join(leftTuple, rightTuple, this.schema); + for (int i = 0; i < preds.length; i++) + if (preds[i].evaluate(nextTuple)) { + nextTupleIsConsumed = false; + return true; + } + } + + if (outer.hasNext()) { + leftTuple = outer.getNext(); + inner.restart(); + } else + return false; + } + } + + /** + * Gets the next tuple in the iteration. + * @throws IllegalStateException if no more tuples + */ + public Tuple getNext() { + + nextTupleIsConsumed = true; + return nextTuple; + } } diff --git a/p3/src/tests/ROTest.java b/p3/src/tests/ROTest.java index c0fc339..28f9e93 100644 --- a/p3/src/tests/ROTest.java +++ b/p3/src/tests/ROTest.java @@ -50,7 +50,7 @@ public class ROTest extends TestDriver { /** Size of tables in test3. */ private static final int SUPER_SIZE = 2000; - + private Scanner in; /** Drivers schema/table/index */ @@ -70,7 +70,7 @@ public class ROTest extends TestDriver { /** Expected result strings for test cases */ private static HashMap results; - + protected void execute_and_compare(String testDesc, String id, Iterator it) { it.execute(); @@ -91,13 +91,13 @@ public class ROTest extends TestDriver { System.out.println(result.wasSuccessful()); } - + @BeforeClass public static void setupDB() { // create a clean Minibase instance rot = new ROTest(); rot.create_minibase(); - + results = new HashMap(); try @@ -110,14 +110,14 @@ public class ROTest extends TestDriver { line = in.nextLine(); res = line.split("="); results.put(res[0],res[1]); - + } } catch (IOException e) { e.printStackTrace(); System.exit(1); } - + Tuple d_tuple, r_tuple, g_tuple; // initialize schema for the "Drivers" table @@ -127,7 +127,7 @@ public class ROTest extends TestDriver { s_drivers.initField(2, AttrType.STRING, 20, "LastName"); s_drivers.initField(3, AttrType.FLOAT, 4, "Age"); s_drivers.initField(4, AttrType.INTEGER, 4, "NumSeats"); - + //Create and populate "Drivers" relation; f_drivers = new HeapFile("drivers"); idx_drivers = new HashIndex("drivers_idx"); @@ -157,7 +157,7 @@ public class ROTest extends TestDriver { s_rides.initField(1, AttrType.INTEGER, 4, "GroupId"); s_rides.initField(2, AttrType.STRING, 10, "FromDate"); s_rides.initField(3, AttrType.STRING, 10, "ToDate"); - + // Create and populate "Rides" table f_rides = new HeapFile("rides"); idx_rides = new HashIndex("rides_idx"); @@ -191,7 +191,7 @@ public class ROTest extends TestDriver { s_groups = new Schema(2); s_groups.initField(0, AttrType.INTEGER, 4, "GroupId"); s_groups.initField(1, AttrType.STRING, 10, "GroupName"); - + // Create and populate "Groups" table f_groups = new HeapFile("groups"); idx_groups = new HashIndex("groups_idx"); @@ -201,11 +201,11 @@ public class ROTest extends TestDriver { g_tuple.setAllFields(i, "Purdue" + i); idx_groups.insertEntry(new SearchKey(i), f_groups.insertRecord(g_tuple.getData())); } - + System.out.println(); - + } - + @AfterClass public static void tearDownDB() { @@ -246,7 +246,7 @@ public class ROTest extends TestDriver { new Predicate(AttrOperator.GT, AttrType.COLNAME, "age", AttrType.FLOAT, 20F)); execute_and_compare("Selection", "selection", selection); } - + @Test public void testSelectionMultiplePredicates() { Iterator selection_preds = new Selection(new FileScan(s_drivers, f_drivers), @@ -254,14 +254,14 @@ public class ROTest extends TestDriver { new Predicate(AttrOperator.LT, AttrType.COLNAME, "age", AttrType.FLOAT, 19F)); execute_and_compare("Selection Multipled Predicates", "selection_preds", selection_preds); } - + @Test public void testProjection() { //Projection on Drivers: {FirstName, NumSeats} Iterator projection = new Projection(new FileScan(s_drivers, f_drivers), s_drivers.fieldNumber("FirstName"), s_drivers.fieldNumber("NumSeats")); execute_and_compare("Projection", "projection", projection); } - + @Test public void testHashJoin() { //HashJoin on Drivers X Rides on DriverID @@ -269,7 +269,7 @@ public class ROTest extends TestDriver { new FileScan(s_rides, f_rides), 0, 0); execute_and_compare("Hash Join", "hashjoin", hashjoin); } - + @Test public void testSelectionPipelining() { //Test all possible Iterator inputs to Selection @@ -295,7 +295,7 @@ public class ROTest extends TestDriver { new Predicate(AttrOperator.EQ, AttrType.COLNAME, "FirstName", AttrType.STRING, "Walid")); execute_and_compare("Selection - Pipelining Hash Join", "sel_jh", sel_hj); } - + @Test public void testProjectionPipelining() { //Test all possible Iterator inputs to HashJoin @@ -321,7 +321,7 @@ public class ROTest extends TestDriver { 0, 3); execute_and_compare("Projection - Pipelining Hash Join", "proj_hj", proj_hj); } - + @Test public void testHashjoinPipelining() { //Test all possible Iterator inputs to HashJoin