Exemplo n.º 1
0
/**
 * First, an emptry file is allocated in the specified file. 
 * Then allocBuf() is called to obtain a buffer pool frame.
 * Next, an entry is inserted into the hash table and Set() is invoked on the frame to set it up properly.
 *
 * @param file the pointer to the file 
 * @param PageNo the index of the page 
 * @param page the pointer to the frame which containing the page 
 * @return status the status of the function call 
 **/ 	
const Status BufMgr::allocPage(File* file, int& pageNo, Page*& page) 
{
 
  Status status;
  int frameNo;

  // return the page number of the newly allocated page to the caller via the pageNo 
  status = file->allocatePage(pageNo);
  if(status != OK)
    return status;
  
  status = allocBuf(frameNo);
  if (status != OK) 
    return status;

  status = hashTable->insert(file, pageNo, frameNo);
  if (status != OK)    
    return status;
  
  bufTable[frameNo].Set(file, pageNo);
  bufTable[frameNo].frameNo = frameNo;

  // return a pointer to the buffer frame allocated for the page via the page parameter
  page = &(bufPool[frameNo]);
  
  return OK;
}
Exemplo n.º 2
0
const Status BufMgr::allocPage(File* file, int& pageNo, Page*& page) 
{
	// Your solution goes here
		bufStats.accesses ++;
	unsigned int frameno;
   	if(allocBuf(frameno) == BUFFEREXCEEDED){
       		return BUFFEREXCEEDED;
   	}

	//cout << frameno << " " << bufTable[frameno].pinCnt << " " << bufTable[frameno].pageNo << endl;
		Status isgood = file->allocatePage(pageNo);
   	if(isgood == UNIXERR){
	//cout << "THERE!" << endl;
       		return UNIXERR;
   	}else if(isgood == OK){
   		bufStats.diskwrites ++;
   	}
   	bufTable[frameno].Set(file, pageNo);
	//cout << "WHERE!" << endl;
   	if(bufMap.insert(file, pageNo, frameno) == BUFMAPERROR){
       		return BUFMAPERROR;
   	}

	page = &bufPool[frameno];//Do I need to dereference this or something else? 
		//cout << " PAGE "  << page << endl;
   	return OK;
}
Exemplo n.º 3
0
/**
 * Reads a page from either the buffer pool or disk.  
 * First check whether the page is already in the bufPool by invoking lookup() method.
 * If it is not in the buffer pool, then read the page from disk into the buffer pool frame.
 *
 * @param file the pointer to the file 
 * @param PageNo the index of the page 
 * @param page the pointer to the frame which containing the page 
 * @return status the status of the function call 
 **/ 	
const Status BufMgr::readPage(File* file, const int PageNo, Page*& page)
{
  Status status;
  int frameNo;
  
  if (hashTable->lookup(file,PageNo,frameNo) != HASHNOTFOUND) {
    // if page is in the bufPool
    bufTable[frameNo].refbit = true;
    bufTable[frameNo].pinCnt++;
  } else {
    
    status = allocBuf(frameNo);
    if(status != OK) 
      return status;
    
    // read the page from disk into the buffer pool frame
    status = file->readPage(PageNo,&bufPool[frameNo]);
    if(status != OK) 
      return status;

    // insert the page into the hashTable
    status = hashTable->insert(file,PageNo,frameNo);
    if(status != OK)
      return status;
   
    bufTable[frameNo].Set(file,PageNo);  
    bufTable[frameNo].frameNo = frameNo;
  }

  // return a pointer to the frame containing the page via the page parameter
  page = &bufPool[frameNo];
  return OK;
}
Exemplo n.º 4
0
/**
* This function allocate an empty page in the specified file by invoking the file->allocatePage() method.
* This method will return the page number of the newly allocated page.
* Then allocBuf() is called to obtain a buffer pool frame.
* Next, an entry is inserted into the hash table and Set() is invoked on the frame to set it up.
* @param file-a pointer to the file handle
* @param pageNo-the number of the page to be read
* @param page-a pointer to the page inside the frame
* @return Status-status information from function
**/
const Status BufMgr::allocPage(File* file, int& pageNo, Page*& page) {

    Status rc;
    int frameNo;
    //I/O call to allocate a page
    rc = file->allocatePage(pageNo);
    if (rc != OK) {
        return rc;
    }
    // allocate a buffer frame for the new page
    rc = allocBuf(frameNo);
    if (rc != OK) {
        return rc;
    }
    //map new buffer addition in the hashTable
    rc = hashTable->insert(file, pageNo, frameNo);
    if (rc != OK) {
        return rc;
    }
    //set page from disc in buffer frame
    bufTable[frameNo].Set(file, pageNo);
    bufTable[frameNo].frameNo = frameNo;
    page = &bufPool[frameNo]; //pointer to the page to be read

    return OK;
}// end function allocPage
Exemplo n.º 5
0
/*
 * Decrements the pinCnt of the frame containing (file, PageNo) 
 *
 * Throws HashNotFoundException when page is not in the buffer pool, 
 *        on the hashtable to get a frame number. 
 * params: File* file, const PageId pageNo, Page*& page
 */
