Skip to content

Commit 6d005d3

Browse files
committed
Add dir based open & create calls
Init files from default names under dir
1 parent 79c1dca commit 6d005d3

File tree

10 files changed

+335
-1
lines changed

10 files changed

+335
-1
lines changed

include/nudb/_experimental/test/test_store.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,12 @@ class basic_test_store
201201
temp_dir td_;
202202
std::uniform_int_distribution<std::size_t> sizef_;
203203
std::function<void(error_code&)> createf_;
204+
std::function<void(error_code&)> create_dirf_;
204205
std::function<void(error_code&)> openf_;
205206
Buffer buf_;
206207

207208
public:
209+
path_type const dirp;
208210
path_type const dp;
209211
path_type const kp;
210212
path_type const lp;
@@ -234,6 +236,9 @@ class basic_test_store
234236
void
235237
create(error_code& ec);
236238

239+
void
240+
create_dir(error_code& ec);
241+
237242
void
238243
open(error_code& ec);
239244

@@ -270,11 +275,20 @@ basic_test_store<File>::basic_test_store(
270275
keySize, blockSize, loadFactor, ec,
271276
args...);
272277
})
278+
, create_dirf_(
279+
[this, args...](error_code& ec)
280+
{
281+
nudb::create<Hasher, File>(
282+
dirp, appnum, salt,
283+
keySize, blockSize, loadFactor, ec,
284+
args...);
285+
})
273286
, openf_(
274287
[this, args...](error_code& ec)
275288
{
276289
db.open(dp, kp, lp, ec, args...);
277290
})
291+
, dirp(td_.path())
278292
, dp(td_.file("nudb.dat"))
279293
, kp(td_.file("nudb.key"))
280294
, lp(td_.file("nudb.log"))
@@ -330,6 +344,15 @@ create(error_code& ec)
330344
createf_(ec);
331345
}
332346

347+
template<class File>
348+
void
349+
basic_test_store<File>::
350+
create_dir(error_code& ec)
351+
{
352+
create_dirf_(ec);
353+
}
354+
355+
333356
template<class File>
334357
void
335358
basic_test_store<File>::

include/nudb/basic_store.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,40 @@ class basic_store
454454
flush() override;
455455
};
456456

457+
/** Open a database.
458+
459+
The database identified by the specified directory
460+
under which default data and key paths are opened.
461+
462+
@par Requirements
463+
464+
The database must be not be open.
465+
466+
@par Thread safety
467+
468+
Not thread safe. The caller is responsible for
469+
ensuring that no other store member functions are
470+
called concurrently.
471+
472+
@param dir_path The path to the directory containing data
473+
and key files.
474+
475+
@param store The store to open.
476+
477+
@param ec Set to the error, if any occurred.
478+
479+
@param args Optional arguments passed to @b File constructors.
480+
481+
*/
482+
template<class Hasher, class File, class... Args>
483+
void
484+
open_dir(
485+
path_type const& dir_path,
486+
basic_store<Hasher, File>& store,
487+
error_code& ec,
488+
Args&&... args);
489+
490+
457491
} // nudb
458492

459493
#include <nudb/impl/basic_store.ipp>

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ 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+
inline
50+
const path_type& default_dat_file() {
51+
static const path_type ddf("nudb.dat");
52+
return ddf;
53+
}
54+
55+
inline
56+
const path_type& default_key_file() {
57+
static const path_type dkf("nudb.key");
58+
return dkf;
59+
}
60+
61+
inline
62+
const path_type& default_log_file() {
63+
static const path_type dkf("nudb.log");
64+
return dkf;
65+
}
66+
4967
struct dat_file_header
5068
{
5169
static std::size_t constexpr size =

include/nudb/error.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ enum class error
221221
size_mismatch,
222222

223223
/// duplicate value
224-
duplicate_value
224+
duplicate_value,
225+
226+
/// directory not found
227+
dir_not_found
225228
};
226229

227230
/// Returns the error category used for database error codes.

include/nudb/file.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ enum class file_mode
5050
write
5151
};
5252

53+
// Return boolean indicating if path exists
54+
bool path_exists(path_type const& path);
55+
56+
// Return boolean indicating if path is a directory
57+
bool is_dir(path_type const& path);
58+
59+
// Recursively make the specified dir tree
60+
bool mkdir_p(path_type const& path);
61+
62+
// Append an rel-path to a local filesystem path.
63+
// The returned path is normalized for the platform.
64+
path_type
65+
path_cat(
66+
path_type const& base,
67+
path_type const& path);
68+
5369
} // nudb
5470

71+
#include <nudb/impl/file.ipp>
72+
5573
#endif

include/nudb/impl/basic_store.ipp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,32 @@ flush()
784784
ecb_.store(true);
785785
}
786786

787+
template<class Hasher, class File, class... Args>
788+
void
789+
open_dir(
790+
path_type const& dir_path,
791+
basic_store<Hasher, File>& store,
792+
error_code& ec,
793+
Args&&... args)
794+
{
795+
if(is_dir(dir_path)){
796+
ec = error::dir_not_found;
797+
return;
798+
}
799+
800+
path_type dat_path = path_cat(dir_path,
801+
detail::default_dat_file());
802+
803+
path_type key_path = path_cat(dir_path,
804+
detail::default_key_file());
805+
806+
path_type log_path = path_cat(dir_path,
807+
detail::default_log_file());
808+
809+
store.open(dat_path, key_path, log_path,
810+
ec, args...);
811+
}
812+
787813
} // nudb
788814

789815
#endif

include/nudb/impl/create.ipp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,46 @@ fail:
158158
erase_file(log_path);
159159
}
160160

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

163203
#endif

0 commit comments

Comments
 (0)