Esempio n. 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;
  }
Esempio n. 2
0
  // see DbFile.java for javadocs
  public ArrayList<Page> insertTuple(TransactionId tid, Tuple t)
      throws DbException, IOException, TransactionAbortedException {
    // some code goes here
    BufferPool bp = Database.getBufferPool();
    int id = getId(), i, slots;
    ArrayList<Page> retlist = new ArrayList<Page>();
    PageId pid = null;
    HeapPage p = null;
    for (i = 0; i < numPages(); i++) {
      pid = new HeapPageId(id, i);
      p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE);
      slots = p.getNumEmptySlots();
      if (slots > 0) {
        p.insertTuple(t);
        retlist.add(p);
        return retlist;
      }
    }

    // create new page and add tuple to it
    pid = new HeapPageId(id, i);
    raf.setLength(raf.length() + BufferPool.PAGE_SIZE);
    p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE);
    p.insertTuple(t);
    retlist.add(p);
    return retlist;
  }
Esempio n. 3
0
 private Page readPage(int pageNumber)
     throws DbException, TransactionAbortedException, IOException {
   // File == table because we do one file per table
   //	System.out.println("readpage:"+_file.id()+" page:"+pageNumber);
   int tableId = _file.id();
   int pageId = pageNumber;
   //	System.out.println("Page is now "+pageNumber);
   HeapPageId pid = new HeapPageId(tableId, pageId);
   return Database.getBufferPool().getPage(_transactionId, pid, Permissions.READ_ONLY);
 }
Esempio n. 4
0
 // see DbFile.java for javadocs
 public Page deleteTuple(TransactionId tid, Tuple t)
     throws DbException, TransactionAbortedException {
   // some code goes here
   BufferPool bp = Database.getBufferPool();
   RecordId rid = t.getRecordId();
   if (rid == null) {
     throw new DbException("Tuple is not a member of this file");
   }
   HeapPage p = (HeapPage) bp.getPage(tid, rid.getPageId(), Permissions.READ_WRITE);
   p.deleteTuple(t);
   return p;
 }
Esempio n. 5
0
 /**
  * Inserts tuples read from child into the relation with the tableid specified by the constructor.
  * It returns a one field tuple containing the number of inserted records (even if there are 0!).
  * Insertions should be passed through BufferPool.insertTuple() with the TransactionId from the
  * constructor. An instance of BufferPool is available via Database.getBufferPool(). Note that
  * insert DOES NOT need to check to see if a particular tuple is a duplicate before inserting it.
  *
  * <p>This operator should keep track if its fetchNext() has already been called, returning null
  * if called multiple times.
  *
  * @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 {
   if (!hasFetched) {
     this.hasFetched = true;
     int numInserts = 0;
     while (this.child.hasNext()) {
       Tuple t = this.child.next();
       try {
         Database.getBufferPool().insertTuple(this.tid, this.tableid, t);
         numInserts += 1;
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
     Type[] fields = new Type[1];
     fields[0] = Type.INT_TYPE;
     TupleDesc td = new TupleDesc(fields);
     Tuple newTuple = new Tuple(td);
     newTuple.setField(0, new IntField(numInserts));
     return newTuple;
   } else {
     return null;
   }
 }
Esempio n. 6
0
 @After
 public void tearDown() throws Exception {
   Database.getBufferPool().transactionComplete(tid);
 }