Skip to content
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
2 changes: 1 addition & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main()
0.5f,
ec);
store db;
db.open(dat_path, key_path, log_path, ec);
db.open(dat_path, key_path, log_path, store::open_mode::read_write, ec);
char data = 0;
// Insert
for(key_type i = 0; i < N; ++i)
Expand Down
25 changes: 24 additions & 1 deletion 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...);
db.open(dp, kp, lp, basic_store<Hasher, File>::open_mode::read_write, 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
63 changes: 63 additions & 0 deletions include/nudb/basic_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class basic_store
using hash_type = Hasher;
using file_type = File;

enum class open_mode {
/// Open the database for both read and write access
read_write,

/// Open the database for reach access only
read
};

private:
using clock_type =
std::chrono::steady_clock;
Expand Down Expand Up @@ -90,9 +98,14 @@ class basic_store
path_type const& dp_, path_type const& kp_,
path_type const& lp_,
detail::key_file_header const& kh_);

state(File&& df_, File&& kf_,
path_type const& dp_, path_type const& kp_,
detail::key_file_header const& kh_);
};

bool open_ = false;
bool is_writable_;

// Use optional because some
// members cannot be default-constructed.
Expand Down Expand Up @@ -173,6 +186,19 @@ class basic_store
return open_;
}

/** Returns `true` if the database is writable

@par Thread safety

Safe to call concurrently with any function
except @ref open.
*/
bool
is_writable() const
{
return is_writable_;
}

/** Return the path to the data file.

@par Requirements
Expand Down Expand Up @@ -329,6 +355,8 @@ class basic_store

@param log_path The path to the log file.

@param open_mode Mode of database access

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

@param args Optional arguments passed to @b File constructors.
Expand All @@ -340,6 +368,7 @@ class basic_store
path_type const& dat_path,
path_type const& key_path,
path_type const& log_path,
open_mode mode,
error_code& ec,
Args&&... args);

Expand Down Expand Up @@ -454,6 +483,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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nudb should have a <nudb/string_type.hpp> which declares nudb::string_view, copy from here:
https://github.com/boostorg/beast/blob/develop/include/boost/beast/core/string_type.hpp

And these functions should use that string_view. Also I think these functions need to be declared inline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miguelportilla will work up a branch that has just this change for us


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
Loading