Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
storm-ptr committed Jan 16, 2021
1 parent 6e1cd73 commit e4cd481
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 188 deletions.
22 changes: 9 additions & 13 deletions db/detail/meta_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <bark/db/meta.hpp>
#include <bark/detail/grid.hpp>
#include <bark/detail/unicode.hpp>
#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <boost/range/algorithm/search.hpp>
Expand Down Expand Up @@ -39,26 +38,23 @@ template <class Indexes, class ColumnNames>
bool indexed(Indexes&& indexes, ColumnNames&& col_nms)
{
return boost::algorithm::any_of(indexes, [&](auto& idx) {
return boost::range::search(idx.columns,
col_nms,
unicode::case_insensitive_equal_to{}) ==
return boost::range::search(idx.columns, col_nms) ==
idx.columns.begin();
});
}

template <class Rng>
auto find(Rng&& rng, std::string_view name)
template <class Columns>
auto find(Columns&& cols, std::string_view col_nm)
{
return boost::range::find_if(rng, [&](auto& item) {
return unicode::case_insensitive_equal_to{}(name, item.name);
});
return boost::range::find_if(cols,
[=](auto& col) { return col_nm == col.name; });
}

template <class Rng>
auto names(Rng&& rng)
template <class Columns>
auto names(Columns&& cols)
{
return as<std::vector<std::string>>(rng,
[&](auto& item) { return item.name; });
return as<std::vector<std::string>>(cols,
[&](auto& col) { return col.name; });
}

inline geometry::box_rtree make_tiles(size_t count, geometry::box ext)
Expand Down
3 changes: 2 additions & 1 deletion db/detail/postgres_dialect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ struct postgres_dialect : dialect {

void columns_sql(sql_builder& bld, const qualified_name& tbl_nm) override
{
auto& tbl = tbl_nm.back();
auto& scm = tbl_nm.at(-2);
bld << "SELECT column_name, LOWER(CASE data_type WHEN "
<< param{"USER-DEFINED"}
<< " THEN udt_name ELSE data_type END), numeric_scale FROM "
"information_schema.columns WHERE table_schema = "
<< param{scm} << " AND table_name = " << param{tbl_nm.back()}
<< param{scm} << " AND table_name = " << param{tbl}
<< " ORDER BY ordinal_position";
}

Expand Down
3 changes: 1 addition & 2 deletions db/detail/table_guide.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ class table_guide {
tbl.columns.push_back(col);
}
if (tbl.columns.empty())
throw std::runtime_error(
"no columns: " + boost::lexical_cast<std::string>(tbl.name));
throw std::runtime_error(concat("no columns: ", tbl.name));
}

void load_indexes(meta::table& tbl)
Expand Down
5 changes: 2 additions & 3 deletions db/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ inline std::ostream& operator<<(std::ostream& os, const column& col)
if (!col.projection.empty())
opts.push_back(proj::abbreviation(col.projection));
if (!col.tiles.empty())
opts.push_back("BOX" +
boost::lexical_cast<std::string>(
boost::geometry::dsv(bounds(col.tiles))));
opts.push_back(
concat("BOX", boost::geometry::dsv(bounds(col.tiles))));
if (!opts.empty())
os << " (" << list{opts, ", "} << ")";
}
Expand Down
2 changes: 1 addition & 1 deletion db/mysql/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class command : public db::command, private transaction<mysql::command> {
: nullptr};
unsigned cols = res ? mysql_num_fields(res.get()) : 0;

