Advertisement
Jodyone

Untitled

Mar 29th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.64 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 numTables = stmt.numTables();
  37.         int colCTR = 0;
  38.         int refMark = 0;                // refMark is used to set the for loop iterator at the
  39.                                         // correct index to store the next column object
  40.         int ctr = 0;
  41.         int sizeOfcolArry = 0;
  42.         int colAdvance = 0;
  43.         /**
  44.          * loop to get sum of columns from each table
  45.          * in order to dynamicaly size columns array
  46.          */
  47.             for (int i = 0; i < numTables; i++) {  
  48.                 Table t = stmt.getTable(i);
  49.                 t.open();
  50.                 sizeOfcolArry += t.numColumns(); // count number of columns in each table  
  51.             }
  52.            
  53.         this.columns = new Column[sizeOfcolArry];
  54.         this.tableIter = new TableIterator[stmt.numTables()];
  55.         /* *
  56.          *   loop over num tables in the stmt
  57.          */
  58.         for (int i = 0; i < numTables; i++) {
  59.             Table table = new Table(stmt.getTable(i).getName()); // create table object to store table info
  60.             table.open();
  61.             TableIterator iter = new TableIterator(stmt,table,false);
  62.             colCTR = ctr + table.numColumns();                         // count number of columns in each table
  63.             tableIter[i] = iter;                                 // assign iter to tableIter array
  64.             /**
  65.              * loop to store column objects into column array
  66.              */
  67.             ctr = 0;
  68.             for (int j = refMark; j < colCTR; j++) {
  69.                     this.columns[j] = table.getColumn(colAdvance);
  70.                     this.columns[j].setTableIterator(iter);
  71.                     colAdvance++;
  72.                     ctr++;
  73.             }
  74.             colAdvance = 0;
  75.             refMark = ctr ;           // update refMark
  76.  
  77.         }
  78.        
  79.         this.where = stmt.getWhere();
  80.         if (this.where == null)
  81.             this.where = new TrueExpression();   // set were clause
  82.        
  83.         this.numTuples = 0;
  84.     }
  85.    
  86.     /**
  87.      * Closes the iterator, which closes any BDB handles that it is using.
  88.      *
  89.      * @throws DatabaseException if Berkeley DB encounters a problem
  90.      *         while closing a handle
  91.      */
  92.     public void close() throws DatabaseException {
  93.         /* not yet implemented */
  94.          for (int i = 0; i < tableIter.length; i++) {
  95.             tableIter[i].close();
  96.         }
  97.     }
  98.    
  99.     /**
  100.      * Advances the iterator to the next tuple in the relation.  If
  101.      * there is a WHERE clause that limits which tuples should be
  102.      * included in the relation, this method will advance the iterator
  103.      * to the next tuple that satisfies the WHERE clause.  If the
  104.      * iterator is newly created, this method will position it on the first
  105.      * tuple in the relation (that satisfies the WHERE clause).
  106.      *
  107.      * @return true if the iterator was advanced to a new tuple, and false
  108.      *         if there are no more tuples to visit
  109.      * @throws DeadlockException if deadlock occurs while accessing the
  110.      *         underlying BDB database(s)
  111.      * @throws DatabaseException if Berkeley DB encounters another problem
  112.      *         while accessing the underlying database(s)
  113.      */
  114.     public boolean next() throws DeadlockException, DatabaseException {
  115.         /* not yet implemented */
  116.  
  117.                 if (numTuples == 0) {
  118.                    if (!tableIter[0].first())
  119.                        return false;   // R is empty
  120.                 }
  121.  
  122.                 // Advance the table iterators as much as needed to get
  123.                 // to a combination of rows from R and S that satisfies the
  124.                 // WHERE clause.
  125.                 do {
  126.                     if (!tableIter[1].next()) {
  127.                         // We've paired all rows of S with the current row of R,
  128.                         // so reset S to its first row...
  129.                         if (!tableIter[1].first())
  130.                             return false;   // S is empty
  131.                        
  132.                         // ...and advance to R's next row (if any).
  133.                         if (!tableIter[0].next())
  134.                             return false;   // no more rows in R, so we're done
  135.                     }
  136.                    
  137.                 } while(!this.where.isTrue());
  138.  
  139.        
  140.         this.numTuples++;
  141.         return true;
  142.     }
  143.    
  144.     /**
  145.      * Gets the column at the specified index in the relation that
  146.      * this iterator iterates over.  The leftmost column has an index of 0.
  147.      *
  148.      * @return  the column
  149.      * @throws  IndexOutOfBoundsException if the specified index is invalid
  150.      */
  151.     public Column getColumn(int colIndex) {
  152.         /* not yet implemented */
  153.         return this.columns[colIndex];
  154.     }
  155.    
  156.     /**
  157.      * Gets the value of the column at the specified index in the row
  158.      * on which this iterator is currently positioned.  The leftmost
  159.      * column has an index of 0.
  160.      *
  161.      * This method will unmarshall the relevant bytes from the
  162.      * key/data pair and return the corresponding Object -- i.e.,
  163.      * an object of type String for CHAR and VARCHAR values, an object
  164.      * of type Integer for INTEGER values, or an object of type Double
  165.      * for REAL values.
  166.      *
  167.      * @return  the value of the column
  168.      * @throws IllegalStateException if the iterator has not yet been
  169.      *         been positioned on a tuple using first() or next()
  170.      * @throws  IndexOutOfBoundsException if the specified index is invalid
  171.      */
  172.     public Object getColumnVal(int colIndex) {
  173.         /* not yet implemented */
  174.         return columns[colIndex].getTableIterator().getColumnVal(colIndex);
  175.     }
  176.    
  177.     public int numColumns() {
  178.         return this.columns.length;
  179.     }
  180.    
  181.     public int numTuples() {
  182.         return this.numTuples;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement