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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-08-23 23:37:06 +02:00
|
|
|
#include <rocksdb/version.h>
|
2016-09-24 06:01:57 +02:00
|
|
|
#include <rocksdb/db.h>
|
2016-09-26 06:07:22 +02:00
|
|
|
#include <rocksdb/cache.h>
|
2017-03-31 00:57:08 +02:00
|
|
|
#include <rocksdb/comparator.h>
|
2017-03-23 22:58:24 +01:00
|
|
|
#include <rocksdb/merge_operator.h>
|
|
|
|
#include <rocksdb/perf_level.h>
|
2017-04-03 06:02:32 +02:00
|
|
|
#include <rocksdb/perf_context.h>
|
2017-03-23 22:58:24 +01:00
|
|
|
#include <rocksdb/listener.h>
|
|
|
|
#include <rocksdb/statistics.h>
|
2017-03-24 00:10:17 +01:00
|
|
|
#include <rocksdb/convenience.h>
|
|
|
|
#include <rocksdb/env.h>
|
2017-08-31 07:12:58 +02:00
|
|
|
#include <rocksdb/slice_transform.h>
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
#include <ircd/m.h>
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
namespace ircd::db
|
2016-09-25 03:25:57 +02:00
|
|
|
{
|
2017-09-08 11:09:34 +02:00
|
|
|
const auto BLOCKING = rocksdb::ReadTier::kReadAllTier;
|
|
|
|
const auto NON_BLOCKING = rocksdb::ReadTier::kBlockCacheTier;
|
|
|
|
const auto DEFAULT_READAHEAD = 4_MiB;
|
2016-09-25 03:25:57 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct throw_on_error
|
|
|
|
{
|
|
|
|
throw_on_error(const rocksdb::Status & = rocksdb::Status::OK());
|
|
|
|
};
|
2016-09-26 06:07:22 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
const char *reflect(const pos &);
|
|
|
|
const std::string &reflect(const rocksdb::Tickers &);
|
|
|
|
const std::string &reflect(const rocksdb::Histograms &);
|
|
|
|
rocksdb::Slice slice(const string_view &);
|
|
|
|
string_view slice(const rocksdb::Slice &);
|
2016-09-26 06:07:22 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
// Frequently used get options and set options are separate from the string/map system
|
|
|
|
rocksdb::WriteOptions make_opts(const sopts &);
|
|
|
|
rocksdb::ReadOptions make_opts(const gopts &, const bool &iterator = false);
|
|
|
|
bool optstr_find_and_remove(std::string &optstr, const std::string &what);
|
2016-09-26 06:07:22 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
// Validation functors
|
|
|
|
bool valid(const rocksdb::Iterator &);
|
|
|
|
bool operator!(const rocksdb::Iterator &);
|
|
|
|
void valid_or_throw(const rocksdb::Iterator &);
|
|
|
|
bool valid_equal(const rocksdb::Iterator &, const string_view &);
|
|
|
|
void valid_equal_or_throw(const rocksdb::Iterator &, const string_view &);
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
// [GET] seek suite
|
|
|
|
template<class pos> bool seek(database::column &, const pos &, rocksdb::ReadOptions &, std::unique_ptr<rocksdb::Iterator> &it);
|
|
|
|
template<class pos> bool seek(database::column &, const pos &, const gopts &, std::unique_ptr<rocksdb::Iterator> &it);
|
|
|
|
std::unique_ptr<rocksdb::Iterator> seek(column &, const gopts &);
|
|
|
|
std::unique_ptr<rocksdb::Iterator> seek(column &, const string_view &key, const gopts &);
|
|
|
|
std::vector<row::value_type> seek(database &, const gopts &);
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
std::pair<string_view, string_view> operator*(const rocksdb::Iterator &);
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
// [SET] writebatch suite
|
|
|
|
void commit(database &, rocksdb::WriteBatch &, const rocksdb::WriteOptions &);
|
|
|
|
void commit(database &, rocksdb::WriteBatch &, const sopts &);
|
|
|
|
void append(rocksdb::WriteBatch &, column &, const column::delta &delta);
|
|
|
|
void append(rocksdb::WriteBatch &, const cell::delta &delta);
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
std::vector<std::string> column_names(const std::string &path, const rocksdb::DBOptions &);
|
|
|
|
std::vector<std::string> column_names(const std::string &path, const std::string &options);
|
|
|
|
}
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
ircd::log::log ircd::db::log
|
|
|
|
{
|
|
|
|
// Dedicated logging facility for the database subsystem
|
|
|
|
"db", 'D'
|
|
|
|
};
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
std::map<ircd::string_view, ircd::db::database *>
|
|
|
|
ircd::db::database::dbs
|
|
|
|
{};
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::database::logs
|
2017-03-31 00:57:08 +02:00
|
|
|
:std::enable_shared_from_this<struct database::logs>
|
2017-03-24 02:36:49 +01:00
|
|
|
,rocksdb::Logger
|
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
database *d;
|
2017-03-24 02:36:49 +01:00
|
|
|
|
|
|
|
// Logger
|
|
|
|
void Logv(const rocksdb::InfoLogLevel level, const char *fmt, va_list ap) override;
|
|
|
|
void Logv(const char *fmt, va_list ap) override;
|
|
|
|
void LogHeader(const char *fmt, va_list ap) override;
|
2017-03-23 22:58:24 +01:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
logs(database *const &d)
|
|
|
|
:d{d}
|
2017-03-24 02:36:49 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::database::stats
|
|
|
|
:std::enable_shared_from_this<struct ircd::db::database::stats>
|
2017-03-24 02:36:49 +01:00
|
|
|
,rocksdb::Statistics
|
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
database *d;
|
2017-03-23 22:58:24 +01:00
|
|
|
std::array<uint64_t, rocksdb::TICKER_ENUM_MAX> ticker {{0}};
|
|
|
|
std::array<rocksdb::HistogramData, rocksdb::HISTOGRAM_ENUM_MAX> histogram;
|
|
|
|
|
|
|
|
uint64_t getTickerCount(const uint32_t tickerType) const override;
|
|
|
|
void recordTick(const uint32_t tickerType, const uint64_t count) override;
|
|
|
|
void setTickerCount(const uint32_t tickerType, const uint64_t count) override;
|
|
|
|
void histogramData(const uint32_t type, rocksdb::HistogramData *) const override;
|
|
|
|
void measureTime(const uint32_t histogramType, const uint64_t time) override;
|
|
|
|
bool HistEnabledForType(const uint32_t type) const override;
|
2017-07-18 19:11:00 +02:00
|
|
|
uint64_t getAndResetTickerCount(const uint32_t tickerType) override;
|
2017-03-23 22:58:24 +01:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
stats(database *const &d)
|
|
|
|
:d{d}
|
2017-03-24 02:36:49 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::database::events
|
|
|
|
:std::enable_shared_from_this<struct ircd::db::database::events>
|
2017-03-24 02:36:49 +01:00
|
|
|
,rocksdb::EventListener
|
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
database *d;
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-03-23 22:58:24 +01:00
|
|
|
void OnFlushCompleted(rocksdb::DB *, const rocksdb::FlushJobInfo &) override;
|
|
|
|
void OnCompactionCompleted(rocksdb::DB *, const rocksdb::CompactionJobInfo &) override;
|
|
|
|
void OnTableFileDeleted(const rocksdb::TableFileDeletionInfo &) override;
|
|
|
|
void OnTableFileCreated(const rocksdb::TableFileCreationInfo &) override;
|
|
|
|
void OnTableFileCreationStarted(const rocksdb::TableFileCreationBriefInfo &) override;
|
|
|
|
void OnMemTableSealed(const rocksdb::MemTableInfo &) override;
|
|
|
|
void OnColumnFamilyHandleDeletionStarted(rocksdb::ColumnFamilyHandle *) override;
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
events(database *const &d)
|
|
|
|
:d{d}
|
2017-03-24 02:36:49 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::database::mergeop
|
|
|
|
:std::enable_shared_from_this<struct ircd::db::database::mergeop>
|
2017-03-24 02:36:49 +01:00
|
|
|
,rocksdb::AssociativeMergeOperator
|
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
database *d;
|
|
|
|
merge_closure merger;
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-03-23 22:58:24 +01:00
|
|
|
bool Merge(const rocksdb::Slice &, const rocksdb::Slice *, const rocksdb::Slice &, std::string *, rocksdb::Logger *) const override;
|
|
|
|
const char *Name() const override;
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
mergeop(database *const &d, merge_closure merger = nullptr)
|
|
|
|
:d{d}
|
|
|
|
,merger{merger? std::move(merger) : ircd::db::merge_operator}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::database::comparator
|
2017-03-31 00:57:08 +02:00
|
|
|
:rocksdb::Comparator
|
|
|
|
{
|
|
|
|
using Slice = rocksdb::Slice;
|
|
|
|
|
|
|
|
database *d;
|
|
|
|
db::comparator user;
|
|
|
|
|
|
|
|
void FindShortestSeparator(std::string *start, const Slice &limit) const override;
|
|
|
|
void FindShortSuccessor(std::string *key) const override;
|
|
|
|
int Compare(const Slice &a, const Slice &b) const override;
|
|
|
|
bool Equal(const Slice &a, const Slice &b) const override;
|
|
|
|
const char *Name() const override;
|
|
|
|
|
|
|
|
comparator(database *const &d, db::comparator user)
|
|
|
|
:d{d}
|
|
|
|
,user{std::move(user)}
|
2017-03-24 02:36:49 +01:00
|
|
|
{}
|
2016-09-26 06:07:22 +02:00
|
|
|
};
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::database::prefix_transform
|
2017-08-31 07:12:58 +02:00
|
|
|
:rocksdb::SliceTransform
|
|
|
|
{
|
|
|
|
using Slice = rocksdb::Slice;
|
|
|
|
|
|
|
|
database *d;
|
|
|
|
db::prefix_transform user;
|
|
|
|
|
|
|
|
const char *Name() const override;
|
|
|
|
bool InDomain(const Slice &key) const override;
|
|
|
|
bool InRange(const Slice &key) const override;
|
|
|
|
Slice Transform(const Slice &key) const override;
|
|
|
|
|
|
|
|
prefix_transform(database *const &d, db::prefix_transform user)
|
|
|
|
:d{d}
|
|
|
|
,user{std::move(user)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::database::column
|
2017-03-31 01:20:01 +02:00
|
|
|
:std::enable_shared_from_this<database::column>
|
|
|
|
,rocksdb::ColumnFamilyDescriptor
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
|
|
|
database *d;
|
|
|
|
std::type_index key_type;
|
|
|
|
std::type_index mapped_type;
|
2017-09-08 10:33:41 +02:00
|
|
|
struct descriptor descriptor;
|
2017-03-31 00:57:08 +02:00
|
|
|
comparator cmp;
|
2017-08-31 07:12:58 +02:00
|
|
|
prefix_transform prefix;
|
2017-04-03 06:02:32 +02:00
|
|
|
custom_ptr<rocksdb::ColumnFamilyHandle> handle;
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
operator const rocksdb::ColumnFamilyOptions &();
|
|
|
|
operator const rocksdb::ColumnFamilyHandle *() const;
|
|
|
|
operator const database &() const;
|
|
|
|
|
|
|
|
operator rocksdb::ColumnFamilyOptions &();
|
|
|
|
operator rocksdb::ColumnFamilyHandle *();
|
|
|
|
operator database &();
|
|
|
|
|
2017-09-08 10:33:41 +02:00
|
|
|
explicit column(database *const &d, struct descriptor);
|
2017-04-03 06:02:32 +02:00
|
|
|
column() = delete;
|
|
|
|
column(column &&) = delete;
|
|
|
|
column(const column &) = delete;
|
|
|
|
column &operator=(column &&) = delete;
|
|
|
|
column &operator=(const column &) = delete;
|
2017-03-31 00:57:08 +02:00
|
|
|
~column() noexcept;
|
|
|
|
};
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
static void init_directory();
|
|
|
|
static void init_version();
|
|
|
|
}
|
2017-08-23 22:37:47 +02:00
|
|
|
|
2017-08-23 23:37:06 +02:00
|
|
|
static char ircd_db_version[64];
|
|
|
|
const char *const ircd::db::version(ircd_db_version);
|
|
|
|
|
|
|
|
// Renders a version string from the defines included here.
|
|
|
|
__attribute__((constructor))
|
|
|
|
static void
|
2017-08-23 22:37:47 +02:00
|
|
|
ircd::db::init_version()
|
2017-08-23 23:37:06 +02:00
|
|
|
{
|
|
|
|
snprintf(ircd_db_version, sizeof(ircd_db_version), "%d.%d.%d",
|
|
|
|
ROCKSDB_MAJOR,
|
|
|
|
ROCKSDB_MINOR,
|
|
|
|
ROCKSDB_PATCH);
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
static void
|
|
|
|
ircd::db::init_directory()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto dbdir(fs::get(fs::DB));
|
|
|
|
if(fs::mkdir(dbdir))
|
|
|
|
log.warning("Created new database directory at `%s'", dbdir);
|
|
|
|
else
|
|
|
|
log.info("Using database directory at `%s'", dbdir);
|
|
|
|
}
|
|
|
|
catch(const fs::error &e)
|
|
|
|
{
|
|
|
|
log.error("Cannot start database system: %s", e.what());
|
|
|
|
if(ircd::debugmode)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::init::init()
|
|
|
|
{
|
|
|
|
init_directory();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database
|
|
|
|
//
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
void
|
|
|
|
ircd::db::sync(database &d)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
d.d->SyncWAL()
|
|
|
|
};
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
uint64_t
|
|
|
|
ircd::db::sequence(const database &d)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
return d.d->GetLatestSequenceNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
uint64_t
|
|
|
|
ircd::db::property(database &d,
|
|
|
|
const string_view &name)
|
|
|
|
{
|
|
|
|
uint64_t ret;
|
|
|
|
if(!d.d->GetAggregatedIntProperty(slice(name), &ret))
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
std::shared_ptr<ircd::db::database::column>
|
|
|
|
ircd::db::shared_from(database::column &column)
|
|
|
|
{
|
|
|
|
return column.shared_from_this();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const ircd::db::database::column>
|
|
|
|
ircd::db::shared_from(const database::column &column)
|
|
|
|
{
|
|
|
|
return column.shared_from_this();
|
|
|
|
}
|
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
//
|
2017-03-31 00:57:08 +02:00
|
|
|
// database
|
2017-03-24 02:36:49 +01:00
|
|
|
//
|
2017-08-31 07:13:50 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
database::description default_description;
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
ircd::db::database::database(std::string name,
|
|
|
|
std::string optstr)
|
2017-08-23 23:37:06 +02:00
|
|
|
:database
|
|
|
|
{
|
2017-08-31 07:13:50 +02:00
|
|
|
std::move(name), std::move(optstr), default_description
|
2017-08-23 23:37:06 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
ircd::db::database::database(std::string name,
|
|
|
|
std::string optstr,
|
2017-04-03 06:02:32 +02:00
|
|
|
description description)
|
2016-09-24 06:01:57 +02:00
|
|
|
try
|
2017-03-31 00:57:08 +02:00
|
|
|
:name
|
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
std::move(name)
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
,path
|
|
|
|
{
|
|
|
|
db::path(this->name)
|
|
|
|
}
|
|
|
|
,logs
|
|
|
|
{
|
|
|
|
std::make_shared<struct logs>(this)
|
|
|
|
}
|
|
|
|
,stats
|
|
|
|
{
|
|
|
|
std::make_shared<struct stats>(this)
|
|
|
|
}
|
|
|
|
,events
|
|
|
|
{
|
|
|
|
std::make_shared<struct events>(this)
|
|
|
|
}
|
|
|
|
,mergeop
|
|
|
|
{
|
|
|
|
std::make_shared<struct mergeop>(this)
|
|
|
|
}
|
2017-08-23 23:37:06 +02:00
|
|
|
,cache{[this]
|
|
|
|
() -> std::shared_ptr<rocksdb::Cache>
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
|
|
|
//TODO: XXX
|
2017-04-03 06:02:32 +02:00
|
|
|
const auto lru_cache_size{64_MiB};
|
|
|
|
return rocksdb::NewLRUCache(lru_cache_size);
|
2017-03-24 02:36:49 +01:00
|
|
|
}()}
|
2017-08-23 23:37:06 +02:00
|
|
|
,d{[this, &description, &optstr]
|
|
|
|
() -> custom_ptr<rocksdb::DB>
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
// RocksDB doesn't parse a read_only option, so we allow that to be added
|
|
|
|
// to open the database as read_only and then remove that from the string.
|
|
|
|
const bool read_only
|
|
|
|
{
|
|
|
|
optstr_find_and_remove(optstr, "read_only=true;"s)
|
|
|
|
};
|
|
|
|
|
|
|
|
// We also allow the user to specify fsck=true to run a repair operation on
|
|
|
|
// the db. This may be expensive to do by default every startup.
|
|
|
|
const bool fsck
|
|
|
|
{
|
|
|
|
optstr_find_and_remove(optstr, "fsck=true;"s)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate RocksDB options from string
|
2017-03-31 00:57:08 +02:00
|
|
|
rocksdb::DBOptions opts
|
|
|
|
{
|
|
|
|
options(optstr)
|
|
|
|
};
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
// Setup sundry
|
2017-03-31 00:57:08 +02:00
|
|
|
opts.create_if_missing = true;
|
|
|
|
opts.create_missing_column_families = true;
|
2017-04-03 06:02:32 +02:00
|
|
|
opts.max_file_opening_threads = 0;
|
2017-08-23 22:37:47 +02:00
|
|
|
//opts.use_fsync = true;
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
// Setup logging
|
|
|
|
logs->SetInfoLogLevel(ircd::debugmode? rocksdb::DEBUG_LEVEL : rocksdb::WARN_LEVEL);
|
2017-03-31 00:57:08 +02:00
|
|
|
opts.info_log_level = logs->GetInfoLogLevel();
|
|
|
|
opts.info_log = logs;
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-03-23 22:58:24 +01:00
|
|
|
// Setup event and statistics callbacks
|
2017-03-31 00:57:08 +02:00
|
|
|
opts.listeners.emplace_back(this->events);
|
|
|
|
//opts.statistics = this->stats; // broken?
|
2017-03-23 22:58:24 +01:00
|
|
|
|
|
|
|
// Setup performance metric options
|
|
|
|
//rocksdb::SetPerfLevel(rocksdb::PerfLevel::kDisable);
|
|
|
|
|
|
|
|
// Setup journal recovery options
|
2017-04-03 06:02:32 +02:00
|
|
|
//opts.wal_recovery_mode = rocksdb::WALRecoveryMode::kTolerateCorruptedTailRecords;
|
|
|
|
//opts.wal_recovery_mode = rocksdb::WALRecoveryMode::kAbsoluteConsistency;
|
|
|
|
opts.wal_recovery_mode = rocksdb::WALRecoveryMode::kPointInTimeRecovery;
|
|
|
|
|
|
|
|
// Setup cache
|
|
|
|
opts.row_cache = this->cache;
|
2017-03-24 02:36:49 +01:00
|
|
|
|
|
|
|
// Setup column families
|
2017-04-03 06:02:32 +02:00
|
|
|
for(auto &desc : description)
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
|
|
|
const auto c(std::make_shared<column>(this, std::move(desc)));
|
|
|
|
columns.emplace(c->name, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Existing columns
|
|
|
|
const auto column_names
|
|
|
|
{
|
|
|
|
db::column_names(path, opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specified column descriptors have to describe all existing columns
|
2017-03-24 02:36:49 +01:00
|
|
|
for(const auto &name : column_names)
|
2017-03-31 00:57:08 +02:00
|
|
|
if(!columns.count(name))
|
|
|
|
throw error("Failed to describe existing column '%s'", name);
|
2017-03-24 02:36:49 +01:00
|
|
|
|
|
|
|
// Setup the database closer.
|
|
|
|
const auto deleter([this](rocksdb::DB *const d)
|
|
|
|
noexcept
|
2017-03-23 22:58:24 +01:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
d->SyncWAL() // blocking
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
columns.clear();
|
2017-04-03 06:02:32 +02:00
|
|
|
//rocksdb::CancelAllBackgroundWork(d, true); // true = blocking
|
|
|
|
//throw_on_error(d->PauseBackgroundWork());
|
2017-03-24 02:36:49 +01:00
|
|
|
const auto seq(d->GetLatestSequenceNumber());
|
|
|
|
delete d;
|
|
|
|
|
|
|
|
log.info("'%s': closed database @ `%s' seq[%zu]",
|
|
|
|
this->name,
|
|
|
|
this->path,
|
|
|
|
seq);
|
|
|
|
});
|
2017-03-23 22:58:24 +01:00
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
// Open DB into ptr
|
2017-03-23 22:58:24 +01:00
|
|
|
rocksdb::DB *ptr;
|
2017-03-31 00:57:08 +02:00
|
|
|
std::vector<rocksdb::ColumnFamilyHandle *> handles;
|
|
|
|
std::vector<rocksdb::ColumnFamilyDescriptor> columns(this->columns.size());
|
|
|
|
std::transform(begin(this->columns), end(this->columns), begin(columns), []
|
|
|
|
(const auto &pair)
|
|
|
|
{
|
|
|
|
return static_cast<const rocksdb::ColumnFamilyDescriptor &>(*pair.second);
|
|
|
|
});
|
2017-08-23 22:37:47 +02:00
|
|
|
|
|
|
|
if(fsck && fs::is_dir(path))
|
2017-04-03 06:02:32 +02:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
log.info("Checking database @ `%s' columns[%zu]",
|
|
|
|
path,
|
|
|
|
columns.size());
|
|
|
|
|
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::RepairDB(path, opts, columns)
|
|
|
|
};
|
|
|
|
|
|
|
|
log.info("Database @ `%s' check complete",
|
|
|
|
path,
|
|
|
|
columns.size());
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
2017-08-23 22:37:47 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
// Announce attempt before usual point where exceptions are thrown
|
|
|
|
log.debug("Opening database \"%s\" @ `%s' columns[%zu]",
|
|
|
|
this->name,
|
|
|
|
path,
|
|
|
|
columns.size());
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
if(read_only)
|
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::DB::OpenForReadOnly(opts, path, columns, &handles, &ptr)
|
|
|
|
};
|
|
|
|
else
|
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::DB::Open(opts, path, columns, &handles, &ptr)
|
|
|
|
};
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
for(const auto &handle : handles)
|
2017-04-03 06:02:32 +02:00
|
|
|
this->columns.at(handle->GetName())->handle.reset(handle);
|
2017-03-24 02:36:49 +01:00
|
|
|
|
|
|
|
return { ptr, deleter };
|
|
|
|
}()}
|
2017-08-23 22:37:47 +02:00
|
|
|
,dbs_it
|
|
|
|
{
|
|
|
|
dbs, dbs.emplace(string_view{this->name}, this).first
|
|
|
|
}
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
log.info("'%s': Opened database @ `%s' (handle: %p) columns[%zu] seq[%zu]",
|
2017-08-23 22:37:47 +02:00
|
|
|
this->name,
|
2017-03-24 02:36:49 +01:00
|
|
|
path,
|
|
|
|
(const void *)this,
|
2017-03-31 00:57:08 +02:00
|
|
|
columns.size(),
|
|
|
|
d->GetLatestSequenceNumber());
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
throw error("Failed to open db '%s': %s",
|
|
|
|
this->name,
|
|
|
|
e.what());
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::database::~database()
|
|
|
|
noexcept
|
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
const auto background_errors
|
|
|
|
{
|
|
|
|
property<uint64_t>(*this, rocksdb::DB::Properties::kBackgroundErrors)
|
|
|
|
};
|
|
|
|
|
|
|
|
log.debug("'%s': closing database @ `%s' (background errors: %lu)",
|
|
|
|
name,
|
|
|
|
path,
|
|
|
|
background_errors);
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 23:07:36 +02:00
|
|
|
void
|
|
|
|
ircd::db::database::operator()(const delta &delta)
|
|
|
|
{
|
|
|
|
operator()(sopts{}, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::database::operator()(const std::initializer_list<delta> &deltas)
|
|
|
|
{
|
|
|
|
operator()(sopts{}, deltas);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::database::operator()(const delta *const &begin,
|
|
|
|
const delta *const &end)
|
|
|
|
{
|
|
|
|
operator()(sopts{}, begin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::database::operator()(const sopts &sopts,
|
|
|
|
const delta &delta)
|
|
|
|
{
|
|
|
|
operator()(sopts, &delta, &delta + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::database::operator()(const sopts &sopts,
|
|
|
|
const std::initializer_list<delta> &deltas)
|
|
|
|
{
|
|
|
|
operator()(sopts, std::begin(deltas), std::end(deltas));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::database::operator()(const sopts &sopts,
|
|
|
|
const delta *const &begin,
|
|
|
|
const delta *const &end)
|
|
|
|
{
|
|
|
|
rocksdb::WriteBatch batch;
|
|
|
|
std::for_each(begin, end, [this, &batch]
|
|
|
|
(const delta &delta)
|
|
|
|
{
|
|
|
|
const auto &op(std::get<op>(delta));
|
|
|
|
const auto &col(std::get<1>(delta));
|
|
|
|
const auto &key(std::get<2>(delta));
|
|
|
|
const auto &val(std::get<3>(delta));
|
|
|
|
db::column column(operator[](col));
|
|
|
|
append(batch, column, db::column::delta
|
|
|
|
{
|
|
|
|
op,
|
|
|
|
key,
|
|
|
|
val
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
commit(*this, batch, sopts);
|
2017-08-30 23:07:36 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::column &
|
|
|
|
ircd::db::database::operator[](const string_view &name)
|
2017-04-03 06:02:32 +02:00
|
|
|
try
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
|
|
|
return *columns.at(name);
|
|
|
|
}
|
2017-04-03 06:02:32 +02:00
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
throw schema_error("'%s': column '%s' is not available or specified in schema",
|
|
|
|
this->name,
|
|
|
|
name);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
const ircd::db::database::column &
|
|
|
|
ircd::db::database::operator[](const string_view &name)
|
2017-04-03 06:02:32 +02:00
|
|
|
const try
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
|
|
|
return *columns.at(name);
|
|
|
|
}
|
2017-04-03 06:02:32 +02:00
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
throw schema_error("'%s': column '%s' is not available or specified in schema",
|
|
|
|
this->name,
|
|
|
|
name);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-03-31 01:20:01 +02:00
|
|
|
ircd::db::database &
|
|
|
|
ircd::db::database::get(column &column)
|
|
|
|
{
|
|
|
|
assert(column.d);
|
|
|
|
return *column.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::db::database &
|
|
|
|
ircd::db::database::get(const column &column)
|
|
|
|
{
|
|
|
|
assert(column.d);
|
|
|
|
return *column.d;
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database::comparator
|
|
|
|
//
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ircd::db::database::comparator::Name()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(!user.name.empty());
|
|
|
|
return user.name.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::db::database::comparator::Equal(const Slice &a,
|
|
|
|
const Slice &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(bool(user.equal));
|
2017-08-23 22:37:47 +02:00
|
|
|
const string_view sa{slice(a)};
|
|
|
|
const string_view sb{slice(b)};
|
2017-03-31 00:57:08 +02:00
|
|
|
return user.equal(sa, sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ircd::db::database::comparator::Compare(const Slice &a,
|
|
|
|
const Slice &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(bool(user.less));
|
2017-08-23 22:37:47 +02:00
|
|
|
const string_view sa{slice(a)};
|
|
|
|
const string_view sb{slice(b)};
|
2017-03-31 00:57:08 +02:00
|
|
|
return user.less(sa, sb)? -1:
|
|
|
|
user.less(sb, sa)? 1:
|
|
|
|
0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::database::comparator::FindShortSuccessor(std::string *key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
}
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
void
|
|
|
|
ircd::db::database::comparator::FindShortestSeparator(std::string *key,
|
|
|
|
const Slice &_limit)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const string_view limit{_limit.data(), _limit.size()};
|
2017-03-24 02:36:49 +01:00
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
struct cmp_string_view;
|
|
|
|
struct cmp_int64_t;
|
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::cmp_string_view
|
2017-03-31 00:57:08 +02:00
|
|
|
:db::comparator
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
cmp_string_view()
|
|
|
|
:db::comparator
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
"string_view"
|
|
|
|
,[](const string_view &a, const string_view &b)
|
|
|
|
{
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
,[](const string_view &a, const string_view &b)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
};
|
|
|
|
|
2017-09-08 11:09:34 +02:00
|
|
|
struct ircd::db::cmp_int64_t
|
2017-03-31 00:57:08 +02:00
|
|
|
:db::comparator
|
|
|
|
{
|
|
|
|
cmp_int64_t()
|
|
|
|
:db::comparator
|
|
|
|
{
|
|
|
|
"int64_t"
|
|
|
|
,[](const string_view &sa, const string_view &sb)
|
|
|
|
{
|
|
|
|
assert(sa.size() == sizeof(int64_t));
|
|
|
|
assert(sb.size() == sizeof(int64_t));
|
|
|
|
const auto &a(*reinterpret_cast<const int64_t *>(sa.data()));
|
|
|
|
const auto &b(*reinterpret_cast<const int64_t *>(sb.data()));
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
,[](const string_view &sa, const string_view &sb)
|
|
|
|
{
|
|
|
|
assert(sa.size() == sizeof(int64_t));
|
|
|
|
assert(sb.size() == sizeof(int64_t));
|
|
|
|
const auto &a(*reinterpret_cast<const int64_t *>(sa.data()));
|
|
|
|
const auto &b(*reinterpret_cast<const int64_t *>(sb.data()));
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
};
|
|
|
|
|
2017-08-31 07:12:58 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database::prefix_transform
|
|
|
|
//
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ircd::db::database::prefix_transform::Name()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(!user.name.empty());
|
|
|
|
return user.name.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::Slice
|
|
|
|
ircd::db::database::prefix_transform::Transform(const Slice &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(bool(user.get));
|
|
|
|
const string_view s{slice(key)};
|
|
|
|
return slice(user.get(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::db::database::prefix_transform::InRange(const Slice &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return InDomain(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::db::database::prefix_transform::InDomain(const Slice &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(bool(user.has));
|
|
|
|
const string_view s{slice(key)};
|
|
|
|
return user.has(s);
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database::column
|
|
|
|
//
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::column::column(database *const &d,
|
2017-09-08 10:33:41 +02:00
|
|
|
struct descriptor descriptor)
|
2017-03-31 00:57:08 +02:00
|
|
|
:rocksdb::ColumnFamilyDescriptor
|
|
|
|
{
|
2017-09-08 10:33:41 +02:00
|
|
|
descriptor.name, database::options(descriptor.options)
|
2017-03-24 02:36:49 +01:00
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
,d{d}
|
2017-09-08 10:33:41 +02:00
|
|
|
,key_type{descriptor.type.first}
|
|
|
|
,mapped_type{descriptor.type.second}
|
|
|
|
,descriptor{std::move(descriptor)}
|
|
|
|
,cmp{d, this->descriptor.cmp}
|
|
|
|
,prefix{d, this->descriptor.prefix}
|
2017-04-03 06:02:32 +02:00
|
|
|
,handle
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
nullptr, [this](rocksdb::ColumnFamilyHandle *const handle)
|
|
|
|
{
|
|
|
|
if(handle)
|
|
|
|
this->d->d->DestroyColumnFamilyHandle(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assert(d->columns.count(this->name) == 0);
|
|
|
|
|
2017-09-08 10:33:41 +02:00
|
|
|
if(!this->descriptor.cmp.less)
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
|
|
|
if(key_type == typeid(string_view))
|
|
|
|
this->cmp.user = cmp_string_view{};
|
|
|
|
else if(key_type == typeid(int64_t))
|
|
|
|
this->cmp.user = cmp_int64_t{};
|
|
|
|
else
|
|
|
|
throw error("column '%s' key type[%s] requires user supplied comparator",
|
|
|
|
this->name,
|
|
|
|
key_type.name());
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:33:41 +02:00
|
|
|
// Set the key comparator
|
2017-03-31 00:57:08 +02:00
|
|
|
this->options.comparator = &this->cmp;
|
|
|
|
|
2017-08-31 07:12:58 +02:00
|
|
|
// Set the prefix extractor
|
|
|
|
if(this->prefix.user.get && this->prefix.user.has)
|
|
|
|
this->options.prefix_extractor = std::shared_ptr<const rocksdb::SliceTransform>
|
|
|
|
{
|
|
|
|
&this->prefix, [](const rocksdb::SliceTransform *) {}
|
|
|
|
};
|
2017-08-23 22:37:47 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
//if(d->mergeop->merger)
|
|
|
|
// this->options.merge_operator = d->mergeop;
|
|
|
|
|
|
|
|
//log.debug("'%s': Creating new column '%s'", d->name, this->name);
|
|
|
|
//throw_on_error(d->d->CreateColumnFamily(this->options, this->name, &this->handle));
|
2017-03-24 02:36:49 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::column::~column()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::database::column::operator
|
|
|
|
database &()
|
|
|
|
{
|
|
|
|
return *d;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::database::column::operator
|
|
|
|
rocksdb::ColumnFamilyHandle *()
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return handle.get();
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::database::column::operator
|
|
|
|
const database &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return *d;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::database::column::operator
|
|
|
|
const rocksdb::ColumnFamilyHandle *()
|
|
|
|
const
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return handle.get();
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::drop(database::column &c)
|
|
|
|
{
|
|
|
|
if(!c.handle)
|
|
|
|
return;
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
c.d->d->DropColumnFamily(c.handle.get())
|
|
|
|
};
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
ircd::db::id(const database::column &c)
|
|
|
|
{
|
|
|
|
if(!c.handle)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return c.handle->GetID();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &
|
|
|
|
ircd::db::name(const database::column &c)
|
|
|
|
{
|
|
|
|
return c.name;
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
const std::string &
|
|
|
|
ircd::db::name(const database &d)
|
|
|
|
{
|
|
|
|
return d.name;
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database::snapshot
|
|
|
|
//
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
ircd::db::sequence(const database::snapshot &s)
|
|
|
|
{
|
|
|
|
const rocksdb::Snapshot *const rs(s);
|
|
|
|
return rs->GetSequenceNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::database::snapshot::snapshot(database &d)
|
2017-08-23 22:37:47 +02:00
|
|
|
:s
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
d.d->GetSnapshot(),
|
|
|
|
[dp(weak_from(d))](const rocksdb::Snapshot *const s)
|
|
|
|
{
|
|
|
|
if(!s)
|
|
|
|
return;
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
const auto d(dp.lock());
|
|
|
|
log.debug("'%s' @%p: snapshot(@%p) release seq[%lu]",
|
|
|
|
db::name(*d),
|
|
|
|
d->d.get(),
|
|
|
|
s,
|
|
|
|
s->GetSequenceNumber());
|
|
|
|
|
|
|
|
d->d->ReleaseSnapshot(s);
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
log.debug("'%s' @%p: snapshot(@%p) seq[%lu]",
|
|
|
|
db::name(d),
|
|
|
|
d.d.get(),
|
|
|
|
s.get(),
|
|
|
|
sequence(*this));
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::database::snapshot::~snapshot()
|
2017-03-24 02:36:49 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database::logs
|
|
|
|
//
|
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
static
|
|
|
|
ircd::log::facility
|
|
|
|
translate(const rocksdb::InfoLogLevel &level)
|
|
|
|
{
|
|
|
|
switch(level)
|
|
|
|
{
|
|
|
|
// Treat all infomational messages from rocksdb as debug here for now.
|
|
|
|
// We can clean them up and make better reports for our users eventually.
|
|
|
|
default:
|
|
|
|
case rocksdb::InfoLogLevel::DEBUG_LEVEL: return ircd::log::facility::DEBUG;
|
|
|
|
case rocksdb::InfoLogLevel::INFO_LEVEL: return ircd::log::facility::DEBUG;
|
|
|
|
|
|
|
|
case rocksdb::InfoLogLevel::WARN_LEVEL: return ircd::log::facility::WARNING;
|
|
|
|
case rocksdb::InfoLogLevel::ERROR_LEVEL: return ircd::log::facility::ERROR;
|
|
|
|
case rocksdb::InfoLogLevel::FATAL_LEVEL: return ircd::log::facility::CRITICAL;
|
|
|
|
case rocksdb::InfoLogLevel::HEADER_LEVEL: return ircd::log::facility::NOTICE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::logs::Logv(const char *const fmt,
|
|
|
|
va_list ap)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
Logv(rocksdb::InfoLogLevel::DEBUG_LEVEL, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::logs::LogHeader(const char *const fmt,
|
|
|
|
va_list ap)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
Logv(rocksdb::InfoLogLevel::DEBUG_LEVEL, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::logs::Logv(const rocksdb::InfoLogLevel level,
|
|
|
|
const char *const fmt,
|
|
|
|
va_list ap)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
if(level < GetInfoLogLevel())
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buf[1024]; const auto len
|
|
|
|
{
|
2017-08-23 23:37:06 +02:00
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap)
|
2017-03-24 02:36:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto str
|
|
|
|
{
|
|
|
|
// RocksDB adds annoying leading whitespace to attempt to right-justify things and idc
|
|
|
|
lstrip(buf, ' ')
|
|
|
|
};
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
// Skip the options for now
|
|
|
|
if(startswith(str, "Options"))
|
|
|
|
return;
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
log(translate(level), "'%s': (rdb) %s", d->name, str);
|
2017-03-24 02:36:49 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database::mergeop
|
|
|
|
//
|
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
const char *
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::mergeop::Name()
|
2017-03-24 02:36:49 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return "<unnamed>";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::mergeop::Merge(const rocksdb::Slice &_key,
|
|
|
|
const rocksdb::Slice *const _exist,
|
|
|
|
const rocksdb::Slice &_update,
|
|
|
|
std::string *const newval,
|
|
|
|
rocksdb::Logger *const)
|
2017-03-24 02:36:49 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
const string_view key
|
|
|
|
{
|
|
|
|
_key.data(), _key.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view exist
|
|
|
|
{
|
|
|
|
_exist? string_view { _exist->data(), _exist->size() } : string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view update
|
|
|
|
{
|
|
|
|
_update.data(), _update.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(exist.empty())
|
|
|
|
{
|
|
|
|
*newval = std::string(update);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//XXX caching opportunity?
|
|
|
|
*newval = merger(key, {exist, update}); // call the user
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::bad_function_call &e)
|
|
|
|
{
|
|
|
|
log.critical("merge: missing merge operator (%s)", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log.error("merge: %s", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// database::stats
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::log_rdb_perf_context(const bool &all)
|
|
|
|
{
|
|
|
|
const bool exclude_zeros(!all);
|
|
|
|
log.debug("%s", rocksdb::perf_context.ToString(exclude_zeros));
|
|
|
|
}
|
|
|
|
|
2017-07-18 19:11:00 +02:00
|
|
|
uint64_t
|
|
|
|
ircd::db::database::stats::getAndResetTickerCount(const uint32_t type)
|
|
|
|
{
|
|
|
|
const auto ret(getTickerCount(type));
|
|
|
|
setTickerCount(type, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
bool
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::stats::HistEnabledForType(const uint32_t type)
|
2017-03-24 02:36:49 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return type < histogram.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::stats::measureTime(const uint32_t type,
|
|
|
|
const uint64_t time)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::stats::histogramData(const uint32_t type,
|
|
|
|
rocksdb::HistogramData *const data)
|
2017-03-24 02:36:49 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
const auto &median(data->median);
|
|
|
|
const auto &percentile95(data->percentile95);
|
|
|
|
const auto &percentile88(data->percentile99);
|
|
|
|
const auto &average(data->average);
|
|
|
|
const auto &standard_deviation(data->standard_deviation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::stats::recordTick(const uint32_t type,
|
|
|
|
const uint64_t count)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
ticker.at(type) += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::stats::setTickerCount(const uint32_t type,
|
|
|
|
const uint64_t count)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
ticker.at(type) = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::stats::getTickerCount(const uint32_t type)
|
2017-03-24 02:36:49 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return ticker.at(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::events::OnFlushCompleted(rocksdb::DB *const db,
|
|
|
|
const rocksdb::FlushJobInfo &info)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
log.debug("'%s' @%p: flushed: column[%s] path[%s] tid[%lu] job[%d] writes[slow:%d stop:%d]",
|
2017-03-31 00:57:08 +02:00
|
|
|
d->name,
|
2017-03-24 02:36:49 +01:00
|
|
|
db,
|
|
|
|
info.cf_name,
|
|
|
|
info.file_path,
|
|
|
|
info.thread_id,
|
|
|
|
info.job_id,
|
|
|
|
info.triggered_writes_slowdown,
|
|
|
|
info.triggered_writes_stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::events::OnCompactionCompleted(rocksdb::DB *const db,
|
|
|
|
const rocksdb::CompactionJobInfo &info)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
log.debug("'%s' @%p: compacted: column[%s] status[%d] tid[%lu] job[%d]",
|
2017-03-31 00:57:08 +02:00
|
|
|
d->name,
|
2017-03-24 02:36:49 +01:00
|
|
|
db,
|
|
|
|
info.cf_name,
|
|
|
|
int(info.status.code()),
|
|
|
|
info.thread_id,
|
|
|
|
info.job_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::events::OnTableFileDeleted(const rocksdb::TableFileDeletionInfo &info)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
log.debug("'%s': table file deleted: db[%s] path[%s] status[%d] job[%d]",
|
2017-03-31 00:57:08 +02:00
|
|
|
d->name,
|
2017-03-24 02:36:49 +01:00
|
|
|
info.db_name,
|
|
|
|
info.file_path,
|
|
|
|
int(info.status.code()),
|
|
|
|
info.job_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::events::OnTableFileCreated(const rocksdb::TableFileCreationInfo &info)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
log.debug("'%s': table file created: db[%s] path[%s] status[%d] job[%d]",
|
2017-03-31 00:57:08 +02:00
|
|
|
d->name,
|
2017-03-24 02:36:49 +01:00
|
|
|
info.db_name,
|
|
|
|
info.file_path,
|
|
|
|
int(info.status.code()),
|
|
|
|
info.job_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::events::OnTableFileCreationStarted(const rocksdb::TableFileCreationBriefInfo &info)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
log.debug("'%s': table file creating: db[%s] column[%s] path[%s] job[%d]",
|
2017-03-31 00:57:08 +02:00
|
|
|
d->name,
|
2017-03-24 02:36:49 +01:00
|
|
|
info.db_name,
|
|
|
|
info.cf_name,
|
|
|
|
info.file_path,
|
|
|
|
info.job_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::events::OnMemTableSealed(const rocksdb::MemTableInfo &info)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
|
|
|
log.debug("'%s': memory table sealed: column[%s] entries[%lu] deletes[%lu]",
|
2017-03-31 00:57:08 +02:00
|
|
|
d->name,
|
2017-03-24 02:36:49 +01:00
|
|
|
info.cf_name,
|
|
|
|
info.num_entries,
|
|
|
|
info.num_deletes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::events::OnColumnFamilyHandleDeletionStarted(rocksdb::ColumnFamilyHandle *const h)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
log.debug("'%s': column[%s] handle closing @ %p",
|
|
|
|
d->name,
|
|
|
|
h->GetName(),
|
2017-03-24 02:36:49 +01:00
|
|
|
h);
|
|
|
|
}
|
|
|
|
|
2017-09-08 11:02:47 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// db/index.h
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::db::index::const_iterator
|
|
|
|
ircd::db::index::find(const string_view &key)
|
|
|
|
{
|
|
|
|
auto ret{column::lower_bound(key)};
|
|
|
|
if(!ret)
|
|
|
|
return const_iterator{};
|
|
|
|
|
|
|
|
const auto &prefix(c->descriptor.prefix);
|
|
|
|
if(prefix.has && prefix.has(ret->first))
|
|
|
|
if(prefix.get && key != prefix.get(ret->first))
|
|
|
|
return const_iterator{};
|
|
|
|
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::index::const_iterator
|
|
|
|
ircd::db::index::begin()
|
|
|
|
{
|
|
|
|
return column::begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::index::const_iterator
|
|
|
|
ircd::db::index::end()
|
|
|
|
{
|
|
|
|
return column::end();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// const_iterator
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::db::index::const_iterator::const_iterator(column::const_iterator it)
|
|
|
|
:column::const_iterator{std::move(it)}
|
|
|
|
{
|
|
|
|
// const auto &prefix(c->descriptor.prefix);
|
|
|
|
// if(unlikely(!prefix.has || !prefix.get))
|
|
|
|
// throw schema_error("column '%s' has no prefix logic; it cannot be an index",
|
|
|
|
// name(*c));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::db::index::const_iterator::value_type &
|
|
|
|
ircd::db::index::const_iterator::operator*()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto t(const_cast<const_iterator *>(this));
|
|
|
|
t->column::const_iterator::operator*();
|
|
|
|
string_view &key(t->val.first);
|
|
|
|
const auto &prefix(c->descriptor.prefix);
|
|
|
|
if(prefix.has && prefix.has(key))
|
|
|
|
{
|
|
|
|
const auto first(prefix.get(key));
|
|
|
|
const auto second(key.substr(first.size()));
|
|
|
|
key = second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::db::index::const_iterator::value_type *
|
|
|
|
ircd::db::index::const_iterator::operator->()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return &this->operator*();
|
|
|
|
}
|
|
|
|
|
2017-09-08 11:23:11 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// db/cursor.h
|
|
|
|
//
|
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-04-03 06:02:32 +02:00
|
|
|
// db/cell.h
|
2017-03-24 02:36:49 +01:00
|
|
|
//
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
uint64_t
|
|
|
|
ircd::db::sequence(const cell &c)
|
|
|
|
{
|
|
|
|
const database::snapshot &ss(c);
|
|
|
|
return sequence(database::snapshot(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &
|
|
|
|
ircd::db::name(const cell &c)
|
|
|
|
{
|
|
|
|
return name(c.c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::write(const cell::delta &delta,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
write(&delta, &delta + 1, sopts);
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
void
|
|
|
|
ircd::db::write(const sopts &sopts,
|
|
|
|
const std::initializer_list<cell::delta> &deltas)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
write(deltas, sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::write(const std::initializer_list<cell::delta> &deltas,
|
|
|
|
const sopts &sopts)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
write(std::begin(deltas), std::end(deltas), sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::write(const cell::delta *const &begin,
|
|
|
|
const cell::delta *const &end,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
if(begin == end)
|
2017-04-03 06:02:32 +02:00
|
|
|
return;
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
// Find the database through one of the cell's columns. cell::deltas
|
|
|
|
// may come from different columns so we do nothing else with this.
|
|
|
|
auto &front(*begin);
|
2017-08-29 03:44:27 +02:00
|
|
|
column &c(std::get<cell *>(front)->c);
|
2017-04-03 06:02:32 +02:00
|
|
|
database &d(c);
|
|
|
|
|
|
|
|
rocksdb::WriteBatch batch;
|
2017-08-23 22:37:47 +02:00
|
|
|
std::for_each(begin, end, [&batch]
|
|
|
|
(const cell::delta &delta)
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
append(batch, delta);
|
2017-08-23 22:37:47 +02:00
|
|
|
});
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
commit(d, batch, sopts);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 23:05:15 +02:00
|
|
|
template<class pos>
|
|
|
|
bool
|
|
|
|
ircd::db::seek(cell &c,
|
|
|
|
const pos &p)
|
|
|
|
{
|
|
|
|
column &cc(c);
|
|
|
|
database::column &dc(cc);
|
|
|
|
|
|
|
|
gopts opts;
|
|
|
|
opts.snapshot = c.ss;
|
|
|
|
auto ropts(make_opts(opts));
|
|
|
|
return seek(dc, p, ropts, c.it);
|
|
|
|
}
|
|
|
|
template bool ircd::db::seek<ircd::db::pos>(cell &, const pos &);
|
|
|
|
template bool ircd::db::seek<ircd::string_view>(cell &, const string_view &);
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
// Linkage for incomplete rocksdb::Iterator
|
|
|
|
ircd::db::cell::cell()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::cell::cell(database &d,
|
|
|
|
const string_view &colname,
|
|
|
|
const string_view &index,
|
|
|
|
gopts opts)
|
|
|
|
:cell
|
|
|
|
{
|
|
|
|
column(d[colname]), index, std::move(opts)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::cell::cell(column column,
|
|
|
|
const string_view &index,
|
|
|
|
gopts opts)
|
|
|
|
:c{std::move(column)}
|
|
|
|
,index{index}
|
|
|
|
,ss{opts.snapshot}
|
|
|
|
,it{ss? seek(this->c, this->index, opts) : std::unique_ptr<rocksdb::Iterator>{}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::cell::cell(column column,
|
|
|
|
const string_view &index,
|
2017-08-23 22:37:47 +02:00
|
|
|
std::unique_ptr<rocksdb::Iterator> it,
|
|
|
|
gopts opts)
|
2017-04-03 06:02:32 +02:00
|
|
|
:c{std::move(column)}
|
|
|
|
,index{index}
|
2017-08-23 22:37:47 +02:00
|
|
|
,ss{std::move(opts.snapshot)}
|
2017-04-03 06:02:32 +02:00
|
|
|
,it{std::move(it)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linkage for incomplete rocksdb::Iterator
|
|
|
|
ircd::db::cell::cell(cell &&o)
|
2017-03-31 00:57:08 +02:00
|
|
|
noexcept
|
2017-04-03 06:02:32 +02:00
|
|
|
:c{std::move(o.c)}
|
|
|
|
,index{std::move(o.index)}
|
|
|
|
,ss{std::move(o.ss)}
|
|
|
|
,it{std::move(o.it)}
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
// Linkage for incomplete rocksdb::Iterator
|
|
|
|
ircd::db::cell &
|
|
|
|
ircd::db::cell::operator=(cell &&o)
|
2017-03-31 00:57:08 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
c = std::move(o.c);
|
|
|
|
index = std::move(o.index);
|
|
|
|
ss = std::move(o.ss);
|
|
|
|
it = std::move(o.it);
|
2017-03-31 00:57:08 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
// Linkage for incomplete rocksdb::Iterator
|
|
|
|
ircd::db::cell::~cell()
|
2017-03-31 00:57:08 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
bool
|
|
|
|
ircd::db::cell::load(gopts opts)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
database &d(c);
|
|
|
|
if(valid() && !opts.snapshot && sequence(ss) == sequence(d))
|
|
|
|
return true;
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
if(bool(opts.snapshot))
|
|
|
|
{
|
|
|
|
this->it.reset();
|
|
|
|
this->ss = std::move(opts.snapshot);
|
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
std::unique_ptr<rocksdb::TransactionLogIterator> tit;
|
|
|
|
throw_on_error(d.d->GetUpdatesSince(0, &tit));
|
|
|
|
while(tit && tit->Valid())
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
auto batchres(tit->GetBatch());
|
2017-08-23 22:37:47 +02:00
|
|
|
//std::cout << "seq: " << batchres.sequence;
|
2017-04-03 06:02:32 +02:00
|
|
|
if(batchres.writeBatchPtr)
|
|
|
|
{
|
|
|
|
auto &batch(*batchres.writeBatchPtr);
|
2017-08-23 22:37:47 +02:00
|
|
|
//std::cout << " count " << batch.Count() << " ds: " << batch.GetDataSize() << " " << batch.Data() << std::endl;
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
tit->Next();
|
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
database::column &c(this->c);
|
2017-08-30 23:05:15 +02:00
|
|
|
return seek(c, index, opts, this->it);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
2017-03-24 02:36:49 +01:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::db::cell::exchange(const string_view &desired)
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
const auto ret(val());
|
|
|
|
(*this) = desired;
|
2017-03-24 02:36:49 +01:00
|
|
|
return ret;
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
bool
|
|
|
|
ircd::db::cell::compare_exchange(string_view &expected,
|
|
|
|
const string_view &desired)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
const auto existing(val());
|
|
|
|
if(expected.size() != existing.size() ||
|
|
|
|
memcmp(expected.data(), existing.data(), expected.size()) != 0)
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
expected = existing;
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
expected = existing;
|
|
|
|
(*this) = desired;
|
|
|
|
return true;
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::cell &
|
|
|
|
ircd::db::cell::operator=(const string_view &s)
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-08-30 23:21:18 +02:00
|
|
|
write(c, key(), s);
|
2017-04-03 06:02:32 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
void
|
|
|
|
ircd::db::cell::operator()(const op &op,
|
|
|
|
const string_view &val,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
write(cell::delta{op, *this, val}, sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::cell::operator
|
|
|
|
string_view()
|
2017-04-03 06:02:32 +02:00
|
|
|
{
|
|
|
|
return val();
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
ircd::db::cell::operator
|
|
|
|
string_view()
|
2017-04-03 06:02:32 +02:00
|
|
|
const
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return val();
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::db::cell::val()
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
if(!valid())
|
|
|
|
load();
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
return likely(valid())? db::val(*it) : string_view{};
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::db::cell::key()
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
if(!valid())
|
|
|
|
load();
|
|
|
|
|
|
|
|
return likely(valid())? db::key(*it) : index;
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::db::cell::val()
|
|
|
|
const
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return likely(valid())? db::val(*it) : string_view{};
|
2017-03-24 02:36:49 +01:00
|
|
|
}
|
2017-03-14 19:39:26 +01:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::db::cell::key()
|
|
|
|
const
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return likely(valid())? db::key(*it) : index;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::db::cell::valid()
|
|
|
|
const
|
|
|
|
{
|
2017-08-30 23:21:18 +02:00
|
|
|
return it && (index.empty() || valid_equal(*it, index));
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-03-31 00:57:08 +02:00
|
|
|
//
|
2017-04-03 06:02:32 +02:00
|
|
|
// db/row.h
|
2017-03-31 00:57:08 +02:00
|
|
|
//
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
void
|
|
|
|
ircd::db::del(row &row,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
write(row::delta{op::DELETE, row}, sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::write(const row::delta &delta,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
write(&delta, &delta + 1, sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::write(const sopts &sopts,
|
|
|
|
const std::initializer_list<row::delta> &deltas)
|
|
|
|
{
|
|
|
|
write(deltas, sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::write(const std::initializer_list<row::delta> &deltas,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
write(std::begin(deltas), std::end(deltas), sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::write(const row::delta *const &begin,
|
|
|
|
const row::delta *const &end,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
// Count the total number of cells for this transaction.
|
|
|
|
const auto cells
|
|
|
|
{
|
|
|
|
std::accumulate(begin, end, size_t(0), []
|
|
|
|
(auto ret, const row::delta &delta)
|
|
|
|
{
|
2017-08-29 03:44:27 +02:00
|
|
|
const auto &row(std::get<row *>(delta));
|
|
|
|
return ret += row->size();
|
2017-08-23 22:37:47 +02:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: allocator?
|
|
|
|
std::vector<cell::delta> deltas;
|
|
|
|
deltas.reserve(cells);
|
|
|
|
|
|
|
|
// Compose all of the cells from all of the rows into a single txn
|
|
|
|
std::for_each(begin, end, [&deltas]
|
|
|
|
(const auto &delta)
|
|
|
|
{
|
|
|
|
const auto &op(std::get<op>(delta));
|
2017-08-29 03:44:27 +02:00
|
|
|
const auto &row(std::get<row *>(delta));
|
|
|
|
std::for_each(std::begin(*row), std::end(*row), [&deltas, &op]
|
2017-08-23 22:37:47 +02:00
|
|
|
(auto &cell)
|
|
|
|
{
|
|
|
|
// For operations like DELETE which don't require a value in
|
|
|
|
// the delta, we can skip a potentially expensive load of the cell.
|
|
|
|
const auto value
|
|
|
|
{
|
|
|
|
value_required(op)? cell.val() : string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
deltas.emplace_back(op, cell, value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Commitment
|
|
|
|
write(&deltas.front(), &deltas.front() + deltas.size(), sopts);
|
|
|
|
}
|
|
|
|
|
2017-08-30 23:05:15 +02:00
|
|
|
template<class pos>
|
2017-09-12 21:03:13 +02:00
|
|
|
size_t
|
2017-08-30 23:05:15 +02:00
|
|
|
ircd::db::seek(row &r,
|
|
|
|
const pos &p)
|
|
|
|
{
|
2017-09-12 21:03:13 +02:00
|
|
|
return std::count_if(begin(r.its), end(r.its), [&p]
|
2017-08-30 23:05:15 +02:00
|
|
|
(auto &cell)
|
|
|
|
{
|
|
|
|
return seek(cell, p);
|
|
|
|
});
|
|
|
|
}
|
2017-09-12 21:03:13 +02:00
|
|
|
template size_t ircd::db::seek<ircd::db::pos>(row &, const pos &);
|
|
|
|
template size_t ircd::db::seek<ircd::string_view>(row &, const string_view &);
|
2017-08-30 23:05:15 +02:00
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::db::trim(row &r)
|
|
|
|
{
|
|
|
|
return trim(r, []
|
|
|
|
(const auto &cell)
|
|
|
|
{
|
|
|
|
return !valid(*cell.it);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::db::trim(row &r,
|
|
|
|
const string_view &index)
|
|
|
|
{
|
|
|
|
return trim(r, [&index]
|
|
|
|
(const auto &cell)
|
|
|
|
{
|
|
|
|
return !valid_equal(*cell.it, index);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::db::trim(row &r,
|
|
|
|
const std::function<bool (cell &)> &closure)
|
|
|
|
{
|
|
|
|
const auto end(std::remove_if(std::begin(r.its), std::end(r.its), closure));
|
|
|
|
const auto ret(std::distance(end, std::end(r.its)));
|
|
|
|
r.its.erase(end, std::end(r.its));
|
|
|
|
r.its.shrink_to_fit();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::row::row(database &d,
|
|
|
|
const string_view &key,
|
|
|
|
const vector_view<string_view> &colnames,
|
2017-08-23 22:37:47 +02:00
|
|
|
gopts opts)
|
2017-04-03 06:02:32 +02:00
|
|
|
:its{[this, &d, &key, &colnames, &opts]
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
using std::end;
|
|
|
|
using std::begin;
|
|
|
|
using rocksdb::Iterator;
|
|
|
|
using rocksdb::ColumnFamilyHandle;
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
if(!opts.snapshot)
|
|
|
|
opts.snapshot = database::snapshot(d);
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
const rocksdb::ReadOptions options
|
|
|
|
{
|
|
|
|
make_opts(opts)
|
|
|
|
};
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
//TODO: allocator
|
2017-04-03 06:02:32 +02:00
|
|
|
std::vector<database::column *> colptr
|
|
|
|
{
|
|
|
|
colnames.empty()? d.columns.size() : colnames.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
if(colnames.empty())
|
|
|
|
std::transform(begin(d.columns), end(d.columns), begin(colptr), [&colnames]
|
|
|
|
(const auto &p)
|
|
|
|
{
|
|
|
|
return p.second.get();
|
|
|
|
});
|
|
|
|
else
|
|
|
|
std::transform(begin(colnames), end(colnames), begin(colptr), [&d]
|
|
|
|
(const auto &name)
|
|
|
|
{
|
|
|
|
return &d[name];
|
|
|
|
});
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
//TODO: allocator
|
2017-04-03 06:02:32 +02:00
|
|
|
std::vector<ColumnFamilyHandle *> handles(colptr.size());
|
|
|
|
std::transform(begin(colptr), end(colptr), begin(handles), []
|
|
|
|
(database::column *const &ptr)
|
|
|
|
{
|
|
|
|
return ptr->handle.get();
|
|
|
|
});
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
//TODO: does this block?
|
2017-04-03 06:02:32 +02:00
|
|
|
std::vector<Iterator *> iterators;
|
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
d.d->NewIterators(options, handles, &iterators)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<cell> ret(iterators.size());
|
|
|
|
for(size_t i(0); i < ret.size(); ++i)
|
|
|
|
{
|
|
|
|
std::unique_ptr<Iterator> it(iterators.at(i));
|
2017-08-23 22:37:47 +02:00
|
|
|
ret[i] = cell { *colptr.at(i), key, std::move(it), opts };
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}()}
|
|
|
|
{
|
|
|
|
if(key.empty())
|
|
|
|
{
|
|
|
|
seek(*this, pos::FRONT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
seek(*this, key);
|
|
|
|
|
|
|
|
// without the noempty flag, all cells for a row show up in the row
|
|
|
|
// i.e all the columns of the db, etc
|
|
|
|
const bool noempty
|
|
|
|
{
|
|
|
|
has_opt(opts, get::NO_EMPTY)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto trimmer([&key, &noempty]
|
|
|
|
(auto &cell)
|
|
|
|
{
|
|
|
|
if(noempty)
|
|
|
|
return cell.key() != key;
|
|
|
|
|
|
|
|
// seek() returns a lower_bound so we have to compare equality
|
|
|
|
// here to not give the user data from the wrong row. The cell itself
|
|
|
|
// is not removed to allow the column to be visible in the row.
|
|
|
|
if(cell.key() != key)
|
|
|
|
cell.it.reset();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
trim(*this, trimmer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::row::operator()(const op &op,
|
|
|
|
const string_view &col,
|
|
|
|
const string_view &val,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
write(cell::delta{op, (*this)[col], val}, sopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::row::iterator
|
|
|
|
ircd::db::row::find(const string_view &col)
|
|
|
|
{
|
|
|
|
iterator ret;
|
|
|
|
ret.it = std::find_if(std::begin(its), std::end(its), [&col]
|
|
|
|
(const auto &cell)
|
|
|
|
{
|
|
|
|
return name(cell.c) == col;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::row::const_iterator
|
|
|
|
ircd::db::row::find(const string_view &col)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const_iterator ret;
|
|
|
|
ret.it = std::find_if(std::begin(its), std::end(its), [&col]
|
|
|
|
(const auto &cell)
|
|
|
|
{
|
|
|
|
return name(cell.c) == col;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-30 23:22:19 +02:00
|
|
|
bool
|
|
|
|
ircd::db::row::valid()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::any_of(std::begin(its), std::end(its), []
|
|
|
|
(const auto &cell)
|
|
|
|
{
|
|
|
|
return cell.valid();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::row::load(const gopts &opts)
|
|
|
|
{
|
|
|
|
std::for_each(std::begin(its), std::end(its), [&opts]
|
|
|
|
(auto &cell)
|
|
|
|
{
|
|
|
|
cell.load(opts);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// db/column.h
|
|
|
|
//
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ircd::db::read(column &column,
|
|
|
|
const string_view &key,
|
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
const auto copy([&ret]
|
|
|
|
(const string_view &src)
|
|
|
|
{
|
|
|
|
ret.assign(begin(src), end(src));
|
|
|
|
});
|
|
|
|
|
|
|
|
column(key, copy, gopts);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::db::read(column &column,
|
|
|
|
const string_view &key,
|
|
|
|
uint8_t *const &buf,
|
|
|
|
const size_t &max,
|
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
const auto copy([&ret, &buf, &max]
|
|
|
|
(const string_view &src)
|
|
|
|
{
|
|
|
|
ret = std::min(src.size(), max);
|
|
|
|
memcpy(buf, src.data(), ret);
|
|
|
|
});
|
|
|
|
|
|
|
|
column(key, copy, gopts);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::db::read(column &column,
|
|
|
|
const string_view &key,
|
|
|
|
char *const &buf,
|
|
|
|
const size_t &max,
|
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
const auto copy([&ret, &buf, &max]
|
|
|
|
(const string_view &src)
|
|
|
|
{
|
|
|
|
ret = strlcpy(buf, src.data(), std::min(src.size(), max));
|
|
|
|
});
|
|
|
|
|
|
|
|
column(key, copy, gopts);
|
|
|
|
return { buf, ret };
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::string
|
|
|
|
ircd::db::property(column &column,
|
|
|
|
const string_view &name)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
database &d(column);
|
|
|
|
database::column &c(column);
|
|
|
|
d.d->GetProperty(c, slice(name), &ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
uint64_t
|
|
|
|
ircd::db::property(column &column,
|
|
|
|
const string_view &name)
|
|
|
|
{
|
|
|
|
uint64_t ret;
|
|
|
|
database &d(column);
|
|
|
|
database::column &c(column);
|
|
|
|
if(!d.d->GetIntProperty(c, slice(name), &ret))
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::db::bytes(column &column)
|
|
|
|
{
|
|
|
|
rocksdb::ColumnFamilyMetaData cfm;
|
|
|
|
database::column &c(column);
|
|
|
|
database &d(c);
|
|
|
|
assert(bool(c.handle));
|
|
|
|
d.d->GetColumnFamilyMetaData(c.handle.get(), &cfm);
|
|
|
|
return cfm.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::db::file_count(column &column)
|
|
|
|
{
|
|
|
|
rocksdb::ColumnFamilyMetaData cfm;
|
|
|
|
database::column &c(column);
|
|
|
|
database &d(c);
|
|
|
|
assert(bool(c.handle));
|
|
|
|
d.d->GetColumnFamilyMetaData(c.handle.get(), &cfm);
|
|
|
|
return cfm.file_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &
|
|
|
|
ircd::db::name(const column &column)
|
|
|
|
{
|
|
|
|
const database::column &c(column);
|
|
|
|
return name(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::flush(column &column,
|
|
|
|
const bool &blocking)
|
|
|
|
{
|
|
|
|
database &d(column);
|
|
|
|
database::column &c(column);
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
rocksdb::FlushOptions opts;
|
|
|
|
opts.wait = blocking;
|
2017-04-03 06:02:32 +02:00
|
|
|
log.debug("'%s':'%s' @%lu FLUSH",
|
|
|
|
name(d),
|
|
|
|
name(c),
|
|
|
|
sequence(d));
|
2017-03-31 00:57:08 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
d.d->Flush(opts, c)
|
|
|
|
};
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::del(column &column,
|
|
|
|
const string_view &key,
|
|
|
|
const sopts &sopts)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
database &d(column);
|
|
|
|
database::column &c(column);
|
2017-04-03 06:02:32 +02:00
|
|
|
log.debug("'%s':'%s' @%lu DELETE key(%zu B)",
|
|
|
|
name(d),
|
|
|
|
name(c),
|
|
|
|
sequence(d),
|
|
|
|
key.size());
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2016-09-25 06:12:43 +02:00
|
|
|
auto opts(make_opts(sopts));
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
d.d->Delete(opts, c, slice(key))
|
|
|
|
};
|
2016-09-25 06:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::write(column &column,
|
|
|
|
const string_view &key,
|
2016-09-25 06:12:43 +02:00
|
|
|
const uint8_t *const &buf,
|
|
|
|
const size_t &size,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
const string_view val
|
|
|
|
{
|
|
|
|
reinterpret_cast<const char *>(buf), size
|
|
|
|
};
|
|
|
|
|
|
|
|
write(column, key, key, sopts);
|
2016-09-25 06:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::write(column &column,
|
|
|
|
const string_view &key,
|
2016-11-29 16:23:38 +01:00
|
|
|
const string_view &val,
|
2016-09-25 06:12:43 +02:00
|
|
|
const sopts &sopts)
|
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
database &d(column);
|
|
|
|
database::column &c(column);
|
2017-04-03 06:02:32 +02:00
|
|
|
log.debug("'%s':'%s' @%lu PUT key(%zu B) val(%zu B)",
|
|
|
|
name(d),
|
|
|
|
name(c),
|
|
|
|
sequence(d),
|
|
|
|
key.size(),
|
|
|
|
val.size());
|
2016-09-25 03:18:54 +02:00
|
|
|
|
|
|
|
auto opts(make_opts(sopts));
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
d.d->Put(opts, c, slice(key), slice(val))
|
|
|
|
};
|
2016-09-25 06:12:43 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 01:20:01 +02:00
|
|
|
bool
|
|
|
|
ircd::db::has(column &column,
|
|
|
|
const string_view &key,
|
|
|
|
const gopts &gopts)
|
|
|
|
{
|
|
|
|
database &d(column);
|
|
|
|
database::column &c(column);
|
|
|
|
|
|
|
|
const auto k(slice(key));
|
|
|
|
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.d->KeyMayExist(opts, c, k, nullptr, nullptr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Perform a query to the cache
|
2017-07-18 19:11:00 +02:00
|
|
|
static std::string *const null_str_ptr(nullptr);
|
|
|
|
auto status(d.d->Get(opts, c, k, null_str_ptr));
|
2017-03-31 01:20:01 +02:00
|
|
|
if(status.IsIncomplete())
|
|
|
|
{
|
|
|
|
// DB cache miss; next query requires I/O, offload it
|
|
|
|
opts.read_tier = BLOCKING;
|
|
|
|
ctx::offload([&d, &c, &k, &opts, &status]
|
|
|
|
{
|
2017-07-18 19:11:00 +02:00
|
|
|
status = d.d->Get(opts, c, k, null_str_ptr);
|
2017-03-31 01:20:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
log.debug("'%s':'%s' @%lu HAS key(%zu B) %s [%s]",
|
|
|
|
name(d),
|
|
|
|
name(c),
|
|
|
|
sequence(d),
|
|
|
|
key.size(),
|
|
|
|
status.ok()? "YES"s : "NO"s,
|
|
|
|
opts.read_tier == BLOCKING? "CACHE MISS"s : "CACHE HIT"s);
|
|
|
|
|
2017-03-31 01:20:01 +02:00
|
|
|
// Finally the result
|
|
|
|
switch(status.code())
|
|
|
|
{
|
|
|
|
using rocksdb::Status;
|
|
|
|
|
|
|
|
case Status::kOk: return true;
|
|
|
|
case Status::kNotFound: return false;
|
|
|
|
default:
|
|
|
|
throw_on_error(status);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 07:13:50 +02:00
|
|
|
//
|
|
|
|
// column
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::db::column::column(database::column &c)
|
|
|
|
:c{&c}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::column::column(database &d,
|
|
|
|
const string_view &column_name)
|
|
|
|
:c{&d[column_name]}
|
|
|
|
{}
|
|
|
|
|
2017-03-23 22:58:24 +01:00
|
|
|
void
|
2017-08-23 22:37:47 +02:00
|
|
|
ircd::db::column::operator()(const delta &delta,
|
2017-03-31 00:57:08 +02:00
|
|
|
const sopts &sopts)
|
2017-03-23 22:58:24 +01:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
operator()(&delta, &delta + 1, sopts);
|
2017-03-23 22:58:24 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
void
|
|
|
|
ircd::db::column::operator()(const sopts &sopts,
|
|
|
|
const std::initializer_list<delta> &deltas)
|
|
|
|
{
|
|
|
|
operator()(deltas, sopts);
|
|
|
|
}
|
|
|
|
|
2017-03-21 07:28:20 +01:00
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::operator()(const std::initializer_list<delta> &deltas,
|
|
|
|
const sopts &sopts)
|
2017-03-21 07:28:20 +01:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
operator()(std::begin(deltas), std::end(deltas), sopts);
|
2017-03-21 07:28:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-23 22:37:47 +02:00
|
|
|
ircd::db::column::operator()(const delta *const &begin,
|
|
|
|
const delta *const &end,
|
2017-03-31 00:57:08 +02:00
|
|
|
const sopts &sopts)
|
2017-03-21 07:28:20 +01:00
|
|
|
{
|
2017-08-23 22:37:47 +02:00
|
|
|
database &d(*this);
|
|
|
|
|
2017-03-21 07:28:20 +01:00
|
|
|
rocksdb::WriteBatch batch;
|
2017-08-23 22:37:47 +02:00
|
|
|
std::for_each(begin, end, [this, &batch]
|
|
|
|
(const delta &delta)
|
|
|
|
{
|
|
|
|
append(batch, *this, delta);
|
|
|
|
});
|
2017-03-21 07:28:20 +01:00
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
commit(d, batch, sopts);
|
2017-03-21 07:28:20 +01:00
|
|
|
}
|
|
|
|
|
2016-09-24 06:01:57 +02:00
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::operator()(const string_view &key,
|
|
|
|
const gopts &gopts,
|
|
|
|
const view_closure &func)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return operator()(key, func, gopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::operator()(const string_view &key,
|
|
|
|
const view_closure &func,
|
|
|
|
const gopts &gopts)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
const auto it(seek(*this, key, gopts));
|
|
|
|
valid_equal_or_throw(*it, key);
|
2017-04-03 06:02:32 +02:00
|
|
|
func(val(*it));
|
|
|
|
}
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::cell
|
|
|
|
ircd::db::column::operator[](const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return { *this, key };
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:33:41 +02:00
|
|
|
ircd::db::column::operator
|
|
|
|
const database::descriptor &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return c->descriptor;
|
|
|
|
}
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-03-23 22:58:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-03-31 00:57:08 +02:00
|
|
|
// column::const_iterator
|
2017-03-23 22:58:24 +01: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
|
|
|
} // namespace db
|
|
|
|
} // namespace ircd
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator
|
|
|
|
ircd::db::column::end(const gopts &gopts)
|
|
|
|
{
|
|
|
|
return cend(gopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::column::const_iterator
|
|
|
|
ircd::db::column::begin(const gopts &gopts)
|
|
|
|
{
|
|
|
|
return cbegin(gopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::db::column::const_iterator
|
|
|
|
ircd::db::column::cend(const gopts &gopts)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
|
|
|
return {};
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator
|
|
|
|
ircd::db::column::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
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
c, {}, gopts
|
2016-09-26 06:07:22 +02:00
|
|
|
};
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2017-09-08 10:33:41 +02:00
|
|
|
ret.all_prefix = true;
|
2017-03-31 00:57:08 +02:00
|
|
|
seek(ret, pos::FRONT);
|
2016-09-26 06:07:22 +02:00
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator
|
|
|
|
ircd::db::column::upper_bound(const string_view &key,
|
|
|
|
const gopts &gopts)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
|
|
|
auto it(lower_bound(key, gopts));
|
2017-03-31 00:57:08 +02:00
|
|
|
if(it && it.it->key().compare(slice(key)) == 0)
|
2016-09-26 06:07:22 +02:00
|
|
|
++it;
|
|
|
|
|
|
|
|
return std::move(it);
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator
|
|
|
|
ircd::db::column::find(const string_view &key,
|
|
|
|
const gopts &gopts)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
auto it(lower_bound(key, gopts));
|
|
|
|
if(!it || it.it->key().compare(slice(key)) != 0)
|
|
|
|
return cend(gopts);
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
2016-09-26 06:07:22 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator
|
|
|
|
ircd::db::column::lower_bound(const string_view &key,
|
|
|
|
const gopts &gopts)
|
|
|
|
{
|
2016-09-26 06:07:22 +02:00
|
|
|
const_iterator ret
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
c, {}, gopts
|
2016-09-26 06:07:22 +02:00
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
seek(ret, key);
|
2016-09-26 06:07:22 +02:00
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator::const_iterator(const_iterator &&o)
|
|
|
|
noexcept
|
2017-09-08 10:33:41 +02:00
|
|
|
:c{std::move(o.c)}
|
|
|
|
,ss{std::move(o.ss)}
|
2017-03-31 00:57:08 +02:00
|
|
|
,it{std::move(o.it)}
|
|
|
|
,val{std::move(o.val)}
|
2017-09-08 10:33:41 +02:00
|
|
|
,all_prefix{std::move(o.all_prefix)}
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
|
|
|
}
|
2016-09-24 06:01:57 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator &
|
|
|
|
ircd::db::column::const_iterator::operator=(const_iterator &&o)
|
|
|
|
noexcept
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
c = std::move(o.c);
|
2017-09-08 10:33:41 +02:00
|
|
|
ss = std::move(o.ss);
|
2017-03-31 00:57:08 +02:00
|
|
|
it = std::move(o.it);
|
|
|
|
val = std::move(o.val);
|
2017-09-08 10:33:41 +02:00
|
|
|
all_prefix = std::move(o.all_prefix);
|
2017-03-31 00:57:08 +02:00
|
|
|
return *this;
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::column::const_iterator::const_iterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-31 07:13:50 +02:00
|
|
|
ircd::db::column::const_iterator::const_iterator(database::column *const &c,
|
2017-03-31 00:57:08 +02:00
|
|
|
std::unique_ptr<rocksdb::Iterator> &&it,
|
2017-09-08 10:33:41 +02:00
|
|
|
const gopts &gopts)
|
|
|
|
:c{c}
|
|
|
|
,ss{gopts.snapshot}
|
2017-03-31 00:57:08 +02:00
|
|
|
,it{std::move(it)}
|
2017-09-08 10:33:41 +02:00
|
|
|
,all_prefix{has_opt(gopts, db::get::ALL_PREFIX)}
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
//if(!has_opt(this->opts, get::READAHEAD))
|
|
|
|
// this->gopts.readahead_size = DEFAULT_READAHEAD;
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator::~const_iterator()
|
2016-09-26 06:07:22 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator &
|
|
|
|
ircd::db::column::const_iterator::operator--()
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
seek(*this, pos::PREV);
|
2016-09-26 06:07:22 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::column::const_iterator &
|
|
|
|
ircd::db::column::const_iterator::operator++()
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
seek(*this, pos::NEXT);
|
2016-09-26 06:07:22 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
const ircd::db::column::const_iterator::value_type &
|
|
|
|
ircd::db::column::const_iterator::operator*()
|
2016-09-26 06:07:22 +02:00
|
|
|
const
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
assert(valid(*it));
|
|
|
|
val.first = db::key(*it);
|
|
|
|
val.second = db::val(*it);
|
2016-09-26 06:07:22 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
const ircd::db::column::const_iterator::value_type *
|
|
|
|
ircd::db::column::const_iterator::operator->()
|
2016-09-26 06:07:22 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return &operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::column::const_iterator::operator!()
|
2016-09-26 06:07:22 +02:00
|
|
|
const
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
if(!it)
|
|
|
|
return true;
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
if(!valid(*it))
|
2017-04-03 06:02:32 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::column::const_iterator::operator bool()
|
2016-09-26 06:07:22 +02:00
|
|
|
const
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return !!*this;
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::operator!=(const column::const_iterator &a, const column::const_iterator &b)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return !(a == b);
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::operator==(const column::const_iterator &a, const column::const_iterator &b)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
if(a && b)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
const auto &ak(a.it->key());
|
|
|
|
const auto &bk(b.it->key());
|
|
|
|
return ak.compare(bk) == 0;
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
if(!a && !b)
|
2016-09-26 06:07:22 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::operator>(const column::const_iterator &a, const column::const_iterator &b)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
if(a && b)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
const auto &ak(a.it->key());
|
|
|
|
const auto &bk(b.it->key());
|
|
|
|
return ak.compare(bk) == 1;
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
if(!a && b)
|
2016-09-26 06:07:22 +02:00
|
|
|
return true;
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
if(!a && !b)
|
2016-09-26 06:07:22 +02:00
|
|
|
return false;
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
assert(!a && b);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::db::operator<(const column::const_iterator &a, const column::const_iterator &b)
|
|
|
|
{
|
|
|
|
if(a && b)
|
|
|
|
{
|
|
|
|
const auto &ak(a.it->key());
|
|
|
|
const auto &bk(b.it->key());
|
|
|
|
return ak.compare(bk) == -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!a && b)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!a && !b)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(a && !b);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class pos>
|
2017-08-30 23:05:15 +02:00
|
|
|
bool
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::seek(column::const_iterator &it,
|
|
|
|
const pos &p)
|
|
|
|
{
|
|
|
|
database::column &c(it);
|
|
|
|
auto opts
|
|
|
|
{
|
2017-09-08 10:33:41 +02:00
|
|
|
make_opts({}, true)
|
2017-04-03 06:02:32 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 10:33:41 +02:00
|
|
|
opts.prefix_same_as_start = !it.all_prefix;
|
2017-08-30 23:05:15 +02:00
|
|
|
return seek(c, p, opts, it.it);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
2017-08-30 23:05:15 +02:00
|
|
|
template bool ircd::db::seek<ircd::db::pos>(column::const_iterator &, const pos &);
|
|
|
|
template bool ircd::db::seek<ircd::string_view>(column::const_iterator &, const string_view &);
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// writebatch
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
template<size_t SIZE> const char *info(char (&buf)[SIZE], const rocksdb::WriteBatch &);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::append(rocksdb::WriteBatch &batch,
|
|
|
|
const cell::delta &delta)
|
|
|
|
{
|
|
|
|
auto &column(std::get<cell *>(delta)->c);
|
|
|
|
append(batch, column, column::delta
|
|
|
|
{
|
|
|
|
std::get<op>(delta),
|
|
|
|
std::get<cell *>(delta)->index,
|
|
|
|
std::get<string_view>(delta)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::append(rocksdb::WriteBatch &batch,
|
|
|
|
column &column,
|
|
|
|
const column::delta &delta)
|
|
|
|
{
|
|
|
|
database::column &c(column);
|
|
|
|
|
|
|
|
const auto k(slice(std::get<1>(delta)));
|
|
|
|
const auto v(slice(std::get<2>(delta)));
|
|
|
|
switch(std::get<0>(delta))
|
|
|
|
{
|
|
|
|
case op::GET: assert(0); break;
|
|
|
|
case op::SET: batch.Put(c, k, v); break;
|
|
|
|
case op::MERGE: batch.Merge(c, k, v); break;
|
|
|
|
case op::DELETE: batch.Delete(c, k); break;
|
|
|
|
case op::DELETE_RANGE: batch.DeleteRange(c, k, v); break;
|
|
|
|
case op::SINGLE_DELETE: batch.SingleDelete(c, k); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::commit(database &d,
|
|
|
|
rocksdb::WriteBatch &batch,
|
|
|
|
const sopts &sopts)
|
|
|
|
{
|
|
|
|
const auto opts(make_opts(sopts));
|
|
|
|
commit(d, batch, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::db::commit(database &d,
|
|
|
|
rocksdb::WriteBatch &batch,
|
|
|
|
const rocksdb::WriteOptions &opts)
|
|
|
|
{
|
|
|
|
char info_buf[96];
|
|
|
|
log.debug("'%s' @%lu COMMIT %s",
|
|
|
|
d.name,
|
|
|
|
sequence(d),
|
|
|
|
info(info_buf, batch));
|
|
|
|
|
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
d.d->Write(opts, &batch)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t SIZE>
|
|
|
|
const char *
|
|
|
|
ircd::db::info(char (&buf)[SIZE],
|
|
|
|
const rocksdb::WriteBatch &batch)
|
|
|
|
{
|
|
|
|
snprintf(buf, SIZE, "%d deltas; size: %zuB :%s%s%s%s%s%s%s%s%s",
|
|
|
|
batch.Count(),
|
|
|
|
batch.GetDataSize(),
|
|
|
|
batch.HasPut()? " PUT" : "",
|
|
|
|
batch.HasDelete()? " DELETE" : "",
|
|
|
|
batch.HasSingleDelete()? " SINGLE_DELETE" : "",
|
|
|
|
batch.HasDeleteRange()? " DELETE_RANGE" : "",
|
|
|
|
batch.HasMerge()? " MERGE" : "",
|
|
|
|
batch.HasBeginPrepare()? " BEGIN_PREPARE" : "",
|
|
|
|
batch.HasEndPrepare()? " END_PREPARE" : "",
|
|
|
|
batch.HasCommit()? " COMMIT" : "",
|
|
|
|
batch.HasRollback()? " ROLLBACK" : "");
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// seek
|
|
|
|
//
|
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
namespace ircd::db
|
|
|
|
{
|
|
|
|
static rocksdb::Iterator &_seek_(rocksdb::Iterator &, const rocksdb::Slice &);
|
|
|
|
static rocksdb::Iterator &_seek_(rocksdb::Iterator &, const string_view &);
|
|
|
|
static rocksdb::Iterator &_seek_(rocksdb::Iterator &, const pos &);
|
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
std::unique_ptr<rocksdb::Iterator>
|
|
|
|
ircd::db::seek(column &column,
|
|
|
|
const string_view &key,
|
|
|
|
const gopts &opts)
|
|
|
|
{
|
|
|
|
database &d(column);
|
|
|
|
database::column &c(column);
|
2017-08-23 22:37:47 +02:00
|
|
|
|
|
|
|
std::unique_ptr<rocksdb::Iterator> ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
seek(c, key, opts, ret);
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class pos>
|
2017-08-30 23:05:15 +02:00
|
|
|
bool
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::seek(database::column &c,
|
|
|
|
const pos &p,
|
|
|
|
const gopts &gopts,
|
|
|
|
std::unique_ptr<rocksdb::Iterator> &it)
|
|
|
|
{
|
|
|
|
auto opts
|
|
|
|
{
|
|
|
|
make_opts(gopts)
|
|
|
|
};
|
|
|
|
|
2017-08-30 23:05:15 +02:00
|
|
|
return seek(c, p, opts, it);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 00:13:15 +02:00
|
|
|
//
|
|
|
|
// Seek with offload-safety in case of blocking IO.
|
|
|
|
//
|
|
|
|
// The options for an iterator cannot be changed after the iterator is created.
|
|
|
|
// This slightly complicates our toggling between blocking and non-blocking queries.
|
|
|
|
//
|
2017-04-03 06:02:32 +02:00
|
|
|
template<class pos>
|
2017-08-30 23:05:15 +02:00
|
|
|
bool
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::seek(database::column &c,
|
|
|
|
const pos &p,
|
|
|
|
rocksdb::ReadOptions &opts,
|
|
|
|
std::unique_ptr<rocksdb::Iterator> &it)
|
|
|
|
{
|
|
|
|
database &d(*c.d);
|
2017-08-19 00:13:15 +02:00
|
|
|
rocksdb::ColumnFamilyHandle *const &cf(c);
|
|
|
|
|
|
|
|
// The ReadOptions created by make_opts(gopts) always sets NON_BLOCKING
|
|
|
|
// mode. The user should never touch this. Only this function will ever
|
|
|
|
// deal with iterators in BLOCKING mode.
|
|
|
|
assert(opts.read_tier == NON_BLOCKING);
|
2017-04-03 06:02:32 +02:00
|
|
|
|
2017-08-19 00:13:15 +02:00
|
|
|
if(!it)
|
|
|
|
it.reset(d.d->NewIterator(opts, cf));
|
|
|
|
|
|
|
|
// Start with a non-blocking query.
|
|
|
|
_seek_(*it, p);
|
|
|
|
|
|
|
|
// Indicate a cache miss and blocking is required.
|
|
|
|
if(!it->status().IsIncomplete())
|
2017-04-03 06:02:32 +02:00
|
|
|
{
|
2017-09-08 11:14:17 +02:00
|
|
|
log.debug("'%s':'%s' @%lu SEEK %s CACHE HIT %s",
|
2017-08-19 00:13:15 +02:00
|
|
|
name(d),
|
|
|
|
name(c),
|
|
|
|
sequence(d),
|
2017-09-08 11:14:17 +02:00
|
|
|
valid(*it)? "VALID" : "INVALID",
|
2017-08-19 00:13:15 +02:00
|
|
|
it->status().ToString());
|
2017-08-30 23:05:15 +02:00
|
|
|
|
|
|
|
return valid(*it);
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 00:13:15 +02:00
|
|
|
// DB cache miss: create a blocking iterator and offload it.
|
|
|
|
rocksdb::ReadOptions blocking_opts(opts);
|
|
|
|
blocking_opts.fill_cache = true;
|
|
|
|
blocking_opts.read_tier = BLOCKING;
|
|
|
|
std::unique_ptr<rocksdb::Iterator> blocking_it
|
2017-04-03 06:02:32 +02:00
|
|
|
{
|
2017-08-19 00:13:15 +02:00
|
|
|
d.d->NewIterator(blocking_opts, cf)
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx::offload([&blocking_it, &it, &p]
|
|
|
|
{
|
|
|
|
// When the non-blocking iterator hit its cache miss in the middle of an
|
|
|
|
// iteration we have to copy its position to the blocking iterator first
|
2017-09-08 11:14:17 +02:00
|
|
|
// and then make the next query. In other words, two seeks, because the
|
|
|
|
// original seek (p) may be a `pos` and not a key. TODO: this can be
|
|
|
|
// avoided if we detect 'p' is a slice and not an increment.
|
2017-08-19 00:13:15 +02:00
|
|
|
if(valid(*it))
|
|
|
|
_seek_(*blocking_it, it->key());
|
|
|
|
|
2017-08-30 23:05:15 +02:00
|
|
|
if(!valid(*it) || valid(*blocking_it))
|
|
|
|
_seek_(*blocking_it, p);
|
2017-08-19 00:13:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// When the blocking iterator comes back invalid the result is propagated
|
|
|
|
if(!valid(*blocking_it))
|
|
|
|
{
|
|
|
|
it.reset(rocksdb::NewErrorIterator(blocking_it->status()));
|
2017-09-08 11:14:17 +02:00
|
|
|
log.debug("'%s':'%s' @%lu SEEK %s CACHE MISS %s",
|
2017-08-19 00:13:15 +02:00
|
|
|
name(d),
|
|
|
|
name(c),
|
|
|
|
sequence(d),
|
2017-09-08 11:14:17 +02:00
|
|
|
valid(*it)? "VALID" : "INVALID",
|
2017-08-19 00:13:15 +02:00
|
|
|
it->status().ToString());
|
2017-08-30 23:05:15 +02:00
|
|
|
|
|
|
|
return false;
|
2017-04-03 06:02:32 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 00:13:15 +02:00
|
|
|
// When the blocking iterator comes back valid the result still has to be
|
|
|
|
// properly transferred back to the user's non-blocking iterator. RocksDB
|
|
|
|
// seems to be forcing us to recreate the iterator after it failed with the
|
|
|
|
// status IsIncomplete(). Regardless of reuse, a non-blocking seek must occur
|
|
|
|
// to match this iterator with the result -- such a seek may fail again if
|
2017-09-08 11:14:17 +02:00
|
|
|
// the blocking_it's data has been evicted from cache between the point the
|
|
|
|
// offload took place and the seek for the user's iterator. That may be
|
|
|
|
// impossible. But if it ever becomes possible, we reenter this seek()
|
|
|
|
// function and enjoy the safety of offloading to try again.
|
2017-08-19 00:13:15 +02:00
|
|
|
it.reset(nullptr);
|
2017-09-08 11:14:17 +02:00
|
|
|
log.debug("'%s':'%s' @%lu SEEK %s CACHE MISS %s",
|
2017-04-03 06:02:32 +02:00
|
|
|
name(d),
|
|
|
|
name(c),
|
|
|
|
sequence(d),
|
2017-09-08 11:14:17 +02:00
|
|
|
valid(*blocking_it)? "VALID" : "INVALID",
|
2017-08-19 00:13:15 +02:00
|
|
|
blocking_it->status().ToString());
|
|
|
|
|
2017-08-30 23:05:15 +02:00
|
|
|
return seek(c, blocking_it->key(), opts, it);
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
rocksdb::Iterator &
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::_seek_(rocksdb::Iterator &it,
|
|
|
|
const pos &p)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
switch(p)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
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();
|
2016-09-26 06:07:22 +02:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 11:14:17 +02:00
|
|
|
|
|
|
|
return it;
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
rocksdb::Iterator &
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::_seek_(rocksdb::Iterator &it,
|
|
|
|
const string_view &sv)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-09-08 11:14:17 +02:00
|
|
|
return _seek_(it, slice(sv));
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 11:14:17 +02:00
|
|
|
rocksdb::Iterator &
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::db::_seek_(rocksdb::Iterator &it,
|
|
|
|
const rocksdb::Slice &sk)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
it.Seek(sk);
|
2017-09-08 11:14:17 +02:00
|
|
|
return it;
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Misc
|
|
|
|
//
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
ircd::db::column_names(const std::string &path,
|
2017-03-31 00:57:08 +02:00
|
|
|
const std::string &options)
|
2017-03-24 02:36:49 +01:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
return column_names(path, database::options(options));
|
2017-03-24 02:36:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
ircd::db::column_names(const std::string &path,
|
|
|
|
const rocksdb::DBOptions &opts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::vector<std::string> ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::DB::ListColumnFamilies(opts, path, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-24 02:36:49 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const io_error &e)
|
|
|
|
{
|
|
|
|
return // No database found at path. Assume fresh.
|
|
|
|
{
|
|
|
|
{ rocksdb::kDefaultColumnFamilyName }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::options(const database &d)
|
|
|
|
:options{d.d->GetDBOptions()}
|
2016-09-25 03:18:54 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::options(const database::column &c)
|
2017-04-03 06:02:32 +02:00
|
|
|
:options
|
2017-03-31 00:57:08 +02:00
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
rocksdb::ColumnFamilyOptions
|
|
|
|
{
|
|
|
|
c.d->d->GetOptions(c.handle.get())
|
|
|
|
}
|
|
|
|
}{}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::options(const rocksdb::DBOptions &opts)
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetStringFromDBOptions(this, opts)
|
|
|
|
};
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::options(const rocksdb::ColumnFamilyOptions &opts)
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetStringFromColumnFamilyOptions(this, opts)
|
|
|
|
};
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::operator rocksdb::PlainTableOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::PlainTableOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetPlainTableOptionsFromString(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::operator rocksdb::BlockBasedTableOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::BlockBasedTableOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetBlockBasedTableOptionsFromString(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::operator rocksdb::ColumnFamilyOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::ColumnFamilyOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetColumnFamilyOptionsFromString(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::operator rocksdb::DBOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::DBOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetDBOptionsFromString(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::operator rocksdb::Options()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::Options ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetOptionsFromString(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::map::map(const options &o)
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::StringToMap(o, this)
|
|
|
|
};
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::map::operator rocksdb::PlainTableOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::PlainTableOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetPlainTableOptionsFromMap(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::map::operator rocksdb::BlockBasedTableOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::BlockBasedTableOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetBlockBasedTableOptionsFromMap(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::map::operator rocksdb::ColumnFamilyOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::ColumnFamilyOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetColumnFamilyOptionsFromMap(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-09-25 03:18:54 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::database::options::map::operator rocksdb::DBOptions()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
rocksdb::DBOptions ret;
|
2017-04-03 06:02:32 +02:00
|
|
|
throw_on_error
|
|
|
|
{
|
|
|
|
rocksdb::GetDBOptionsFromMap(ret, *this, &ret)
|
|
|
|
};
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
bool
|
|
|
|
ircd::db::optstr_find_and_remove(std::string &optstr,
|
|
|
|
const std::string &what)
|
|
|
|
{
|
|
|
|
const auto pos(optstr.find(what));
|
|
|
|
if(pos == std::string::npos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
optstr.erase(pos, what.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
rocksdb::ReadOptions
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::make_opts(const gopts &opts,
|
|
|
|
const bool &iterator)
|
2016-09-25 03:18:54 +02:00
|
|
|
{
|
|
|
|
rocksdb::ReadOptions ret;
|
2017-03-31 00:57:08 +02:00
|
|
|
ret.snapshot = opts.snapshot;
|
2017-08-19 00:13:15 +02:00
|
|
|
ret.read_tier = NON_BLOCKING;
|
2017-08-31 07:12:58 +02:00
|
|
|
ret.prefix_same_as_start = true;
|
2017-08-19 00:13:15 +02:00
|
|
|
//ret.total_order_seek = true;
|
|
|
|
//ret.iterate_upper_bound = nullptr;
|
2016-09-25 03:18:54 +02:00
|
|
|
|
|
|
|
if(iterator)
|
2017-08-19 00:13:15 +02:00
|
|
|
{
|
2016-09-25 03:18:54 +02:00
|
|
|
ret.fill_cache = false;
|
2017-08-19 00:13:15 +02:00
|
|
|
ret.readahead_size = 4_KiB;
|
|
|
|
}
|
|
|
|
else ret.fill_cache = true;
|
2016-09-25 03:18:54 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
case get::NO_SNAPSHOT:
|
|
|
|
ret.tailing = true;
|
|
|
|
continue;
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
case get::NO_CHECKSUM:
|
|
|
|
ret.verify_checksums = false;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case get::READAHEAD:
|
|
|
|
ret.readahead_size = opt.second;
|
|
|
|
continue;
|
|
|
|
|
2017-08-31 07:12:58 +02:00
|
|
|
case get::ALL_PREFIX:
|
|
|
|
ret.prefix_same_as_start = false;
|
|
|
|
continue;
|
|
|
|
|
2016-09-25 03:18:54 +02:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::WriteOptions
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::make_opts(const sopts &opts)
|
2016-09-25 03:18:54 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
void
|
|
|
|
ircd::db::valid_equal_or_throw(const rocksdb::Iterator &it,
|
|
|
|
const string_view &sv)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
|
|
|
valid_or_throw(it);
|
2017-03-31 00:57:08 +02:00
|
|
|
if(it.key().compare(slice(sv)) != 0)
|
2016-09-26 06:07:22 +02:00
|
|
|
throw not_found();
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
bool
|
|
|
|
ircd::db::valid_equal(const rocksdb::Iterator &it,
|
|
|
|
const string_view &sv)
|
|
|
|
{
|
|
|
|
if(!valid(it))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(it.key().compare(slice(sv)) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
void
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::valid_or_throw(const rocksdb::Iterator &it)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
|
|
|
if(!valid(it))
|
|
|
|
{
|
|
|
|
throw_on_error(it.status());
|
2017-03-20 12:25:01 +01:00
|
|
|
throw not_found();
|
|
|
|
//assert(0); // status == ok + !Valid() == ???
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::operator!(const rocksdb::Iterator &it)
|
|
|
|
{
|
2017-09-08 11:14:17 +02:00
|
|
|
return !valid(it);
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::db::valid(const rocksdb::Iterator &it)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-09-08 11:14:17 +02:00
|
|
|
switch(it.status().code())
|
|
|
|
{
|
|
|
|
using rocksdb::Status;
|
|
|
|
|
|
|
|
case Status::kOk: break;
|
|
|
|
case Status::kNotFound: break;
|
|
|
|
case Status::kIncomplete: break;
|
|
|
|
default:
|
|
|
|
throw_on_error(it.status());
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
return it.Valid();
|
2016-09-25 03:18:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::throw_on_error::throw_on_error(const rocksdb::Status &s)
|
2016-09-24 06:01:57 +02:00
|
|
|
{
|
|
|
|
using rocksdb::Status;
|
|
|
|
|
|
|
|
switch(s.code())
|
|
|
|
{
|
|
|
|
case Status::kOk: return;
|
2017-03-31 00:57:08 +02:00
|
|
|
case Status::kNotFound: throw not_found("%s", s.ToString());
|
|
|
|
case Status::kCorruption: throw corruption("%s", s.ToString());
|
|
|
|
case Status::kNotSupported: throw not_supported("%s", s.ToString());
|
|
|
|
case Status::kInvalidArgument: throw invalid_argument("%s", s.ToString());
|
|
|
|
case Status::kIOError: throw io_error("%s", s.ToString());
|
|
|
|
case Status::kMergeInProgress: throw merge_in_progress("%s", s.ToString());
|
|
|
|
case Status::kIncomplete: throw incomplete("%s", s.ToString());
|
|
|
|
case Status::kShutdownInProgress: throw shutdown_in_progress("%s", s.ToString());
|
|
|
|
case Status::kTimedOut: throw timed_out("%s", s.ToString());
|
|
|
|
case Status::kAborted: throw aborted("%s", s.ToString());
|
|
|
|
case Status::kBusy: throw busy("%s", s.ToString());
|
|
|
|
case Status::kExpired: throw expired("%s", s.ToString());
|
|
|
|
case Status::kTryAgain: throw try_again("%s", s.ToString());
|
2016-09-24 06:01:57 +02:00
|
|
|
default:
|
2017-03-31 00:57:08 +02:00
|
|
|
throw error("code[%d] %s", s.code(), s.ToString());
|
2016-09-24 06:01:57 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-25 06:12:43 +02:00
|
|
|
|
2017-03-31 00:57:08 +02:00
|
|
|
std::vector<std::string>
|
|
|
|
ircd::db::available()
|
|
|
|
{
|
|
|
|
const auto prefix(fs::get(fs::DB));
|
|
|
|
const auto dirs(fs::ls(prefix));
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
2016-09-26 06:07:22 +02:00
|
|
|
std::string
|
2017-03-31 00:57:08 +02:00
|
|
|
ircd::db::path(const std::string &name)
|
|
|
|
{
|
|
|
|
const auto prefix(fs::get(fs::DB));
|
|
|
|
return fs::make_path({prefix, name});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<ircd::string_view, ircd::string_view>
|
|
|
|
ircd::db::operator*(const rocksdb::Iterator &it)
|
|
|
|
{
|
2017-04-03 06:02:32 +02:00
|
|
|
return { key(it), val(it) };
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::db::key(const rocksdb::Iterator &it)
|
|
|
|
{
|
|
|
|
return slice(it.key());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::db::val(const rocksdb::Iterator &it)
|
|
|
|
{
|
|
|
|
return slice(it.value());
|
2017-03-31 00:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rocksdb::Slice
|
|
|
|
ircd::db::slice(const string_view &sv)
|
2016-09-26 06:07:22 +02:00
|
|
|
{
|
2017-03-31 00:57:08 +02:00
|
|
|
return { sv.data(), sv.size() };
|
2016-09-26 06:07:22 +02:00
|
|
|
}
|
2017-03-23 22:58:24 +01:00
|
|
|
|
2017-04-03 06:02:32 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::db::slice(const rocksdb::Slice &sk)
|
|
|
|
{
|
|
|
|
return { sk.data(), sk.size() };
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:58:24 +01:00
|
|
|
const std::string &
|
|
|
|
ircd::db::reflect(const rocksdb::Tickers &type)
|
|
|
|
{
|
|
|
|
const auto &names(rocksdb::TickersNameMap);
|
|
|
|
const auto it(std::find_if(begin(names), end(names), [&type]
|
|
|
|
(const auto &pair)
|
|
|
|
{
|
|
|
|
return pair.first == type;
|
|
|
|
}));
|
|
|
|
|
|
|
|
static const auto empty{"<ticker>?????"s};
|
|
|
|
return it != end(names)? it->second : empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &
|
|
|
|
ircd::db::reflect(const rocksdb::Histograms &type)
|
|
|
|
{
|
|
|
|
const auto &names(rocksdb::HistogramsNameMap);
|
|
|
|
const auto it(std::find_if(begin(names), end(names), [&type]
|
|
|
|
(const auto &pair)
|
|
|
|
{
|
|
|
|
return pair.first == type;
|
|
|
|
}));
|
|
|
|
|
|
|
|
static const auto empty{"<histogram>?????"s};
|
|
|
|
return it != end(names)? it->second : empty;
|
|
|
|
}
|
2017-08-23 22:37:47 +02:00
|
|
|
|
2017-08-30 23:06:34 +02:00
|
|
|
const char *
|
|
|
|
ircd::db::reflect(const pos &pos)
|
|
|
|
{
|
|
|
|
switch(pos)
|
|
|
|
{
|
|
|
|
case pos::NEXT: return "NEXT";
|
|
|
|
case pos::PREV: return "PREV";
|
|
|
|
case pos::FRONT: return "FRONT";
|
|
|
|
case pos::BACK: return "BACK";
|
|
|
|
case pos::END: return "END";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "?????";
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:37:47 +02:00
|
|
|
bool
|
|
|
|
ircd::db::value_required(const op &op)
|
|
|
|
{
|
|
|
|
switch(op)
|
|
|
|
{
|
|
|
|
case op::SET:
|
|
|
|
case op::MERGE:
|
|
|
|
case op::DELETE_RANGE:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case op::GET:
|
|
|
|
case op::DELETE:
|
|
|
|
case op::SINGLE_DELETE:
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 23:21:18 +02:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return false;
|
2017-08-23 22:37:47 +02:00
|
|
|
}
|