extern RC readPreviousBlock(SM_FileHandle *fHandle, SM_PageHandle memPage) { int prevPage = getBlockPos(fHandle) - 1; return readBlock(prevPage, fHandle, memPage); }
asagi::Grid::Error grid::NumaLocalStaticGrid::init() { unsigned long blockSize = getTotalBlockSize(); size_t block[3]; unsigned long masterBlockCount = getThreadBlockCount()*m_threadHandle.getThreadCount(); //the first thread allocates the memory. if (m_threadHandle.getThreadRank(pthread_self())==0) { asagi::Grid::Error error; error = m_allocator.allocate(getType().getSize() * blockSize * masterBlockCount, m_data); if (error != asagi::Grid::SUCCESS) return error; // Load the blocks from the file, which we control /* for (unsigned long i = 0; i < getLocalBlockCount(); i++) { if (getGlobalBlock(i) >= getBlockCount()){ // Last process(es) may control less blocks break; } // Get x, y and z coordinates of the block getBlockPos(getGlobalBlock(i), block); // Get x, y and z coordinates of the first value in the block for (unsigned char j = 0; j < 3; j++) block[j] *= getBlockSize(j); getType().load(getInputFile(), block, getBlockSize(), &m_data[getType().getSize() * blockSize * i]); }*/ //m_threadHandle.m_staticPtr[pthread_self()][m_id]=m_data; // std::cout << "Thread: " << pthread_self() << " Pointer: " << &m_data; m_threadHandle.setStaticPtr(pthread_self(), m_data, m_id); } else { //The memory was already allocated by the masterthread. //Simply shift the Pointer in the right space. m_data = m_threadHandle.getStaticPtr(m_threadHandle.getMasterthreadId(), m_id) + (getType().getSize() * blockSize * m_threadHandle.getThreadRank(pthread_self()) * getThreadBlockCount()); // std::cout << "Thread: " << pthread_self() << " Pointer: " << &m_data; m_threadHandle.setStaticPtr(pthread_self(), m_data, m_id); } // Load the blocks from the file, which we control for (unsigned long i = m_threadHandle.getThreadRank(pthread_self())*getThreadBlockCount(); i < (m_threadHandle.getThreadRank(pthread_self())*getThreadBlockCount()+getThreadBlockCount()); i++) { if (getGlobalBlock(i) >= getBlockCount()){ // Last process(es) may control less blocks break; } // Get x, y and z coordinates of the block getBlockPos(getGlobalBlock(i), block); // Get x, y and z coordinates of the first value in the block for (unsigned char j = 0; j < 3; j++) block[j] *= getBlockSize(j); getType().load(m_threadHandle.getInputFile(), block, getBlockSize(), &m_data[getType().getSize() * blockSize * (i-m_threadHandle.getThreadRank(pthread_self())*getThreadBlockCount())]); } return asagi::Grid::SUCCESS; }
RC readPreviousBlock (SM_FileHandle *fHandle, SM_PageHandle memPage) { printf("*** Inside readPreviousBlock method ***\n"); int rc = RC_NO_PREVIOUS_BLOCK;//Error message if no previous block exists. if(fHandle != NULL)//check if fHandle exists { int prevBlock = getBlockPos(fHandle) - 1; //get current block position and subtract 1 block rc = readBlock(prevBlock,fHandle,memPage);//calling readblock method } printf("*** Exiting readPreviousBlock method ***\n"); return rc; }
/* readNextBlock method takes fhandle and character array as input parameters and reads the nextblock of 4096 bytes of memory page from the current postion of the file and returns RC_OK if operation is completed. */ RC readNextBlock (SM_FileHandle *fHandle, SM_PageHandle memPage) { printf("*** Inside readNextBlock method ***\n"); int rc = RC_NO_NEXT_BLOCK;//Error message if no next block exists. if(fHandle != NULL)//check if fHandle exists { int nextBlock = getBlockPos(fHandle) + 1; //get current block position and add 1 block; rc = readBlock(nextBlock,fHandle,memPage);//calling readblock method } printf("*** Exiting readNextBlock method ***\n"); return rc; }
/* readCurrentBlock method reads data from the file and returns RC_OK if execution is successful. */ RC readCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage) { printf("*** Inside readCurrentBlock method ***\n"); int rc = RC_NO_CURRENT_BLOCK;//Error message if no current block exists. if(fHandle != NULL)//check if fHandle exists { int currentBlock = getBlockPos(fHandle);// get current block/page position. rc = readBlock(currentBlock,fHandle,memPage);// calling readBlock method. } printf("*** Exiting readCurrentBlock method ***\n"); return rc; }
/* Try to create, open, and close a multi-page file */ void testMultiPageContent(void) { SM_FileHandle fh; SM_PageHandle ph; int i; testName = "test multi-page content"; ph = (SM_PageHandle) malloc(PAGE_SIZE); // create a new page file TEST_CHECK(createPageFile (TESTPF)); TEST_CHECK(openPageFile (TESTPF, &fh)); printf("created and opened file\n"); //ensure the capacity of the file with 4 pages TEST_CHECK(ensureCapacity(4,&fh)); // read the current page into handle TEST_CHECK(readCurrentBlock(&fh, ph)); // the page should be empty (zero bytes) for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in current page of freshly initialized page"); printf("current block was empty with the page number =%d\n", getBlockPos(&fh)); // read the next page into handle TEST_CHECK(readNextBlock(&fh, ph)); // the page should be empty (zero bytes) for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in next page of freshly initialized page"); printf("the next block was empty\n"); printf("the updated page number is =%d\n",getBlockPos(&fh)); // change ph to be a string and write that one to disk for (i=0; i < PAGE_SIZE; i++) ph[i] = (i % 10) + '0'; TEST_CHECK(writeCurrentBlock(&fh, ph)); printf("writing current block with the page number =%d\n", getBlockPos(&fh)); // read back the page containing the string and check that it is correct TEST_CHECK(readCurrentBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected."); printf("reading back current block into memory with page number =%d\n", getBlockPos(&fh)); // read the last page into handle TEST_CHECK(readLastBlock(&fh, ph)); // the page should be empty (zero bytes) for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in last page of freshly initialized page"); printf("last block was empty with the page number =%d\n", getBlockPos(&fh)); // change ph to be a string and write that one to disk for (i=0; i < PAGE_SIZE; i++) ph[i] = (i % 10) + '0'; TEST_CHECK(writeCurrentBlock(&fh, ph)); printf("writing current block with the page number =%d\n", getBlockPos(&fh)); // read back the page containing the string and check that it is correct TEST_CHECK(readCurrentBlock (&fh, ph)); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected."); printf("reading back current block into memory with page number =%d\n", getBlockPos(&fh)); // destroy new page file TEST_CHECK(destroyPageFile (TESTPF)); TEST_DONE(); }
extern RC readNextBlock(SM_FileHandle *fHandle, SM_PageHandle memPage) { int nextBlock = getBlockPos(fHandle) + 1; return readBlock(nextBlock, fHandle, memPage); }
extern RC readCurrentBlock(SM_FileHandle *fHandle, SM_PageHandle memPage) { int currBlock = getBlockPos(fHandle); return readBlock(currBlock, fHandle, memPage); }
/* Read next page relative to the curPagePos of the file * by calling readBlock() with pageNum = curPagePos + 1. * The value of curPagePos was set to curPagePos + 1 after reading. */ RC readNextBlock (SM_FileHandle *fHandle, SM_PageHandle memPage) { return readBlock(getBlockPos(fHandle)+1,fHandle,memPage);; }
/* Read current page according to the curPagePos of the file. * by calling readBlock() with pageNum = curPagePos. */ RC readCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage) { return readBlock(getBlockPos(fHandle),fHandle,memPage);; }
/* Read previous page relative to the curPagePos of the file, * by calling readBlock() with pageNum = curPagePos - 1. * The value of curPagePos was set to curPagePos - 1 after reading. */ RC readPreviousBlock (SM_FileHandle *fHandle, SM_PageHandle memPage) { return readBlock(getBlockPos(fHandle)-1,fHandle,memPage);; }
//Write Current Block RC writeCurrentBlock(SM_FileHandle *fHandle, SM_PageHandle memPage) { int pos = getBlockPos(fHandle->curPagePos); writeBlock (pos, fHandle, memPage); }
void testAppendEnsureCapMetaData() { SM_FileHandle fh; SM_PageHandle ph; ph = (SM_PageHandle) malloc(PAGE_SIZE); TEST_CHECK(createPageFile (TESTPF)); TEST_CHECK(openPageFile (TESTPF, &fh)); //Append an empty block to the file. appendEmptyBlock(&fh); //Check whether the appended block has only 4096 '\0' in the currentBlock. readBlock(getBlockPos(&fh),&fh,ph); int i; for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); printf("Appended Block was empty\n"); //Page File should contain only 2 blocks.first block during createPage and second during appendBlock ASSERT_TRUE((fh.totalNumPages == 2), "Number of Blocks : 2"); //Current Block postion should be 1 ASSERT_TRUE((fh.curPagePos == 1), "Current Page Position is 1"); //add 3 more blocks to the Page File. ensureCapacity(5,&fh); //Verify whether the freshly added 3 blocks are of '\0' characters //[START] readBlock(2,&fh,ph); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); readBlock(3,&fh,ph); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); readBlock(4,&fh,ph); for (i=0; i < PAGE_SIZE; i++) ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page"); printf("Freshly appended 3 blocks are empty\n"); //[END] //Page File should contain only 5 blocks, as we have called ensureCapacity(5) ASSERT_TRUE((fh.totalNumPages == 5), "Number of Blocks : 5"); //Current Block postion should be 4 ASSERT_TRUE((fh.curPagePos == 4), "Current Page Position is 4"); //Store the metaData into the file and close the pagefile. int totalNoOfPages = fh.totalNumPages; char fileName[100]; memset(fileName,'\0',100); strcpy(fileName,fh.fileName); char metaDataFromFile[100]; memset(metaDataFromFile,'\0',100); closePageFile(&fh); //Verify whether the written MetaData is correct or not //[START] char metaDataToBeVerified[100]; memset(metaDataToBeVerified,'\0',100); char returnData[100]; memset(returnData,'\0',100); metaDataToBeVerified[0]= 'P';metaDataToBeVerified[1]= 'S';metaDataToBeVerified[2]= ':';metaDataToBeVerified[3]= '\0'; getString(PAGE_SIZE,returnData); strcat(metaDataToBeVerified,returnData); strcat(metaDataToBeVerified,";"); memset(returnData,'\0',100); strcat(metaDataToBeVerified,"NP:"); getString(totalNoOfPages,returnData); strcat(metaDataToBeVerified,returnData); strcat(metaDataToBeVerified,";"); readMetaDataFromFile(fileName,metaDataFromFile); ASSERT_TRUE((strcmp(metaDataToBeVerified, metaDataFromFile) == 0), "MetaData read from file is correct"); printf("Read Meta Data from file is :: %s\n",metaDataToBeVerified); //[END] TEST_CHECK(destroyPageFile(TESTPF)); free(ph); TEST_DONE(); }