2004-07-15 00:42:36 +00:00
|
|
|
/**
|
|
|
|
|
|
|
|
This file handles all of the file I/O for pages.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <lladd/bufferManager.h>
|
|
|
|
|
|
|
|
#include "page.h"
|
|
|
|
#include "pageFile.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include "logger/logWriter.h"
|
|
|
|
|
|
|
|
static FILE * stable = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
This function blocks until there are no events pending for this page.
|
|
|
|
|
|
|
|
@see addPendingEvent(), removePendingEvent()
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
void finalize(Page * p) {
|
2004-07-15 00:42:36 +00:00
|
|
|
pthread_mutex_lock(&(p->pending_mutex));
|
|
|
|
p->waiting++;
|
|
|
|
|
|
|
|
while(p->pending) {
|
|
|
|
|
|
|
|
pthread_cond_wait(&(p->noMorePending), &(p->pending_mutex));
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&(p->pending_mutex));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* This function is declared in page.h */
|
|
|
|
void pageRead(Page *ret) {
|
2004-07-20 00:15:17 +00:00
|
|
|
long fileSize;
|
|
|
|
|
|
|
|
long pageoffset;
|
2004-07-15 00:42:36 +00:00
|
|
|
long offset;
|
|
|
|
|
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
/** @todo pageRead() is using fseek to calculate the file size on each read, which is inefficient. */
|
|
|
|
pageoffset = ret->id * PAGE_SIZE;
|
|
|
|
flockfile(stable);
|
|
|
|
|
|
|
|
fileSize = myFseekNoLock(stable, 0, SEEK_END);
|
|
|
|
|
|
|
|
|
|
|
|
/* DEBUG("Reading page %d\n", ret->id); */
|
|
|
|
|
|
|
|
/* if(!ret->memAddr) {
|
2004-07-15 00:42:36 +00:00
|
|
|
ret->memAddr = malloc(PAGE_SIZE);
|
|
|
|
}
|
2004-07-20 00:15:17 +00:00
|
|
|
if(!ret->memAddr) {
|
|
|
|
perror("pageFile.c");
|
|
|
|
fflush(NULL);
|
|
|
|
}
|
|
|
|
assert(ret->memAddr); */
|
2004-07-15 00:42:36 +00:00
|
|
|
|
|
|
|
if ((ret->id)*PAGE_SIZE >= fileSize) {
|
2004-07-20 00:15:17 +00:00
|
|
|
myFseekNoLock(stable, (1+ ret->id) * PAGE_SIZE -1, SEEK_SET);
|
2004-07-15 00:42:36 +00:00
|
|
|
if(1 != fwrite("", 1, 1, stable)) {
|
|
|
|
if(feof(stable)) { printf("Unexpected eof extending storefile!\n"); fflush(NULL); abort(); }
|
|
|
|
if(ferror(stable)) { printf("Error extending storefile! %d", ferror(stable)); fflush(NULL); abort(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2004-07-20 00:15:17 +00:00
|
|
|
offset = myFseekNoLock(stable, pageoffset, SEEK_SET);
|
2004-07-15 00:42:36 +00:00
|
|
|
assert(offset == pageoffset);
|
|
|
|
|
|
|
|
if(1 != fread(ret->memAddr, PAGE_SIZE, 1, stable)) {
|
|
|
|
|
|
|
|
if(feof(stable)) { printf("Unexpected eof reading!\n"); fflush(NULL); abort(); }
|
|
|
|
if(ferror(stable)) { printf("Error reading stream! %d", ferror(stable)); fflush(NULL); abort(); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
funlockfile(stable);
|
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is declared in page.h */
|
|
|
|
void pageWrite(Page * ret) {
|
|
|
|
|
|
|
|
long pageoffset = ret->id * PAGE_SIZE;
|
2004-07-20 00:15:17 +00:00
|
|
|
long offset ;
|
2004-07-15 00:42:36 +00:00
|
|
|
|
|
|
|
if(flushedLSN() < pageReadLSN(ret)) {
|
2004-07-20 00:15:17 +00:00
|
|
|
DEBUG("pageWrite is calling syncLog()!\n");
|
2004-07-15 00:42:36 +00:00
|
|
|
syncLog();
|
|
|
|
}
|
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
flockfile(stable);
|
|
|
|
offset = myFseekNoLock(stable, pageoffset, SEEK_SET);
|
|
|
|
assert(offset == pageoffset);
|
|
|
|
assert(ret->memAddr);
|
|
|
|
|
|
|
|
/* DEBUG("Writing page %d\n", ret->id); */
|
2004-07-15 00:42:36 +00:00
|
|
|
|
|
|
|
if(1 != fwrite(ret->memAddr, PAGE_SIZE, 1, stable)) {
|
|
|
|
|
|
|
|
if(feof(stable)) { printf("Unexpected eof writing!\n"); fflush(NULL); abort(); }
|
|
|
|
if(ferror(stable)) { printf("Error writing stream! %d", ferror(stable)); fflush(NULL); abort(); }
|
|
|
|
|
|
|
|
}
|
2004-07-20 00:15:17 +00:00
|
|
|
|
|
|
|
funlockfile(stable);
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void openPageFile() {
|
|
|
|
|
|
|
|
DEBUG("Opening storefile.\n");
|
|
|
|
if( ! (stable = fopen(STORE_FILE, "r+"))) { /* file may not exist */
|
|
|
|
byte* zero = calloc(1, PAGE_SIZE);
|
|
|
|
|
|
|
|
if(!(stable = fopen(STORE_FILE, "w+"))) { perror("Couldn't open or create store file"); abort(); }
|
|
|
|
|
|
|
|
/* Write out one page worth of zeros to get started. */
|
|
|
|
|
|
|
|
if(1 != fwrite(zero, PAGE_SIZE, 1, stable)) { assert (0); }
|
|
|
|
|
|
|
|
free(zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG("storefile opened.\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
void closePageFile() {
|
|
|
|
|
|
|
|
int ret = fclose(stable);
|
|
|
|
assert(!ret);
|
|
|
|
stable = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
long myFseek(FILE * f, long offset, int whence) {
|
|
|
|
long ret;
|
|
|
|
flockfile(f);
|
2004-07-20 00:15:17 +00:00
|
|
|
ret = myFseekNoLock(f, offset, whence);
|
|
|
|
funlockfile(f);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
long myFseekNoLock(FILE * f, long offset, int whence) {
|
|
|
|
long ret;
|
2004-07-15 00:42:36 +00:00
|
|
|
if(0 != fseek(f, offset, whence)) { perror ("fseek"); fflush(NULL); abort(); }
|
|
|
|
if(-1 == (ret = ftell(f))) { perror("ftell"); fflush(NULL); abort(); }
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void myFwrite(const void * dat, long size, FILE * f) {
|
|
|
|
int nmemb = fwrite(dat, size, 1, f);
|
|
|
|
/* test */
|
|
|
|
if(nmemb != 1) {
|
|
|
|
perror("myFwrite");
|
|
|
|
abort();
|
|
|
|
/* return FILE_WRITE_OPEN_ERROR; */
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|