Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refectoring transaction handling #204

Merged
merged 8 commits into from
Jul 26, 2019
2 changes: 0 additions & 2 deletions include/cgimap/backend/apidb/changeset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ struct changeset {
changeset(bool dp, const std::string &dn, osm_user_id_t id);
};

changeset *fetch_changeset(pqxx::transaction_base &w, osm_changeset_id_t id);

std::map<osm_changeset_id_t, changeset *> fetch_changesets(pqxx::transaction_base &w, std::set<osm_changeset_id_t> id);


Expand Down
5 changes: 3 additions & 2 deletions include/cgimap/backend/apidb/pgsql_update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class pgsql_update : public data_update {

public:
pgsql_update(pqxx::connection &conn, bool is_readonly);
pgsql_update(Transaction_Owner_Base& to, bool is_readonly);

~pgsql_update();

Expand Down Expand Up @@ -41,7 +41,8 @@ class pgsql_update : public data_update {
public:
factory(const boost::program_options::variables_map &);
virtual ~factory();
virtual std::shared_ptr<data_update> make_data_update();
virtual std::shared_ptr<data_update> make_data_update(Transaction_Owner_Base& to);
virtual std::unique_ptr<Transaction_Owner_Base> get_default_transaction();

private:
pqxx::connection m_connection;
Expand Down
13 changes: 7 additions & 6 deletions include/cgimap/backend/apidb/readonly_pgsql_selection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "cgimap/data_selection.hpp"
#include "cgimap/backend/apidb/changeset.hpp"
#include "cgimap/backend/apidb/cache.hpp"
#include "cgimap/backend/apidb/transaction_manager.hpp"

#include <pqxx/pqxx>
#include <boost/program_options.hpp>
#include <chrono>
Expand All @@ -19,7 +21,7 @@
*/
class readonly_pgsql_selection : public data_selection {
public:
readonly_pgsql_selection(pqxx::connection &conn,
readonly_pgsql_selection(Transaction_Owner_Base& to,
cache<osm_changeset_id_t, changeset> &changeset_cache);
~readonly_pgsql_selection();

Expand Down Expand Up @@ -72,7 +74,8 @@ class readonly_pgsql_selection : public data_selection {
public:
factory(const boost::program_options::variables_map &);
virtual ~factory();
virtual std::shared_ptr<data_selection> make_selection();
virtual std::shared_ptr<data_selection> make_selection(Transaction_Owner_Base& );
virtual std::unique_ptr<Transaction_Owner_Base> get_default_transaction();

private:
pqxx::connection m_connection, m_cache_connection;
Expand All @@ -82,10 +85,6 @@ class readonly_pgsql_selection : public data_selection {
};

private:
// the transaction in which the selection takes place. this is
// fully read-only, and cannot create any temporary tables,
// unlike writeable_pgsql_selection.
pqxx::work w;

// true if we want to include changeset discussions along with
// the changesets themselves. defaults to false.
Expand All @@ -100,6 +99,8 @@ class readonly_pgsql_selection : public data_selection {
std::set<osm_nwr_id_t> sel_nodes, sel_ways, sel_relations;
std::set<osm_edition_t> sel_historic_nodes, sel_historic_ways, sel_historic_relations;
cache<osm_changeset_id_t, changeset> &cc;

Transaction_Manager m;
};

#endif /* READONLY_PGSQL_SELECTION_HPP */
50 changes: 48 additions & 2 deletions include/cgimap/backend/apidb/transaction_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,55 @@
#include <boost/format.hpp>
#include <pqxx/pqxx>

#include <iostream>


class Transaction_Owner_Base
{
public:
Transaction_Owner_Base() {}
virtual pqxx::transaction_base& get_transaction() = 0;
virtual ~Transaction_Owner_Base() {}
};


class Transaction_Owner_ReadOnly : public Transaction_Owner_Base
{
public:
explicit Transaction_Owner_ReadOnly(pqxx::connection &conn);
virtual pqxx::transaction_base& get_transaction();
~Transaction_Owner_ReadOnly() {}

private:
pqxx::read_transaction m_txn;
};


class Transaction_Owner_ReadWrite : public Transaction_Owner_Base
{
public:
explicit Transaction_Owner_ReadWrite(pqxx::connection &conn);
virtual pqxx::transaction_base& get_transaction();
~Transaction_Owner_ReadWrite() {}

private:
pqxx::work m_txn;
};

class Transaction_Owner_Void : public Transaction_Owner_Base
{
public:
explicit Transaction_Owner_Void();
virtual pqxx::transaction_base& get_transaction();
~Transaction_Owner_Void() {}
};



class Transaction_Manager {

public:
explicit Transaction_Manager(pqxx::connection &conn);
explicit Transaction_Manager(Transaction_Owner_Base &to);

void prepare(const std::string &name, const std::string &);

Expand Down Expand Up @@ -55,7 +100,8 @@ class Transaction_Manager {
return (pass_param<Args...>(in(std::forward<Arg>(arg)), std::forward<Args>(args)...));
}

pqxx::work m_txn;
pqxx::transaction_base & m_txn;
};


#endif
5 changes: 4 additions & 1 deletion include/cgimap/data_selection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "cgimap/types.hpp"
#include "cgimap/output_formatter.hpp"
#include "cgimap/backend/apidb/transaction_manager.hpp"

#include <chrono>
#include <memory>
Expand Down Expand Up @@ -166,7 +167,9 @@ class data_selection {

/// get a handle to a selection which can be used to build up
/// a working set of data.
virtual std::shared_ptr<data_selection> make_selection() = 0;
virtual std::shared_ptr<data_selection> make_selection(Transaction_Owner_Base&) = 0;

virtual std::unique_ptr<Transaction_Owner_Base> get_default_transaction() = 0;
};
};

Expand Down
6 changes: 5 additions & 1 deletion include/cgimap/data_update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "cgimap/api06/changeset_upload/relation_updater.hpp"
#include "cgimap/api06/changeset_upload/way_updater.hpp"

#include "cgimap/backend/apidb/transaction_manager.hpp"

#include <memory>
#include <vector>

Expand Down Expand Up @@ -47,7 +49,9 @@ class data_update {

/// get a handle to a selection which can be used to build up
/// a working set of data.
virtual std::shared_ptr<data_update> make_data_update() = 0;
virtual std::shared_ptr<data_update> make_data_update(Transaction_Owner_Base&) = 0;

virtual std::unique_ptr<Transaction_Owner_Base> get_default_transaction() = 0;
};
};

Expand Down
4 changes: 4 additions & 0 deletions src/backend/apidb/changeset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ std::map<osm_changeset_id_t, changeset *> fetch_changesets(pqxx::transaction_bas
if (ids.empty())
return result;

w.conn().prepare("extract_changeset_userdetails",
"SELECT c.id, u.data_public, u.display_name, u.id from users u "
"join changesets c on u.id=c.user_id where c.id = ANY($1)");

pqxx::result res =
w.prepared("extract_changeset_userdetails")(ids).exec();

Expand Down
16 changes: 12 additions & 4 deletions src/backend/apidb/pgsql_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ std::string connect_db_str(const po::variables_map &options) {
} // anonymous namespace

pgsql_update::pgsql_update(
pqxx::connection &conn, bool readonly)
: m{ conn }, m_readonly{ readonly } {
Transaction_Owner_Base& to, bool readonly)
: m{ to }, m_readonly{ readonly } {

if (is_api_write_disabled())
return;
Expand Down Expand Up @@ -167,6 +167,14 @@ pgsql_update::factory::factory(const po::variables_map &opts)
pgsql_update::factory::~factory() = default;

std::shared_ptr<data_update>
pgsql_update::factory::make_data_update() {
return std::make_shared<pgsql_update>(std::ref(m_connection), m_api_write_disabled);
pgsql_update::factory::make_data_update(Transaction_Owner_Base& to) {
return std::make_shared<pgsql_update>(to, m_api_write_disabled);
}

std::unique_ptr<Transaction_Owner_Base>
pgsql_update::factory::get_default_transaction()
{
return std::unique_ptr<Transaction_Owner_ReadWrite>(new Transaction_Owner_ReadWrite(std::ref(m_connection)));
}


Loading