-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pager_template.c
64 lines (50 loc) · 1.59 KB
/
Pager_template.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
typedef struct {
int file_descriptor;
uint32_t file_length;
void* pages[TABLE_MAX_PAGES];
} Pager;
typedef struct {
Pager* pager;
uint32_t num_rows;
} Table;
//change (rename) new_table fxn to open_database
// create Pager* pager, initialize table and pager values
// accordingly
//for fd
int fd = open(filename,O_RDWR | // Read/Write mode
O_CREAT, // Create file if it does not exist
S_IWUSR | // User write permission
S_IRUSR // User read permission
);
if (fd == -1) {
printf("Unable to open file\n");
exit(EXIT_FAILURE);
}
// in row_slot fxn, get page from pager
// handle partial page/ new page case
//Ex:
if (pager->pages[page_num] == NULL)
// Cache miss. Allocate memory and load from file.
// flush the cache to disk only when .exit is done. Call db_close() upon exit:
// db_close() does the following:
// flushes the page cache to disk
// closes the database file
// frees the memory for the Pager and Table data structures
// Go through each page of pager and do the above things
// handle partial page also
off_t offset = lseek(pager->file_descriptor, page_num * PAGE_SIZE, SEEK_SET);
write(pager->file_descriptor, pager->pages[page_num], size);
int main(int argc, char* argv[]){
char* filename = argv[1];
...
}
//Extra: memcpy can have certain issues, what are they, what can be used instead