stasis-aries-wal/src/lladd/pageFile.c

152 lines
3.9 KiB
C
Raw Normal View History

/**
This file handles all of the file I/O for pages.
*/
#include "page.h"
#include <lladd/bufferManager.h>
#include "pageFile.h"
#include <assert.h>
#include "logger/logWriter.h"
static FILE * stable = NULL;
/** Defined in bufferManager.c */
extern pthread_mutex_t add_pending_mutex;
void pageRead(Page *ret) {
2004-07-20 00:15:17 +00:00
long fileSize;
long pageoffset;
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) {
ret->memAddr = malloc(PAGE_SIZE);
}
2004-07-20 00:15:17 +00:00
if(!ret->memAddr) {
perror("pageFile.c");
fflush(NULL);
}
assert(ret->memAddr); */
if ((ret->id)*PAGE_SIZE >= fileSize) {
2004-07-20 00:15:17 +00:00
myFseekNoLock(stable, (1+ ret->id) * PAGE_SIZE -1, SEEK_SET);
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);
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);
}
void pageWrite(Page * ret) {
long pageoffset = ret->id * PAGE_SIZE;
2004-07-20 00:15:17 +00:00
long offset ;
/* assert(ret->pending == 0); */
if(flushedLSN() < pageReadLSN(ret)) {
2004-07-20 00:15:17 +00:00
DEBUG("pageWrite is calling syncLog()!\n");
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); */
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);
}
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;
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; */
}
}
long pageCount() {
long fileSize = myFseek(stable, 0, SEEK_END);
assert(! (fileSize % PAGE_SIZE));
return fileSize / PAGE_SIZE;
}