backup
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 26 Mar 2007 16:32:46 +0000 (16:32 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Mon, 26 Mar 2007 16:32:46 +0000 (16:32 +0000)
use some indexes to increase 100x speed
support namedcap, filecap and vercap

zypp2/cache/CacheQuery.cc
zypp2/cache/schema/schema.sql

index f2aef173792945eb14d747d3a1fcdfbeb1fceb95..d85daad0bb821bd2e2e61c55c1147e8b49ab135f 100644 (file)
@@ -28,17 +28,25 @@ typedef shared_ptr<sqlite3_reader> sqlite3_reader_ptr;
 struct DatabaseContext
 {
   sqlite3_connection con;
-  sqlite3_command_ptr select_vercap_cmd;
+  sqlite3_command_ptr select_versionedcap_cmd;
+  sqlite3_command_ptr select_namedcap_cmd;
+  sqlite3_command_ptr select_filecap_cmd;
   Pathname dbdir;
 };
 
 typedef shared_ptr<DatabaseContext> DatabaseContext_Ptr;
 
+///////////////////////////////////////////////////////////////
+// CAPABILITY QUERY                                         //
+//////////////////////////////////////////////////////////////
+
 class CapabilityQuery::Impl
 {
   public:
   Impl( DatabaseContext_Ptr p_context, const zypp::Dep &deptype, const data::RecordId &resolvable_id  )
-    : context(p_context), _read(false), _deptype(deptype), _resolvable_id(resolvable_id)
+    : context(p_context), _deptype(deptype), _resolvable_id(resolvable_id)
+      , _vercap_read(false), _namedcap_read(false), _filecap_read(false)
+      , _vercap_done(false), _namedcap_done(false), _filecap_done(false)
   {}
   
   ~Impl()
@@ -47,19 +55,37 @@ class CapabilityQuery::Impl
   }
   
   DatabaseContext_Ptr context;
-  sqlite3_reader_ptr _reader;
-  bool _read;
+  sqlite3_reader_ptr _versioned_reader;
+  sqlite3_reader_ptr _named_reader;
+  sqlite3_reader_ptr _file_reader;
   zypp::Dep _deptype;
   data::RecordId _resolvable_id;
+  bool _vercap_read;
+  bool _namedcap_read;
+  bool _filecap_read;
+  
+  bool _vercap_done;
+  bool _namedcap_done;
+  bool _filecap_done;
+  
 };
 
 CapabilityQuery::CapabilityQuery( Impl *impl)
   : _pimpl( impl)
 {
-    //cmd.bind(":id", id);
-    _pimpl->context->select_vercap_cmd->bind(":rid", _pimpl->_resolvable_id);
-    _pimpl->context->select_vercap_cmd->bind(":dtype", static_cast<int>( zypp_deptype2db_deptype(_pimpl->_deptype) ) );
-    _pimpl->_reader.reset( new sqlite3_reader(_pimpl->context->select_vercap_cmd->executereader()));
+     int deptype = static_cast<int>(zypp_deptype2db_deptype(_pimpl->_deptype));
+    _pimpl->context->select_versionedcap_cmd->bind(":rid", _pimpl->_resolvable_id);
+    _pimpl->context->select_versionedcap_cmd->bind(":dtype", deptype );
+    _pimpl->_versioned_reader.reset( new sqlite3_reader(_pimpl->context->select_versionedcap_cmd->executereader()));
+    
+    _pimpl->context->select_namedcap_cmd->bind(":rid", _pimpl->_resolvable_id);
+    _pimpl->context->select_namedcap_cmd->bind(":dtype", deptype );
+    _pimpl->_named_reader.reset( new sqlite3_reader(_pimpl->context->select_namedcap_cmd->executereader()));
+    
+    _pimpl->context->select_filecap_cmd->bind(":rid", _pimpl->_resolvable_id);
+    _pimpl->context->select_filecap_cmd->bind(":dtype", deptype );
+    _pimpl->_file_reader.reset( new sqlite3_reader(_pimpl->context->select_filecap_cmd->executereader()));
+    
     MIL << "Done setup query" << endl;
     read();
     MIL << "Done first read" << endl;
@@ -69,30 +95,82 @@ CapabilityQuery::~CapabilityQuery()
 {
   //MIL << endl;
   // it has to be disposed before the commands
-  _pimpl->_reader.reset();
+  //_pimpl->_versioned_reader.reset();
+  //_pimpl->_named_reader.reset();
+  //_pimpl->_file_reader.reset();
 }
 
 bool CapabilityQuery::read()
 {
-  //MIL << endl;
-  return ( _pimpl->_read = _pimpl->_reader->read() );
+  if (!_pimpl->_vercap_done)
+  {
+    if ( _pimpl->_vercap_read = _pimpl->_versioned_reader->read() )
+      return true;
+    else
+      _pimpl->_vercap_done = true;
+  }
+        
+  if (!_pimpl->_namedcap_done)
+  {
+    if ( _pimpl->_namedcap_read = _pimpl->_named_reader->read() )
+      return true;
+    else
+      _pimpl->_namedcap_done = true;
+  }
+  
+  if (!_pimpl->_filecap_done)
+  {
+    if ( _pimpl->_filecap_read = _pimpl->_file_reader->read() )
+      return true;
+    else
+      _pimpl->_filecap_done = true;
+  }
+  
+  return false;
 }
   
 bool CapabilityQuery::valid() const
 {
-  return _pimpl->_read;
+ if ( _pimpl->_vercap_read )
+    return true;
+  
+  if ( _pimpl->_namedcap_read )
+    return true;
+  
+  if ( _pimpl->_filecap_read )
+    return true;
+  
+  return false;
 }
 
 CapabilityImpl::Ptr CapabilityQuery::value()
 {
-  Resolvable::Kind refer = db_kind2zypp_kind( static_cast<db::Kind>(_pimpl->_reader->getint(0)) );
-  //Resolvable::Kind refer = ResTraits<Package>::kind;
-  zypp::Rel rel = db_rel2zypp_rel( static_cast<db::Rel>(_pimpl->_reader->getint(5)) );
-  //zypp::Rel rel = Rel::EQ;
-  capability::VersionedCap *vcap = new capability::VersionedCap( refer, _pimpl->_reader->getstring(1), /* rel */ rel, Edition( _pimpl->_reader->getstring(2), _pimpl->_reader->getstring(3), _pimpl->_reader->getint(4) ) );
-  return capability::VersionedCap::Ptr(vcap);
+  if ( _pimpl->_vercap_read )
+  {
+    Resolvable::Kind refer = db_kind2zypp_kind( static_cast<db::Kind>(_pimpl->_versioned_reader->getint(0)) );
+    zypp::Rel rel = db_rel2zypp_rel( static_cast<db::Rel>(_pimpl->_versioned_reader->getint(5)) );
+    capability::VersionedCap *vcap = new capability::VersionedCap( refer, _pimpl->_versioned_reader->getstring(1), /* rel */ rel, Edition( _pimpl->_versioned_reader->getstring(2), _pimpl->_versioned_reader->getstring(3), _pimpl->_versioned_reader->getint(4) ) );
+    return capability::VersionedCap::Ptr(vcap);
+  }
+  if ( _pimpl->_namedcap_read )
+  {
+    Resolvable::Kind refer = db_kind2zypp_kind( static_cast<db::Kind>(_pimpl->_named_reader->getint(0)) );
+    capability::NamedCap *ncap = new capability::NamedCap( refer, _pimpl->_named_reader->getstring(1) );
+    return capability::NamedCap::Ptr(ncap);
+  }
+  if ( _pimpl->_filecap_read )
+  {
+    Resolvable::Kind refer = db_kind2zypp_kind( static_cast<db::Kind>(_pimpl->_file_reader->getint(0)) );
+    capability::FileCap *fcap = new capability::FileCap( refer, _pimpl->_file_reader->getstring(1) + "/" + _pimpl->_file_reader->getstring(2) );
+    return capability::FileCap::Ptr(fcap);
+  }
+  return 0L;
 }
 
+///////////////////////////////////////////////////////////////
+// CACHE QUERY                                              //
+//////////////////////////////////////////////////////////////
+
 struct CacheQuery::Impl
 {
   Impl( const Pathname &pdbdir )
@@ -109,9 +187,11 @@ struct CacheQuery::Impl
     try
     {
       context->con.open( (pdbdir + "zypp.db").asString().c_str());
-      //_insert_resolvable_cmd = new sqlite3_command( *_con, INSERT_RESOLVABLE_QUERY );                                    
-      //_insert_package_cmd = new sqlite3_command( *_con, INSERT_PACKAGE_QUERY );
-      context->select_vercap_cmd.reset( new sqlite3_command( context->con, "select c.refers_kind, n.name, v.version, v.release, v.epoch, v.relation from names n, capabilities c, versioned_capabilities v  where v.name_id=n.id and c.id=v.dependency_id and c.resolvable_id=:rid and c.dependency_type=:dtype;"));
+      
+      // precompile statements
+      context->select_versionedcap_cmd.reset( new sqlite3_command( context->con, "select c.refers_kind, n.name, v.version, v.release, v.epoch, v.relation from names n, capabilities c, versioned_capabilities v  where v.name_id=n.id and c.id=v.dependency_id and c.resolvable_id=:rid and c.dependency_type=:dtype;"));
+      context->select_namedcap_cmd.reset( new sqlite3_command( context->con, "select c.refers_kind, n.name from names n, capabilities c, named_capabilities nc  where nc.name_id=n.id and c.id=nc.dependency_id and c.resolvable_id=:rid and c.dependency_type=:dtype;"));
+      context->select_filecap_cmd.reset( new sqlite3_command( context->con, "select c.refers_kind, dn.name, fn.name  from file_names fn, dir_names dn, capabilities c, file_capabilities fc, files f  where f.id=fc.file_id and f.dir_name_id=dn.id and f.file_name_id=fn.id and c.id=fc.dependency_id and c.resolvable_id=:rid and c.dependency_type=:dtype;"));
     }
     catch(exception &ex)
     {
index e9150c252920e4d64f8d9d163b1edfa2706e8314..dd2f07a084e7d3c66b7a88517f7d6d3f13baf70f 100644 (file)
@@ -332,12 +332,14 @@ CREATE TABLE named_capabilities (
   , dependency_id INTEGER REFERENCES capabilities (id)
   , name_id INTEGER REFERENCES names(id)
 );
+CREATE INDEX named_capabilities_dependency_id ON named_capabilities (dependency_id);
 
 CREATE TABLE file_capabilities (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL 
   , dependency_id INTEGER REFERENCES capabilities (id)
   , file_id INTEGER REFERENCES files(id)
 );
+CREATE INDEX file_capabilities_dependency_id ON file_capabilities (dependency_id);
 
 CREATE TABLE split_capabilities (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL