userver: userver/storages/mysql/cursor_result_set.hpp Source File
Loading...
Searching...
No Matches
cursor_result_set.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mysql/cursor_result_set.hpp
4
5#include <userver/storages/mysql/statement_result_set.hpp>
6
7USERVER_NAMESPACE_BEGIN
8
9namespace storages::mysql {
10
11/// @brief A wrapper for read-only cursor.
12///
13/// Although this class can be constructed from StatementResultSet, you should
14/// always retrieve it from `storages::mysql::Cluster` for correct behavior.
15template <typename T>
16class CursorResultSet final {
17public:
18 explicit CursorResultSet(StatementResultSet&& result_set);
19 ~CursorResultSet();
20
21 CursorResultSet(const CursorResultSet& other) = delete;
22 CursorResultSet(CursorResultSet&& other) noexcept;
23
24 /// @brief Fetches all the rows from cursor and for each new row executes
25 /// row_callback.
26 ///
27 /// Usable when the result set is expected to be big enough to put too
28 /// much memory pressure if fetched as a whole.
29 // TODO : deadline?
30 template <typename RowCallback>
31 void ForEach(RowCallback&& row_callback, engine::Deadline deadline) &&;
32
33private:
34 StatementResultSet result_set_;
35};
36
37template <typename T>
38CursorResultSet<T>::CursorResultSet(StatementResultSet&& result_set) : result_set_{std::move(result_set)} {}
39
40template <typename T>
41CursorResultSet<T>::~CursorResultSet() = default;
42
43template <typename T>
44CursorResultSet<T>::CursorResultSet(CursorResultSet<T>&& other) noexcept = default;
45
46template <typename T>
47template <typename RowCallback>
48void CursorResultSet<T>::ForEach(
49 RowCallback&& row_callback,
50 // TODO : think about separate deadline here
51 [[maybe_unused]] engine::Deadline deadline
52) && {
53 using IntermediateStorage = std::vector<T>;
54
55 bool keep_going = true;
56 auto extractor = impl::io::TypedExtractor<IntermediateStorage, T, RowTag>{};
57
58 while (keep_going) {
59 tracing::ScopeTime fetch{impl::tracing::kFetchScope};
60 keep_going = result_set_.FetchResult(extractor);
61
62 fetch.Reset(impl::tracing::kForEachScope);
63 IntermediateStorage data{extractor.ExtractData()};
64 for (auto&& row : data) {
65 row_callback(std::move(row));
66 }
67 }
68}
69
70} // namespace storages::mysql
71
72USERVER_NAMESPACE_END