rrudb 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +1 -0
- data/LICENSE.txt +22 -0
- data/README.md +26 -0
- data/examples/example.rb +39 -0
- data/ext/rudb/NuDB/include/nudb/CMakeLists.txt +104 -0
- data/ext/rudb/NuDB/include/nudb/_experimental/basic_seconds_clock.hpp +200 -0
- data/ext/rudb/NuDB/include/nudb/_experimental/chrono_util.hpp +58 -0
- data/ext/rudb/NuDB/include/nudb/_experimental/test/fail_file.hpp +343 -0
- data/ext/rudb/NuDB/include/nudb/_experimental/test/temp_dir.hpp +73 -0
- data/ext/rudb/NuDB/include/nudb/_experimental/test/test_store.hpp +451 -0
- data/ext/rudb/NuDB/include/nudb/_experimental/test/xor_shift_engine.hpp +105 -0
- data/ext/rudb/NuDB/include/nudb/_experimental/util.hpp +288 -0
- data/ext/rudb/NuDB/include/nudb/basic_store.hpp +461 -0
- data/ext/rudb/NuDB/include/nudb/concepts.hpp +205 -0
- data/ext/rudb/NuDB/include/nudb/context.hpp +144 -0
- data/ext/rudb/NuDB/include/nudb/create.hpp +117 -0
- data/ext/rudb/NuDB/include/nudb/detail/arena.hpp +296 -0
- data/ext/rudb/NuDB/include/nudb/detail/bucket.hpp +473 -0
- data/ext/rudb/NuDB/include/nudb/detail/buffer.hpp +86 -0
- data/ext/rudb/NuDB/include/nudb/detail/bulkio.hpp +196 -0
- data/ext/rudb/NuDB/include/nudb/detail/cache.hpp +236 -0
- data/ext/rudb/NuDB/include/nudb/detail/endian.hpp +93 -0
- data/ext/rudb/NuDB/include/nudb/detail/field.hpp +265 -0
- data/ext/rudb/NuDB/include/nudb/detail/format.hpp +630 -0
- data/ext/rudb/NuDB/include/nudb/detail/gentex.hpp +259 -0
- data/ext/rudb/NuDB/include/nudb/detail/mutex.hpp +26 -0
- data/ext/rudb/NuDB/include/nudb/detail/pool.hpp +243 -0
- data/ext/rudb/NuDB/include/nudb/detail/store_base.hpp +45 -0
- data/ext/rudb/NuDB/include/nudb/detail/stream.hpp +149 -0
- data/ext/rudb/NuDB/include/nudb/detail/xxhash.hpp +328 -0
- data/ext/rudb/NuDB/include/nudb/error.hpp +257 -0
- data/ext/rudb/NuDB/include/nudb/file.hpp +55 -0
- data/ext/rudb/NuDB/include/nudb/impl/basic_store.ipp +785 -0
- data/ext/rudb/NuDB/include/nudb/impl/context.ipp +241 -0
- data/ext/rudb/NuDB/include/nudb/impl/create.ipp +163 -0
- data/ext/rudb/NuDB/include/nudb/impl/error.ipp +175 -0
- data/ext/rudb/NuDB/include/nudb/impl/posix_file.ipp +248 -0
- data/ext/rudb/NuDB/include/nudb/impl/recover.ipp +209 -0
- data/ext/rudb/NuDB/include/nudb/impl/rekey.ipp +248 -0
- data/ext/rudb/NuDB/include/nudb/impl/verify.ipp +634 -0
- data/ext/rudb/NuDB/include/nudb/impl/visit.ipp +96 -0
- data/ext/rudb/NuDB/include/nudb/impl/win32_file.ipp +264 -0
- data/ext/rudb/NuDB/include/nudb/native_file.hpp +76 -0
- data/ext/rudb/NuDB/include/nudb/nudb.hpp +27 -0
- data/ext/rudb/NuDB/include/nudb/posix_file.hpp +228 -0
- data/ext/rudb/NuDB/include/nudb/progress.hpp +32 -0
- data/ext/rudb/NuDB/include/nudb/recover.hpp +73 -0
- data/ext/rudb/NuDB/include/nudb/rekey.hpp +110 -0
- data/ext/rudb/NuDB/include/nudb/store.hpp +27 -0
- data/ext/rudb/NuDB/include/nudb/type_traits.hpp +63 -0
- data/ext/rudb/NuDB/include/nudb/verify.hpp +200 -0
- data/ext/rudb/NuDB/include/nudb/version.hpp +21 -0
- data/ext/rudb/NuDB/include/nudb/visit.hpp +63 -0
- data/ext/rudb/NuDB/include/nudb/win32_file.hpp +246 -0
- data/ext/rudb/NuDB/include/nudb/xxhasher.hpp +45 -0
- data/ext/rudb/extconf.rb +12 -0
- data/ext/rudb/rudb.cpp +234 -0
- data/lib/rudb/version.rb +3 -0
- data/lib/rudb.rb +1 -0
- metadata +104 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
//
|
2
|
+
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
3
|
+
//
|
4
|
+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
5
|
+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
6
|
+
//
|
7
|
+
|
8
|
+
#ifndef NUDB_IMPL_VISIT_IPP
|
9
|
+
#define NUDB_IMPL_VISIT_IPP
|
10
|
+
|
11
|
+
#include <nudb/concepts.hpp>
|
12
|
+
#include <nudb/error.hpp>
|
13
|
+
#include <nudb/type_traits.hpp>
|
14
|
+
#include <nudb/native_file.hpp>
|
15
|
+
#include <nudb/detail/bulkio.hpp>
|
16
|
+
#include <nudb/detail/format.hpp>
|
17
|
+
#include <algorithm>
|
18
|
+
#include <cstddef>
|
19
|
+
#include <string>
|
20
|
+
|
21
|
+
namespace nudb {
|
22
|
+
|
23
|
+
template<
|
24
|
+
class Callback,
|
25
|
+
class Progress>
|
26
|
+
void
|
27
|
+
visit(
|
28
|
+
path_type const& path,
|
29
|
+
Callback&& callback,
|
30
|
+
Progress&& progress,
|
31
|
+
error_code& ec)
|
32
|
+
{
|
33
|
+
// VFALCO Need concept check for Callback
|
34
|
+
static_assert(is_Progress<Progress>::value,
|
35
|
+
"Progress requirements not met");
|
36
|
+
using namespace detail;
|
37
|
+
using File = native_file;
|
38
|
+
auto const readSize = 1024 * block_size(path);
|
39
|
+
File df;
|
40
|
+
df.open(file_mode::scan, path, ec);
|
41
|
+
if(ec)
|
42
|
+
return;
|
43
|
+
dat_file_header dh;
|
44
|
+
read(df, dh, ec);
|
45
|
+
if(ec)
|
46
|
+
return;
|
47
|
+
verify(dh, ec);
|
48
|
+
if(ec)
|
49
|
+
return;
|
50
|
+
auto const fileSize = df.size(ec);
|
51
|
+
if(ec)
|
52
|
+
return;
|
53
|
+
bulk_reader<File> r(df,
|
54
|
+
dat_file_header::size, fileSize, readSize);
|
55
|
+
progress(0, fileSize);
|
56
|
+
while(! r.eof())
|
57
|
+
{
|
58
|
+
// Data Record or Spill Record
|
59
|
+
nsize_t size;
|
60
|
+
auto is = r.prepare(
|
61
|
+
field<uint48_t>::size, ec); // Size
|
62
|
+
if(ec)
|
63
|
+
return;
|
64
|
+
detail::read_size48(is, size);
|
65
|
+
if(size > 0)
|
66
|
+
{
|
67
|
+
// Data Record
|
68
|
+
is = r.prepare(
|
69
|
+
dh.key_size + // Key
|
70
|
+
size, ec); // Data
|
71
|
+
std::uint8_t const* const key =
|
72
|
+
is.data(dh.key_size);
|
73
|
+
callback(key, dh.key_size,
|
74
|
+
is.data(size), size, ec);
|
75
|
+
if(ec)
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
else
|
79
|
+
{
|
80
|
+
// Spill Record
|
81
|
+
is = r.prepare(
|
82
|
+
field<std::uint16_t>::size, ec);
|
83
|
+
if(ec)
|
84
|
+
return;
|
85
|
+
read<std::uint16_t>(is, size); // Size
|
86
|
+
r.prepare(size, ec); // skip bucket
|
87
|
+
if(ec)
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
progress(r.offset(), fileSize);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
} // nudb
|
95
|
+
|
96
|
+
#endif
|
@@ -0,0 +1,264 @@
|
|
1
|
+
//
|
2
|
+
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
3
|
+
//
|
4
|
+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
5
|
+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
6
|
+
//
|
7
|
+
|
8
|
+
#ifndef NUDB_IMPL_WIN32_FILE_IPP
|
9
|
+
#define NUDB_IMPL_WIN32_FILE_IPP
|
10
|
+
|
11
|
+
#include <boost/assert.hpp>
|
12
|
+
|
13
|
+
namespace nudb {
|
14
|
+
|
15
|
+
inline
|
16
|
+
win32_file::
|
17
|
+
~win32_file()
|
18
|
+
{
|
19
|
+
close();
|
20
|
+
}
|
21
|
+
|
22
|
+
inline
|
23
|
+
win32_file::
|
24
|
+
win32_file(win32_file&& other)
|
25
|
+
: hf_(other.hf_)
|
26
|
+
{
|
27
|
+
other.hf_ = INVALID_HANDLE_VALUE;
|
28
|
+
}
|
29
|
+
|
30
|
+
inline
|
31
|
+
win32_file&
|
32
|
+
win32_file::
|
33
|
+
operator=(win32_file&& other)
|
34
|
+
{
|
35
|
+
if(&other == this)
|
36
|
+
return *this;
|
37
|
+
close();
|
38
|
+
hf_ = other.hf_;
|
39
|
+
other.hf_ = INVALID_HANDLE_VALUE;
|
40
|
+
return *this;
|
41
|
+
}
|
42
|
+
|
43
|
+
inline
|
44
|
+
void
|
45
|
+
win32_file::
|
46
|
+
close()
|
47
|
+
{
|
48
|
+
if(hf_ != INVALID_HANDLE_VALUE)
|
49
|
+
{
|
50
|
+
::CloseHandle(hf_);
|
51
|
+
hf_ = INVALID_HANDLE_VALUE;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
inline
|
56
|
+
void
|
57
|
+
win32_file::
|
58
|
+
create(file_mode mode, path_type const& path, error_code& ec)
|
59
|
+
{
|
60
|
+
BOOST_ASSERT(! is_open());
|
61
|
+
auto const f = flags(mode);
|
62
|
+
hf_ = ::CreateFileA(path.c_str(),
|
63
|
+
f.first,
|
64
|
+
0,
|
65
|
+
NULL,
|
66
|
+
CREATE_NEW,
|
67
|
+
f.second,
|
68
|
+
NULL);
|
69
|
+
if(hf_ == INVALID_HANDLE_VALUE)
|
70
|
+
return last_err(ec);
|
71
|
+
}
|
72
|
+
|
73
|
+
inline
|
74
|
+
void
|
75
|
+
win32_file::
|
76
|
+
open(file_mode mode, path_type const& path, error_code& ec)
|
77
|
+
{
|
78
|
+
BOOST_ASSERT(! is_open());
|
79
|
+
auto const f = flags(mode);
|
80
|
+
hf_ = ::CreateFileA(path.c_str(),
|
81
|
+
f.first,
|
82
|
+
0,
|
83
|
+
NULL,
|
84
|
+
OPEN_EXISTING,
|
85
|
+
f.second,
|
86
|
+
NULL);
|
87
|
+
if(hf_ == INVALID_HANDLE_VALUE)
|
88
|
+
return last_err(ec);
|
89
|
+
}
|
90
|
+
|
91
|
+
inline
|
92
|
+
void
|
93
|
+
win32_file::
|
94
|
+
erase(path_type const& path, error_code& ec)
|
95
|
+
{
|
96
|
+
BOOL const bSuccess =
|
97
|
+
::DeleteFileA(path.c_str());
|
98
|
+
if(! bSuccess)
|
99
|
+
return last_err(ec);
|
100
|
+
}
|
101
|
+
|
102
|
+
inline
|
103
|
+
std::uint64_t
|
104
|
+
win32_file::
|
105
|
+
size(error_code& ec) const
|
106
|
+
{
|
107
|
+
BOOST_ASSERT(is_open());
|
108
|
+
LARGE_INTEGER fileSize;
|
109
|
+
if(! ::GetFileSizeEx(hf_, &fileSize))
|
110
|
+
{
|
111
|
+
last_err(ec);
|
112
|
+
return 0;
|
113
|
+
}
|
114
|
+
return fileSize.QuadPart;
|
115
|
+
}
|
116
|
+
|
117
|
+
inline
|
118
|
+
void
|
119
|
+
win32_file::
|
120
|
+
read(std::uint64_t offset, void* buffer, std::size_t bytes, error_code& ec)
|
121
|
+
{
|
122
|
+
while(bytes > 0)
|
123
|
+
{
|
124
|
+
DWORD bytesRead;
|
125
|
+
LARGE_INTEGER li;
|
126
|
+
li.QuadPart = static_cast<LONGLONG>(offset);
|
127
|
+
OVERLAPPED ov;
|
128
|
+
ov.Offset = li.LowPart;
|
129
|
+
ov.OffsetHigh = li.HighPart;
|
130
|
+
ov.hEvent = NULL;
|
131
|
+
DWORD amount;
|
132
|
+
if(bytes > std::numeric_limits<DWORD>::max())
|
133
|
+
amount = std::numeric_limits<DWORD>::max();
|
134
|
+
else
|
135
|
+
amount = static_cast<DWORD>(bytes);
|
136
|
+
BOOL const bSuccess = ::ReadFile(
|
137
|
+
hf_, buffer, amount, &bytesRead, &ov);
|
138
|
+
if(! bSuccess)
|
139
|
+
{
|
140
|
+
DWORD const dwError = ::GetLastError();
|
141
|
+
if(dwError != ERROR_HANDLE_EOF)
|
142
|
+
return err(dwError, ec);
|
143
|
+
ec = make_error_code(error::short_read);
|
144
|
+
return;
|
145
|
+
}
|
146
|
+
if(bytesRead == 0)
|
147
|
+
{
|
148
|
+
ec = make_error_code(error::short_read);
|
149
|
+
return;
|
150
|
+
}
|
151
|
+
offset += bytesRead;
|
152
|
+
bytes -= bytesRead;
|
153
|
+
buffer = reinterpret_cast<char*>(
|
154
|
+
buffer) + bytesRead;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
inline
|
159
|
+
void
|
160
|
+
win32_file::
|
161
|
+
write(std::uint64_t offset,
|
162
|
+
void const* buffer, std::size_t bytes, error_code& ec)
|
163
|
+
{
|
164
|
+
while(bytes > 0)
|
165
|
+
{
|
166
|
+
LARGE_INTEGER li;
|
167
|
+
li.QuadPart = static_cast<LONGLONG>(offset);
|
168
|
+
OVERLAPPED ov;
|
169
|
+
ov.Offset = li.LowPart;
|
170
|
+
ov.OffsetHigh = li.HighPart;
|
171
|
+
ov.hEvent = NULL;
|
172
|
+
DWORD amount;
|
173
|
+
if(bytes > std::numeric_limits<DWORD>::max())
|
174
|
+
amount = std::numeric_limits<DWORD>::max();
|
175
|
+
else
|
176
|
+
amount = static_cast<DWORD>(bytes);
|
177
|
+
DWORD bytesWritten;
|
178
|
+
BOOL const bSuccess = ::WriteFile(
|
179
|
+
hf_, buffer, amount, &bytesWritten, &ov);
|
180
|
+
if(! bSuccess)
|
181
|
+
return last_err(ec);
|
182
|
+
if(bytesWritten == 0)
|
183
|
+
{
|
184
|
+
ec = error_code{errc::no_space_on_device,
|
185
|
+
generic_category()};;
|
186
|
+
return;
|
187
|
+
}
|
188
|
+
offset += bytesWritten;
|
189
|
+
bytes -= bytesWritten;
|
190
|
+
buffer = reinterpret_cast<char const*>(
|
191
|
+
buffer) + bytesWritten;
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
195
|
+
inline
|
196
|
+
void
|
197
|
+
win32_file::
|
198
|
+
sync(error_code& ec)
|
199
|
+
{
|
200
|
+
if(! ::FlushFileBuffers(hf_))
|
201
|
+
return last_err(ec);
|
202
|
+
}
|
203
|
+
|
204
|
+
inline
|
205
|
+
void
|
206
|
+
win32_file::
|
207
|
+
trunc(std::uint64_t length, error_code& ec)
|
208
|
+
{
|
209
|
+
LARGE_INTEGER li;
|
210
|
+
li.QuadPart = length;
|
211
|
+
BOOL bSuccess;
|
212
|
+
bSuccess = ::SetFilePointerEx(
|
213
|
+
hf_, li, NULL, FILE_BEGIN);
|
214
|
+
if(bSuccess)
|
215
|
+
bSuccess = ::SetEndOfFile(hf_);
|
216
|
+
if(! bSuccess)
|
217
|
+
return last_err(ec);
|
218
|
+
}
|
219
|
+
|
220
|
+
inline
|
221
|
+
std::pair<DWORD, DWORD>
|
222
|
+
win32_file::
|
223
|
+
flags(file_mode mode)
|
224
|
+
{
|
225
|
+
std::pair<DWORD, DWORD> result{0, 0};
|
226
|
+
switch(mode)
|
227
|
+
{
|
228
|
+
case file_mode::scan:
|
229
|
+
result.first =
|
230
|
+
GENERIC_READ;
|
231
|
+
result.second =
|
232
|
+
FILE_FLAG_SEQUENTIAL_SCAN;
|
233
|
+
break;
|
234
|
+
|
235
|
+
case file_mode::read:
|
236
|
+
result.first =
|
237
|
+
GENERIC_READ;
|
238
|
+
result.second =
|
239
|
+
FILE_FLAG_RANDOM_ACCESS;
|
240
|
+
break;
|
241
|
+
|
242
|
+
case file_mode::append:
|
243
|
+
result.first =
|
244
|
+
GENERIC_READ | GENERIC_WRITE;
|
245
|
+
result.second =
|
246
|
+
FILE_FLAG_RANDOM_ACCESS
|
247
|
+
//| FILE_FLAG_NO_BUFFERING
|
248
|
+
//| FILE_FLAG_WRITE_THROUGH
|
249
|
+
;
|
250
|
+
break;
|
251
|
+
|
252
|
+
case file_mode::write:
|
253
|
+
result.first =
|
254
|
+
GENERIC_READ | GENERIC_WRITE;
|
255
|
+
result.second =
|
256
|
+
FILE_FLAG_RANDOM_ACCESS;
|
257
|
+
break;
|
258
|
+
}
|
259
|
+
return result;
|
260
|
+
}
|
261
|
+
|
262
|
+
} // nudb
|
263
|
+
|
264
|
+
#endif
|
@@ -0,0 +1,76 @@
|
|
1
|
+
//
|
2
|
+
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
3
|
+
//
|
4
|
+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
5
|
+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
6
|
+
//
|
7
|
+
|
8
|
+
#ifndef NUDB_NATIVE_FILE_HPP
|
9
|
+
#define NUDB_NATIVE_FILE_HPP
|
10
|
+
|
11
|
+
#include <nudb/error.hpp>
|
12
|
+
#include <nudb/file.hpp>
|
13
|
+
#include <nudb/posix_file.hpp>
|
14
|
+
#include <nudb/win32_file.hpp>
|
15
|
+
#include <string>
|
16
|
+
|
17
|
+
namespace nudb {
|
18
|
+
|
19
|
+
/** A native file handle.
|
20
|
+
|
21
|
+
This type is set to the appropriate platform-specific
|
22
|
+
implementation to meet the file wrapper requirements.
|
23
|
+
*/
|
24
|
+
using native_file =
|
25
|
+
#ifdef _MSC_VER
|
26
|
+
win32_file;
|
27
|
+
#else
|
28
|
+
posix_file;
|
29
|
+
#endif
|
30
|
+
|
31
|
+
/** Erase a file if it exists.
|
32
|
+
|
33
|
+
This function attempts to erase the specified file.
|
34
|
+
No error is generated if the file does not already
|
35
|
+
exist.
|
36
|
+
|
37
|
+
@param path The path to the file to erase.
|
38
|
+
|
39
|
+
@param ec Set to the error, if any occurred.
|
40
|
+
|
41
|
+
@tparam File A type meeting the requirements of @b File.
|
42
|
+
If this type is unspecified, @ref native_file is used.
|
43
|
+
*/
|
44
|
+
template<class File = native_file>
|
45
|
+
inline
|
46
|
+
void
|
47
|
+
erase_file(path_type const& path, error_code& ec)
|
48
|
+
{
|
49
|
+
native_file::erase(path, ec);
|
50
|
+
if(ec == errc::no_such_file_or_directory)
|
51
|
+
ec = {};
|
52
|
+
}
|
53
|
+
|
54
|
+
/** Erase a file without returnign an error.
|
55
|
+
|
56
|
+
This function attempts to erase the specified file.
|
57
|
+
Any errors are ignored, including if the file does
|
58
|
+
not exist.
|
59
|
+
|
60
|
+
@param path The path to the file to erase.
|
61
|
+
|
62
|
+
@tparam File A type meeting the requirements of @b File.
|
63
|
+
If this type is unspecified, @ref native_file is used.
|
64
|
+
*/
|
65
|
+
template<class File = native_file>
|
66
|
+
inline
|
67
|
+
void
|
68
|
+
erase_file(path_type const& path)
|
69
|
+
{
|
70
|
+
error_code ec;
|
71
|
+
File::erase(path, ec);
|
72
|
+
}
|
73
|
+
|
74
|
+
} // nudb
|
75
|
+
|
76
|
+
#endif
|
@@ -0,0 +1,27 @@
|
|
1
|
+
//
|
2
|
+
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
3
|
+
//
|
4
|
+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
5
|
+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
6
|
+
//
|
7
|
+
|
8
|
+
#ifndef NUDB_HPP
|
9
|
+
#define NUDB_HPP
|
10
|
+
|
11
|
+
#include <nudb/concepts.hpp>
|
12
|
+
#include <nudb/create.hpp>
|
13
|
+
#include <nudb/error.hpp>
|
14
|
+
#include <nudb/file.hpp>
|
15
|
+
#include <nudb/posix_file.hpp>
|
16
|
+
#include <nudb/progress.hpp>
|
17
|
+
#include <nudb/recover.hpp>
|
18
|
+
#include <nudb/rekey.hpp>
|
19
|
+
#include <nudb/store.hpp>
|
20
|
+
#include <nudb/type_traits.hpp>
|
21
|
+
#include <nudb/verify.hpp>
|
22
|
+
#include <nudb/version.hpp>
|
23
|
+
#include <nudb/visit.hpp>
|
24
|
+
#include <nudb/win32_file.hpp>
|
25
|
+
#include <nudb/xxhasher.hpp>
|
26
|
+
|
27
|
+
#endif
|
@@ -0,0 +1,228 @@
|
|
1
|
+
//
|
2
|
+
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
3
|
+
//
|
4
|
+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
5
|
+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
6
|
+
//
|
7
|
+
|
8
|
+
#ifndef NUDB_DETAIL_POSIX_FILE_HPP
|
9
|
+
#define NUDB_DETAIL_POSIX_FILE_HPP
|
10
|
+
|
11
|
+
#include <nudb/file.hpp>
|
12
|
+
#include <nudb/error.hpp>
|
13
|
+
#include <cerrno>
|
14
|
+
#include <cstring>
|
15
|
+
#include <string>
|
16
|
+
#include <utility>
|
17
|
+
|
18
|
+
#ifndef NUDB_POSIX_FILE
|
19
|
+
# ifdef _MSC_VER
|
20
|
+
# define NUDB_POSIX_FILE 0
|
21
|
+
# else
|
22
|
+
# define NUDB_POSIX_FILE 1
|
23
|
+
# endif
|
24
|
+
#endif
|
25
|
+
|
26
|
+
#if NUDB_POSIX_FILE
|
27
|
+
# include <fcntl.h>
|
28
|
+
# include <sys/types.h>
|
29
|
+
# include <sys/uio.h>
|
30
|
+
# include <sys/stat.h>
|
31
|
+
# include <unistd.h>
|
32
|
+
#endif
|
33
|
+
|
34
|
+
#if NUDB_POSIX_FILE
|
35
|
+
|
36
|
+
namespace nudb {
|
37
|
+
|
38
|
+
class posix_file
|
39
|
+
{
|
40
|
+
int fd_ = -1;
|
41
|
+
|
42
|
+
public:
|
43
|
+
/// Constructor
|
44
|
+
posix_file() = default;
|
45
|
+
|
46
|
+
/// Copy constructor (disallowed)
|
47
|
+
posix_file(posix_file const&) = delete;
|
48
|
+
|
49
|
+
// Copy assignment (disallowed)
|
50
|
+
posix_file& operator=(posix_file const&) = delete;
|
51
|
+
|
52
|
+
/** Destructor.
|
53
|
+
|
54
|
+
If open, the file is closed.
|
55
|
+
*/
|
56
|
+
~posix_file();
|
57
|
+
|
58
|
+
/** Move constructor.
|
59
|
+
|
60
|
+
@note The state of the moved-from object is as if default constructed.
|
61
|
+
*/
|
62
|
+
posix_file(posix_file&&);
|
63
|
+
|
64
|
+
/** Move assignment.
|
65
|
+
|
66
|
+
@note The state of the moved-from object is as if default constructed.
|
67
|
+
*/
|
68
|
+
posix_file&
|
69
|
+
operator=(posix_file&& other);
|
70
|
+
|
71
|
+
/// Returns `true` if the file is open.
|
72
|
+
bool
|
73
|
+
is_open() const
|
74
|
+
{
|
75
|
+
return fd_ != -1;
|
76
|
+
}
|
77
|
+
|
78
|
+
/// Close the file if it is open.
|
79
|
+
void
|
80
|
+
close();
|
81
|
+
|
82
|
+
/** Create a new file.
|
83
|
+
|
84
|
+
After the file is created, it is opened as if by `open(mode, path, ec)`.
|
85
|
+
|
86
|
+
@par Requirements
|
87
|
+
|
88
|
+
The file must not already exist, or else `errc::file_exists`
|
89
|
+
is returned.
|
90
|
+
|
91
|
+
@param mode The open mode, which must be a valid @ref file_mode.
|
92
|
+
|
93
|
+
@param path The path of the file to create.
|
94
|
+
|
95
|
+
@param ec Set to the error, if any occurred.
|
96
|
+
*/
|
97
|
+
void
|
98
|
+
create(file_mode mode, path_type const& path, error_code& ec);
|
99
|
+
|
100
|
+
/** Open a file.
|
101
|
+
|
102
|
+
@par Requirements
|
103
|
+
|
104
|
+
The file must not already be open.
|
105
|
+
|
106
|
+
@param mode The open mode, which must be a valid @ref file_mode.
|
107
|
+
|
108
|
+
@param path The path of the file to open.
|
109
|
+
|
110
|
+
@param ec Set to the error, if any occurred.
|
111
|
+
*/
|
112
|
+
void
|
113
|
+
open(file_mode mode, path_type const& path, error_code& ec);
|
114
|
+
|
115
|
+
/** Remove a file from the file system.
|
116
|
+
|
117
|
+
It is not an error to attempt to erase a file that does not exist.
|
118
|
+
|
119
|
+
@param path The path of the file to remove.
|
120
|
+
|
121
|
+
@param ec Set to the error, if any occurred.
|
122
|
+
*/
|
123
|
+
static
|
124
|
+
void
|
125
|
+
erase(path_type const& path, error_code& ec);
|
126
|
+
|
127
|
+
/** Return the size of the file.
|
128
|
+
|
129
|
+
@par Requirements
|
130
|
+
|
131
|
+
The file must be open.
|
132
|
+
|
133
|
+
@param ec Set to the error, if any occurred.
|
134
|
+
|
135
|
+
@return The size of the file, in bytes.
|
136
|
+
*/
|
137
|
+
std::uint64_t
|
138
|
+
size(error_code& ec) const;
|
139
|
+
|
140
|
+
/** Read data from a location in the file.
|
141
|
+
|
142
|
+
@par Requirements
|
143
|
+
|
144
|
+
The file must be open.
|
145
|
+
|
146
|
+
@param offset The position in the file to read from,
|
147
|
+
expressed as a byte offset from the beginning.
|
148
|
+
|
149
|
+
@param buffer The location to store the data.
|
150
|
+
|
151
|
+
@param bytes The number of bytes to read.
|
152
|
+
|
153
|
+
@param ec Set to the error, if any occurred.
|
154
|
+
*/
|
155
|
+
void
|
156
|
+
read(std::uint64_t offset,
|
157
|
+
void* buffer, std::size_t bytes, error_code& ec);
|
158
|
+
|
159
|
+
/** Write data to a location in the file.
|
160
|
+
|
161
|
+
@par Requirements
|
162
|
+
|
163
|
+
The file must be open with a mode allowing writes.
|
164
|
+
|
165
|
+
@param offset The position in the file to write from,
|
166
|
+
expressed as a byte offset from the beginning.
|
167
|
+
|
168
|
+
@param buffer The data the write.
|
169
|
+
|
170
|
+
@param bytes The number of bytes to write.
|
171
|
+
|
172
|
+
@param ec Set to the error, if any occurred.
|
173
|
+
*/
|
174
|
+
void
|
175
|
+
write(std::uint64_t offset,
|
176
|
+
void const* buffer, std::size_t bytes, error_code& ec);
|
177
|
+
|
178
|
+
/** Perform a low level file synchronization.
|
179
|
+
|
180
|
+
@par Requirements
|
181
|
+
|
182
|
+
The file must be open with a mode allowing writes.
|
183
|
+
|
184
|
+
@param ec Set to the error, if any occurred.
|
185
|
+
*/
|
186
|
+
void
|
187
|
+
sync(error_code& ec);
|
188
|
+
|
189
|
+
/** Truncate the file at a specific size.
|
190
|
+
|
191
|
+
@par Requirements
|
192
|
+
|
193
|
+
The file must be open with a mode allowing writes.
|
194
|
+
|
195
|
+
@param length The new file size.
|
196
|
+
|
197
|
+
@param ec Set to the error, if any occurred.
|
198
|
+
*/
|
199
|
+
void
|
200
|
+
trunc(std::uint64_t length, error_code& ec);
|
201
|
+
|
202
|
+
private:
|
203
|
+
static
|
204
|
+
void
|
205
|
+
err(int ev, error_code& ec)
|
206
|
+
{
|
207
|
+
ec = error_code{ev, system_category()};
|
208
|
+
}
|
209
|
+
|
210
|
+
static
|
211
|
+
void
|
212
|
+
last_err(error_code& ec)
|
213
|
+
{
|
214
|
+
err(errno, ec);
|
215
|
+
}
|
216
|
+
|
217
|
+
static
|
218
|
+
std::pair<int, int>
|
219
|
+
flags(file_mode mode);
|
220
|
+
};
|
221
|
+
|
222
|
+
} // nudb
|
223
|
+
|
224
|
+
#include <nudb/impl/posix_file.ipp>
|
225
|
+
|
226
|
+
#endif
|
227
|
+
|
228
|
+
#endif
|
@@ -0,0 +1,32 @@
|
|
1
|
+
//
|
2
|
+
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
3
|
+
//
|
4
|
+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
5
|
+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
6
|
+
//
|
7
|
+
|
8
|
+
#ifndef NUDB_PROGRESS_HPP
|
9
|
+
#define NUDB_PROGRESS_HPP
|
10
|
+
|
11
|
+
namespace nudb {
|
12
|
+
|
13
|
+
/** Progress function that does nothing.
|
14
|
+
|
15
|
+
This type meets the requirements of @b Progress,
|
16
|
+
and does nothing when invoked.
|
17
|
+
*/
|
18
|
+
struct
|
19
|
+
no_progress
|
20
|
+
{
|
21
|
+
no_progress() = default;
|
22
|
+
|
23
|
+
/// Called to indicate progress
|
24
|
+
void
|
25
|
+
operator()(std::uint64_t, std::uint64_t) const noexcept
|
26
|
+
{
|
27
|
+
};
|
28
|
+
};
|
29
|
+
|
30
|
+
} // nudb
|
31
|
+
|
32
|
+
#endif
|