diff --git a/include/nudb/basic_store.hpp b/include/nudb/basic_store.hpp index 445e2e6..32aff53 100644 --- a/include/nudb/basic_store.hpp +++ b/include/nudb/basic_store.hpp @@ -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 + void + open( + path_type const& dir_path, + error_code& ec, + Args&&... args); + /** Fetch a value. The function checks the database for the specified diff --git a/include/nudb/create.hpp b/include/nudb/create.hpp index cb37ecb..13a5cc2 100644 --- a/include/nudb/create.hpp +++ b/include/nudb/create.hpp @@ -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 diff --git a/include/nudb/detail/format.hpp b/include/nudb/detail/format.hpp index 1711f51..2148bde 100644 --- a/include/nudb/detail/format.hpp +++ b/include/nudb/detail/format.hpp @@ -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 = diff --git a/include/nudb/impl/basic_store.ipp b/include/nudb/impl/basic_store.ipp index 4ac79e4..6d29560 100644 --- a/include/nudb/impl/basic_store.ipp +++ b/include/nudb/impl/basic_store.ipp @@ -13,6 +13,7 @@ #include #include #include +#include #ifndef NUDB_DEBUG_LOG #define NUDB_DEBUG_LOG 0 @@ -187,6 +188,32 @@ open( ctx_->insert(*this); } +template +template +void +basic_store:: +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 void basic_store:: diff --git a/include/nudb/impl/create.ipp b/include/nudb/impl/create.ipp index b0b8511..fe67a07 100644 --- a/include/nudb/impl/create.ipp +++ b/include/nudb/impl/create.ipp @@ -17,6 +17,7 @@ #include #include #include +#include namespace nudb { @@ -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(dat_path.string(), + key_path.string(), + log_path.string(), + appnum, + salt, + key_size, + blockSize, + load_factor, + ec, + args...); +} + } // nudb #endif