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 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 300 insertions(+) create mode 100644 common/optimistic/driver.cxx (limited to 'common/optimistic/driver.cxx') 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; + } +} -- cgit v1.1