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 Read only Mode, does not allow inserts #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

movitto
Copy link
Contributor

@movitto movitto commented Jun 19, 2019

This would be useful for situations in which the database filesystem / files are not writeable by the local process but read functionality is desired. Mirrors the existing open API with the omission of the 'log path' parameter as this will not be created / written to anyways.

The synchronized write mechanism is skipped when the database is open in read only and attempting to call insert results in failure.

Based on PR #74, will rebase if/when that gets merged.

const std::string& default_log_file() {
static const std::string 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

, c1(kh_.key_size, kh_.block_size, "c1")
, kh(kh_)
{
static_assert(is_File<File>::value,
Copy link
Member

Choose a reason for hiding this comment

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

BOOST_STATIC_ASSERT

error_code& ec,
Args&&... args)
{
BOOST_ASSERT(boost::filesystem::exists(dir_path));
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be an assert, it should be a run-time error code, e.g. dir_not_found. Or perhaps we just propagate the boost::filesystem error if one exists.

Copy link
Member

@vinniefalco vinniefalco Jun 19, 2019

Choose a reason for hiding this comment

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

If we don't use filesystem we need to add our own error enum value

};

bool open_ = false;
bool read_only_ = false;
Copy link
Member

Choose a reason for hiding this comment

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

bool is_writable_ and it should not have a default value

except @ref open.
*/
bool
is_read_only() const
Copy link
Member

Choose a reason for hiding this comment

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

bool is_writable()

*/
template<class... Args>
void
open(
Copy link
Member

Choose a reason for hiding this comment

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

This really feels like it should be a free function which calls into the existing open member function. The function could be open_dir.

*/
template<class... Args>
void
open_read_only(
Copy link
Member

Choose a reason for hiding this comment

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

To avoid a combinatorial explosion of signatures, the open mode should be a parameter.

@@ -13,6 +13,7 @@
#include <boost/assert.hpp>
#include <cmath>
#include <memory>
#include <boost/filesystem.hpp>
Copy link
Member

Choose a reason for hiding this comment

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

This is a hefty dependency to add, and we are using so little of it. I would prefer if we had our own functions to perform the needed operations, e.g. path_cat:

// Append an HTTP rel-path to a local filesystem path.
// The returned path is normalized for the platform.
std::string
path_cat(
    beast::string_view base,
    beast::string_view path)
{
    if(base.empty())
        return std::string(path);
    std::string result(base);
#ifdef BOOST_MSVC
    char constexpr path_separator = '\\';
    if(result.back() == path_separator)
        result.resize(result.size() - 1);
    result.append(path.data(), path.size());
    for(auto& c : result)
        if(c == '/')
            c = path_separator;
#else
    char constexpr path_separator = '/';
    if(result.back() == path_separator)
        result.resize(result.size() - 1);
    result.append(path.data(), path.size());
#endif
    return result;
}

Copy link
Member

Choose a reason for hiding this comment

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

We can maybe fall back to boost::filesystem if we can't identify the platform

df.open(file_mode::read, dat_path, ec);
if(ec)
return;
kf.open(file_mode::read, key_path, ec);
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot of duplicated code here...

@vinniefalco
Copy link
Member

This will need tests and coverage (i.e. 100% coverage for all new and changed lines). We should make the indicated changes first, then write the tests.

@movitto
Copy link
Contributor Author

movitto commented Jun 20, 2019

@vinniefalco sounds good, I'll get back to this & incorporate your feedback in the near future

@vinniefalco
Copy link
Member

I believe @miguelportilla is working on adding nudb::string_view

@movitto
Copy link
Contributor Author

movitto commented Jun 25, 2019

@vinniefalco this was rebased ontop of the updated PR #74 and updated to incorporate feedback. Tests have not been written for this yet, I wanted to make sure this is what you were describing in your feedback. Specifically the 'open_mode' enum which was added to basic_store and specified as a param to 'open'. Is this what you were referring to regarding non-duplicate method signatures? This would cause an API incompatability and in the case of readonly access the log file param is unused.

@vinniefalco
Copy link
Member

in the case of readonly access the log file param is unused.

You still need the log file in order to know if the database is in a valid state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants