Skip to content

Commit

Permalink
Add dir based open & create calls, init files from default names unde…
Browse files Browse the repository at this point in the history
…r dir
  • Loading branch information
movitto committed Jun 19, 2019
1 parent 79c1dca commit 37ee4fd
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/nudb/basic_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,36 @@ class basic_store
error_code& ec,
Args&&... args);

/** Open a database.
The database identified by the specified directory
under which default data and key paths are opened.
@par Requirements
The database must be not be open.
@par Thread safety
Not thread safe. The caller is responsible for
ensuring that no other member functions are
called concurrently.
@param dir_path The path to the directory containing data
and key files.
@param ec Set to the error, if any occurred.
@param args Optional arguments passed to @b File constructors.
*/
template<class... Args>
void
open(
path_type const& dir_path,
error_code& ec,
Args&&... args);

/** Fetch a value.
The function checks the database for the specified
Expand Down
25 changes: 25 additions & 0 deletions include/nudb/create.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ create(
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>
Expand Down
15 changes: 15 additions & 0 deletions include/nudb/detail/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ value size up to 32 bits (or 32-bit builds can't read it)

static std::size_t constexpr currentVersion = 2;

const std::string& default_dat_file() {
static const std::string ddf("nudb.dat");
return ddf;
}

const std::string& default_key_file() {
static const std::string dkf("nudb.key");
return dkf;
}

const std::string& default_log_file() {
static const std::string dkf("nudb.log");
return dkf;
}

struct dat_file_header
{
static std::size_t constexpr size =
Expand Down
27 changes: 27 additions & 0 deletions include/nudb/impl/basic_store.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <boost/assert.hpp>
#include <cmath>
#include <memory>
#include <boost/filesystem.hpp>

#ifndef NUDB_DEBUG_LOG
#define NUDB_DEBUG_LOG 0
Expand Down Expand Up @@ -187,6 +188,32 @@ open(
ctx_->insert(*this);
}

template<class Hasher, class File>
template<class... Args>
void
basic_store<Hasher, File>::
open(
path_type const& dir_path,
error_code& ec,
Args&&... args)
{
BOOST_ASSERT(boost::filesystem::exists(dir_path));

boost::filesystem::path fs_dir_path(dir_path);

boost::filesystem::path dat_path =
fs_dir_path / detail::default_dat_file();
boost::filesystem::path key_path =
fs_dir_path / detail::default_key_file();
boost::filesystem::path log_path =
fs_dir_path / detail::default_log_file();

open(dat_path.string(),
key_path.string(),
log_path.string(),
ec, args...);
}

template<class Hasher, class File>
void
basic_store<Hasher, File>::
Expand Down
41 changes: 41 additions & 0 deletions include/nudb/impl/create.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <random>
#include <stdexcept>
#include <utility>
#include <boost/filesystem.hpp>

namespace nudb {

Expand Down Expand Up @@ -158,6 +159,46 @@ fail:
erase_file(log_path);
}

template<
class Hasher,
class 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)
{
if(!boost::filesystem::exists(dir_path))
boost::filesystem::create_directories(dir_path);

boost::filesystem::path fs_dir_path(dir_path);

boost::filesystem::path dat_path =
fs_dir_path / detail::default_dat_file();
boost::filesystem::path key_path =
fs_dir_path / detail::default_key_file();
boost::filesystem::path log_path =
fs_dir_path / detail::default_log_file();

create<Hasher, File, Args...>(dat_path.string(),
key_path.string(),
log_path.string(),
appnum,
salt,
key_size,
blockSize,
load_factor,
ec,
args...);
}

} // nudb

#endif

0 comments on commit 37ee4fd

Please sign in to comment.