From 3fe2f0f8570ad9b6db2a550d2c313e5367e61fd1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 13 Sep 2010 11:43:09 +0200 Subject: Handle buffer growth in cached result --- mysql/truncation/driver.cxx | 165 ++++++++++++++++++++++++++++++-------------- 1 file changed, 114 insertions(+), 51 deletions(-) (limited to 'mysql/truncation/driver.cxx') diff --git a/mysql/truncation/driver.cxx b/mysql/truncation/driver.cxx index 46350d6..76985f0 100644 --- a/mysql/truncation/driver.cxx +++ b/mysql/truncation/driver.cxx @@ -24,72 +24,135 @@ using namespace odb; int main (int argc, char* argv[]) { + // The default pre-allocated buffer is 512 bytes long. + // + string long_str (640, 'c'); // This will get the buffer to 1024 + string longer_str (1025, 'b'); + try { - auto_ptr db (create_database (argc, argv)); - - // The default pre-allocated buffer is 512 bytes long. + // Test basic operations. // - string long_str (640, 'c'); // This will get the buffer to 1024 - string longer_str (1025, 'b'); + { + auto_ptr db (create_database (argc, argv)); + + // Run persist/load so that the initial bindings are established + // (version == 0). + // + { + object1 o (1); + o.str_ = "test string"; + + transaction t (db->begin_transaction ()); + db->persist (o); + db->load (1, o); + t.commit (); + } + + { + object2 o (2); + o.str_ = "test string"; + + transaction t (db->begin_transaction ()); + db->persist (o); + db->load (2, o); + t.commit (); + } + + // Store/load the long string which should trigger buffer growth. + // + { + object1 o (3); + o.str_ = long_str; + + transaction t (db->begin_transaction ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin_transaction ()); + auto_ptr o (db->load (3)); + assert (o->str_ == long_str); + t.commit (); + } + + // Store/load longer string. + // + { + object1 o (3); + o.str_ = longer_str; + + transaction t (db->begin_transaction ()); + db->store (o); + t.commit (); + } + + { + transaction t (db->begin_transaction ()); + auto_ptr o (db->load (3)); + assert (o->str_ == longer_str); + t.commit (); + } + } - // Run persist/load so that the initial bindings are established - // (version == 0). + // Test query. // { - object1 o (1); - o.str_ = "test string"; + typedef odb::query query; + typedef odb::result result; - transaction t (db->begin_transaction ()); - db->persist (o); - db->load (1, o); - t.commit (); - } + auto_ptr db (create_database (argc, argv)); - { - object2 o (2); - o.str_ = "test string"; + // Run persist/query so that the initial bindings are established + // (version == 0). + // + { + object1 o (20); + o.str_ = "test string"; - transaction t (db->begin_transaction ()); - db->persist (o); - db->load (2, o); - t.commit (); - } + transaction t (db->begin_transaction ()); + db->persist (o); + o.id_++; + db->persist (o); + o.id_++; + db->persist (o); - // Store/load the long string which should trigger buffer growth. - // - { - object1 o (3); - o.str_ = long_str; + result r (db->query (query::id == 20)); + assert (r.begin ()->id_ == 20); + t.commit (); + } - transaction t (db->begin_transaction ()); - db->persist (o); - t.commit (); - } + // Test buffer growth with cached result. + // + { + object1 o; - { - transaction t (db->begin_transaction ()); - auto_ptr o (db->load (3)); - assert (o->str_ == long_str); - t.commit (); - } + transaction t (db->begin_transaction ()); - // Store/load longer string. - // - { - object1 o (3); - o.str_ = longer_str; + result r (db->query (query::id >= 20)); + r.cache (); + result::iterator i (r.begin ()); - transaction t (db->begin_transaction ()); - db->store (o); - t.commit (); - } + o.id_ = i->id_; + o.str_ = long_str; - { - transaction t (db->begin_transaction ()); - auto_ptr o (db->load (3)); - assert (o->str_ == longer_str); - t.commit (); + // This forces buffer growth in the middle of result iteration. + // + db->store (o); + + ++i; + assert (i->str_ == "test string"); + + o.id_ = i->id_; + o.str_ = longer_str; + db->store (o); + + ++i; + assert (i->str_ == "test string"); + + t.commit (); + } } } catch (const odb::exception& e) -- cgit v1.1