2016-09-24 06:01:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rocksdb/db.h>
|
2016-09-26 06:07:22 +02:00
|
|
|
#include <rocksdb/cache.h>
|
2016-09-24 06:01:57 +02:00
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace db {
|
|
|
|
|
2016-09-25 03:25:57 +02:00
|
|
|
struct log::log log
|
|
|
|
{
|
|
|
|
"db", 'D' // Database subsystem takes SNOMASK +D
|
|
|
|
};
|
|
|
|
|
2016-09-24 06:01:57 +02:00
|
|
|
void throw_on_error(const rocksdb::Status &);
|
2016-09-26 06:07:22 +02:00
|
|
|
bool valid(const rocksdb::Iterator &);
|
|
|
|
void valid_or_throw(const rocksdb::Iterator &);
|
|
|
|
void valid_equal_or_throw(const rocksdb::Iterator &, const rocksdb::Slice &);
|
|
|
|
|
|
|
|
const auto BLOCKING = rocksdb::ReadTier::kReadAllTier;
|
|
|
|
const auto NON_BLOCKING = rocksdb::ReadTier::kBlockCacheTier;
|
|
|
|
enum class pos
|
|
|
|
{
|
|
|
|
FRONT = -2, // .front() | first element
|
|
|
|
PREV = -1, // std::prev() | previous element
|
|
|
|
END = 0, // break; | exit iteration (or past the end)
|
|
|
|
NEXT = 1, // continue; | next element
|
|
|
|
BACK = 2, // .back() | last element
|
|
|
|
};
|
|
|
|
void seek(rocksdb::Iterator &, const pos &);
|
|
|
|
void seek(rocksdb::Iterator &, const rocksdb::Slice &);
|
|
|
|
template<class pos> void seek(rocksdb::DB &, const pos &, rocksdb::ReadOptions &, std::unique_ptr<rocksdb::Iterator> &);
|
|
|
|
std::unique_ptr<rocksdb::Iterator> seek(rocksdb::DB &, const rocksdb::Slice &, rocksdb::ReadOptions &);
|
|
|
|
|
|
|
|
// This is important to prevent thrashing the iterators which have to reset on iops
|
|
|
|
const auto DEFAULT_READAHEAD = 4_MiB;
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
rocksdb::WriteOptions make_opts(const sopts &);
|
|
|
|
rocksdb::ReadOptions make_opts(const gopts &, const bool &iterator = false);
|
|
|
|
rocksdb::Options make_opts(const opts &);
|
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
struct meta
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
std::string path;
|
|
|
|
rocksdb::Options opts;
|
|
|
|
std::shared_ptr<rocksdb::Cache> cache;
|
|
|
|
};
|
2016-09-24 06:01:57 +02:00
|
|
|
|
|
|
|
} // namespace db
|
|
|
|
} // namespace ircd
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
db::init::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
db::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
void
|
|
|
|
db::write(handle &handle,
|
|
|
|
const string_view &key,
|
|
|
|
const json::doc &obj,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
printf("Got this object %zu\n", obj.size());
|
|
|
|
}
|
|
|
|
|
2016-09-24 06:01:57 +02:00
|
|
|
db::handle::handle(const std::string &name,
|
|
|
|
const opts &opts)
|
|
|
|
try
|
2016-09-25 03:18:54 +02:00
|
|
|
:meta{[&name, &opts]
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2016-09-25 03:18:54 +02:00
|
|
|
auto meta(std::make_unique<struct meta>());
|
|
|
|
meta->name = name;
|
|
|
|
meta->path = path(name);
|
|
|
|
meta->opts = make_opts(opts);
|
2016-09-26 06:07:22 +02:00
|
|
|
|
|
|
|
const auto lru_cache_size(opt_val(opts, opt::LRU_CACHE));
|
|
|
|
if(lru_cache_size > 0)
|
|
|
|
meta->cache = rocksdb::NewLRUCache(lru_cache_size);
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
meta->opts.row_cache = meta->cache;
|
|
|
|
return std::move(meta);
|
|
|
|
}()}
|
2016-11-29 16:23:38 +01:00
|
|
|
,d{[this, &opts]
|
2016-09-25 03:18:54 +02:00
|
|
|
{
|
|
|
|
rocksdb::DB *ptr;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
if(has_opt(opts, opt::READ_ONLY))
|
|
|
|
throw_on_error(rocksdb::DB::OpenForReadOnly(meta->opts, meta->path, &ptr));
|
|
|
|
else
|
|
|
|
throw_on_error(rocksdb::DB::Open(meta->opts, meta->path, &ptr));
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
return std::unique_ptr<rocksdb::DB>{ptr};
|
2016-09-24 06:01:57 +02:00
|
|
|
}()}
|
|
|
|
{
|
2016-09-25 03:18:54 +02:00
|
|
|
log.info("Opened database \"%s\" @ `%s' (handle: %p)",
|
|
|
|
meta->name.c_str(),
|
|
|
|
meta->path.c_str(),
|
|
|
|
(const void *)this);
|
|
|
|
}
|
|
|
|
catch(const invalid_argument &e)
|
|
|
|
{
|
|
|
|
const bool no_create(has_opt(opts, opt::NO_CREATE));
|
|
|
|
const bool no_existing(has_opt(opts, opt::NO_EXISTING));
|
|
|
|
const char *const helpstr
|
|
|
|
{
|
|
|
|
no_create? " (The database is missing and will not be created)":
|
|
|
|
no_existing? " (The database already exists but must be fresh)":
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
throw error("Failed to open db '%s': %s%s",
|
|
|
|
name.c_str(),
|
|
|
|
e.what(),
|
|
|
|
helpstr);
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw error("Failed to open db '%s': %s",
|
|
|
|
name.c_str(),
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
db::handle::~handle()
|
|
|
|
noexcept
|
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
log.info("Closing database \"%s\" @ `%s' (handle: %p)",
|
|
|
|
meta->name.c_str(),
|
|
|
|
meta->path.c_str(),
|
|
|
|
(const void *)this);
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::del(const string_view &key,
|
2016-09-25 03:18:54 +02:00
|
|
|
const sopts &sopts)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
|
|
|
using rocksdb::Slice;
|
|
|
|
|
2016-09-25 06:12:43 +02:00
|
|
|
auto opts(make_opts(sopts));
|
2016-09-24 06:01:57 +02:00
|
|
|
const Slice k(key.data(), key.size());
|
2016-09-25 06:12:43 +02:00
|
|
|
throw_on_error(d->Delete(opts, k));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::set(const string_view &key,
|
2016-09-25 06:12:43 +02:00
|
|
|
const uint8_t *const &buf,
|
|
|
|
const size_t &size,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
const string_view val{reinterpret_cast<const char *>(buf), size};
|
|
|
|
set(key, key, sopts);
|
2016-09-25 06:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::set(const string_view &key,
|
|
|
|
const string_view &val,
|
2016-09-25 06:12:43 +02:00
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
using rocksdb::Slice;
|
2016-09-25 03:18:54 +02:00
|
|
|
|
|
|
|
auto opts(make_opts(sopts));
|
2016-09-25 06:12:43 +02:00
|
|
|
const Slice k(key.data(), key.size());
|
2016-11-29 16:23:38 +01:00
|
|
|
const Slice v(val.data(), val.size());
|
2016-09-25 03:18:54 +02:00
|
|
|
throw_on_error(d->Put(opts, k, v));
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 06:12:43 +02:00
|
|
|
std::string
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::get(const string_view &key,
|
2016-09-25 06:12:43 +02:00
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
const auto copy([&ret]
|
2016-11-29 16:23:38 +01:00
|
|
|
(const string_view &src)
|
2016-09-25 06:12:43 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
ret.assign(begin(src), end(src));
|
2016-09-25 06:12:43 +02:00
|
|
|
});
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
operator()(key, copy, gopts);
|
2016-09-25 06:12:43 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::get(const string_view &key,
|
2016-09-25 06:12:43 +02:00
|
|
|
uint8_t *const &buf,
|
|
|
|
const size_t &max,
|
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
const auto copy([&ret, &buf, &max]
|
2016-11-29 16:23:38 +01:00
|
|
|
(const string_view &src)
|
2016-09-25 06:12:43 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
ret = std::min(src.size(), max);
|
|
|
|
memcpy(buf, src.data(), ret);
|
2016-09-25 06:12:43 +02:00
|
|
|
});
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
operator()(key, copy, gopts);
|
2016-09-25 06:12:43 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::get(const string_view &key,
|
2016-09-25 06:12:43 +02:00
|
|
|
char *const &buf,
|
|
|
|
const size_t &max,
|
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
const auto copy([&ret, &buf, &max]
|
2016-11-29 16:23:38 +01:00
|
|
|
(const string_view &src)
|
2016-09-25 06:12:43 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
ret = strlcpy(buf, src.data(), std::min(src.size(), max));
|
2016-09-25 06:12:43 +02:00
|
|
|
});
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
operator()(key, copy, gopts);
|
2016-09-25 06:12:43 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-24 06:01:57 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::operator()(const string_view &key,
|
|
|
|
const gopts &gopts,
|
|
|
|
const closure &func)
|
|
|
|
{
|
|
|
|
return operator()(key, func, gopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
db::handle::operator()(const string_view &key,
|
|
|
|
const closure &func,
|
|
|
|
const gopts &gopts)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
|
|
|
using rocksdb::Slice;
|
2016-09-26 06:07:22 +02:00
|
|
|
using rocksdb::Iterator;
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
auto opts(make_opts(gopts));
|
2016-09-24 06:01:57 +02:00
|
|
|
const Slice sk(key.data(), key.size());
|
2016-09-26 06:07:22 +02:00
|
|
|
const auto it(seek(*d, sk, opts));
|
|
|
|
valid_equal_or_throw(*it, sk);
|
|
|
|
const auto &v(it->value());
|
2016-11-29 16:23:38 +01:00
|
|
|
func(string_view{v.data(), v.size()});
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::has(const string_view &key,
|
2016-09-25 03:18:54 +02:00
|
|
|
const gopts &gopts)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
|
|
|
using rocksdb::Slice;
|
2016-09-25 03:18:54 +02:00
|
|
|
using rocksdb::Status;
|
2016-09-24 06:01:57 +02:00
|
|
|
|
|
|
|
const Slice k(key.data(), key.size());
|
2016-09-26 06:07:22 +02:00
|
|
|
auto opts(make_opts(gopts));
|
|
|
|
|
|
|
|
// Perform queries which are stymied from any sysentry
|
|
|
|
opts.read_tier = NON_BLOCKING;
|
|
|
|
|
|
|
|
// Perform a co-RP query to the filtration
|
|
|
|
if(!d->KeyMayExist(opts, k, nullptr, nullptr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Perform a query to the cache
|
|
|
|
auto status(d->Get(opts, k, nullptr));
|
|
|
|
if(status.IsIncomplete())
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
// DB cache miss; next query requires I/O, offload it
|
|
|
|
opts.read_tier = BLOCKING;
|
2016-09-27 10:40:33 +02:00
|
|
|
ctx::offload([this, &opts, &k, &status]
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
status = d->Get(opts, k, nullptr);
|
|
|
|
});
|
|
|
|
}
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
// Finally the result
|
|
|
|
switch(status.code())
|
|
|
|
{
|
|
|
|
case Status::kOk: return true;
|
|
|
|
case Status::kNotFound: return false;
|
|
|
|
default:
|
|
|
|
throw_on_error(status);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
}
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace db {
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
struct const_iterator::state
|
|
|
|
{
|
|
|
|
struct handle *handle;
|
|
|
|
rocksdb::ReadOptions ropts;
|
|
|
|
std::shared_ptr<const rocksdb::Snapshot> snap;
|
|
|
|
std::unique_ptr<rocksdb::Iterator> it;
|
|
|
|
|
|
|
|
state(struct handle *const & = nullptr, const gopts & = {});
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace db
|
|
|
|
} // namespace ircd
|
|
|
|
|
|
|
|
db::const_iterator
|
|
|
|
db::handle::cend(const gopts &gopts)
|
|
|
|
{
|
|
|
|
return {};
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
db::const_iterator
|
|
|
|
db::handle::cbegin(const gopts &gopts)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
const_iterator ret
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
std::make_unique<struct const_iterator::state>(this, gopts)
|
|
|
|
};
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
auto &state(*ret.state);
|
|
|
|
if(!has_opt(gopts, db::get::READAHEAD))
|
|
|
|
state.ropts.readahead_size = DEFAULT_READAHEAD;
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
seek(*state.handle->d, pos::FRONT, state.ropts, state.it);
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::upper_bound(const string_view &key,
|
2016-09-26 06:07:22 +02:00
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
using rocksdb::Slice;
|
|
|
|
|
|
|
|
auto it(lower_bound(key, gopts));
|
|
|
|
const Slice sk(key.data(), key.size());
|
|
|
|
if(it && it.state->it->key().compare(sk) == 0)
|
|
|
|
++it;
|
|
|
|
|
|
|
|
return std::move(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator
|
2016-11-29 16:23:38 +01:00
|
|
|
db::handle::lower_bound(const string_view &key,
|
2016-09-26 06:07:22 +02:00
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
using rocksdb::Slice;
|
|
|
|
|
|
|
|
const_iterator ret
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
std::make_unique<struct const_iterator::state>(this, gopts)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &state(*ret.state);
|
|
|
|
if(!has_opt(gopts, db::get::READAHEAD))
|
|
|
|
state.ropts.readahead_size = DEFAULT_READAHEAD;
|
|
|
|
|
|
|
|
const Slice sk(key.data(), key.size());
|
|
|
|
seek(*state.handle->d, sk, state.ropts, state.it);
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator::state::state(struct handle *const &handle,
|
|
|
|
const gopts &gopts)
|
|
|
|
:handle{handle}
|
|
|
|
,ropts{make_opts(gopts, true)}
|
|
|
|
,snap
|
|
|
|
{
|
|
|
|
[this, &handle, &gopts]() -> const rocksdb::Snapshot *
|
|
|
|
{
|
|
|
|
if(handle && !has_opt(gopts, get::NO_SNAPSHOT))
|
|
|
|
ropts.snapshot = handle->d->GetSnapshot();
|
|
|
|
|
|
|
|
return ropts.snapshot;
|
|
|
|
}()
|
|
|
|
,[this](const auto *const &snap)
|
|
|
|
{
|
|
|
|
if(this->handle && this->handle->d)
|
|
|
|
this->handle->d->ReleaseSnapshot(snap);
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
db::const_iterator::const_iterator(std::unique_ptr<struct state> &&state)
|
|
|
|
:state{std::move(state)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator::const_iterator(const_iterator &&o)
|
|
|
|
noexcept
|
|
|
|
:state{std::move(o.state)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator::~const_iterator()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator &
|
|
|
|
db::const_iterator::operator--()
|
|
|
|
{
|
|
|
|
seek(*state->handle->d, pos::PREV, state->ropts, state->it);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator &
|
|
|
|
db::const_iterator::operator++()
|
|
|
|
{
|
|
|
|
seek(*state->handle->d, pos::NEXT, state->ropts, state->it);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const db::const_iterator::value_type &
|
|
|
|
db::const_iterator::operator*()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &k(state->it->key());
|
|
|
|
const auto &v(state->it->value());
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
val.first = { k.data(), k.size() };
|
|
|
|
val.second = { v.data(), v.size() };
|
2016-09-26 06:07:22 +02:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
const db::const_iterator::value_type *
|
|
|
|
db::const_iterator::operator->()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return &operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::const_iterator::operator>=(const const_iterator &o)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return (*this > o) || (*this == o);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::const_iterator::operator<=(const const_iterator &o)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return (*this < o) || (*this == o);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::const_iterator::operator!=(const const_iterator &o)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !(*this == o);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::const_iterator::operator==(const const_iterator &o)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(*this && o)
|
|
|
|
{
|
|
|
|
const auto &a(state->it->key());
|
|
|
|
const auto &b(o.state->it->key());
|
|
|
|
return a.compare(b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!*this && !o)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::const_iterator::operator>(const const_iterator &o)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(*this && o)
|
|
|
|
{
|
|
|
|
const auto &a(state->it->key());
|
|
|
|
const auto &b(o.state->it->key());
|
|
|
|
return a.compare(b) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!*this && o)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!*this && !o)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(!*this && o);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::const_iterator::operator<(const const_iterator &o)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(*this && o)
|
|
|
|
{
|
|
|
|
const auto &a(state->it->key());
|
|
|
|
const auto &b(o.state->it->key());
|
|
|
|
return a.compare(b) == -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!*this && o)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!*this && !o)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(*this && !o);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::const_iterator::operator!()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!state)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!state->it)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!state->it->Valid())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
db::const_iterator::operator bool()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !!*this;
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
rocksdb::Options
|
|
|
|
db::make_opts(const opts &opts)
|
|
|
|
{
|
|
|
|
rocksdb::Options ret;
|
|
|
|
ret.create_if_missing = true; // They default this to false, but we invert the option
|
|
|
|
|
|
|
|
for(const auto &o : opts) switch(o.first)
|
|
|
|
{
|
|
|
|
case opt::NO_CREATE:
|
|
|
|
ret.create_if_missing = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::NO_EXISTING:
|
|
|
|
ret.error_if_exists = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::NO_CHECKSUM:
|
|
|
|
ret.paranoid_checks = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::NO_MADV_DONTNEED:
|
|
|
|
ret.allow_os_buffer = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::NO_MADV_RANDOM:
|
|
|
|
ret.advise_random_on_open = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::FALLOCATE:
|
|
|
|
ret.allow_fallocate = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::NO_FALLOCATE:
|
|
|
|
ret.allow_fallocate = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::NO_FDATASYNC:
|
|
|
|
ret.disableDataSync = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::FSYNC:
|
|
|
|
ret.use_fsync = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::MMAP_READS:
|
|
|
|
ret.allow_mmap_reads = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::MMAP_WRITES:
|
|
|
|
ret.allow_mmap_writes = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::STATS_THREAD:
|
|
|
|
ret.enable_thread_tracking = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::STATS_MALLOC:
|
|
|
|
ret.dump_malloc_stats = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::OPEN_FAST:
|
|
|
|
ret.skip_stats_update_on_db_open = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::OPEN_BULKLOAD:
|
|
|
|
ret.PrepareForBulkLoad();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case opt::OPEN_SMALL:
|
|
|
|
ret.OptimizeForSmallDb();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::ReadOptions
|
|
|
|
db::make_opts(const gopts &opts,
|
|
|
|
const bool &iterator)
|
|
|
|
{
|
|
|
|
rocksdb::ReadOptions ret;
|
|
|
|
|
|
|
|
if(iterator)
|
|
|
|
ret.fill_cache = false;
|
|
|
|
|
|
|
|
for(const auto &opt : opts) switch(opt.first)
|
|
|
|
{
|
|
|
|
case get::PIN:
|
|
|
|
ret.pin_data = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case get::CACHE:
|
|
|
|
ret.fill_cache = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case get::NO_CACHE:
|
|
|
|
ret.fill_cache = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case get::NO_CHECKSUM:
|
|
|
|
ret.verify_checksums = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case get::READAHEAD:
|
|
|
|
ret.readahead_size = opt.second;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::WriteOptions
|
|
|
|
db::make_opts(const sopts &opts)
|
|
|
|
{
|
|
|
|
rocksdb::WriteOptions ret;
|
|
|
|
for(const auto &opt : opts) switch(opt.first)
|
|
|
|
{
|
|
|
|
case set::FSYNC:
|
|
|
|
ret.sync = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case set::NO_JOURNAL:
|
|
|
|
ret.disableWAL = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case set::MISSING_COLUMNS:
|
|
|
|
ret.ignore_missing_column_families = true;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
std::unique_ptr<rocksdb::Iterator>
|
|
|
|
db::seek(rocksdb::DB &db,
|
|
|
|
const rocksdb::Slice &key,
|
|
|
|
rocksdb::ReadOptions &opts)
|
2016-09-25 03:18:54 +02:00
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
using rocksdb::Iterator;
|
|
|
|
|
|
|
|
// Perform a query which won't be allowed to do kernel IO
|
|
|
|
opts.read_tier = NON_BLOCKING;
|
|
|
|
|
|
|
|
std::unique_ptr<Iterator> it(db.NewIterator(opts));
|
|
|
|
seek(*it, key);
|
|
|
|
|
|
|
|
if(it->status().IsIncomplete())
|
2016-09-25 03:18:54 +02:00
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
// DB cache miss: reset the iterator to blocking mode and offload it
|
|
|
|
opts.read_tier = BLOCKING;
|
|
|
|
it.reset(db.NewIterator(opts));
|
2016-09-27 10:40:33 +02:00
|
|
|
ctx::offload([&] { seek(*it, key); });
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
// else DB cache hit; no context switch; no thread switch; no kernel I/O; gg
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
return std::move(it);
|
2016-09-25 03:18:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
template<class pos>
|
|
|
|
void
|
|
|
|
db::seek(rocksdb::DB &db,
|
|
|
|
const pos &p,
|
|
|
|
rocksdb::ReadOptions &opts,
|
|
|
|
std::unique_ptr<rocksdb::Iterator> &it)
|
|
|
|
{
|
|
|
|
// Start with a non-blocking query
|
|
|
|
if(!it || opts.read_tier == BLOCKING)
|
|
|
|
{
|
|
|
|
opts.read_tier = NON_BLOCKING;
|
|
|
|
it.reset(db.NewIterator(opts));
|
|
|
|
}
|
|
|
|
|
|
|
|
seek(*it, p);
|
|
|
|
if(it->status().IsIncomplete())
|
|
|
|
{
|
|
|
|
// DB cache miss: reset the iterator to blocking mode and offload it
|
|
|
|
opts.read_tier = BLOCKING;
|
|
|
|
it.reset(db.NewIterator(opts));
|
2016-09-27 10:40:33 +02:00
|
|
|
ctx::offload([&] { seek(*it, p); });
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
db::seek(rocksdb::Iterator &it,
|
|
|
|
const rocksdb::Slice &sk)
|
|
|
|
{
|
|
|
|
it.Seek(sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
db::seek(rocksdb::Iterator &it,
|
|
|
|
const pos &p)
|
|
|
|
{
|
|
|
|
switch(p)
|
|
|
|
{
|
|
|
|
case pos::NEXT: it.Next(); break;
|
|
|
|
case pos::PREV: it.Prev(); break;
|
|
|
|
case pos::FRONT: it.SeekToFirst(); break;
|
|
|
|
case pos::BACK: it.SeekToLast(); break;
|
|
|
|
default:
|
|
|
|
case pos::END:
|
|
|
|
{
|
|
|
|
it.SeekToLast();
|
|
|
|
if(it.Valid())
|
|
|
|
it.Next();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
db::valid_equal_or_throw(const rocksdb::Iterator &it,
|
|
|
|
const rocksdb::Slice &sk)
|
|
|
|
{
|
|
|
|
valid_or_throw(it);
|
|
|
|
if(it.key().compare(sk) != 0)
|
|
|
|
throw not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
db::valid_or_throw(const rocksdb::Iterator &it)
|
|
|
|
{
|
|
|
|
if(!valid(it))
|
|
|
|
{
|
|
|
|
throw_on_error(it.status());
|
|
|
|
assert(0); // status == ok + !Valid() == ???
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
db::valid(const rocksdb::Iterator &it)
|
|
|
|
{
|
|
|
|
return it.Valid();
|
2016-09-25 03:18:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 06:01:57 +02:00
|
|
|
void
|
|
|
|
db::throw_on_error(const rocksdb::Status &s)
|
|
|
|
{
|
|
|
|
using rocksdb::Status;
|
|
|
|
|
|
|
|
switch(s.code())
|
|
|
|
{
|
|
|
|
case Status::kOk: return;
|
|
|
|
case Status::kNotFound: throw not_found();
|
|
|
|
case Status::kCorruption: throw corruption();
|
|
|
|
case Status::kNotSupported: throw not_supported();
|
|
|
|
case Status::kInvalidArgument: throw invalid_argument();
|
|
|
|
case Status::kIOError: throw io_error();
|
|
|
|
case Status::kMergeInProgress: throw merge_in_progress();
|
|
|
|
case Status::kIncomplete: throw incomplete();
|
|
|
|
case Status::kShutdownInProgress: throw shutdown_in_progress();
|
|
|
|
case Status::kTimedOut: throw timed_out();
|
|
|
|
case Status::kAborted: throw aborted();
|
|
|
|
case Status::kBusy: throw busy();
|
|
|
|
case Status::kExpired: throw expired();
|
|
|
|
case Status::kTryAgain: throw try_again();
|
|
|
|
default:
|
|
|
|
throw error("Unknown error");
|
|
|
|
}
|
|
|
|
}
|
2016-09-25 06:12:43 +02:00
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
std::string
|
|
|
|
db::path(const std::string &name)
|
|
|
|
{
|
|
|
|
const auto prefix(path::get(path::DB));
|
|
|
|
return path::build({prefix, name});
|
|
|
|
}
|