From 6215f11fafd416316293333c8f8fc421aa90a7c4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 1 Nov 2011 12:41:02 +0200 Subject: Implement support for optimistic concurrency New pragmas: optimistic, version. New test: optimistic. New database function: reload(). --- common/optimistic/driver.cxx | 300 +++++++++++++++++++++++++++++++++++++++++++ common/optimistic/makefile | 109 ++++++++++++++++ common/optimistic/test.hxx | 78 +++++++++++ common/optimistic/test.std | 0 4 files changed, 487 insertions(+) create mode 100644 common/optimistic/driver.cxx create mode 100644 common/optimistic/makefile create mode 100644 common/optimistic/test.hxx create mode 100644 common/optimistic/test.std (limited to 'common/optimistic') diff --git a/common/optimistic/driver.cxx b/common/optimistic/driver.cxx new file mode 100644 index 0000000..46929d4 --- /dev/null +++ b/common/optimistic/driver.cxx @@ -0,0 +1,300 @@ +// file : common/optimistic/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test optimistic concurrency support. +// + +#include // std::auto_ptr +#include +#include + +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +unsigned long +version (const auto_ptr& db, unsigned long id) +{ + typedef odb::query query; + typedef odb::result result; + + result r (db->query (query::id == id)); + return r.empty () ? 0 : r.begin ()->ver; +} + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + object o (1); + o.num = 123; + o.str = "abc"; + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // Verify initial version in the instance and database. + // + assert (o.ver == 1); + { + transaction t (db->begin ()); + assert (version (db, 1) == 1); + t.commit (); + } + + object c (o); + o.num++; + o.str += 'd'; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + // Verify updated version in the instance and database. + // + assert (o.ver == 2); + { + transaction t (db->begin ()); + assert (version (db, 1) == 2); + t.commit (); + } + + // Verify the data has been updated. + // + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (1)); + t.commit (); + + assert (o1->ver == 2 && o1->num == 124 && o1->str == "abcd"); + } + + // Try to update using outdated object. + // + c.num--; + c.str += 'z'; + + { + transaction t (db->begin ()); + + try + { + db->update (c); + assert (false); + } + catch (const object_changed&) {} + + // Verify the data hasn't changed. + // + auto_ptr o1 (db->load (1)); + assert (o1->ver == 2 && o1->num == 124 && o1->str == "abcd"); + + // Reload the object. + // + db->reload (c); + assert (c.ver == 2 && c.num == 124); + + // Check that we don't reload an object that is up-to-date. + // + c.num--; + db->reload (c); + assert (c.ver == 2 && c.num == 123); + + t.commit (); + } + + // Try to delete using an outdated object. + // + { + transaction t (db->begin ()); + + try + { + db->update (o); + db->erase (c); + assert (false); + } + catch (const object_changed&) {} + + t.commit (); + } + + // Try to delete using an up-to-date object. + // + { + transaction t (db->begin ()); + db->erase (o); + t.commit (); + } + + // Try to update deleted object. + // + { + transaction t (db->begin ()); + + try + { + db->update (o); + assert (false); + } + catch (const object_not_persistent&) + { + assert (false); + } + catch (const object_changed&) {} + + t.commit (); + } + + // Optimistic delete of objects with container requires + // extra logic. Test it here. + // + { + container o ("abc"); + o.nums.push_back (1); + o.nums.push_back (2); + o.nums.push_back (3); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + container c (o); + o.nums.pop_back (); + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + // Try to delete using an outdated object. + // + { + transaction t (db->begin ()); + + try + { + db->erase (c); + assert (false); + } + catch (const object_changed&) {} + + // Verify the container data hasn't changed. + // + auto_ptr o1 (db->load ("abc")); + assert (o1->nums.size () == 2 && o1->nums[0] == 1 && o1->nums[1] == 2); + + t.commit (); + } + + // Try to delete using an up-to-date object. + // + { + transaction t (db->begin ()); + db->erase (o); + t.commit (); + } + } + + // Test optimistic class inheritance. This is a shortened version + // of the object test. + // + { + derived o; + o.num = 123; + o.str = "abc"; + + // Persist. + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + derived c (o); + o.num++; + o.str += 'd'; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + // Try to update using outdated object. + // + c.num--; + c.str += 'z'; + + { + transaction t (db->begin ()); + + try + { + db->update (c); + assert (false); + } + catch (const object_changed&) {} + + // Reload the object. + // + db->reload (c); + assert (c.ver == 2 && c.num == 124); + + t.commit (); + } + + // Try to delete using an outdated object. + // + { + transaction t (db->begin ()); + + try + { + db->update (o); + db->erase (c); + assert (false); + } + catch (const object_changed&) {} + + t.commit (); + } + + // Try to delete using an up-to-date object. + // + { + transaction t (db->begin ()); + db->erase (o); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/optimistic/makefile b/common/optimistic/makefile new file mode 100644 index 0000000..955cc2f --- /dev/null +++ b/common/optimistic/makefile @@ -0,0 +1,109 @@ +# file : common/optimistic/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.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 + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# 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) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query --table-prefix optimistic_ +$(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 +# +name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(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/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(odb_rules)) +$(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/common/optimistic/test.hxx b/common/optimistic/test.hxx new file mode 100644 index 0000000..06dadff --- /dev/null +++ b/common/optimistic/test.hxx @@ -0,0 +1,78 @@ +// file : common/optimistic/test.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include + +#include + +#pragma db object optimistic +struct object +{ + object (): ver (123) {} + object (unsigned long id): id_ (id), ver (123) {} + + #pragma db id + unsigned long id_; + + #pragma db version + unsigned long ver; + + unsigned int num; + std::string str; +}; + +#pragma db view object(object) +struct object_version +{ + unsigned long ver; +}; + +// Optimistic class with a container. +// +#pragma db object optimistic +struct container +{ + container (): ver (123) {} + container (std::string const& id): id_ (id), ver (123) {} + + #pragma db id + std::string id_; + + #pragma db version + unsigned long ver; + + std::vector nums; +}; + +// Optimistic class inheritance. +// +#pragma db object abstract optimistic +struct base +{ + base (): ver (123) {} + + #pragma db id auto + unsigned long id_; + + #pragma db version + unsigned long ver; + + std::string str; + + #pragma db readonly + std::string ro; +}; + +#pragma db object +struct derived: base +{ + unsigned int num; +}; + +#endif // TEST_HXX diff --git a/common/optimistic/test.std b/common/optimistic/test.std new file mode 100644 index 0000000..e69de29 -- cgit v1.1