libpqxx  7.0.5
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  class PQXX_PRIVATE Query
160  {
161  public:
162  explicit Query(std::string_view q) :
163  m_query{std::make_shared<std::string>(q)},
164  m_res{}
165  {}
166 
167  result const &get_result() const noexcept { return m_res; }
168  void set_result(result const &r) noexcept { m_res = r; }
169  std::shared_ptr<std::string> get_query() const noexcept { return m_query; }
170 
171  private:
172  std::shared_ptr<std::string> m_query;
173  result m_res;
174  };
175 
176  using QueryMap = std::map<query_id, Query>;
177 
178  void init();
179  void attach();
180  void detach();
181 
183  static constexpr query_id qid_limit() noexcept
184  {
185  // Parenthesise this to work around an eternal Visual C++ problem:
186  // Without the extra parentheses, unless NOMINMAX is defined, the
187  // preprocessor will mistake this "max" for its annoying built-in macro
188  // of the same name.
189  return (std::numeric_limits<query_id>::max)();
190  }
191 
193  PQXX_PRIVATE query_id generate_id();
194 
195  bool have_pending() const noexcept
196  {
197  return m_issuedrange.second != m_issuedrange.first;
198  }
199 
200  PQXX_PRIVATE void issue();
201 
203  void set_error_at(query_id qid) noexcept
204  {
205  if (qid < m_error)
206  m_error = qid;
207  }
208 
210  [[noreturn]] PQXX_PRIVATE void internal_error(std::string const &err);
211 
212  PQXX_PRIVATE bool obtain_result(bool expect_none = false);
213 
214  PQXX_PRIVATE void obtain_dummy();
215  PQXX_PRIVATE void get_further_available_results();
216  PQXX_PRIVATE void check_end_results();
217 
219  PQXX_PRIVATE void receive_if_available();
220 
222  PQXX_PRIVATE void receive(pipeline::QueryMap::const_iterator stop);
223  std::pair<pipeline::query_id, result> retrieve(pipeline::QueryMap::iterator);
224 
225  QueryMap m_queries;
226  std::pair<QueryMap::iterator, QueryMap::iterator> m_issuedrange;
227  int m_retain = 0;
228  int m_num_waiting = 0;
229  query_id m_q_id = 0;
230 
232  bool m_dummy_pending = false;
233 
235  query_id m_error = qid_limit();
236 };
237 } // namespace pqxx
238 
239 #include "pqxx/internal/compiler-internal-post.hxx"
240 #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
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:97
Result set containing data returned by a query or command.
Definition: result.hxx:70
pipeline(transaction_base &t)
Definition: pipeline.hxx:50
Definition: transaction_base.hxx:42
Internal error in libpqxx library.
Definition: except.hxx:157