void BufMgr::readPage(File* file, const PageId pageNo, Page*& page)
{
  FrameId frame = -1;
  // look up hashtable
  try{
    hashTable->lookup(file, pageNo, frame);
    // page is within the hashtable
    
    BufDesc* bufDesc = &(bufDescTable[frame]);
    bufDesc->refbit = true;
    bufDesc->pinCnt = bufDesc->pinCnt + 1;
    page = &(bufPool[frame]);
  } 
  // Page is not in the buffer pool
  catch(HashNotFoundException) {
	// calling this first to see if the page is valid
	file->readPage(pageNo);
	
	// Allocate frame
	FrameId frame = -1;
	allocBuf(frame);
	page = &(bufPool[frame]);
	*page = file->readPage(pageNo);
	
	// insert into hash table
	hashTable->insert(file, pageNo, frame);
	
	// call Set()
    BufDesc* bufDesc = &(bufDescTable[frame]);
    bufDesc->Set(file, pageNo);
  }
}
Exemplo n.º 6
0
const Status BufMgr::readPage(File* file, const int PageNo, Page*& page)
{
	Status status = OK;
	int frameNo = 0;
	status = hashTable->lookup(file, PageNo, frameNo);
	if (status == OK)	//	Page is in the buffer pool
	{
		BufDesc * tmpbuf = &(bufTable[frameNo]);
		tmpbuf->pinCnt++;
	}
	else		//	Page is not in the buffer pool
	{
		status	= allocBuf(frameNo);
		if (status != OK)
		{
		  return status;
		}
		page	= &(bufPool[frameNo]);
		status	= file->readPage(PageNo, page);
		if (status != OK)
		{
		  return status;
		}
		bufTable[frameNo].Set(file, PageNo);
		status = hashTable->insert(file, PageNo, frameNo);
	}
	page = &(bufPool[frameNo]);
	return status;
}
Exemplo n.º 7
0
EncodeOutputCharStream::EncodeOutputCharStream(OutputByteStream *byteStream,
					       Encoder *encoder)
: buf_(0),
  byteStream_(byteStream),
  escaper_(0),
  encoder_(encoder)
{
  allocBuf(0);
}
Exemplo n.º 8
0
void UnicodeEncoder::output(const Char *s, size_t n, streambuf *sb)
{
  if (sizeof(Char) == 2) {
    sb->sputn((char *)s, n*2);
    return;
  }
  allocBuf(n);
  for (size_t i = 0; i < n; i++)
    buf_[i] = s[i] & 0xffff;
  sb->sputn((char *)buf_, n*2);
}
Exemplo n.º 9
0
/**
* Allocates a new, empty page in the file and returns the Page object.
* The newly allocated page is also assigned a frame in the buffer pool.
*
* @param file   	File object
* @param PageNo  Page number. The number assigned to the page in the file is returned via this reference.
* @param page  	Reference to page pointer. The newly allocated in-memory Page object is returned via this reference.
*/
void BufMgr::allocPage(File* file, PageId &pageNo, Page*& page) 
{
	Page newPage = file->allocatePage();
	allocBuf(fid);
	bufPool[fid] = newPage;

	page = &bufPool[fid];
	pageNo = bufPool[fid].page_number();

	hashTable->insert(file, pageNo, fid);
	bufDescTable[fid].Set(file, pageNo);
}
Exemplo n.º 10
0
EncodeOutputCharStream::EncodeOutputCharStream(OutputByteStream *byteStream,
					       const OutputCodingSystem *codingSystem)
