aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-09-13 11:43:09 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-09-13 11:43:09 +0200
commit3fe2f0f8570ad9b6db2a550d2c313e5367e61fd1 (patch)
treeffc86a1cca92163e14288a4768a30049de0a5ba6
parenta9d527ffd7e4a2222d5e80c8110f092af0cb5a74 (diff)
Handle buffer growth in cached result
-rw-r--r--mysql/truncation/driver.cxx165
-rw-r--r--mysql/truncation/makefile3
2 files changed, 116 insertions, 52 deletions
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<database> 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<database> 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<object2> o (db->load<object2> (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<object2> o (db->load<object2> (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<object1> query;
+ typedef odb::result<object1> result;
- transaction t (db->begin_transaction ());
- db->persist (o);
- db->load (1, o);
- t.commit ();
- }
+ auto_ptr<database> 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<object1> (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<object2> o (db->load<object2> (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<object1> (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<object2> o (db->load<object2> (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)
diff --git a/mysql/truncation/makefile b/mysql/truncation/makefile
index a798c8d..331bc34 100644
--- a/mysql/truncation/makefile
+++ b/mysql/truncation/makefile
@@ -35,7 +35,8 @@ gen := $(addprefix $(out_base)/,$(genf))
$(gen): $(odb)
$(gen): odb := $(odb)
-$(gen) $(dist): export odb_options += --database mysql --generate-schema
+$(gen) $(dist): export odb_options += --database mysql --generate-query \
+--generate-schema
$(gen): cpp_options := -I$(out_base)
$(gen): $(common.l.cpp-options)