Skip to content

Commit 37ee4fd

Browse files
committed
Add dir based open & create calls, init files from default names under dir
1 parent 79c1dca commit 37ee4fd

5 files changed

Lines changed: 138 additions & 0 deletions

File tree

include/nudb/basic_store.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,36 @@ class basic_store
343343
error_code& ec,
344344
Args&&... args);
345345

346+
/** Open a database.
347+
348+
The database identified by the specified directory
349+
under which default data and key paths are opened.
350+
351+
@par Requirements
352+
353+
The database must be not be open.
354+
355+
@par Thread safety
356+
357+
Not thread safe. The caller is responsible for
358+
ensuring that no other member functions are
359+
called concurrently.
360+
361+
@param dir_path The path to the directory containing data
362+
and key files.
363+
364+
@param ec Set to the error, if any occurred.
365+
366+
@param args Optional arguments passed to @b File constructors.
367+
368+
*/
369+
template<class... Args>
370+
void
371+
open(
372+
path_type const& dir_path,
373+
error_code& ec,
374+
Args&&... args);
375+
346376
/** Fetch a value.
347377
348378
The function checks the database for the specified

include/nudb/create.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ create(
110110
error_code& ec,
111111
Args&&... args);
112112

113+
/** Create a new database in specified directory.
114+
115+
Similar to create method with explicit dat,
116+
key, and log paths, with the default filenames
117+
being used.
118+
119+
@param dir_path The path to the data file.
120+
121+
*/
122+
template<
123+
class Hasher,
124+
class File = native_file,
125+
class... Args
126+
>
127+
void
128+
create(
129+
path_type const& dir_path,
130+
std::uint64_t appnum,
131+
std::uint64_t salt,
132+
nsize_t key_size,
133+
nsize_t blockSize,
134+
float load_factor,
135+
error_code& ec,
136+
Args&&... args);
137+
113138
} // nudb
114139

115140
#include <nudb/impl/create.ipp>

include/nudb/detail/format.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ value size up to 32 bits (or 32-bit builds can't read it)
4646

4747
static std::size_t constexpr currentVersion = 2;
4848

49+
const std::string& default_dat_file() {
50+
static const std::string ddf("nudb.dat");
51+
return ddf;
52+
}
53+
54+
const std::string& default_key_file() {
55+
static const std::string dkf("nudb.key");
56+
return dkf;
57+
}
58+
59+
const std::string& default_log_file() {
60+
static const std::string dkf("nudb.log");
61+
return dkf;
62+
}
63+
4964
struct dat_file_header
5065
{
5166
static std::size_t constexpr size =

include/nudb/impl/basic_store.ipp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <boost/assert.hpp>
1414
#include <cmath>
1515
#include <memory>
16+
#include <boost/filesystem.hpp>
1617

1718
#ifndef NUDB_DEBUG_LOG
1819
#define NUDB_DEBUG_LOG 0
@@ -187,6 +188,32 @@ open(
187188
ctx_->insert(*this);
188189
}
189190

191+
template<class Hasher, class File>
192+
template<class... Args>
193+
void
194+
basic_store<Hasher, File>::
195+
open(
196+
path_type const& dir_path,
197+
error_code& ec,
198+
Args&&... args)
199+
{
200+
BOOST_ASSERT(boost::filesystem::exists(dir_path));
201+
202+
boost::filesystem::path fs_dir_path(dir_path);
203+
204+
boost::filesystem::path dat_path =
205+
fs_dir_path / detail::default_dat_file();
206+
boost::filesystem::path key_path =
207+
fs_dir_path / detail::default_key_file();
208+
boost::filesystem::path log_path =
209+
fs_dir_path / detail::default_log_file();
210+
211+
open(dat_path.string(),
212+
key_path.string(),
213+
log_path.string(),
214+
ec, args...);
215+
}
216+
190217
template<class Hasher, class File>
191218
void
192219
basic_store<Hasher, File>::

include/nudb/impl/create.ipp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <random>
1818
#include <stdexcept>
1919
#include <utility>
20+
#include <boost/filesystem.hpp>
2021

2122
namespace nudb {
2223

@@ -158,6 +159,46 @@ fail:
158159
erase_file(log_path);
159160
}
160161

162+
template<
163+
class Hasher,
164+
class File,
165+
class... Args
166+
>
167+
void
168+
create(
169+
path_type const& dir_path,
170+
std::uint64_t appnum,
171+
std::uint64_t salt,
172+
nsize_t key_size,
173+
nsize_t blockSize,
174+
float load_factor,
175+
error_code& ec,
176+
Args&&... args)
177+
{
178+
if(!boost::filesystem::exists(dir_path))
179+
boost::filesystem::create_directories(dir_path);
180+
181+
boost::filesystem::path fs_dir_path(dir_path);
182+
183+
boost::filesystem::path dat_path =
184+
fs_dir_path / detail::default_dat_file();
185+
boost::filesystem::path key_path =
186+
fs_dir_path / detail::default_key_file();
187+
boost::filesystem::path log_path =
188+
fs_dir_path / detail::default_log_file();
189+
190+
create<Hasher, File, Args...>(dat_path.string(),
191+
key_path.string(),
192+
log_path.string(),
193+
appnum,
194+
salt,
195+
key_size,
196+
blockSize,
197+
load_factor,
198+
ec,
199+
args...);
200+
}
201+
161202
} // nudb
162203

163204
#endif

0 commit comments

Comments
 (0)