: buf_(0),
  byteStream_(byteStream),
  escaper_(0),
  ownedEncoder_(codingSystem->makeEncoder())
{
  encoder_ = ownedEncoder_.pointer();
  encoder_->setUnencodableHandler(this);
  allocBuf(codingSystem->fixedBytesPerChar());
  encoder_->startFile(byteStream_);
}
Exemplo n.º 11
0
/**
 * Allocates a new, empty page in the file and returns the Page object.
 * The newly allocated page is also assigned a frame in the buffer pool.
 *
 * @param file   	File object
 * @param PageNo  Page number. The number assigned to the page in the file is returned via this reference.
 * @param page  	Reference to page pointer. The newly allocated in-memory Page object is returned via this reference.
 */
void BufMgr::allocPage(File* file, PageId &pageNo, Page*& page) 
{
	FrameId frame;
	allocBuf(frame);
	
	bufPool[frame] = file->allocatePage();
	pageNo = bufPool[frame].page_number();

	bufDescTable[frame].Set(file, pageNo);
	hashTable->insert(file, pageNo, frame);
	page = &bufPool[frame];
}
Exemplo n.º 12
0
void BufMgr::allocPage(File* file, PageId &pageNo, Page*& page)
{
	FrameId frameNo;
	allocBuf(frameNo);
	bufPool[frameNo] = file->allocatePage();
	//returns newly allocated page to the caller via the pageNo parameter
	PageId pageNo1 = bufPool[frameNo].page_number();
	hashTable->insert(file, pageNo1, frameNo);
	bufDescTable[frameNo].Set(file, pageNo1);
	//returns pointer to the frame via the page parameter
	page = &bufPool[frameNo];
	pageNo = pageNo1;
}
Exemplo n.º 13
0
void EncodeOutputCharStream::open(OutputByteStream *byteStream,
				  const OutputCodingSystem *codingSystem)
{
  if (byteStream_)
    flush();
  byteStream_ = byteStream;
  ownedEncoder_ = codingSystem->makeEncoder();
  encoder_ = ownedEncoder_.pointer();
  encoder_->setUnencodableHandler(this);
  delete [] buf_;
  buf_ = 0;
  ptr_ = end_ = buf_;
  allocBuf(codingSystem->fixedBytesPerChar());
  encoder_->startFile(byteStream_);
}
Exemplo n.º 14
0
/**
* This function reads in a page. It will either be in the buffer pool or on disc.
* It first checks whether the page is already in the buffer pool
* by invoking the lookup() method on the hashtable to get a frame number.
* If it is not in buffer pool, Call allocBuf() to allocate a buffer frame 
* and then call the method file->readPage() to read the page from disk
* into the buffer pool frame.
* @param file-a pointer to the file handle
* @param pageNo-the number of the page to be read
* @param page-a pointer to the page inside the frame
* @return Status-status information from function
**/
const Status BufMgr::readPage(File* file, const int pageNo, Page*& page) {

    Status rc; 
    int frameNo = 0;
    //Check to see if the page is in the buffer
    rc = hashTable->lookup(file, pageNo, frameNo);
    //if not OK or HASHNOTFOUND, than an error has been detected
    if (rc != OK && rc != HASHNOTFOUND) {
        return rc; //return error status message
    }
    if (rc == HASHNOTFOUND) {
        //Page is not in the buffer pool.  
        int newFrameNo; //declaration for new frame pointer
        // allocate a buffer frame for the page to be read
        rc = allocBuf(newFrameNo);
        if (rc != OK) {
            return rc;
        }
        //map new buffer addition in the hashTable
        rc = hashTable->insert(file, pageNo, newFrameNo);
        //if not OK than we do not want to call Set()
        if (rc != OK) {
            return rc; 
        }
        //set page from disc in buffer frame
        bufTable[newFrameNo].Set(file, pageNo);
        bufTable[newFrameNo].frameNo = newFrameNo; //update new frame number
        rc = file->readPage(pageNo, &bufPool[newFrameNo]);
        if (rc != OK) {
            //Repair damage
            disposePage(file, pageNo);
            return rc;
        }
        page = &bufPool[newFrameNo]; //pointer to the page to be read
    } else {
        /*Page is in the buffer pool:
          Set the appropriate refbit, 
          increment the pinCnt for the page,
          and then return a pointer to the frame.*/
        bufTable[frameNo].refbit = true;
        bufTable[frameNo].pinCnt++;
        page = &bufPool[frameNo]; //pointer to the page to be read
    }
    //Returns OK if no errors occurred.
    return OK;
}// end function readPage
Exemplo n.º 15
0
void IdentityEncoder::output(const Char *s, size_t n, streambuf *sb)
{
  if (sizeof(Char) != sizeof(char)) {
    allocBuf(n);
    size_t j = 0;
    for (size_t i = 0; i < n; i++) {
      if (s[i] > UCHAR_MAX) {
	sb->sputn(buf_, j);
	j = 0;
	handleUnencodable(s[i], sb);
      }
      else
	buf_[j++] = char(s[i]);
    }
    sb->sputn(buf_, j);
  }
  else
    sb->sputn((const char *)s, n);
}
Exemplo n.º 16
0
/**
* Reads the given page from the file into a frame and returns the pointer to page.
* If the requested page is already present in the buffer pool pointer to that frame is returned
* otherwise a new frame is allocated from the buffer pool for reading the page.
*
* @param file   	File object
* @param PageNo  Page number in the file to be read
* @param page  	Reference to page pointer. Used to fetch the Page object in which requested page from file is read in.
*/
void BufMgr::readPage(File* file, const PageId pageNo, Page*& page)
{
	bool excepThrown = false;
	try{
		hashTable->lookup(file, pageNo, fid);
	}
	catch(HashNotFoundException e){
		excepThrown = true;
		allocBuf(fid);
		Page tPage = file->readPage(pageNo);
		bufPool[fid] = tPage;
		hashTable->insert(file, pageNo, fid);
		bufDescTable[fid].Set(file, pageNo);
	}
	if(!excepThrown){
		bufDescTable[fid].refbit = true;
		bufDescTable[fid].pinCnt++;
	}
	page = &bufPool[fid];
}
Exemplo n.º 17
0
/*
 * This method is to to allocate an empty page in the specified file.
 *
 * params: File* file, PageId &pageNo, Page*& page
 */
