rrudb 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,288 @@
|
|
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_UTIL_HPP
|
9
|
+
#define NUDB_UTIL_HPP
|
10
|
+
|
11
|
+
#include "basic_seconds_clock.hpp"
|
12
|
+
|
13
|
+
#include <chrono>
|
14
|
+
#include <iomanip>
|
15
|
+
#include <iostream>
|
16
|
+
#include <sstream>
|
17
|
+
|
18
|
+
namespace nudb {
|
19
|
+
|
20
|
+
template<class = void>
|
21
|
+
int
|
22
|
+
log2(std::uint64_t n)
|
23
|
+
{
|
24
|
+
int i = -(n == 0);
|
25
|
+
|
26
|
+
auto const S =
|
27
|
+
[&](int k)
|
28
|
+
{
|
29
|
+
if(n >=(std::uint64_t{1} << k))
|
30
|
+
{
|
31
|
+
i += k;
|
32
|
+
n >>= k;
|
33
|
+
}
|
34
|
+
};
|
35
|
+
S(32); S(16); S(8); S(4); S(2); S(1);
|
36
|
+
return i;
|
37
|
+
}
|
38
|
+
|
39
|
+
// Format a decimal integer with comma separators
|
40
|
+
template<class T>
|
41
|
+
std::string
|
42
|
+
fdec(T t)
|
43
|
+
{
|
44
|
+
std::string s = std::to_string(t);
|
45
|
+
std::reverse(s.begin(), s.end());
|
46
|
+
std::string s2;
|
47
|
+
s2.reserve(s.size() +(s.size()+2)/3);
|
48
|
+
int n = 0;
|
49
|
+
for(auto c : s)
|
50
|
+
{
|
51
|
+
if(n == 3)
|
52
|
+
{
|
53
|
+
n = 0;
|
54
|
+
s2.insert(s2.begin(), ',');
|
55
|
+
}
|
56
|
+
++n;
|
57
|
+
s2.insert(s2.begin(), c);
|
58
|
+
}
|
59
|
+
return s2;
|
60
|
+
}
|
61
|
+
|
62
|
+
// format 64-bit unsigned as fixed width, 0 padded hex
|
63
|
+
template<class T>
|
64
|
+
std::string
|
65
|
+
fhex(T v)
|
66
|
+
{
|
67
|
+
std::string s{"0x0000000000000000"};
|
68
|
+
auto it = s.end();
|
69
|
+
for(it = s.end(); v; v >>= 4)
|
70
|
+
*--it = "0123456789abcdef"[v & 0xf];
|
71
|
+
return s;
|
72
|
+
}
|
73
|
+
|
74
|
+
// Format an array of integers as a comma separated list
|
75
|
+
template<class T, std::size_t N>
|
76
|
+
static
|
77
|
+
std::string
|
78
|
+
fhist(std::array<T, N> const& hist)
|
79
|
+
{
|
80
|
+
std::size_t n;
|
81
|
+
for(n = hist.size() - 1; n > 0; --n)
|
82
|
+
if(hist[n])
|
83
|
+
break;
|
84
|
+
std::string s = std::to_string(hist[0]);
|
85
|
+
for(std::size_t i = 1; i <= n; ++i)
|
86
|
+
s += ", " + std::to_string(hist[i]);
|
87
|
+
return s;
|
88
|
+
}
|
89
|
+
|
90
|
+
class save_stream_state
|
91
|
+
{
|
92
|
+
std::ostream& os_;
|
93
|
+
std::streamsize precision_;
|
94
|
+
std::ios::fmtflags flags_;
|
95
|
+
std::ios::char_type fill_;
|
96
|
+
|
97
|
+
public:
|
98
|
+
~save_stream_state()
|
99
|
+
{
|
100
|
+
os_.precision(precision_);
|
101
|
+
os_.flags(flags_);
|
102
|
+
os_.fill(fill_);
|
103
|
+
}
|
104
|
+
save_stream_state(save_stream_state const&) = delete;
|
105
|
+
save_stream_state& operator=(save_stream_state const&) = delete;
|
106
|
+
explicit save_stream_state(std::ostream& os)
|
107
|
+
: os_(os)
|
108
|
+
, precision_(os.precision())
|
109
|
+
, flags_(os.flags())
|
110
|
+
, fill_(os.fill())
|
111
|
+
{
|
112
|
+
}
|
113
|
+
};
|
114
|
+
|
115
|
+
template<class Rep, class Period>
|
116
|
+
std::ostream&
|
117
|
+
pretty_time(std::ostream& os, std::chrono::duration<Rep, Period> d)
|
118
|
+
{
|
119
|
+
save_stream_state _(os);
|
120
|
+
using namespace std::chrono;
|
121
|
+
if(d < microseconds{1})
|
122
|
+
{
|
123
|
+
// use nanoseconds
|
124
|
+
if(d < nanoseconds{100})
|
125
|
+
{
|
126
|
+
// use floating
|
127
|
+
using ns = duration<float, std::nano>;
|
128
|
+
os << std::fixed << std::setprecision(1) << ns(d).count();
|
129
|
+
}
|
130
|
+
else
|
131
|
+
{
|
132
|
+
// use integral
|
133
|
+
os << round<nanoseconds>(d).count();
|
134
|
+
}
|
135
|
+
os << "ns";
|
136
|
+
}
|
137
|
+
else if(d < milliseconds{1})
|
138
|
+
{
|
139
|
+
// use microseconds
|
140
|
+
if(d < microseconds{100})
|
141
|
+
{
|
142
|
+
// use floating
|
143
|
+
using ms = duration<float, std::micro>;
|
144
|
+
os << std::fixed << std::setprecision(1) << ms(d).count();
|
145
|
+
}
|
146
|
+
else
|
147
|
+
{
|
148
|
+
// use integral
|
149
|
+
os << round<microseconds>(d).count();
|
150
|
+
}
|
151
|
+
os << "us";
|
152
|
+
}
|
153
|
+
else if(d < seconds{1})
|
154
|
+
{
|
155
|
+
// use milliseconds
|
156
|
+
if(d < milliseconds{100})
|
157
|
+
{
|
158
|
+
// use floating
|
159
|
+
using ms = duration<float, std::milli>;
|
160
|
+
os << std::fixed << std::setprecision(1) << ms(d).count();
|
161
|
+
}
|
162
|
+
else
|
163
|
+
{
|
164
|
+
// use integral
|
165
|
+
os << round<milliseconds>(d).count();
|
166
|
+
}
|
167
|
+
os << "ms";
|
168
|
+
}
|
169
|
+
else if(d < minutes{1})
|
170
|
+
{
|
171
|
+
// use seconds
|
172
|
+
if(d < seconds{100})
|
173
|
+
{
|
174
|
+
// use floating
|
175
|
+
using s = duration<float>;
|
176
|
+
os << std::fixed << std::setprecision(1) << s(d).count();
|
177
|
+
}
|
178
|
+
else
|
179
|
+
{
|
180
|
+
// use integral
|
181
|
+
os << round<seconds>(d).count();
|
182
|
+
}
|
183
|
+
os << "s";
|
184
|
+
}
|
185
|
+
else
|
186
|
+
{
|
187
|
+
// use minutes
|
188
|
+
if(d < minutes{100})
|
189
|
+
{
|
190
|
+
// use floating
|
191
|
+
using m = duration<float, std::ratio<60>>;
|
192
|
+
os << std::fixed << std::setprecision(1) << m(d).count();
|
193
|
+
}
|
194
|
+
else
|
195
|
+
{
|
196
|
+
// use integral
|
197
|
+
os << round<minutes>(d).count();
|
198
|
+
}
|
199
|
+
os << "min";
|
200
|
+
}
|
201
|
+
return os;
|
202
|
+
}
|
203
|
+
|
204
|
+
template<class Period, class Rep>
|
205
|
+
std::string
|
206
|
+
fmtdur(std::chrono::duration<Period, Rep> const& d)
|
207
|
+
{
|
208
|
+
std::stringstream ss;
|
209
|
+
pretty_time(ss, d);
|
210
|
+
return ss.str();
|
211
|
+
}
|
212
|
+
|
213
|
+
//------------------------------------------------------------------------------
|
214
|
+
|
215
|
+
class progress
|
216
|
+
{
|
217
|
+
using clock_type = basic_seconds_clock<std::chrono::steady_clock>;
|
218
|
+
|
219
|
+
std::ostream& os_;
|
220
|
+
clock_type::time_point start_;
|
221
|
+
clock_type::time_point now_;
|
222
|
+
clock_type::time_point report_;
|
223
|
+
std::uint64_t prev_;
|
224
|
+
bool estimate_;
|
225
|
+
|
226
|
+
public:
|
227
|
+
explicit
|
228
|
+
progress(std::ostream& os)
|
229
|
+
: os_(os)
|
230
|
+
{
|
231
|
+
}
|
232
|
+
|
233
|
+
void
|
234
|
+
operator()(std::uint64_t amount, std::uint64_t total)
|
235
|
+
{
|
236
|
+
using namespace std::chrono;
|
237
|
+
auto const now = clock_type::now();
|
238
|
+
if(amount == 0)
|
239
|
+
{
|
240
|
+
now_ = clock_type::now();
|
241
|
+
start_ = now_;
|
242
|
+
report_ = now_;
|
243
|
+
prev_ = 0;
|
244
|
+
estimate_ = false;
|
245
|
+
return;
|
246
|
+
}
|
247
|
+
if(now == now_)
|
248
|
+
return;
|
249
|
+
now_ = now;
|
250
|
+
auto const elapsed = now - start_;
|
251
|
+
if(! estimate_)
|
252
|
+
{
|
253
|
+
// Wait a bit before showing the first estimate
|
254
|
+
if(elapsed < seconds{30})
|
255
|
+
return;
|
256
|
+
estimate_ = true;
|
257
|
+
}
|
258
|
+
else if(now - report_ < seconds{60})
|
259
|
+
{
|
260
|
+
// Only show estimates periodically
|
261
|
+
return;
|
262
|
+
}
|
263
|
+
auto const rate = double(amount) / elapsed.count();
|
264
|
+
auto const remain = clock_type::duration{
|
265
|
+
static_cast<clock_type::duration::rep>(
|
266
|
+
(total - amount) / rate)};
|
267
|
+
os_ <<
|
268
|
+
"Remaining: " << fmtdur(remain) <<
|
269
|
+
" (" << fdec(amount) << " of " << fdec(total) <<
|
270
|
+
" in " << fmtdur(elapsed) <<
|
271
|
+
", " << fdec(amount - prev_) <<
|
272
|
+
" in " << fmtdur(now - report_) <<
|
273
|
+
")\n";
|
274
|
+
report_ = now;
|
275
|
+
prev_ = amount;
|
276
|
+
}
|
277
|
+
|
278
|
+
clock_type::duration
|
279
|
+
elapsed() const
|
280
|
+
{
|
281
|
+
using namespace std::chrono;
|
282
|
+
return now_ - start_;
|
283
|
+
}
|
284
|
+
};
|
285
|
+
|
286
|
+
} // nudb
|
287
|
+
|
288
|
+
#endif
|