Beispiel #1
0
  /**
   * Inserts tuples read from child into the tableid specified by the constructor. It returns a one
   * field tuple containing the number of inserted records. Inserts should be passed through
   * BufferPool. An instances of BufferPool is available via Database.getBufferPool(). Note that
   * insert DOES NOT need check to see if a particular tuple is a duplicate before inserting it.
   *
   * @return A 1-field tuple containing the number of inserted records, or null if called more than
   *     once.
   * @see Database#getBufferPool
   * @see BufferPool#insertTuple
   */
  protected Tuple fetchNext() throws TransactionAbortedException, DbException {
    Tuple result = new Tuple(td); // use to store the insertion result
    int count = 0; // use to keep track of numbers of tuple insertion

    if (fetchNextNum
        > 0) // meaning this is not the first time calling fetchNext(), and should not return any
             // tuples
    return null;
    else {
      try {
        while (dbIt.hasNext()) {
          try {
            Database.getBufferPool().insertTuple(tranId, tableId, dbIt.next());
          } catch (IOException e) {
            e.printStackTrace();
          }
          count++;
        }

        result.setField(0, new IntField(count));
        fetchNextNum++;
      } catch (DbException e) {
        e.printStackTrace();
      } catch (TransactionAbortedException e) {
        e.printStackTrace();
      }
    }
    return result;
  }
Beispiel #2
0
  public static void main(String[] args) {
    DbClient client = null;
    try {
      client = new DbClient();
      client.connect();

      // client.runUpdate("INSERT INTO DrWeb VALUES(0,null,null,'boogle','worp')");
      // ResultSet rs =
      // client.runQuery("SELECT LASTNAME FROM DrWeb WHERE FIRSTNAME='worp'"
      // );
      ResultSet rs = client.runQuery("SELECT LASTNAME FROM DrWeb");

      while (rs.next()) {
        System.out.println("lastname=" + rs.getString(1));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } catch (DbException e) {
      e.printStackTrace();
    } finally {
      if ((client != null) && client.isConnected()) {
        try {
          client.disconnect();
        } catch (DbException e) {
        }
      }
    }

    System.exit(0);
  }
Beispiel #3
0
 public void rewind() throws DbException, TransactionAbortedException {
   try {
     dbIt.rewind();
   } catch (DbException e) {
     e.printStackTrace();
   } catch (TransactionAbortedException e) {
     e.printStackTrace();
   }
 }
Beispiel #4
0
 public void open() throws DbException, TransactionAbortedException {
   try {
     super.open();
     dbIt.open();
   } catch (DbException e) {
     e.printStackTrace();
   } catch (TransactionAbortedException e) {
     e.printStackTrace();
   }
 }
Beispiel #5
0
 // see DbFile.java for javadocs
 public DbFileIterator iterator(TransactionId tid) {
   try {
     return new HeapFileIterator(tid, this);
   } catch (DbException dbe) {
     dbe.printStackTrace();
   } catch (TransactionAbortedException transe) {
     transe.printStackTrace();
   } catch (NoSuchElementException nosuche) {
     nosuche.printStackTrace();
   }
   return null;
 }
Beispiel #6
0
  /**
   * Create a new TableStats object, that keeps track of statistics on each column of a table
   *
   * @param tableid The table over which to compute statistics
   * @param ioCostPerPage The cost per page of IO. This doesn't differentiate between
   *     sequential-scan IO and disk seeks.
   */
  public TableStats(int tableid, int ioCostPerPage) {
    // For this function, we use the DbFile for the table in question,
    // then scan through its tuples and calculate the values that you
    // to build the histograms.

    // TODO: Fill out the rest of the constructor.
    // Feel free to change anything already written, it's only a guideline

    this.ioCostPerPage = ioCostPerPage;
    DbFile file = Database.getCatalog().getDbFile(tableid);
    tupleDesc = file.getTupleDesc();
    numPages = ((HeapFile) file).numPages();
    numTuples = 0;

    int numFields = tupleDesc.numFields();

    // TODO: what goes here?
    statistics = new ArrayList<Object>();

    for (int i = 0; i < numFields; i++) {
      if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) {
        statistics.add(new IntStatistics(NUM_HIST_BINS));
      } else {
        statistics.add(new StringHistogram(NUM_HIST_BINS));
      }
    }

    final DbFileIterator iter = file.iterator(null);
    try {
      iter.open();

      while (iter.hasNext()) {
        Tuple t = iter.next();
        numTuples++;

        // TODO: and here?
        for (int i = 0; i < numFields; i++) {
          if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) {
            ((IntStatistics) statistics.get(i)).addValue(((IntField) t.getField(i)).getValue());
          } else {
            ((StringHistogram) statistics.get(i))
                .addValue(((StringField) t.getField(i)).getValue());
          }
        }
      }
      iter.close();
    } catch (DbException e) {
      e.printStackTrace();
    } catch (TransactionAbortedException e) {
      e.printStackTrace();
    }
  }