-
Notifications
You must be signed in to change notification settings - Fork 47
/
backing_store.cpp
48 lines (42 loc) · 1.49 KB
/
backing_store.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
#include "backing_store.hpp"
#include <iostream>
#include <ext/stdio_filebuf.h>
#include <unistd.h>
#include <cassert>
/////////////////////////////////////////////////////////////
// Implementation of the one_file_per_object_backing_store //
/////////////////////////////////////////////////////////////
one_file_per_object_backing_store::one_file_per_object_backing_store(std::string rt)
: root(rt),
nextid(1)
{}
uint64_t one_file_per_object_backing_store::allocate(size_t n) {
uint64_t id = nextid++;
std::string filename = root + "/" + std::to_string(id);
std::fstream dummy(filename, std::fstream::out);
dummy.flush();
assert(dummy.good());
return id;
}
void one_file_per_object_backing_store::deallocate(uint64_t id) {
std::string filename = root + "/" + std::to_string(id);
assert(unlink(filename.c_str()) == 0);
}
std::iostream * one_file_per_object_backing_store::get(uint64_t id) {
__gnu_cxx::stdio_filebuf<char> *fb = new __gnu_cxx::stdio_filebuf<char>;
std::string filename = root + "/" + std::to_string(id);
fb->open(filename, std::fstream::in | std::fstream::out);
std::fstream *ios = new std::fstream;
ios->std::ios::rdbuf(fb);
ios->exceptions(std::fstream::badbit | std::fstream::failbit | std::fstream::eofbit);
assert(ios->good());
return ios;
}
void one_file_per_object_backing_store::put(std::iostream *ios)
{
ios->flush();
__gnu_cxx::stdio_filebuf<char> *fb = (__gnu_cxx::stdio_filebuf<char> *)ios->rdbuf();
fsync(fb->fd());
delete ios;
delete fb;
}