void BufMgr::allocPage(File* file, PageId &pageNo, Page*& page) 
{
  // get a new page and allocate frame
  Page newPage = file->allocatePage();
  pageNo = newPage.page_number();
  FrameId frame = -1;
  allocBuf(frame);
  page = &(bufPool[frame]);
  *page = file->readPage(pageNo);
  
  
  // insert into hash table
  hashTable->insert(file, pageNo, frame);
  
  // call Set()
  BufDesc* bufDesc = &(bufDescTable[frame]);
  bufDesc->Set(file, pageNo);
  
  //std::cout << "Allocated new page: " << frame << "\n";
}
Exemplo n.º 18
0
/**
 * Reads the given page from the file into a frame and returns the pointer to page.
 * If the requested page is already present in the buffer pool pointer to that frame is returned
 * otherwise a new frame is allocated from the buffer pool for reading the page.
 *
 * @param file   	File object
 * @param PageNo  Page number in the file to be read
 * @param page  	Reference to page pointer. Used to fetch the Page object in which requested page from file is read in.
 */
void BufMgr::readPage(File* file, const PageId pageNo, Page*& page)
{
	FrameId frame_check;
	try{
		hashTable->lookup(file, pageNo, frame_check);
	}catch(HashNotFoundException ex){
		
	//the page is not in the pool, so allocate one
	FrameId frame_no;
	allocBuf(frame_no);
	bufPool[frame_no] = file->readPage(pageNo);
	hashTable->insert(file, pageNo, frame_no);
	bufDescTable[frame_no].Set(file, pageNo);
	page = &bufPool[frame_no];
	return;
	}

	bufDescTable[frame_check].pinCnt++;
	bufDescTable[frame_check].refbit = true;
	page = &bufPool[frame_check];
}
Exemplo n.º 19
0
void BufMgr::readPage(File* file, const PageId pageNo, Page*& page)
{
	FrameId frameNo;
	try{
		hashTable->lookup(file, pageNo, frameNo);  // Case 2: Page is in the buffer pool
		bufDescTable[frameNo].refbit = true;
		bufDescTable[frameNo].pinCnt++;
		// "Return a pointer to the frame containing the page via the page parameter"
		page = &bufPool[frameNo];
	}
	catch(HashNotFoundException e){ // Case 1: Page is not in the buffer pool
		allocBuf(frameNo);
		bufStats.diskreads++;
		bufPool[frameNo] = file->readPage(pageNo);
		bufDescTable[frameNo].Set(file, pageNo);
		//bufDescTable[frameNo].refbit = true;
		// "Return a pointer to the frame containing the page via the page parameter"
		page = &bufPool[frameNo];
		hashTable->insert(file, pageNo, frameNo);
	}
}
Exemplo n.º 20
0
const Status BufMgr::allocPage(File* file, int& pageNo, Page*& page) 
{
    int frameNo;

    // allocate a new page in the file
    Status status = file->allocatePage(pageNo);
    if (status != OK)  return status; 

    // alloc a new frame
     status = allocBuf(frameNo);
     if (status != OK) return status;

     // set up the entry properly
     bufTable[frameNo].Set(file, pageNo);
     page = &bufPool[frameNo];

     // insert in thehash table
     status = hashTable->insert(file, pageNo, frameNo);
     if (status != OK) { return status; }
     // cout << "allocated page " << pageNo <<  " to file " << file << "frame is: " << frameNo  << endl;
    return OK;
}
Exemplo n.º 21
0
const Status BufMgr::allocPage(File* file, int& pageNo, Page*& page) 
{
	Status status = OK;
	int frameNo = 0;
	status = file->allocatePage(pageNo);
	if (status != OK)
	{
	  return status;
	}
	status = allocBuf(frameNo);
	if (status != OK)
	{
		return status;
	}
	status = hashTable->insert(file, pageNo, frameNo);
	if (status != OK)
	{
		return status;
	}
	bufTable[frameNo].Set(file, pageNo);
	page = &(bufPool[frameNo]);
	return status;
}
Exemplo n.º 22
0
const Status BufMgr::readPage(File* file, const int PageNo, Page*& page)
{
	// Your solution goes here
	bufStats.accesses ++;
	unsigned int frameno;
   	Status stats = bufMap.lookup(file, PageNo, frameno);
   	if(stats == OK){
				bufTable[frameno].pinCnt++;
      		page = &(bufPool[frameno]);
       		return OK;
   	}else{
       		Status tempstatus2 = allocBuf(frameno);
				if(tempstatus2 == BUFFEREXCEEDED) return BUFFEREXCEEDED;//I think this is how to add the BUFFEREXCEEDED error.
      			Page *pgpointer = &bufPool[frameno];
      	 		Status tempstatus = file->readPage(PageNo, pgpointer); //probably need to add error catch!
      	 		//cout << "Page pointer = " << pgpointer << endl;
      	 		if(tempstatus == UNIXERR){    
      	     		return UNIXERR;
      	 		} else if (tempstatus == OK){
      	 			bufStats.diskreads ++;
      	     		bufTable[frameno].Set(file, PageNo);
      	     		if(bufMap.insert(file, PageNo, frameno) == BUFMAPERROR){
       					return BUFMAPERROR;
   					}

						//page = &bufPool[frameno];
      	     		page = pgpointer;
      	     		//cout << "Buf pool test = " << bufPool[frameno].data << endl;
      	     		return OK;
                }        
   		}
   		return OK;
   	
   //BUFFEREXCEEDED error = ?
   // do we need to write to the bufmap here? 
   // I don't think so...
}
Exemplo n.º 23
0
const Status BufMgr::readPage(File* file, const int PageNo, Page*& page)
{
    // check to see if it is already in the buffer pool
    // cout << "readPage called on file.page " << file << "." << PageNo << endl;
    int frameNo = 0;
    Status status = hashTable->lookup(file, PageNo, frameNo);
    if (status == OK)
    {
        // set the referenced bit
        bufTable[frameNo].refbit = true;
        bufTable[frameNo].pinCnt++;
        page = &bufPool[frameNo];
    }
    else // not in the buffer pool, must allocate a new page
    {
        // alloc a new frame
        status = allocBuf(frameNo);
        if (status != OK) return status;

        // read the page into the new frame
        bufStats.diskreads++;
        status = file->readPage(PageNo, &bufPool[frameNo]);
        if (status != OK) return status;

        // set up the entry properly
        bufTable[frameNo].Set(file, PageNo);
        page = &bufPool[frameNo];

        // insert in the hash table
        status = hashTable->insert(file, PageNo, frameNo);
        if (status != OK) { return status; }

    }

    return OK;
}