Advertisement
Jodyone

ps3

Apr 4th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.51 KB | None | 0 0
  1. /*
  2.  * CrossIterator.java
  3.  *
  4.  * DBMS Implementation
  5.  */
  6.  
  7. import java.io.*;
  8. import com.sleepycat.db.*;
  9.  
  10. /**
  11.  * A class that serves as an iterator over some or all of the tuples
  12.  * in the cross product (i.e., Cartesian product) of two or more
  13.  * tables.
  14.  */
  15. public class CrossIterator extends RelationIterator {
  16.     private TableIterator[] tableIter;
  17.     private Column[] columns;
  18.     private ConditionalExpression where;
  19.     private int numTuples;
  20.    
  21.  
  22.     /**
  23.      * Constructs a CrossIterator object for the subset of the cross
  24.      * product specified by the given SQLStatement.  If the
  25.      * SQLStatement has a WHERE clause, the iterator will only visit
  26.      * rows that satisfy the WHERE clause.
  27.      *
  28.      * @param  stmt  the SQL statement that specifies the cross product
  29.      * @throws IllegalStateException if one of the Table objects in stmt
  30.      *         has not already been opened
  31.      * @throws DatabaseException if Berkeley DB encounters a problem
  32.      *         while accessing one of the underlying database(s)
  33.      */
  34.     public CrossIterator(SQLStatement stmt) throws DatabaseException {
  35.         /* not yet implemented */
  36.         int sizeOfcolArry = 0;
  37.         int colIndex = 0;
  38.  
  39.         /**
  40.          * loop to get sum of columns from each table
  41.          * in order to dynamicaly size columns array
  42.          */
  43.         for (int i = 0; i < stmt.numTables(); i++) {   
  44.              Table t = new Table(stmt.getTable(i).getName());
  45.              t.open();
  46.             sizeOfcolArry += t.numColumns();                     // count number of columns in each table  
  47.            
  48.         }
  49.            
  50.         this.columns = new Column[sizeOfcolArry];
  51.         this.tableIter = new TableIterator[stmt.numTables()];
  52.         /* *
  53.          *   loop over num tables in the stmt
  54.          */
  55.         for (int i = 0; i < stmt.numTables(); i++) {
  56.             stmt.getTable(i).open();
  57.             TableIterator iter = new TableIterator(stmt,stmt.getTable(i),false);
  58.              
  59.             tableIter[i] = iter;                                 // assign iter to tableIter array
  60.             /**
  61.              * loop to store column objects into column array
  62.              */
  63.             for (int j = 0; j < stmt.getTable(i).numColumns(); j++) {
  64.                 this.columns[colIndex] = stmt.getTable(i).getColumn(j);
  65.                 this.columns[colIndex].setTableIterator(iter);
  66.                 colIndex++;
  67.             }
  68.         }
  69.        
  70.         this.where = stmt.getWhere();
  71.         if (this.where == null)
  72.             this.where = new TrueExpression();   // set were clause
  73.        
  74.         this.numTuples = 0;
  75.     }
  76.    
  77.     /**
  78.      * Closes the iterator, which closes any BDB handles that it is using.
  79.      *
  80.      * @throws DatabaseException if Berkeley DB encounters a problem
  81.      *         while closing a handle
  82.      */
  83.     public void close() throws DatabaseException {
  84.         /* not yet implemented */
  85.          for (int i = 0; i < tableIter.length; i++) {
  86.             tableIter[i].close();
  87.         }
  88.     }
  89.    
  90.     /**
  91.      * Advances the iterator to the next tuple in the relation.  If
  92.      * there is a WHERE clause that limits which tuples should be
  93.      * included in the relation, this method will advance the iterator
  94.      * to the next tuple that satisfies the WHERE clause.  If the
  95.      * iterator is newly created, this method will position it on the first
  96.      * tuple in the relation (that satisfies the WHERE clause).
  97.      *
  98.      * @return true if the iterator was advanced to a new tuple, and false
  99.      *         if there are no more tuples to visit
  100.      * @throws DeadlockException if deadlock occurs while accessing the
  101.      *         underlying BDB database(s)
  102.      * @throws DatabaseException if Berkeley DB encounters another problem
  103.      *         while accessing the underlying database(s)
  104.      */
  105.     public boolean next() throws DeadlockException, DatabaseException {
  106.         /* not yet implemented */
  107.                
  108.                 if (numTuples == 0) {
  109.                    if (!tableIter[0].first())
  110.                        return false;   // R is empty      
  111.                 }
  112.  
  113.                 // Advance the table iterators as much as needed to get
  114.                 // to a combination of rows from R and S that satisfies the
  115.                 // WHERE clause.
  116.                 do {
  117.                        
  118.                        
  119.                     if (!tableIter[1].next()) {
  120.                        
  121.                         // We've paired all rows of S with the current row of R,
  122.                         // so reset S to its first row...
  123.                         if (!tableIter[1].first()){
  124.                             return false;   // S is empty
  125.                         }
  126.                         // ...and advance to R's next row (if any).
  127.                         if (!tableIter[0].next())
  128.                             return false;   // no more rows in R, so we're done
  129.                     }
  130.                    
  131.                  } while(!this.where.isTrue());
  132.        
  133.         this.numTuples++;
  134.         return true;
  135.     }
  136.    
  137.     /**
  138.      * Gets the column at the specified index in the relation that
  139.      * this iterator iterates over.  The leftmost column has an index of 0.
  140.      *
  141.      * @return  the column
  142.      * @throws  IndexOutOfBoundsException if the specified index is invalid
  143.      */
  144.     public Column getColumn(int colIndex) {
  145.         /* not yet implemented */
  146.         return this.columns[colIndex];
  147.     }
  148.    
  149.     /**
  150.      * Gets the value of the column at the specified index in the row
  151.      * on which this iterator is currently positioned.  The leftmost
  152.      * column has an index of 0.
  153.      *
  154.      * This method will unmarshall the relevant bytes from the
  155.      * key/data pair and return the corresponding Object -- i.e.,
  156.      * an object of type String for CHAR and VARCHAR values, an object
  157.      * of type Integer for INTEGER values, or an object of type Double
  158.      * for REAL values.
  159.      *
  160.      * @return  the value of the column
  161.      * @throws IllegalStateException if the iterator has not yet been
  162.      *         been positioned on a tuple using first() or next()
  163.      * @throws  IndexOutOfBoundsException if the specified index is invalid
  164.      */
  165.     public Object getColumnVal(int colIndex) {
  166.         /* not yet implemented */
  167.         return columns[colIndex].getTableIterator().getColumnVal(colIndex);
  168.     }
  169.    
  170.     public int numColumns() {
  171.         return this.columns.length;
  172.     }
  173.    
  174.     public int numTuples() {
  175.         return this.numTuples;
  176.     }
  177. }
  178.  
  179.  
  180.  
  181.  
  182. output
  183.  
  184.  | name                 | enrollment  | course               | location   |
  185. ----------------------------------------------------------------------------
  186.  | CS 165               | 31         Index: 2, Size: 2.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement