libpqxx  7.4.1
pipeline.hxx
1 /* Definition of the pqxx::pipeline class.
2  *
3  * Throughput-optimized query manager
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/pipeline instead.
6  *
7  * Copyright (c) 2000-2021, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_H_PIPELINE
14 #define PQXX_H_PIPELINE
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include <limits>
20 #include <map>
21 #include <string>
22 
23 #include "pqxx/transaction_base.hxx"
24 
25 
26 namespace pqxx
27 {
29 
47 class PQXX_LIBEXPORT pipeline : public transaction_focus
48 {
49 public:
51  using query_id = long;
52 
53  pipeline(pipeline const &) = delete;
54  pipeline &operator=(pipeline const &) = delete;
55 
57  explicit pipeline(transaction_base &t) : transaction_focus{t, s_classname}
58  {
59  init();
60  }
62  pipeline(transaction_base &t, std::string_view tname) :
63  transaction_focus{t, s_classname, tname}
64  {
65  init();
66  }
67 
69  ~pipeline() noexcept;
70 
72 
78  query_id insert(std::string_view);
79 
81 
87  void complete();
88 
90 
99  void flush();
100 
102 
110  void cancel();
111 
113  [[nodiscard]] bool is_finished(query_id) const;
114 
116 
123  {
124  return retrieve(m_queries.find(qid)).second;
125  }
126 
128 
129  std::pair<query_id, result> retrieve();
130 
131  [[nodiscard]] bool empty() const noexcept { return std::empty(m_queries); }
132 
135 
146  int retain(int retain_max = 2);
147 
148 
150  void resume();
151 
152 private:
153  struct PQXX_PRIVATE Query
154  {
155  explicit Query(std::string_view q) :
156  query{std::make_shared<std::string>(q)}
157  {}
158 
159  std::shared_ptr<std::string> query;
160  result res;
161  };
162 
163  using QueryMap = std::map<query_id, Query>;
164 
165  void init();
166  void attach();
167  void detach();
168 
170  static constexpr query_id qid_limit() noexcept
171  {
172  // Parenthesise this to work around an eternal Visual C++ problem:
173  // Without the extra parentheses, unless NOMINMAX is defined, the
174  // preprocessor will mistake this "max" for its annoying built-in macro
175  // of the same name.
176  return (std::numeric_limits<query_id>::max)();
177  }
178 
180  PQXX_PRIVATE query_id generate_id();
181 
182  bool have_pending() const noexcept
183  {
184  return m_issuedrange.second != m_issuedrange.first;
185  }
186 
187  PQXX_PRIVATE void issue();
188 
190  void set_error_at(query_id qid) noexcept
191  {
192  if (qid < m_error)
193  m_error = qid;
194  }
195 
197  [[noreturn]] PQXX_PRIVATE void internal_error(std::string const &err);
198 
199  PQXX_PRIVATE bool obtain_result(bool expect_none = false);
200 
201  PQXX_PRIVATE void obtain_dummy();
202  PQXX_PRIVATE void get_further_available_results();
203  PQXX_PRIVATE void check_end_results();
204 
206  PQXX_PRIVATE void receive_if_available();
207 
209  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
210  std::pair<pipeline::query_id, result> retrieve(pipeline::QueryMap::iterator);
211 
212  QueryMap m_queries;
213  std::pair<QueryMap::iterator, QueryMap::iterator> m_issuedrange;
214  int m_retain = 0;
215  int m_num_waiting = 0;
216  query_id m_q_id = 0;
217 
219  bool m_dummy_pending = false;
220 
222  query_id m_error = qid_limit();
223 
225 
228  internal::encoding_group m_encoding;
229 
230  constexpr static std::string_view s_classname{"pipeline"};
231 };
232 } // namespace pqxx
233 
234 #include "pqxx/internal/compiler-internal-post.hxx"
235 #endif
long query_id
Identifying numbers for queries.
Definition: pipeline.hxx:51
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
result retrieve(query_id qid)
Retrieve result for given query.
Definition: pipeline.hxx:122
pipeline(transaction_base &t, std::string_view tname)
Start a pipeline. Assign it a name, for more helpful error messages.
Definition: pipeline.hxx:62
Processes several queries in FIFO manner, optimized for high throughput.
Definition: pipeline.hxx:47
bool empty() const noexcept
Definition: pipeline.hxx:131
Base class for things that monopolise a transaction&#39;s attention.
Definition: transaction_focus.hxx:27
pipeline(transaction_base &t)
Start a pipeline.
Definition: pipeline.hxx:57
Internal error in libpqxx library.
Definition: except.hxx:157
Result set containing data returned by a query or command.
Definition: result.hxx:70
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:74