-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.hpp
177 lines (143 loc) · 4.77 KB
/
util.hpp
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
176
177
#pragma once
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
namespace io {
template<typename T = char>
struct MMapping {
int handle;
uintptr_t file_size;
T* mapping;
using iterator = T*;
public:
MMapping() : handle(-1), file_size(0), mapping(nullptr) {}
MMapping(const std::string& filename, int flags = 0, uintptr_t size = 0) : MMapping() { open(filename.data(), flags, size); }
MMapping(uintptr_t size, int flags = 0) : MMapping() { open(nullptr, flags, size); }
~MMapping() { close(); }
inline void open(const char* file, int flags, std::size_t size) {
close();
int h = -1;
if (file) {
h = ::open(file, O_RDWR | flags, 0655);
if (h < 0) {
auto err = errno;
throw std::logic_error("Coud not open file " + std::string(file) +
":" + std::string(strerror(err)));
}
if (size == 0) {
lseek(h, 0, SEEK_END);
size = lseek(h, 0, SEEK_CUR);
} else {
auto res = ::ftruncate(h, size);
if (res < 0) {
auto err = errno;
throw std::logic_error("Could not resize file: " +
std::string(strerror(err)));
}
}
}
file_size = size;
auto m = mmap(nullptr, file_size, PROT_READ | PROT_WRITE, h == -1 ? MAP_PRIVATE | MAP_ANONYMOUS : MAP_SHARED, h, 0);
if (m == MAP_FAILED) {
auto err = errno;
::close(h);
throw std::logic_error("Cloud not mmap file: " +
std::string(strerror(err)));
}
handle = h;
mapping = static_cast<T*>(m);
}
inline void close() {
if (mapping) {
::munmap(mapping, file_size);
mapping = nullptr;
}
if (handle >= 0) {
::close(handle);
handle = -1;
}
}
inline void flush() const {
if (handle >= 0) { ::fdatasync(handle); }
}
inline T* data() const { return mapping; }
inline const iterator begin() const { return data(); }
inline const iterator end() const { return data() + (this->file_size / sizeof(T)); }
};
struct fixed_size {
static constexpr bool IS_VARIABLE = false;
};
template <typename T>
struct DataColumn : MMapping<T> {
using size_tag = fixed_size;
using iterator = T*;
using value_type = T;
static constexpr uintptr_t GLOBAL_OVERHEAD = 0;
static constexpr uintptr_t PER_ITEM_OVERHEAD = 0;
uintptr_t count = 0ul;
DataColumn(const char* filename, int flags = 0, uintptr_t size = 0)
: MMapping<T>(filename, flags, size)
, count(this->file_size / sizeof(T)) {
assert(this->file_size % sizeof(T) == 0);
}
uintptr_t size() const { return count; }
const T& operator[](std::size_t idx) const { return this->data()[idx]; }
};
struct variable_size {
static constexpr bool IS_VARIABLE = true;
struct StringIndexSlot {
uint64_t size;
uint64_t offset;
};
struct StringData {
uint64_t count;
StringIndexSlot slot[];
};
};
template <>
struct DataColumn<std::string_view> : MMapping<variable_size::StringData> {
using size_tag = variable_size;
using Data = typename variable_size::StringData;
using value_type = std::string_view;
static constexpr uintptr_t PER_ITEM_OVERHEAD = sizeof(variable_size::StringIndexSlot);
static constexpr uintptr_t GLOBAL_OVERHEAD = sizeof(uint64_t);
struct iterator {
uint64_t idx;
Data* data;
inline iterator& operator++() {
++idx;
return *this;
}
inline iterator operator++(int) {
auto prev = *this;
++idx;
return prev;
}
std::string_view operator*() {
auto slot = data->slot[idx];
return std::string_view(reinterpret_cast<char*>(data) + slot.offset,
slot.size);
}
bool operator==(const iterator& rhs) {
return this->idx == rhs.idx && this->data == rhs.data;
}
};
DataColumn(const char* filename, int flags = 0, uintptr_t size = 0)
: MMapping<Data>(filename, flags, size) {}
Data* data() const { return this->mapping; }
uintptr_t size() const { return data()->count; }
inline const iterator begin() const { return iterator{0, data()}; }
inline const iterator end() const { return iterator{data()->count, data()}; }
inline variable_size::StringIndexSlot& slot_at(std::size_t idx) const {
return data()->slot[idx];
}
inline std::string_view operator[](std::size_t idx) const {
auto slot = data()->slot[idx];
return std::string_view(reinterpret_cast<char*>(data()) + slot.offset,
slot.size);
}
};
} // namespace io