Exemple #1
0
  /**
   * Writes this messages body to the given output stream. This method may only be called once
   * during the lifetime of this message.
   *
   * @param os an output stream
   * @exception IOException if there is an I/O error
   */
  public void write(OutputStream os) throws IOException {
    Assert.isTrue(!inputRead);
    Assert.isTrue(!hasInputStream);

    int bytesRead = 0;
    int totalBytesRead = 0;
    byte[] buffer = bufferPool.getBuffer();
    long contentLength = getContentLength();

    try {
      while (bytesRead != -1 && (contentLength == -1 || contentLength > totalBytesRead)) {
        if (contentLength == -1) {
          bytesRead = is.read(buffer);
        } else {
          bytesRead =
              is.read(buffer, 0, (int) Math.min(buffer.length, contentLength - totalBytesRead));
        }
        if (bytesRead == -1) {
          if (contentLength >= 0) {
            throw new IOException(Policy.bind("exception.unexpectedEndStream")); // $NON-NLS-1$
          }
        } else {
          totalBytesRead += bytesRead;
          os.write(buffer, 0, bytesRead);
        }
      }
    } finally {
      bufferPool.putBuffer(buffer);
      inputRead = true;
    }
  }
  // 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;
  }
 // 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;
 }
Exemple #4
0
 public Tuple getNext() throws NoSuchElementException, TransactionAbortedException {
   try {
     Tuple tuple = new Tuple(td);
     for (int i = 0; i < td.numFields(); i++) {
       IntField intf = IntField.createIntField(in.readInt());
       tuple.setField(i, intf);
     }
     return tuple;
   } catch (EOFException eof) {
     throw new NoSuchElementException(eof.getMessage());
   } catch (Exception e) {
     e.printStackTrace();
     BufferPool.Instance().abortTransaction(tid);
     closeConnection();
     throw new TransactionAbortedException(e);
   }
 }