libpqxx  7.1.2
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-2020, 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 
42 class PQXX_LIBEXPORT pipeline : public internal::transactionfocus
43 {
44 public:
45  using query_id = long;
46 
47  pipeline(pipeline const &) = delete;
48  pipeline &operator=(pipeline const &) = delete;
49 
50  explicit pipeline(transaction_base &t) :
51  namedclass{"pipeline"}, transactionfocus{t}
52  {
53  init();
54  }
55  pipeline(transaction_base &t, char const name[]) :
56  namedclass{"pipeline", name}, transactionfocus{t}
57  {
58  init();
59  }
60  pipeline(transaction_base &t, std::string &&name) :
61  namedclass{"pipeline", std::move(name)}, transactionfocus{t}
62  {
63  init();
64  }
65  pipeline(transaction_base &t, std::string_view name) :
66  namedclass{"pipeline", name}, transactionfocus{t}
67  {
68  init();
69  }
70 
71  ~pipeline() noexcept;
72 
74 
80  query_id insert(std::string_view);
81 
83 
89  void complete();
90 
92 
101  void flush();
102 
104 
112  void cancel();
113 
115  [[nodiscard]] bool is_finished(query_id) const;
116 
118 
125  {
126  return retrieve(m_queries.find(qid)).second;
127  }
128 
130 
131  std::pair<query_id, result> retrieve();
132 
133  [[nodiscard]] bool empty() const noexcept { return m_queries.empty(); }
134 
137 
148  int retain(int retain_max = 2);
149 
150 
152  void resume();
153 
154 private:
155  struct PQXX_PRIVATE Query
156  {
157  explicit Query(std::string_view q) :
158  query{std::make_shared<std::string>(q)}
159  {}
160 
161  std::shared_ptr<std::string> query;
162  result res;
163  };
164 
165  using QueryMap = std::map<query_id, Query>;
166 
167  void init();
168  void attach();
169  void detach();
170 
172  static constexpr query_id qid_limit() noexcept
173  {
174  // Parenthesise this to work around an eternal Visual C++ problem:
175  // Without the extra parentheses, unless NOMINMAX is defined, the
176  // preprocessor will mistake this "max" for its annoying built-in macro
177  // of the same name.
178  return (std::numeric_limits<query_id>::max)();
179  }
180 
182  PQXX_PRIVATE query_id generate_id();
183 
184  bool have_pending() const noexcept
185  {
186  return m_issuedrange.second != m_issuedrange.first;
187  }
188 
189  PQXX_PRIVATE void issue();
190 
192  void set_error_at(query_id qid) noexcept
193  {
194  if (qid < m_error)
195  m_error = qid;
196  }
197 
199  [[noreturn]] PQXX_PRIVATE void internal_error(std::string const &err);
200 
201  PQXX_PRIVATE bool obtain_result(bool expect_none = false);
202 
203  PQXX_PRIVATE void obtain_dummy();
204  PQXX_PRIVATE void get_further_available_results();
205  PQXX_PRIVATE void check_end_results();
206 
208  PQXX_PRIVATE void receive_if_available();
209 
211  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
212  std::pair<pipeline::query_id, result> retrieve(pipeline::QueryMap::iterator);
213 
214  QueryMap m_queries;
215  std::pair<QueryMap::iterator, QueryMap::iterator> m_issuedrange;
216  int m_retain = 0;
217  int m_num_waiting = 0;
218  query_id m_q_id = 0;
219 
221  bool m_dummy_pending = false;
222 
224  query_id m_error = qid_limit();
225 
227 
230  internal::encoding_group m_encoding;
231 };
232 } // namespace pqxx
233 
234 #include "pqxx/internal/compiler-internal-post.hxx"
235 #endif
result retrieve(query_id qid)
Retrieve result for given query.
Definition: pipeline.hxx:124
pipeline(transaction_base &t, std::string_view name)
Definition: pipeline.hxx:65
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
bool empty() const noexcept
Definition: pipeline.hxx:133
Processes several queries in FIFO manner, optimized for high throughput.
Definition: pipeline.hxx:42
pipeline(transaction_base &t, char const name[])
Definition: pipeline.hxx:55
long query_id
Definition: pipeline.hxx:45
pipeline(transaction_base &t, std::string &&name)
Definition: pipeline.hxx:60
Result set containing data returned by a query or command.
Definition: result.hxx:70
pipeline(transaction_base &t)
Definition: pipeline.hxx:50
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:67
Internal error in libpqxx library.
Definition: except.hxx:158