std::vector<std::string> names(cols);
auto names = std::vector<std::string>(cols);
binds_.resize(cols);
memset(binds_.data(), 0, binds_.size() * sizeof(MYSQL_BIND));
cols_.resize(cols);
Expand Down
11 changes: 9 additions & 2 deletions db/provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct provider {

/// Returns a balanced data grid.

/// @param layer is a data set;
/// @param layer is a data set identifier;
/// @param extent is a spatial filter;
/// @param pixel selects the level of the raster pyramid.
virtual geometry::multi_box tile_coverage(const qualified_name& layer,
Expand All @@ -45,7 +45,7 @@ struct provider {
/// Returns @ref rowset with spatial data set.

/// Columns @code GEOMETRY[,IMAGE][,ATTRIBUTES...] @endcode
/// @param layer is a data set;
/// @param layer is a data set identifier;
/// @param extent is a spatial filter;
/// @param pixel selects the level of the raster pyramid.
virtual rowset spatial_objects(const qualified_name& layer,
Expand Down Expand Up @@ -147,6 +147,13 @@ sql_builder insert_sql(provider& pvd,
return res;
};

inline sql_builder insert_sql(provider& pvd,
const qualified_name& tbl_nm,
const rowset& rows)
{
return insert_sql(pvd, tbl_nm, rows.columns, select(rows));
}

inline sql_builder drop_sql(provider& pvd, const qualified_name& tbl_nm)
{
auto res = builder(pvd);
Expand Down
13 changes: 5 additions & 8 deletions db/rowset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ inline auto select(const rowset& from)

/// Returns tuples of @ref variant_t
template <class Columns>
auto select(const Columns& columns, const rowset& from)
auto select(const Columns& cols, const rowset& from)
{
auto cols = as<std::vector<std::string>>(
columns, [](const auto& col) { return unicode::to_lower(col); });

auto idxs = as<std::vector<size_t>>(from.columns, [&](const auto& col) {
auto it = std::find(cols.begin(), cols.end(), unicode::to_lower(col));
return std::distance(cols.begin(), it);
auto idxs = as<std::vector<size_t>>(from.columns, [&](auto&& col) {
auto it = std::find(std::begin(cols), std::end(cols), col);
return std::distance(std::begin(cols), it);
});

auto res = std::vector<std::vector<variant_t>>{};
for (auto is = variant_istream{from.data}; !is.data.empty();) {
auto& row = res.emplace_back(cols.size());
auto& row = res.emplace_back(idxs.size());
for (size_t idx : idxs)
if (idx < row.size())
is >> row[idx];
Expand Down
2 changes: 1 addition & 1 deletion detail/lru_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class lru_cache {
auto res = mapped_type::result_of(std::forward<F>(f),
std::forward<Args>(args)...);
insert({scoped_key, res});
return res.get();
return std::move(res).get();
}

template <class Key>
Expand Down
8 changes: 0 additions & 8 deletions detail/unicode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ auto to_upper(const Str& str)
return unicode::to_string<range_value_t<Str>>(wstr);
}

struct case_insensitive_equal_to {
template <class Lhs, class Rhs>
bool operator()(const Lhs& lhs, const Rhs& rhs) const
{
return to_lower(lhs) == to_lower(rhs);
}
};

} // namespace bark::unicode

#endif // BARK_UNICODE_HPP
34 changes: 16 additions & 18 deletions detail/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,31 @@ constexpr size_t variant_index()
/// expected<T> is either a T or the exception preventing its creation.
template <class T>
class expected {
std::variant<std::exception_ptr, T> val_;

public:
template <class F, class... Args>
static expected result_of(F&& f, Args&&... args) noexcept
{
expected res;
try {
res.state_ =
res.val_ =
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
}
catch (const std::exception&) {
res.state_ = std::current_exception();
catch (...) {
res.val_ = std::current_exception();
}
return res;
}

T get()
T get() &&
{
return std::visit(overloaded{[](T& val) -> T { return std::move(val); },
[](std::exception_ptr& e) -> T {
std::rethrow_exception(std::move(e));
}},
state_);
return std::visit(
overloaded{
[](std::exception_ptr e) -> T { std::rethrow_exception(e); },
[](T&& val) -> T { return std::move(val); }},
std::move(val_));
}

private:
std::variant<std::exception_ptr, T> state_;
};

/// Joins 'items' by adding user defined separator.
Expand Down Expand Up @@ -240,6 +239,11 @@ streamable(T) -> streamable<T>;
/// @see https://en.cppreference.com/w/cpp/named_req/TimedLockable
template <class T, class Hash = std::hash<T>, class Equal = std::equal_to<T>>
class timed_lockable {
T val_;
inline static std::mutex guard_;
inline static std::condition_variable notifier_;
inline static std::unordered_set<T, Hash, Equal> locked_;

public:
explicit timed_lockable(const T& val) : val_{val} {}

Expand All @@ -260,12 +264,6 @@ class timed_lockable {
}
notifier_.notify_all();
}

private:
T val_;
inline static std::mutex guard_;
inline static std::condition_variable notifier_;
inline static std::unordered_set<T, Hash, Equal> locked_;
};

} // namespace bark
Expand Down
Loading

0 comments on commit e4cd481

Please sign in to comment.