-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacking_store.cpp
70 lines (56 loc) · 2.38 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "backing_store.hpp"
#include <iostream>
#include <ext/stdio_filebuf.h>
#include <unistd.h>
#include <cassert>
#include "debug.hpp"
/////////////////////////////////////////////////////////////
// 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)
{}
//allocate space for a new version of an object
//requires that version be >> any previous version
//logic for this is now handled by the swap space
void one_file_per_object_backing_store::allocate(uint64_t obj_id, uint64_t version) {
//uint64_t id = nextid++;
std::string filename = get_filename(obj_id, version);
std::fstream dummy(filename, std::fstream::out);
dummy.flush();
assert(dummy.good());
//return id;
}
//delete the file associated with an specific version of a node
void one_file_per_object_backing_store::deallocate(uint64_t obj_id, uint64_t version) {
std::string filename = get_filename(obj_id, version);
assert(unlink(filename.c_str()) == 0);
}
//return filestream corresponding to an item. Needed for deserialization.
std::iostream * one_file_per_object_backing_store::get(uint64_t obj_id, uint64_t version) {
__gnu_cxx::stdio_filebuf<char> *fb = new __gnu_cxx::stdio_filebuf<char>;
std::string filename = get_filename(obj_id, version);
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;
}
//push changes from iostream and close.
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;
}
//Given an object and version, return the filename corresponding to it.
std::string one_file_per_object_backing_store::get_filename(uint64_t obj_id, uint64_t version){
return root + "/" + std::to_string(obj_id) + "_" + std::to_string(version);
}
std::string one_file_per_object_backing_store::get_version_file(uint64_t obj_id, uint64_t version){
debug(std::cout << "Loading " << obj_id << "_" << obj_id << std::endl);
return "tmpdir/" + std::to_string(obj_id) + "_" + std::to_string(version);
}