-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathcreate.hpp
More file actions
142 lines (113 loc) · 3.77 KB
/
create.hpp
File metadata and controls
142 lines (113 loc) · 3.77 KB
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
//
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef NUDB_CREATE_HPP
#define NUDB_CREATE_HPP
#include <nudb/native_file.hpp>
#include <nudb/detail/bucket.hpp>
#include <nudb/detail/format.hpp>
#include <algorithm>
#include <cstring>
#include <random>
#include <stdexcept>
#include <utility>
namespace nudb {
/** Return a random salt.
This function will use the system provided random
number device to generate a uniformly distributed
64-bit unsigned value suitable for use the salt
value in a call to @ref create.
*/
template<class = void>
std::uint64_t
make_salt();
/** Create a new database.
This function creates a set of new database files with
the given parameters. The files must not already exist or
else an error is returned.
If an error occurs while the files are being created,
the function attempts to remove the files before
returning.
@par Example
@code
error_code ec;
create<xxhasher>(
"db.dat", "db.key", "db.log",
1, make_salt(), 8, 4096, 0.5f, ec);
@endcode
@par Template Parameters
@tparam Hasher The hash function to use. This type must
meet the requirements of @b Hasher. The same hash
function must be used every time the database is opened,
or else an error is returned. The provided @ref xxhasher
is a suitable general purpose hash function.
@tparam File The type of file to use. Use the default of
@ref native_file unless customizing the file behavior.
@param dat_path The path to the data file.
@param key_path The path to the key file.
@param log_path The path to the log file.
@param appnum A caller-defined value stored in the file
headers. When opening the database, the same value is
preserved and returned to the caller.
@param salt A random unsigned integer used to permute
the hash function to make it unpredictable. The return
value of @ref make_salt returns a suitable value.
@param key_size The number of bytes in each key.
@param blockSize The size of a key file block. Larger
blocks hold more keys but require more I/O cycles per
operation. The ideal block size the largest size that
may be read in a single I/O cycle, and device dependent.
The return value of @ref block_size returns a suitable
value for the volume of a given path.
@param load_factor A number between zero and one
representing the average bucket occupancy (number of
items). A value of 0.5 is perfect. Lower numbers
waste space, and higher numbers produce negligible
savings at the cost of increased I/O cycles.
@param ec Set to the error, if any occurred.
@param args Optional arguments passed to @b File constructors.
*/
template<
class Hasher,
class File = native_file,
class... Args
>
void
create(
path_type const& dat_path,
path_type const& key_path,
path_type const& log_path,
std::uint64_t appnum,
std::uint64_t salt,
nsize_t key_size,
nsize_t blockSize,
float load_factor,
error_code& ec,
Args&&... args);
/** Create a new database in specified directory.
Similar to create method with explicit dat,
key, and log paths, with the default filenames
being used.
@param dir_path The path to the data file.
*/
template<
class Hasher,
class File = native_file,
class... Args
>
void
create(
path_type const& dir_path,
std::uint64_t appnum,
std::uint64_t salt,
nsize_t key_size,
nsize_t blockSize,
float load_factor,
error_code& ec,
Args&&... args);
} // nudb
#include <nudb/impl/create.ipp>
#endif