libpqxx  7.1.0
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"},
52  transactionfocus{t}
53  {
54  init();
55  }
56  pipeline(transaction_base &t, char const name[]) :
57  namedclass{"pipeline", name},
58  transactionfocus{t}
59  {
60  init();
61  }
62  pipeline(transaction_base &t, std::string &&name) :
63  namedclass{"pipeline", std::move(name)},
64  transactionfocus{t}
65  {
66  init();
67  }
68  pipeline(transaction_base &t, std::string_view name) :
69  namedclass{"pipeline", name},
70  transactionfocus{t}
71  {
72  init();
73  }
74 
75  ~pipeline() noexcept;
76 
78 
84  query_id insert(std::string_view);
85 
87 
93  void complete();
94 
96 
105  void flush();
106 
108 
116  void cancel();
117 
119  [[nodiscard]] bool is_finished(query_id) const;
120 
122 
129  {
130  return retrieve(m_queries.find(qid)).second;
131  }
132 
134 
135  std::pair<query_id, result> retrieve();
136 
137  [[nodiscard]] bool empty() const noexcept { return m_queries.empty(); }
138 
141 
152  int retain(int retain_max = 2);
153 
154 
156  void resume();
157 
158 private:
159  struct PQXX_PRIVATE Query
160  {
161  explicit Query(std::string_view q) :
162  query{std::make_shared<std::string>(q)}
163  {}
164 
165  std::shared_ptr<std::string> query;
166  result res;
167  };
168 
169  using QueryMap = std::map<query_id, Query>;
170 
171  void init();
172  void attach();
173  void detach();
174 
176  static constexpr query_id qid_limit() noexcept
177  {
178  // Parenthesise this to work around an eternal Visual C++ problem:
179  // Without the extra parentheses, unless NOMINMAX is defined, the
180  // preprocessor will mistake this "max" for its annoying built-in macro
181  // of the same name.
182  return (std::numeric_limits<query_id>::max)();
183  }
184 
186  PQXX_PRIVATE query_id generate_id();
187 
188  bool have_pending() const noexcept
189  {
190  return m_issuedrange.second != m_issuedrange.first;
191  }
192 
193  PQXX_PRIVATE void issue();
194 
196  void set_error_at(query_id qid) noexcept
197  {
198  if (qid < m_error)
199  m_error = qid;
200  }
201 
203  [[noreturn]] PQXX_PRIVATE void internal_error(std::string const &err);
204 
205  PQXX_PRIVATE bool obtain_result(bool expect_none = false);
206 
207  PQXX_PRIVATE void obtain_dummy();
208  PQXX_PRIVATE void get_further_available_results();
209  PQXX_PRIVATE void check_end_results();
210 
212  PQXX_PRIVATE void receive_if_available();
213 
215  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
216  std::pair<pipeline::query_id, result> retrieve(pipeline::QueryMap::iterator);
217 
218  QueryMap m_queries;
219  std::pair<QueryMap::iterator, QueryMap::iterator> m_issuedrange;
220  int m_retain = 0;
221  int m_num_waiting = 0;
222  query_id m_q_id = 0;
223 
225  bool m_dummy_pending = false;
226 
228  query_id m_error = qid_limit();
229 
231 
234  internal::encoding_group m_encoding;
235 };
236 } // namespace pqxx
237 
238 #include "pqxx/internal/compiler-internal-post.hxx"
239 #endif
result retrieve(query_id qid)
Retrieve result for given query.
Definition: pipeline.hxx:128
pipeline(transaction_base &t, std::string_view name)
Definition: pipeline.hxx:68
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
bool empty() const noexcept
Definition: pipeline.hxx:137
Processes several queries in FIFO manner, optimized for high throughput.
Definition: pipeline.hxx:42
pipeline(transaction_base &t, char const name[])
Definition: pipeline.hxx:56
long query_id
Definition: pipeline.hxx:45
pipeline(transaction_base &t, std::string &&name)
Definition: pipeline.hxx:62
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:157