Advertisement
Jodyone

problem10

Mar 28th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 7.02 KB | None | 0 0
  1. /*
  2.  * ProjectionIterator.java
  3.  *
  4.  * DBMS Implementation
  5.  */
  6.  
  7. import java.io.FileNotFoundException;
  8.  
  9. import com.sleepycat.bind.tuple.TupleOutput;
  10. import com.sleepycat.db.*;
  11.  
  12. /**
  13.  * A class that serves as an iterator over some or all of the tuples
  14.  * in a relation that results from performing a projection on an
  15.  * another relation.  It is used to implement all SELECT statements
  16.  * except ones that specify SELECT *
  17.  */
  18. public class ProjectionIterator extends RelationIterator {
  19.     private Column[] columns;
  20.     private RelationIterator subrel;
  21.     private boolean checkDistinct;
  22.     private int numTuples;
  23.     ConditionalExpression where ;
  24.  
  25.     /**
  26.      * Constructs a ProjectionIterator object for the relation formed by
  27.      * applying a projection to the relation over which the specified
  28.      * subrel iterator will iterate.  This other relation is referred to
  29.      * as the "subrelation" of this iterator.
  30.      *
  31.      * @param  stmt    the SQL SELECT statement that specifies the projection
  32.      * @param  subrel  the subrelation
  33.      * @throws DatabaseException
  34.      * @throws IllegalStateException if subrel is null
  35.      */
  36.     public ProjectionIterator(SelectStatement stmt, RelationIterator subrel){
  37.  
  38.         SelectStatement st = stmt;
  39.        
  40.         this.where = st.getWhere();
  41.         if (this.where == null)
  42.             this.where = new TrueExpression();
  43.        
  44.         columns = new Column[stmt.numColumns()];
  45.         if (subrel!= null)
  46.             this.subrel = subrel;
  47.         else
  48.             throw new IllegalStateException();
  49.  
  50.         for (int i = 0; i < stmt.numColumns(); i++) {
  51.             columns[i] = stmt.getColumn(i);
  52.         }
  53.         if(stmt.distinctSpecified())
  54.             this.checkDistinct = true;
  55.         else
  56.             this.checkDistinct = false;
  57.         this.numTuples = 0;
  58.        
  59.     }
  60.    
  61.     /**
  62.      * Closes the iterator, which closes any BDB handles that it is using.
  63.      *
  64.      * @throws DatabaseException if Berkeley DB encounters a problem
  65.      *         while closing a handle
  66.      */
  67.     public void close() throws DatabaseException {
  68.        
  69.         if (this.subrel != null)
  70.             this.subrel.close();
  71.         this.subrel = null;
  72.     }
  73.    
  74.     /**
  75.      * Advances the iterator to the next tuple in the relation.  If
  76.      * there is a WHERE clause that limits which tuples should be
  77.      * included in the relation, this method will advance the iterator
  78.      * to the next tuple that satisfies the WHERE clause.  If the
  79.      * iterator is newly created, this method will position it on the first
  80.      * tuple in the relation (that satisfies the WHERE clause).
  81.      *
  82.      * @return true if the iterator was advanced to a new tuple, and false
  83.      *         if there are no more tuples to visit
  84.      * @throws DeadlockException if deadlock occurs while accessing the
  85.      *         underlying BDB database(s)
  86.      * @throws DatabaseException if Berkeley DB encounters another problem
  87.      *         while accessing the underlying database(s)
  88.      */
  89.     public boolean next() throws DatabaseException, DeadlockException {
  90.         /* not yet implemented */
  91.        
  92.         TupleOutput keyBuffer = new TupleOutput(); 
  93.         DatabaseEntry key = null;
  94.         DatabaseEntry data = null;
  95.        
  96.         if (this.subrel == null)
  97.             throw new IllegalStateException("this iterator has been closed");
  98.        
  99.         if (this.checkDistinct){
  100.             OperationStatus ret = OperationStatus.SUCCESS;
  101.             Table tempTable = new Table("tempTable");
  102.             // Create the BDB database for the table.
  103.             DatabaseConfig config = new DatabaseConfig();
  104.             config.setType(DatabaseType.RECNO);
  105.             config.setAllowCreate(true);
  106.               try {
  107.                 tempTable.setDB(DBMS.getEnv().openDatabase(null, null,
  108.                                                          null, config));
  109.             } catch (FileNotFoundException e) {
  110.                 // TODO Auto-generated catch block
  111.                 e.printStackTrace();
  112.             }
  113.               Object val = null;
  114.              if(columns.length == 1){    
  115.                  val = columns[0].getTableIterator().getColumnVal(0);
  116.              }
  117.              else {
  118.                 for (int i = 0; i < columns.length; i++) {
  119.                     val = columns[i].getTableIterator().getColumnVal(i);
  120.                 }
  121.              }
  122.              if (val instanceof Integer)
  123.                 keyBuffer.writeInt((Integer)val);
  124.              else if (val instanceof Double)
  125.                  keyBuffer.writeDouble((Double)val);
  126.              else {
  127.                   String newVal = (String)val ;
  128.                   keyBuffer.writeBytes(newVal);
  129.              }
  130.  
  131.               if (keyBuffer != null){  
  132.                  key = new DatabaseEntry(keyBuffer.getBufferBytes(), 0,
  133.                                                     keyBuffer.getBufferLength());
  134.               }
  135.               data = new DatabaseEntry();
  136.               ret = tempTable.getDB().append(null, key, data);
  137.               if (ret == OperationStatus.KEYEXIST){
  138.                   subrel.next();
  139.                   return true;
  140.               }
  141.               else {
  142.                     do {
  143.                          if(!this.subrel.next())
  144.                              return false;
  145.                          this.numTuples++;
  146.              
  147.                     } while (!this.where.isTrue());  
  148.                 return true;
  149.               }
  150.                  
  151.              
  152.         }
  153.         else{
  154.      
  155.  
  156.             if (this.numTuples() == 0 && this.where.isTrue()){
  157.                     this.subrel.next();
  158.                     this.numTuples++;
  159.             }
  160.            
  161.             else {
  162.                 do {
  163.                      if(!this.subrel.next())
  164.                          return false;
  165.                      this.numTuples++;
  166.          
  167.                 } while (!this.where.isTrue());  
  168.             }
  169.          }
  170.  
  171.         return true;
  172.     }
  173.    
  174.     /**
  175.      * Gets the column at the specified index in the relation that
  176.      * this iterator iterates over.  The leftmost column has an index of 0.
  177.      *
  178.      * @return  the column
  179.      * @throws  IndexOutOfBoundsException if the specified index is invalid
  180.      */
  181.     public Column getColumn(int colIndex) {
  182.            
  183.         return this.columns[colIndex];
  184.     }
  185.    
  186.     /**
  187.      * Gets the value of the column at the specified index in the row
  188.      * on which this iterator is currently positioned.  The leftmost
  189.      * column has an index of 0.
  190.      *
  191.      * This method will unmarshall the relevant bytes from the
  192.      * key/data pair and return the corresponding Object -- i.e.,
  193.      * an object of type String for CHAR and VARCHAR values, an object
  194.      * of type Integer for INTEGER values, or an object of type Double
  195.      * for REAL values.
  196.      *
  197.      * @return  the value of the column
  198.      * @throws IllegalStateException if the iterator has not yet been
  199.      *         been positioned on a tuple using first() or next()
  200.      * @throws  IndexOutOfBoundsException if the specified index is invalid
  201.      */
  202.     public Object getColumnVal(int colIndex) {
  203.        
  204.         return columns[colIndex].getTableIterator().getColumnVal(colIndex);
  205.     }
  206.    
  207.     public int numColumns() {
  208.         return this.columns.length;
  209.     }
  210.    
  211.     public int numTuples() {
  212.         return this.numTuples;
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement