Author: dmacvicar
Date: Fri Jul 6 15:05:42 2007
New Revision: 5917
URL: http://svn.opensuse.org/viewcvs/zypp?rev=5917&view=rev
Log:
- Implement cache schema versioning and automatic invalidation of
cache when schema changes
Modified:
branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.cc
branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.h
branches/REFACTORING-10_3/libzypp/zypp/cache/schema/schema.sql
Modified: branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.cc
URL: http://svn.opensuse.org/viewcvs/zypp/branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.cc?rev=5917&r1=5916&r2=5917&view=diff
==============================================================================
--- branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.cc (original)
+++ branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.cc Fri Jul 6 15:05:42 2007
@@ -17,11 +17,9 @@
#include "zypp/cache/CacheInitializer.h"
#include "zypp/target/store/PersistentStorage.h"
#include "zypp/cache/Utils.h"
-
+#include "zypp/PathInfo.h"
#include "sqlite-schema.h"
-#define ZYPP_DB_FILE "/var/lib/zypp/zypp.db"
-
using namespace sqlite3x;
using namespace std;
using zypp::debug::Measure;
@@ -36,13 +34,15 @@
struct CacheInitializer::Impl
{
Impl( const Pathname &root_r )
- : root(root_r), just_initialized(false)
+ : root(root_r), just_initialized(false),
+ just_reinitialized(false)
{
}
//typedef std::map MediaMap
shared_ptrsqlite3x::sqlite3_connection con;
Pathname root;
bool just_initialized;
+ bool just_reinitialized;
};
CacheInitializer::CacheInitializer( const Pathname &root_r, const Pathname &db_file )
@@ -51,17 +51,47 @@
try
{
_pimpl->con.reset( new sqlite3_connection( ( _pimpl->root + db_file).asString().c_str()) );
-
+ _pimpl->con->executenonquery("begin;");
if( ! tablesCreated() )
{
createTables();
_pimpl->just_initialized = true;
- _pimpl->con->close();
- MIL << "Source cache initialized" << std::endl;
+ //_pimpl->con->close();
+ MIL << "Repository cache initialized" << std::endl;
}
else
{
- MIL << "Source cache already initialized" << std::endl;
+ int version = _pimpl->con->executeint("select version from db_info;");
+ if ( version != ZYPP_CACHE_SCHEMA_VERSION )
+ {
+ WAR << "cache schema version is different from ZYpp cache version" << endl;
+ // FIXME should this code ask first?
+ sqlite3_command tables_cmd( *_pimpl->con, "select name from sqlite_master where type='table';");
+ sqlite3_reader reader = tables_cmd.executereader();
+ list<string> tables;
+ while ( reader.read() )
+ {
+ string tablename = reader.getstring(0);
+ if ( tablename != "sqlite_sequence" )
+ tables.push_back(tablename);
+ }
+ reader.close();
+
+ for ( list<string>::const_iterator it = tables.begin();
+ it != tables.end();
+ ++it )
+ {
+ MIL << "Removing table " << *it << endl;
+ _pimpl->con->execute("drop table " + (*it) + ";");
+ }
+
+ createTables();
+ _pimpl->just_reinitialized = true;
+ MIL << "Repository cache re-initialized" << std::endl;
+ return;
+ }
+
+ MIL << "Repository cache already initialized" << std::endl;
}
}
catch( const exception &ex )
@@ -69,7 +99,8 @@
ZYPP_RETHROW(Exception(ex.what()));
//ERR << "Exception Occured: " << ex.what() << endl;
}
-
+ _pimpl->con->executenonquery("commit;");
+ _pimpl->con->close();
}
bool CacheInitializer::justInitialized() const
@@ -77,6 +108,11 @@
return _pimpl->just_initialized;
}
+bool CacheInitializer::justReinitialized() const
+{
+ return _pimpl->just_reinitialized;
+}
+
CacheInitializer::~CacheInitializer()
{
@@ -92,17 +128,20 @@
void CacheInitializer::createTables()
{
- Measure timer("Create database tables");
+ //Measure timer("Create database tables");
MIL << "Initializing cache schema..." << endl;
- sqlite3_transaction trans(*_pimpl->con);
- {
+ //sqlite3_transaction trans(*_pimpl->con);
+ //{
string sql( schemaData, _schemaData_size);
//ERR << "Executing " << statements[i] << endl;
MIL << "Schema size: " << sql.size() << endl;
_pimpl->con->execute(sql.c_str());
- }
- trans.commit();
- timer.elapsed();
+ sqlite3_command version_cmd( *_pimpl->con, "insert into db_info (version) values(:version);");
+ version_cmd.bind(":version", ZYPP_CACHE_SCHEMA_VERSION);
+ version_cmd.executenonquery();
+ //}
+ //trans.commit();
+ //timer.elapsed();
}
std::ostream & CacheInitializer::dumpOn( std::ostream & str ) const
Modified: branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.h
URL: http://svn.opensuse.org/viewcvs/zypp/branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.h?rev=5917&r1=5916&r2=5917&view=diff
==============================================================================
--- branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.h (original)
+++ branches/REFACTORING-10_3/libzypp/zypp/cache/CacheInitializer.h Fri Jul 6 15:05:42 2007
@@ -19,6 +19,8 @@
#include "zypp/Pathname.h"
#include "zypp/cache/sqlite3x/sqlite3x.hpp"
+#define ZYPP_CACHE_SCHEMA_VERSION 1000
+
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
@@ -43,10 +45,22 @@
virtual ~CacheInitializer();
/**
+ * \short Has the cache been reinitialized?
+ *
* only true when cache was not initialized before
* and was just initialized with success
*/
bool justInitialized() const;
+
+ /**
+ * \short Has the cache been reinitialized?
+ *
+ * This is true when the cache was already initialized
+ * but an old schema was detected, so the cache
+ * was reinitialized in order to get the new schema
+ */
+ bool justReinitialized() const;
+
protected:
bool tablesCreated() const;
void createTables();
Modified: branches/REFACTORING-10_3/libzypp/zypp/cache/schema/schema.sql
URL: http://svn.opensuse.org/viewcvs/zypp/branches/REFACTORING-10_3/libzypp/zypp/cache/schema/schema.sql?rev=5917&r1=5916&r2=5917&view=diff
==============================================================================
--- branches/REFACTORING-10_3/libzypp/zypp/cache/schema/schema.sql (original)
+++ branches/REFACTORING-10_3/libzypp/zypp/cache/schema/schema.sql Fri Jul 6 15:05:42 2007
@@ -40,7 +40,7 @@
CREATE TABLE db_info (
version INTEGER
);
---INSERT INTO db_info (version) VALUES ('
+
------------------------------------------------
-- Basic types like archs, attributes, languages
------------------------------------------------
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org