This repository has been archived by the owner on Apr 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.cpp
177 lines (150 loc) · 4.76 KB
/
filesystem.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// filesystem.cpp
// fdb2
//
// Created by Святослав Фельдшеров on 12.04.15.
// Copyright (c) 2015 Святослав Фельдшеров. All rights reserved.
//
#include "filesystem.h"
FileSystemApi::FileSystemApi(const std::string &path) {
if (!boost::filesystem::exists(path)) {
std::ofstream f(path);
}
file.open(path);
file.setf(std::ios::binary);
file.setf(std::ios::unitbuf);
ccache_size = 0;
}
void FileSystemApi::open(const std::string &path) {
assert(!file.is_open());
if (!boost::filesystem::exists(path)) {
std::ofstream f(path);
}
file.open(path);
file.setf(std::ios::binary);
file.setf(std::ios::unitbuf);
ccache_size = 0;
}
void FileSystemApi::read_meta(DBC &conf) {
assert(file.is_open());
file.seekg(0, std::ios_base::beg);
file.read((char *) &conf, sizeof(DBC));
}
void FileSystemApi::write_meta(DBC &conf) {
assert(file.is_open());
file.seekp(0, std::ios_base::beg);
file.write((char *) &conf, sizeof(DBC));
}
inline int FileSystemApi::get_bitset_offset(DBC &conf) {
return sizeof(DBC);
}
inline int FileSystemApi::get_bitset_size(DBC &conf) {
return (int)(conf.db_size / conf.page_size + 7) / 8;
}
inline int FileSystemApi::get_page_offset(DBC &conf, Page &pg) {
return get_bitset_offset(conf) + get_bitset_size(conf) + (int)conf.page_size * pg.get_page_num();
}
void FileSystemApi::write_bitset(DBC &conf, Bitset &b) {
assert(file.is_open());
assert(file.good());
file.seekp(get_bitset_offset(conf), std::ios_base::beg);
file.write(b.as_bytes(), b.num_blocks());
file.seekg(get_bitset_offset(conf), std::ios_base::beg);
char *buff = new char[get_bitset_size(conf)];
file.read(buff, get_bitset_size(conf));
delete[] buff;
}
void FileSystemApi::read_bitset(DBC &conf, Bitset &b) {
assert(file.good());
file.seekg(get_bitset_offset(conf), std::ios_base::beg);
char *buff = new char[get_bitset_size(conf)];
file.read(buff, get_bitset_size(conf));
b.from_bytes(get_bitset_size(conf), buff);
delete[] buff;
}
void FileSystemApi::write_page(DBC &conf, Page &pg) {
push_to_cache(conf, pg);
}
void FileSystemApi::push_to_cache(DBC &conf, Page &pg) {
if (cached.count(pg.get_page_num())) {
del_from_cache(conf, pg);
}
cached.insert(pg.get_page_num());
lru.push_front(pg);
ccache_size += pg.num_bytes();
balance_cache(conf);
return;
}
void FileSystemApi::del_from_cache(DBC &conf, Page &pg) {
cached.erase(pg.get_page_num());
for (std::list<Page>::iterator it = lru.begin(); it != lru.end(); ++it) {
if (it->get_page_num() == pg.get_page_num()) {
ccache_size -= it->num_bytes();
lru.erase(it);
return;
}
}
}
void FileSystemApi::balance_cache(DBC &conf) {
if (ccache_size <= conf.cache_size) {
return;
}
while (ccache_size > conf.cache_size) {
//std::cerr << lru.back().get_page_num() << std::endl;
ccache_size -= lru.back().num_bytes();
cached.erase(lru.back().get_page_num());
flush_page(conf, lru.back());
lru.pop_back();
}
return;
}
void FileSystemApi::read_page(DBC &conf, Page &pg) {
//std::cerr << "read_page: " << pg.get_page_num() << std::endl;
if (cached.count(pg.get_page_num())) {
get_from_cache(conf, pg);
return;
}
fetch(conf, pg);
return;
}
void FileSystemApi::fetch(DBC &conf, Page &pg) {
//std::cerr << "fetch: " << pg.get_page_num() << std::endl;
if (cached.count(pg.get_page_num())) {
del_from_cache(conf, pg);
}
read_page_from_disk(conf, pg);
cached.insert(pg.get_page_num());
lru.push_front(pg);
ccache_size += pg.num_bytes();
balance_cache(conf);
return;
}
void FileSystemApi::get_from_cache(DBC &conf, Page &pg) {
assert(cached.count(pg.get_page_num()));
for (std::list<Page>::iterator it = lru.begin(); it != lru.end(); ++it) {
if (it->get_page_num() == pg.get_page_num()) {
pg = *it;
return;
}
}
}
void FileSystemApi::flush_page(DBC &conf, Page &pg) {
assert(file.good());
file.seekp(get_page_offset(conf, pg), std::ios_base::beg);
assert(pg.num_bytes() <= conf.page_size);
file.write(pg.as_bytes((int)conf.page_size), conf.page_size);
}
void FileSystemApi::read_page_from_disk(DBC &conf, Page &pg) {
assert(file.is_open());
file.seekg(get_page_offset(conf, pg), std::ios_base::beg);
char *buff = new char[conf.page_size];
file.read(buff, conf.page_size);
pg.from_bytes(buff);
delete[] buff;
}
void FileSystemApi::sync(DBC &conf) {
for (std::list<Page>::iterator it = lru.begin(); it != lru.end(); ++it) {
flush_page(conf, *it);
}
return;
}