-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
230 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
.o | ||
.bundle | ||
bin/ | ||
test.c | ||
test.c | ||
tinydb | ||
mydb.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
run: | ||
gcc main.c -o tinydb | ||
./tinydb | ||
./tinydb mydb.db | ||
test: | ||
gcc test.c -o a.out | ||
./a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include <stdint.h> | ||
#include <sys/fcntl.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "types.h" | ||
#ifndef PAGER_H | ||
#define PAGER_H | ||
|
||
pager_t *pager_open(const char *filename) | ||
{ | ||
// S_IWUSR means User write permission | ||
// S_IRUSR means User read permission | ||
int fd = open(filename, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR); | ||
|
||
if (fd < 0) | ||
{ | ||
printf("Unable to open file\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
off_t file_length = lseek(fd, 0, SEEK_END); | ||
|
||
pager_t *pager = (pager_t *)malloc(sizeof(pager_t)); | ||
pager->file_descriptor = fd; | ||
pager->file_length = file_length; | ||
|
||
for (uint32_t i = 0; i < TABLE_MAX_PAGES; i++) | ||
{ | ||
pager->pages[i] = NULL; | ||
} | ||
return pager; | ||
} | ||
|
||
void *get_page(pager_t *pager, uint32_t page_num) | ||
{ | ||
if (page_num > TABLE_MAX_PAGES) | ||
{ | ||
printf("Tried to fetch page number out of bounds. %d > %d\n", page_num, TABLE_MAX_PAGES); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
// Cache miss. Allocate memory and load from file. | ||
if (pager->pages[page_num] == NULL) | ||
{ | ||
void *page = malloc(PAGE_SIZE); | ||
uint32_t num_pages = pager->file_length / PAGE_SIZE; | ||
|
||
if (pager->file_length % PAGE_SIZE) | ||
{ | ||
num_pages += 1; | ||
} | ||
|
||
if (page_num <= num_pages) | ||
{ | ||
lseek(pager->file_descriptor, page_num * PAGE_SIZE, SEEK_SET); | ||
ssize_t bytes_read = read(pager->file_descriptor, page, PAGE_SIZE); | ||
if (bytes_read == -1) | ||
{ | ||
printf("Error reading file: %d\n", errno); | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
pager->pages[page_num] = page; | ||
} | ||
return pager->pages[page_num]; | ||
} | ||
|
||
void pager_flush(pager_t *pager, uint32_t page_num, uint32_t size) | ||
{ | ||
if (pager->pages[page_num] == NULL) | ||
{ | ||
printf("Tried to flush null page\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
off_t offset = lseek(pager->file_descriptor, page_num * PAGE_SIZE, SEEK_SET); | ||
|
||
if (offset == -1) | ||
{ | ||
printf("Error seeking: %d\n", errno); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
ssize_t bytes_written = write(pager->file_descriptor, pager->pages[page_num], size); | ||
|
||
if (bytes_written == -1) | ||
{ | ||
printf("Error writing: %d\n", errno); | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters