diff options
Diffstat (limited to 'pgsql')
-rw-r--r-- | pgsql/bulk/driver.cxx | 340 | ||||
-rw-r--r-- | pgsql/bulk/makefile | 107 | ||||
-rw-r--r-- | pgsql/bulk/test.hxx | 34 | ||||
-rw-r--r-- | pgsql/bulk/test.std | 0 | ||||
-rw-r--r-- | pgsql/savepoint/driver.cxx | 58 | ||||
-rw-r--r-- | pgsql/savepoint/makefile | 107 | ||||
-rw-r--r-- | pgsql/savepoint/test.hxx | 27 | ||||
-rw-r--r-- | pgsql/savepoint/test.std | 0 |
8 files changed, 673 insertions, 0 deletions
diff --git a/pgsql/bulk/driver.cxx b/pgsql/bulk/driver.cxx new file mode 100644 index 0000000..fd629b8 --- /dev/null +++ b/pgsql/bulk/driver.cxx @@ -0,0 +1,340 @@ +// file : pgsql/savepoint/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test transaction savepoints. +// + +#include <libpq-fe.h> + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stddef.h> +#include <assert.h> +#include <sys/select.h> + +// Note: hack. +// +#include <arpa/inet.h> +#define htonll(x) ((((long long)htonl(x)) << 32) + htonl((x) >> 32)) + +static const size_t columns = 3; + +struct data +{ + long long id; + long long idata; + const char* sdata; +}; + +static char* values[columns]; +static int lengths[columns]; +static int formats[columns] = {1, 1, 1}; + +static const unsigned int types[columns] = { + 20, // int8 + 20, // int8 + 25 // text +}; + +static void +init (const struct data* d) +{ + values[0] = (char*)&d->id; + lengths[0] = sizeof (d->id); + + values[1] = (char*)&d->idata; + lengths[1] = sizeof (d->idata); + + values[2] = (char*)d->sdata; + lengths[2] = strlen (d->sdata); +} + +static void +execute (PGconn* conn, const struct data* ds, size_t n) +{ + int sock = PQsocket (conn); + assert (sock != -1); + + if (PQsetnonblocking (conn, 1) == -1 || + PQenterPipelineMode (conn) == 0) + assert (false); + + // True if we've written and read everything, respectively. + // + bool wdone = false; + bool rdone = false; + + size_t wn = 0; + size_t rn = 0; + + while (!rdone) + { + fd_set wds; + if (!wdone) + { + FD_ZERO (&wds); + FD_SET (sock, &wds); + } + + fd_set rds; + FD_ZERO (&rds); + FD_SET (sock, &rds); + + if (select (sock + 1, &rds, wdone ? NULL : &wds, NULL, NULL) == -1) + { + if (errno == EINTR) + continue; + + assert (false); + } + + // Try to minimize the chance of blocking the server by first processing + // the result and then sending more queries. + // + if (FD_ISSET (sock, &rds)) + { + if (PQconsumeInput (conn) == 0) + assert (false); + + while (wn > rn && PQisBusy (conn) == 0) + { + //fprintf (stderr, "PQgetResult %zu\n", rn); + + PGresult* res = PQgetResult (conn); + assert (res != NULL); + ExecStatusType stat = PQresultStatus (res); + + if (stat == PGRES_PIPELINE_SYNC) + { + assert (wdone && rn == n); + PQclear (res); + rdone = true; + break; + } + + if (stat == PGRES_FATAL_ERROR) + { + const char* s = PQresultErrorField (res, PG_DIAG_SQLSTATE); + + if (strcmp (s, "23505") == 0) + fprintf (stderr, "duplicate id at %zu\n", rn); + } + + PQclear (res); + assert (rn != n); + ++rn; + + // We get a NULL result after each query result. + // + { + PGresult* end = PQgetResult (conn); + assert (end == NULL); + } + } + } + + if (!wdone && FD_ISSET (sock, &wds)) + { + // Send queries until we get blocked (write-biased). This feels like + // a better overall strategy to keep the server busy compared to + // sending one query at a time and then re-checking if there is + // anything to read because the results of INSERT/UPDATE/DELETE are + // presumably small and quite a few of them can get buffered before + // the server gets blocked. + // + for (;;) + { + if (wn < n) + { + //fprintf (stderr, "PQsendQueryPrepared %zu\n", wn); + + init (ds + wn); + + if (PQsendQueryPrepared (conn, + "persist_object", + (int)(columns), + values, + lengths, + formats, + 1) == 0) + assert (false); + + if (++wn == n) + { + if (PQpipelineSync (conn) == 0) + assert (false); + + ++wn; + } + } + + // PQflush() result: + // + // 0 -- success (queue is now empty) + // 1 -- blocked + // -1 -- error + // + int r = PQflush (conn); + assert (r != -1); + + if (r == 0) + { + if (wn < n) + { + // If we continue here, then we are write-biased. And if we + // break, then we are read-biased. + // +#if 0 + break; +#else + continue; +#endif + } + + wdone = true; + } + + break; // Blocked or done. + } + } + } + + if (PQexitPipelineMode (conn) == 0 || + PQsetnonblocking (conn, 0) == -1) + assert (false); +} + +static void +test (PGconn* conn) +{ + const size_t batch = 500; + struct data ds[batch]; + + for (size_t i = 0; i != batch; ++i) + { + ds[i].id = htonll (i == batch / 2 ? i - 1 : i); // Cause duplicate PK. + ds[i].idata = htonll (i); + ds[i].sdata = "abc"; + } + + // Prepare the statement. + // + { + PGresult* res = PQprepare ( + conn, + "persist_object", + "INSERT INTO \"pgsql_bulk_object\" " + "(\"id\", " + "\"idata\", " + "\"sdata\") " + "VALUES " + "($1, $2, $3)", + (int)(columns), + types); + assert (PQresultStatus (res) == PGRES_COMMAND_OK); + PQclear (res); + } + + // Begin transaction. + // + { + PGresult* res = PQexec (conn, "begin"); + assert (PQresultStatus (res) == PGRES_COMMAND_OK); + PQclear (res); + } + + execute (conn, ds, batch); + + // Commit transaction. + // + { + PGresult* res = PQexec (conn, "commit"); + assert (PQresultStatus (res) == PGRES_COMMAND_OK); + PQclear (res); + } +} + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <vector> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_specific_database<database> (argc, argv)); + + connection_ptr cn (db->connection ()); + + if (false) + { + PGconn* conn (cn->handle ()); + test (conn); + } + + { + const unsigned long n (500); + + vector<object> os; + + for (unsigned long i (0); i != n; ++i) + { + os.push_back (object {i, i, string (i, 'x')}); + + if (i == n / 2) + os.push_back (object {i, i, to_string (i)}); + } + + { + transaction t (cn->begin ()); + db->persist (os.begin (), os.end ()); + t.commit (); + } + + { + transaction t (cn->begin ()); + db->find<object> (2); + t.commit (); + } + + for (unsigned long i (0); i != n; ++i) + { + //assert (os[i].id == i + 1); + os[i].idata++; + } + + { + transaction t (cn->begin ()); + db->update (os.begin (), os.end ()); + t.commit (); + } + + { + transaction t (cn->begin ()); + db->erase (os.begin (), os.end ()); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/pgsql/bulk/makefile b/pgsql/bulk/makefile new file mode 100644 index 0000000..12f8dbb --- /dev/null +++ b/pgsql/bulk/makefile @@ -0,0 +1,107 @@ +# file : pgsql/savepoint/makefile +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +genf := $(call odb-gen,$(odb_hdr)) +gen := $(addprefix $(out_base)/,$(genf)) +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database pgsql --generate-schema \ +--generate-query --table-prefix pgsql_bulk_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/pgsql/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) \ +$(name)-vc8.vcproj $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters \ +$(name)-vc11.vcxproj $(name)-vc11.vcxproj.filters \ +$(name)-vc12.vcxproj $(name)-vc12.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc8proj,../template/template-vc8.vcproj,$(name)-vc8.vcproj) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + $(call meta-vc11proj,../template/template-vc11.vcxproj,$(name)-vc11.vcxproj) + $(call meta-vc12proj,../template/template-vc12.vcxproj,$(name)-vc12.vcxproj) + +# Test. +# +$(eval $(call test-rule)) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addsuffix .hxx.clean,$(filter %.cxx,$(gen))) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc8proj.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.make) +$(call include,$(bld_root)/meta/vc12proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/pgsql/bulk/test.hxx b/pgsql/bulk/test.hxx new file mode 100644 index 0000000..25dd138 --- /dev/null +++ b/pgsql/bulk/test.hxx @@ -0,0 +1,34 @@ +// file : pgsql/savepoint/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object bulk(1000) +struct object +{ + /* + object (unsigned long id) + : id_ (id) + { + } + + object () + { + } + */ + + #pragma db id //auto + unsigned long id; + + unsigned long idata; + + //#pragma db + std::string sdata; +}; + +#endif // TEST_HXX diff --git a/pgsql/bulk/test.std b/pgsql/bulk/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pgsql/bulk/test.std diff --git a/pgsql/savepoint/driver.cxx b/pgsql/savepoint/driver.cxx new file mode 100644 index 0000000..7ac3df0 --- /dev/null +++ b/pgsql/savepoint/driver.cxx @@ -0,0 +1,58 @@ +// file : pgsql/savepoint/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test transaction savepoints. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/pgsql/database.hxx> +#include <odb/pgsql/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +namespace pgsql = odb::pgsql; +using namespace pgsql; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_specific_database<database> (argc, argv)); + + { + object o1 (1); + object o2 (2); + + transaction t (db->begin ()); + db->persist (o1); + + /* + try + { + db->persist (o1); + assert (false); + } + catch (const odb::object_already_persistent&) + { + } + */ + + //db->persist (o2); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/pgsql/savepoint/makefile b/pgsql/savepoint/makefile new file mode 100644 index 0000000..f2046f8 --- /dev/null +++ b/pgsql/savepoint/makefile @@ -0,0 +1,107 @@ +# file : pgsql/savepoint/makefile +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +genf := $(call odb-gen,$(odb_hdr)) +gen := $(addprefix $(out_base)/,$(genf)) +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) $(filter %.o,$(gen:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database pgsql --default-database \ +common --generate-schema --generate-query --table-prefix pgsql_savepoint_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(subst /,-,$(subst $(src_root)/pgsql/,,$(src_base))) +$(dist): export extra_dist := $(data_dist) \ +$(name)-vc8.vcproj $(name)-vc9.vcproj \ +$(name)-vc10.vcxproj $(name)-vc10.vcxproj.filters \ +$(name)-vc11.vcxproj $(name)-vc11.vcxproj.filters \ +$(name)-vc12.vcxproj $(name)-vc12.vcxproj.filters +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc8proj,../template/template-vc8.vcproj,$(name)-vc8.vcproj) + $(call meta-vc9proj,../template/template-vc9.vcproj,$(name)-vc9.vcproj) + $(call meta-vc10proj,../template/template-vc10.vcxproj,$(name)-vc10.vcxproj) + $(call meta-vc11proj,../template/template-vc11.vcxproj,$(name)-vc11.vcxproj) + $(call meta-vc12proj,../template/template-vc12.vcxproj,$(name)-vc12.vcxproj) + +# Test. +# +$(eval $(call test-rule)) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addsuffix .hxx.clean,$(filter %.cxx,$(gen))) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc8proj.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/vc11proj.make) +$(call include,$(bld_root)/meta/vc12proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/pgsql/savepoint/test.hxx b/pgsql/savepoint/test.hxx new file mode 100644 index 0000000..7e01992 --- /dev/null +++ b/pgsql/savepoint/test.hxx @@ -0,0 +1,27 @@ +// file : pgsql/savepoint/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <string> + +#include <odb/core.hxx> + +#pragma db object bulk(100) +struct object +{ + object (unsigned long id) + : id_ (id) + { + } + + object () + { + } + + #pragma db id + unsigned long id_; +}; + +#endif // TEST_HXX diff --git a/pgsql/savepoint/test.std b/pgsql/savepoint/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pgsql/savepoint/test.std |