Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directory based open & create calls #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/nudb/_experimental/test/test_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ class basic_test_store
temp_dir td_;
std::uniform_int_distribution<std::size_t> sizef_;
std::function<void(error_code&)> createf_;
std::function<void(error_code&)> create_dirf_;
std::function<void(error_code&)> openf_;
Buffer buf_;

public:
path_type const dirp;
path_type const dp;
path_type const kp;
path_type const lp;
Expand Down Expand Up @@ -234,6 +236,9 @@ class basic_test_store
void
create(error_code& ec);

void
create_dir(error_code& ec);

void
open(error_code& ec);

Expand Down Expand Up @@ -270,11 +275,20 @@ basic_test_store<File>::basic_test_store(
keySize, blockSize, loadFactor, ec,
args...);
})
, create_dirf_(
[this, args...](error_code& ec)
{
nudb::create<Hasher, File>(
dirp, appnum, salt,
keySize, blockSize, loadFactor, ec,
args...);
})
, openf_(
[this, args...](error_code& ec)
{
db.open(dp, kp, lp, ec, args...);
})
, dirp(td_.path())
, dp(td_.file("nudb.dat"))
, kp(td_.file("nudb.key"))
, lp(td_.file("nudb.log"))
Expand Down Expand Up @@ -330,6 +344,15 @@ create(error_code& ec)
createf_(ec);
}

template<class File>
void
basic_test_store<File>::
create_dir(error_code& ec)
{
create_dirf_(ec);
}


template<class File>
void
basic_test_store<File>::
Expand Down
34 changes: 34 additions & 0 deletions include/nudb/basic_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,40 @@ class basic_store
flush() override;
};

/** 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 store member functions are
called concurrently.

@param dir_path The path to the directory containing data
and key files.

@param store The store to open.

@param ec Set to the error, if any occurred.

@param args Optional arguments passed to @b File constructors.

*/
template<class Hasher, class File, class... Args>
void
open_dir(
path_type const& dir_path,
basic_store<Hasher, File>& store,
error_code& ec,
Args&&... args);


} // nudb

#include <nudb/impl/basic_store.ipp>
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
18 changes: 18 additions & 0 deletions include/nudb/detail/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ value size up to 32 bits (or 32-bit builds can't read it)

static std::size_t constexpr currentVersion = 2;

inline
const path_type& default_dat_file() {
static const path_type ddf("nudb.dat");
return ddf;
}

inline
const path_type& default_key_file() {
static const path_type dkf("nudb.key");
return dkf;
}

inline
const path_type& default_log_file() {
static const path_type dkf("nudb.log");
return dkf;
}

struct dat_file_header
{
static std::size_t constexpr size =
Expand Down
5 changes: 4 additions & 1 deletion include/nudb/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ enum class error
size_mismatch,

/// duplicate value
duplicate_value
duplicate_value,

/// directory not found
dir_not_found
};

/// Returns the error category used for database error codes.
Expand Down
18 changes: 18 additions & 0 deletions include/nudb/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ enum class file_mode
write
};

// Return boolean indicating if path exists
bool path_exists(path_type const& path);

// Return boolean indicating if path is a directory
bool is_dir(path_type const& path);

// Recursively make the specified dir tree
bool mkdir_p(path_type const& path);

// Append an rel-path to a local filesystem path.
// The returned path is normalized for the platform.
path_type
path_cat(
path_type const& base,
path_type const& path);

} // nudb

#include <nudb/impl/file.ipp>

#endif
26 changes: 26 additions & 0 deletions include/nudb/impl/basic_store.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,32 @@ flush()
ecb_.store(true);
}

template<class Hasher, class File, class... Args>
void
open_dir(
path_type const& dir_path,
basic_store<Hasher, File>& store,
error_code& ec,
Args&&... args)
{
if(is_dir(dir_path)){
ec = error::dir_not_found;
return;
}

path_type dat_path = path_cat(dir_path,
detail::default_dat_file());

path_type key_path = path_cat(dir_path,
detail::default_key_file());

path_type log_path = path_cat(dir_path,
detail::default_log_file());

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

} // nudb

#endif
40 changes: 40 additions & 0 deletions include/nudb/impl/create.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,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(!is_dir(dir_path))
mkdir_p(dir_path);

path_type dat_path = path_cat(dir_path,
detail::default_dat_file());

path_type key_path = path_cat(dir_path,
detail::default_key_file());

path_type log_path = path_cat(dir_path,
detail::default_log_file());

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

} // nudb

#endif
Loading