Tracing: added some more details for db operations
This commit is contained in:
@@ -45,22 +45,74 @@ namespace lms::db::utils
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Query>
|
||||
auto execSingleResultQuery(const Query& query)
|
||||
template <typename T>
|
||||
auto fetchFirstResult(const Wt::Dbo::collection<T>& collection)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "ExecSingleResultQuery", "Query", query.asString());
|
||||
LMS_SCOPED_TRACE_DETAILED("Database", "FetchFirstResult");
|
||||
return collection.begin();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fetchNextResult(typename Wt::Dbo::collection<T>::const_iterator& it)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED("Database", "FetchNextResult");
|
||||
it++;
|
||||
}
|
||||
|
||||
template <typename T, typename Func>
|
||||
void forEachResult(const Wt::Dbo::collection<T>& collection, Func&& func)
|
||||
{
|
||||
typename Wt::Dbo::collection<T>::const_iterator it{ fetchFirstResult(collection) };
|
||||
while (it != collection.end())
|
||||
{
|
||||
func(*it);
|
||||
fetchNextResult<T>(it);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct QueryResultType;
|
||||
|
||||
template <class ResultType, typename BindStrategy>
|
||||
struct QueryResultType<Wt::Dbo::Query<ResultType, BindStrategy>>
|
||||
{
|
||||
using type = ResultType;
|
||||
};
|
||||
|
||||
template <typename Query, typename UnaryFunc>
|
||||
void forEachQueryResult(const Query& query, UnaryFunc&& func)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "ForEachQueryResult", "Query", query.asString());
|
||||
forEachResult(query.resultList(), std::forward<UnaryFunc>(func));
|
||||
}
|
||||
|
||||
template <typename T, typename Query>
|
||||
std::vector<T> fetchQueryResults(const Query& query)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQueryResults", "Query", query.asString());
|
||||
|
||||
auto collection{ query.resultList() };
|
||||
return std::vector<T>(collection.begin(), collection.end());
|
||||
}
|
||||
|
||||
template <typename Query>
|
||||
std::vector<typename QueryResultType<Query>::type> fetchQueryResults(const Query& query)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQueryResults", "Query", query.asString());
|
||||
|
||||
auto collection{ query.resultList() };
|
||||
return std::vector<typename QueryResultType<Query>::type>(collection.begin(), collection.end());
|
||||
}
|
||||
|
||||
template <typename Query>
|
||||
auto fetchQuerySingleResult(const Query& query)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQuerySingleResult", "Query", query.asString());
|
||||
return query.resultValue();
|
||||
}
|
||||
|
||||
template <typename Query>
|
||||
auto execMultiResultQuery(const Query& query)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "ExecMultiResultQuery", "Query", query.asString());
|
||||
return query.resultList();
|
||||
}
|
||||
|
||||
template <typename ResultType, typename Query>
|
||||
RangeResults<ResultType> execRangeQuery(Query& query, std::optional<Range> range)
|
||||
RangeResults<ResultType> execRangeQuery(Query& query, const std::optional<Range> range)
|
||||
{
|
||||
RangeResults<ResultType> res;
|
||||
|
||||
@@ -72,16 +124,12 @@ namespace lms::db::utils
|
||||
res.results.reserve(range->size);
|
||||
}
|
||||
|
||||
auto collection{ execMultiResultQuery(query) };
|
||||
for (auto itResult{ collection.begin() }; itResult != collection.end(); ++itResult)
|
||||
// TODO optim useless last copy
|
||||
res.results = utils::fetchQueryResults<ResultType>(query);
|
||||
if (range && (res.results.size() == range->size + 1))
|
||||
{
|
||||
if (range && res.results.size() == range->size)
|
||||
{
|
||||
res.moreResults = true;
|
||||
break;
|
||||
}
|
||||
|
||||
res.results.push_back(std::move(*itResult));
|
||||
res.moreResults = true;
|
||||
res.results.pop_back();
|
||||
}
|
||||
|
||||
res.range.size = res.results.size();
|
||||
@@ -89,31 +137,29 @@ namespace lms::db::utils
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename ResultType, typename Query>
|
||||
void execRangeQuery(Query& query, std::optional<Range> range, const std::function<void(const ResultType&)>& func)
|
||||
template <typename Query, typename UnaryFunc>
|
||||
void forEachQueryRangeResult(Query& query, std::optional<Range> range, UnaryFunc&& func)
|
||||
{
|
||||
if (range)
|
||||
applyRange(query, range);
|
||||
|
||||
auto collection{ execMultiResultQuery(query) };
|
||||
for (const auto& res : collection)
|
||||
{
|
||||
LMS_SCOPED_TRACE_DETAILED("Database", "ExecQueryRangeForEach");
|
||||
func(res);
|
||||
}
|
||||
forEachQueryResult(query, std::forward<UnaryFunc>(func));
|
||||
}
|
||||
|
||||
template <typename ResultType, typename Query>
|
||||
void execRangeQuery(Query& query, std::optional<Range> range, bool& moreResults, const std::function<void(const ResultType&)>& func)
|
||||
template <typename Query, typename UnaryFunc>
|
||||
void forEachQueryRangeResult(Query& query, std::optional<Range> range, bool& moreResults, UnaryFunc&& func)
|
||||
{
|
||||
using ResultType = typename QueryResultType<Query>::type;
|
||||
|
||||
if (range)
|
||||
applyRange(query, Range{ range->offset, range->size + 1 });
|
||||
|
||||
moreResults = false;
|
||||
|
||||
std::size_t count{};
|
||||
auto collection{ execMultiResultQuery(query) };
|
||||
for (const auto& res : collection)
|
||||
const auto collection{ query.resultList() };
|
||||
auto it{ fetchFirstResult(collection) };
|
||||
while (it != collection.end())
|
||||
{
|
||||
if (range && (count++ == static_cast<std::size_t>(range->size)))
|
||||
{
|
||||
@@ -121,8 +167,8 @@ namespace lms::db::utils
|
||||
break;
|
||||
}
|
||||
|
||||
LMS_SCOPED_TRACE_DETAILED("Database", "ExecQueryRangeForEach");
|
||||
func(res);
|
||||
func(*it);
|
||||
fetchNextResult<ResultType>(it);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user