From f34ebec1cfdc489fdb44e5fd3e42ac79c48cfda9 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 4 May 2018 15:57:50 -0700 Subject: [PATCH] ircd::db: Add convenience read(column) nothrow overloads. --- include/ircd/db/column.h | 5 +++++ ircd/db.cc | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/ircd/db/column.h b/include/ircd/db/column.h index 21ec63b48..a5a9d880a 100644 --- a/include/ircd/db/column.h +++ b/include/ircd/db/column.h @@ -38,6 +38,11 @@ namespace ircd::db string_view read(column &, const string_view &key, const mutable_buffer &, const gopts & = {}); std::string read(column &, const string_view &key, const gopts & = {}); + // [GET] Nothrow convenience functions to copy data into your buffer; since + // a key can exist with an empty value we must overload on this bool here. + string_view read(column &, const string_view &key, bool &found, const mutable_buffer &, const gopts & = {}); + std::string read(column &, const string_view &key, bool &found, const gopts & = {}); + // [SET] Write data to the db void write(column &, const string_view &key, const const_buffer &value, const sopts & = {}); diff --git a/ircd/db.cc b/ircd/db.cc index c27acb7a7..a28d7a3f4 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -3863,6 +3863,41 @@ ircd::db::read(column &column, return ret; } +std::string +ircd::db::read(column &column, + const string_view &key, + bool &found, + const gopts &gopts) +{ + std::string ret; + const auto closure([&ret] + (const string_view &src) + { + ret.assign(begin(src), end(src)); + }); + + found = column(key, std::nothrow, closure, gopts); + return ret; +} + +ircd::string_view +ircd::db::read(column &column, + const string_view &key, + bool &found, + const mutable_buffer &buf, + const gopts &gopts) +{ + string_view ret; + const auto closure([&buf, &ret] + (const string_view &src) + { + ret = { data(buf), copy(buf, src) }; + }); + + found = column(key, std::nothrow, closure, gopts); + return ret; +} + template<> ircd::db::prop_str ircd::db::property(const column &column,