From f97c9fff996c6b7bf96177d18e6fb8b03c595bcb Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 14 May 2018 16:17:43 -0700 Subject: [PATCH] ircd::db: Add interface to database & column caches. --- include/ircd/db/cache.h | 35 ++++++++++++ include/ircd/db/column.h | 6 ++ include/ircd/db/database/database.h | 4 ++ include/ircd/db/db.h | 1 + ircd/db.cc | 87 +++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 include/ircd/db/cache.h diff --git a/include/ircd/db/cache.h b/include/ircd/db/cache.h new file mode 100644 index 000000000..3b4c37ce6 --- /dev/null +++ b/include/ircd/db/cache.h @@ -0,0 +1,35 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_DB_CACHE_H + +// Interface to a cache. This interface is included in the standard include +// stack by forward declaring the rocksdb::Cache symbol. +// +// The interface overloads on a pointer to a cache for developer convenience. +// This is because caches may not always exist for some entity, and accessors +// will return a null pointer. The pointer overloads here will safely accept +// null pointers and provide the appropriate no-op behavior. + +namespace ircd::db +{ + // Get capacity + size_t capacity(const rocksdb::Cache &); + size_t capacity(const rocksdb::Cache *const &); + + // Set capacity + void capacity(rocksdb::Cache &, const size_t &); + bool capacity(rocksdb::Cache *const &, const size_t &); + + // Get usage + size_t usage(const rocksdb::Cache &); + size_t usage(const rocksdb::Cache *const &); +} diff --git a/include/ircd/db/column.h b/include/ircd/db/column.h index 6370b2a3e..c20bc1b1a 100644 --- a/include/ircd/db/column.h +++ b/include/ircd/db/column.h @@ -31,6 +31,12 @@ namespace ircd::db template<> prop_int property(const column &, const string_view &name); template<> prop_map property(const column &, const string_view &name); + // Access to the column's caches (see cache.h interface) + const rocksdb::Cache *cache_compressed(const column &); + const rocksdb::Cache *cache(const column &); + rocksdb::Cache *cache_compressed(column &); + rocksdb::Cache *cache(column &); + // [GET] Tests if key exists bool has(column &, const string_view &key, const gopts & = {}); diff --git a/include/ircd/db/database/database.h b/include/ircd/db/database/database.h index 055c0dcf4..4b459e5f1 100644 --- a/include/ircd/db/database/database.h +++ b/include/ircd/db/database/database.h @@ -30,6 +30,10 @@ namespace ircd::db template R property(const database &, const string_view &name); template<> prop_int property(const database &, const string_view &name); + // Access to the database's row cache (see cache.h interface) + const rocksdb::Cache *cache(const database &); + rocksdb::Cache *cache(database &); + // Ticker extern const uint32_t ticker_max; string_view ticker_id(const uint32_t &id); diff --git a/include/ircd/db/db.h b/include/ircd/db/db.h index 5bb04b043..eafedc05a 100644 --- a/include/ircd/db/db.h +++ b/include/ircd/db/db.h @@ -68,6 +68,7 @@ enum class ircd::db::pos #include "database/descriptor.h" #include "database/options.h" #include "database/snapshot.h" +#include "cache.h" #include "opts.h" #include "column.h" #include "cell.h" diff --git a/ircd/db.cc b/ircd/db.cc index 6008977a6..858c142f8 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -308,6 +308,18 @@ ircd::db::sequence(const database &cd) return d.d->GetLatestSequenceNumber(); } +rocksdb::Cache * +ircd::db::cache(database &d) +{ + return d.cache.get(); +} + +const rocksdb::Cache * +ircd::db::cache(const database &d) +{ + return d.cache.get(); +} + template<> ircd::db::prop_int ircd::db::property(const database &cd, @@ -3906,6 +3918,34 @@ ircd::db::read(column &column, return ret; } +rocksdb::Cache * +ircd::db::cache(column &column) +{ + database::column &c(column); + return c.table_opts.block_cache.get(); +} + +rocksdb::Cache * +ircd::db::cache_compressed(column &column) +{ + database::column &c(column); + return c.table_opts.block_cache_compressed.get(); +} + +const rocksdb::Cache * +ircd::db::cache(const column &column) +{ + const database::column &c(column); + return c.table_opts.block_cache.get(); +} + +const rocksdb::Cache * +ircd::db::cache_compressed(const column &column) +{ + const database::column &c(column); + return c.table_opts.block_cache_compressed.get(); +} + template<> ircd::db::prop_str ircd::db::property(const column &column, @@ -5029,6 +5069,53 @@ ircd::db::_seek_(rocksdb::Iterator &it, return it; } +/////////////////////////////////////////////////////////////////////////////// +// +// cache.h +// + +size_t +ircd::db::usage(const rocksdb::Cache *const &cache) +{ + return cache? usage(*cache) : 0; +} + +size_t +ircd::db::usage(const rocksdb::Cache &cache) +{ + return cache.GetUsage(); +} + +bool +ircd::db::capacity(rocksdb::Cache *const &cache, + const size_t &cap) +{ + if(!cache) + return false; + + capacity(*cache, cap); + return true; +} + +void +ircd::db::capacity(rocksdb::Cache &cache, + const size_t &cap) +{ + cache.SetCapacity(cap); +} + +size_t +ircd::db::capacity(const rocksdb::Cache *const &cache) +{ + return cache? capacity(*cache): 0; +} + +size_t +ircd::db::capacity(const rocksdb::Cache &cache) +{ + return cache.GetCapacity(); +} + /////////////////////////////////////////////////////////////////////////////// // // Misc