# # # add_dir "tests/in_memory_database" # # add_file "tests/in_memory_database/__driver__.lua" # content [24ffdfb8ba9f5a518a8a6bc2a6cf16abce120533] # # patch "database.cc" # from [368b708cda872cc8c480464ae0bbe756252e2f4b] # to [8d092cff405c1f08af7289dccf224037ada9dd53] # # patch "options_list.hh" # from [820a2e9dfb65c7771b047f333f9e26d067b4ee97] # to [556f0e2b7f83c5baece9886bcf2ef842ef2fc29e] # ============================================================ --- tests/in_memory_database/__driver__.lua 24ffdfb8ba9f5a518a8a6bc2a6cf16abce120533 +++ tests/in_memory_database/__driver__.lua 24ffdfb8ba9f5a518a8a6bc2a6cf16abce120533 @@ -0,0 +1,14 @@ +include("common/netsync.lua") +mtn_setup() +netsync.setup() + +addfile("foo", "bar") +commit() + + +srv = netsync.start({"-d", ":memory:"}) + +srv:push("*", 1) +srv:pull("*", 2) + +check_same_db_contents("test.db", "test2.db") ============================================================ --- database.cc 368b708cda872cc8c480464ae0bbe756252e2f4b +++ database.cc 8d092cff405c1f08af7289dccf224037ada9dd53 @@ -209,7 +209,7 @@ public: // for scoped_ptr's sake public: - explicit database_impl(system_path const &); + explicit database_impl(system_path const & f, bool mem); ~database_impl(); private: @@ -218,6 +218,7 @@ private: // --== Opening the database and schema checking ==-- // system_path const filename; + bool use_memory_db; struct sqlite3 * __sql; void install_functions(); @@ -457,8 +458,9 @@ sqlite3_hex_fn(sqlite3_context *f, int n } #endif -database_impl::database_impl(system_path const & f) : +database_impl::database_impl(system_path const & f, bool mem) : filename(f), + use_memory_db(mem), __sql(NULL), transaction_level(0), roster_cache(constants::db_roster_cache_sz, @@ -499,7 +501,7 @@ database::database(app_state & app) boost::shared_ptr & i = app.dbcache->dbs[app.opts.dbname]; if (!i) - i.reset(new database_impl(app.opts.dbname)); + i.reset(new database_impl(app.opts.dbname, app.opts.dbname_is_memory)); imp = i; } @@ -516,6 +518,8 @@ database::is_dbfile(any_path const & fil bool database::is_dbfile(any_path const & file) { + if (imp->use_memory_db) + return false; system_path fn(file); // canonicalize bool same = (imp->filename == fn); if (same) @@ -526,7 +530,7 @@ database::database_specified() bool database::database_specified() { - return !imp->filename.empty(); + return imp->use_memory_db || !imp->filename.empty(); } void @@ -605,23 +609,36 @@ database_impl::sql(enum open_mode mode) { if (! __sql) { - check_filename(); - check_db_exists(); - open(); + if (use_memory_db) + { + open(); - if (mode != schema_bypass_mode) + sqlite3_exec(__sql, schema_constant, NULL, NULL, NULL); + assert_sqlite3_ok(__sql); + + sqlite3_exec(__sql, (FL("PRAGMA user_version = %u;") + % mtn_creator_code).str().c_str(), NULL, NULL, NULL); + assert_sqlite3_ok(__sql); + } + else { - check_sql_schema(__sql, filename); + check_filename(); + check_db_exists(); + open(); - if (mode != format_bypass_mode) + if (mode != schema_bypass_mode) { - check_format(); + check_sql_schema(__sql, filename); - if (mode != cache_bypass_mode) - check_caches(); + if (mode != format_bypass_mode) + { + check_format(); + + if (mode != cache_bypass_mode) + check_caches(); + } } } - install_functions(); } else @@ -4302,7 +4319,12 @@ database_impl::open() { I(!__sql); - if (sqlite3_open(filename.as_external().c_str(), &__sql) == SQLITE_NOMEM) + char const * to_open; + if (use_memory_db) + to_open = ":memory:"; + else + to_open = filename.as_external().c_str(); + if (sqlite3_open(to_open, &__sql) == SQLITE_NOMEM) throw std::bad_alloc(); I(__sql); ============================================================ --- options_list.hh 820a2e9dfb65c7771b047f333f9e26d067b4ee97 +++ options_list.hh 556f0e2b7f83c5baece9886bcf2ef842ef2fc29e @@ -217,10 +217,12 @@ OPT(format_dates, "no-format-dates", boo } #endif +OPTVAR(globals, bool, dbname_is_memory, false); GOPT(dbname, "db,d", system_path, , gettext_noop("set name of database")) #ifdef option_bodies { dbname = system_path(arg, origin::user); + dbname_is_memory = (arg == ":memory:"); } #endif