- initial implementation of cache search
authorKlaus Kaempf <kkaempf@suse.de>
Thu, 16 Aug 2007 21:50:57 +0000 (21:50 +0000)
committerKlaus Kaempf <kkaempf@suse.de>
Thu, 16 Aug 2007 21:50:57 +0000 (21:50 +0000)
  requires libzypp >= 3.16.0
- r6722, version 0.8.12

VERSION.cmake
package/zypper.changes
src/zypper-search.cc
src/zypper-search.h
src/zypper.cc
zypper.spec.cmake

index adfe8d0..43d45ed 100644 (file)
@@ -20,4 +20,4 @@
 
 SET(VERSION_MAJOR "0")
 SET(VERSION_MINOR "8")
-SET(VERSION_PATCH "11")
+SET(VERSION_PATCH "12")
index fd3f2fe..46c822b 100644 (file)
@@ -1,4 +1,11 @@
 -------------------------------------------------------------------
+Thu Aug 16 23:46:15 CEST 2007 - kkaempf@suse.de
+
+- initial implementation of cache search
+  requires libzypp >= 3.16.0
+- r6722, version 0.8.12
+
+-------------------------------------------------------------------
 Thu Aug 16 13:10:44 CEST 2007 - jkupec@suse.cz
 
 - more comprehensive message after adding a repository
index 56fedb4..a72958e 100644 (file)
@@ -35,8 +35,8 @@ ZyppSearch::ZyppSearch (
     const ZyppSearchOptions & options,
     const vector<string> qstrings
     ) :
-    _zypp(zypp), _options(options), _qstrings(qstrings) {
-
+    _zypp(zypp), _options(options), _qstrings(qstrings), _query( _manager_options.repoCachePath ) {
+#if 0
   // no repos warning
   if (gData.repos.empty()) {
     cerr << _("No repositories configured. Please, add at least one"
@@ -44,32 +44,57 @@ ZyppSearch::ZyppSearch (
          << endl;
     exit(ZYPPER_EXIT_NO_REPOS); // TODO #define zypper error codes?
   }
-
+#endif
   setupRegexp();
   cacheInstalled();
+#if 0
   load_repo_resolvables(); // populates ResPool with resolvables from repos
-
+#endif
   // cache identification strings of source resolvables (used to check for
   // duplicates of target resolvables in repos - DuplicateFilter)
-  invokeOnEachSearched(not_c(ByInstalled()), functorRef<bool,const zypp::PoolItem &>(_idcache));
+  invokeOnEachSearched(not_c(ByInstalled()), functorRef<bool,const zypp::PoolItem &>(_idcache), functorRef<bool, const data::RecordId &, data::ResObject_Ptr>(_idcache));
 }
 
 /**
  * Invokes zypp::invokeOnEach() on a subset of pool items restricted by
  * some search criteria (--type,--match-exact).
  */
-template <class _Filter, class _Function>
-int ZyppSearch::invokeOnEachSearched(_Filter filter_r, _Function fnc_r) {
+template <class _Filter, class _PoolCallback, class _CacheCallback>
+int
+ZyppSearch::invokeOnEachSearched(_Filter filter_r, _PoolCallback pool_cb, _CacheCallback cache_cb)
+{
+  // pool only contains _installed_ resolvables
   ResPool pool = _zypp->pool();
 
   // search for specific resolvable type only
   if (_options.kind() != Resolvable::Kind()) {
     cerr_vv << "invokeOnEachSearched(): search by type" << endl;
 
-    return invokeOnEach(
+    if (_options.installedFilter() != ZyppSearchOptions::UNINSTALLED_ONLY)
+    {
+      // search pool on ALL or INSTALLED
+      invokeOnEach(
         pool.byKindBegin(_options.kind()), pool.byKindEnd(_options.kind()),
-        filter_r, fnc_r);
+        filter_r, pool_cb);
+    }
+
+    if (_options.installedFilter() != ZyppSearchOptions::INSTALLED_ONLY)
+    {
+      try
+      {
+      // search cache on ALL or UNINSTALLED by TYPE
+
+      _query.query( "%libzypp%", cache_cb );
+      }
+      catch ( const Exception & excpt_r )
+      {
+        ZYPP_CAUGHT( excpt_r );
+        cerr << "cache::ResolvableQuery failed: " << excpt_r.asUserString() << endl;
+      }
+    }
+    return 0;
   }
+
   // search for exact package using byName_iterator
   // usable only if there is only one query string and if this string
   // doesn't contain wildcards
@@ -78,20 +103,60 @@ int ZyppSearch::invokeOnEachSearched(_Filter filter_r, _Function fnc_r) {
       _qstrings[0].find('?') == string::npos) {
     cerr_vv << "invokeOnEachSearched(): exact name match" << endl;
 
-    return invokeOnEach(
+    if (_options.installedFilter() != ZyppSearchOptions::UNINSTALLED_ONLY)
+    {
+      // search pool on ALL or INSTALLED
+      invokeOnEach(
         pool.byNameBegin(_qstrings[0]), pool.byNameEnd(_qstrings[0]),
-        filter_r, fnc_r);
+        filter_r, pool_cb);
+    }
+
+    if (_options.installedFilter() != ZyppSearchOptions::INSTALLED_ONLY)
+    {
+      try
+      {
+      // search cache on ALL or UNINSTALLED by EXACT NAME
+
+      _query.queryByName( _qstrings[0], 0, cache_cb );
+      }
+      catch ( const Exception & excpt_r )
+      {
+        ZYPP_CAUGHT( excpt_r );
+        cerr << "cache::ResolvableQuery failed: " << excpt_r.asUserString() << endl;
+      }
+    }
+    return 0;
   }
 
   // search among all resolvables
   else {
     cerr_vv << "invokeOnEachSearched(): search among all resolvables" << endl;
 
-    return invokeOnEach(pool.begin(), pool.end(), filter_r, fnc_r);
+    if (_options.installedFilter() != ZyppSearchOptions::UNINSTALLED_ONLY)
+    {
+      // search pool on ALL or INSTALLED
+      invokeOnEach(pool.begin(), pool.end(), filter_r, pool_cb);
+    }
+
+    if (_options.installedFilter() != ZyppSearchOptions::INSTALLED_ONLY)
+    {
+      try
+      {
+      // search cache on ALL or UNINSTALLED by WILD NAME
+
+      _query.queryByName( _qstrings[0], 3, cache_cb );
+      }
+      catch ( const Exception & excpt_r )
+      {
+        ZYPP_CAUGHT( excpt_r );
+        cerr << "cache::ResolvableQuery failed: " << excpt_r.asUserString() << endl;
+      }
+    }
   }
+  return 0;
 }
 
-/**
+/** PRIVATE
  * Cache installed packages matching given search criteria into a hash_map.
  * Assumption made: names of currently installed resolvables + kind
  * (+version???) are unique.
@@ -109,18 +174,20 @@ void ZyppSearch::cacheInstalled() {
   _zypp->addResolvables(tgt_resolvables, true /*installed*/);
 
   invokeOnEachSearched(Match(_reg,_options.searchDescriptions()),
-    functorRef<bool,const zypp::PoolItem &>(_icache));
+    functorRef<bool,const zypp::PoolItem &>(_icache), functorRef<bool, const data::RecordId &, data::ResObject_Ptr>(_icache));
 
   cout_v << _icache.size() << _(" out of (") <<  tgt_resolvables.size() << ")"  
     << _("cached.") << endl;
 }
 
-/**
+/** PUBLIC
  * Invokes functor f on each pool item matching search criteria. 
  */
-void ZyppSearch::doSearch(const boost::function<bool(const PoolItem &)> & f) {
+void
+ZyppSearch::doSearch(const boost::function<bool(const PoolItem &)> & f, const zypp::cache::ProcessResolvable & r)
+{
   boost::function<bool (const PoolItem &)> filter;
-
+cerr << "ZyppSearch::doSearch()" << endl;
   switch (_options.installedFilter()) {
     case ZyppSearchOptions::INSTALLED_ONLY:
       filter = chain(ByInstalledCache(_icache),Match(_reg,_options.searchDescriptions()));
@@ -135,7 +202,7 @@ void ZyppSearch::doSearch(const boost::function<bool(const PoolItem &)> & f) {
   
   filter = chain(filter,DuplicateFilter(_idcache));
 
-  invokeOnEachSearched(filter, f);
+  invokeOnEachSearched(filter, f, r);
 }
 
 //! macro for word boundary tags for regexes
index 3f578ad..9d0bd32 100644 (file)
@@ -16,6 +16,8 @@
 #include <boost/function.hpp>
 #include <zypp/ZYpp.h>
 #include <zypp/base/Hash.h>
+#include <zypp/cache/ResolvableQuery.h>
+#include <zypp/RepoManager.h>
 
 #include "zypper.h"
 #include "zypper-getopt.h"
@@ -122,6 +124,14 @@ public:
     addItem(pi);
     return true;
   }
+
+  /** defined for use as a functor for filling the hashmap in a for_each */ 
+  // FIXME: should be cache::ProcessResolvable
+  bool operator()(const zypp::data::RecordId & id, const zypp::data::ResObject_Ptr res) {
+    // dummy
+    return true;
+  }
+
 };
 
 
@@ -154,6 +164,13 @@ public:
     return true;
   }
 
+  /** defined for use as a functor for filling the IdSet in a for_each */ 
+  // FIXME: should be cache::ProcessResolvable
+  bool operator()(const zypp::data::RecordId & id, const zypp::data::ResObject_Ptr res) {
+    // dummy
+    return true;
+  }
+
   int size() { return _items.size(); }
 };
 
@@ -166,12 +183,14 @@ public:
   ZyppSearch (zypp::ZYpp::Ptr & zypp, const ZyppSearchOptions & options,
       const std::vector<std::string> qstrings = std::vector<std::string>());
 
-  void doSearch(const boost::function<bool(const zypp::PoolItem &)> & f);
+  void doSearch(const boost::function<bool(const zypp::PoolItem &)> & f, const zypp::cache::ProcessResolvable & r);
 
   InstalledCache & installedCache() { return _icache; }
 
-  template <class _Filter, class _Function>
-  int invokeOnEachSearched(_Filter filter_r, _Function fnc_r);
+  template <class _Filter, class _PoolCallback, class _CacheCallback>
+    int invokeOnEachSearched(_Filter filter_r, _PoolCallback pool_cb, _CacheCallback cache_cb);
+
+  zypp::cache::ResolvableQuery *getQueryInstancePtr( void ) { return &_query; }
 
 private:
   zypp::ZYpp::Ptr & _zypp;
@@ -182,6 +201,10 @@ private:
   InstalledCache _icache;
   IdCache _idcache;
 
+  zypp::RepoManagerOptions _manager_options;
+
+  zypp::cache::ResolvableQuery _query;
+
   void setupRegexp();
   void cacheInstalled();
   std::string wildcards2regex(const std::string & str) const;
@@ -227,8 +250,9 @@ struct ByInstalledCache
  */
 struct FillTable
 {
-  FillTable(Table & table, InstalledCache & icache) :
-    _table(&table), _icache(&icache) {
+  FillTable(Table & table, InstalledCache & icache, zypp::cache::ResolvableQuery * query) :
+    _table(&table), _icache(&icache), _query(query)
+  {
     TableHeader header;
 
     // TranslatorExplanation S as Status
@@ -282,9 +306,45 @@ struct FillTable
     return true;
   }
 
+  bool operator()(const zypp::data::RecordId & id, const zypp::data::ResObject_Ptr res) {
+    TableRow row;
+
+    // add status to the result table
+    zypp::PoolItem inst_item; // = _icache->getItem(pool_item);
+    if (inst_item) {
+      // check whether the pool item is installed...
+      if (inst_item.resolvable()->edition() == res->edition &&
+          inst_item.resolvable()->arch() == res->arch)
+        row << "i";
+      // ... or there's just another version of it installed
+      else
+        row << "v";
+    }
+    // or it's not installed at all
+    else {
+      row << "";
+    }
+
+    string alias = _query->queryRepositoryAlias( res->repository );
+
+    // add other fields to the result table
+    row << alias
+        // TODO what about rug's Bundle?
+        << "kind"              // FIXME: (gSettings.is_rug_compatible ? "" : res->kind().asString()) 
+        << res->name
+        << res->edition.asString()
+        << res->arch.asString();
+  
+    *_table << row;
+
+    return true;
+  }
+
   Table * _table;
 
   InstalledCache * _icache;
+
+  zypp::cache::ResolvableQuery * _query;
 };
 
 /**
index 57917c0..66679df 100644 (file)
@@ -1157,12 +1157,14 @@ int one_command(int argc, char **argv)
     cond_init_target();         // calls ZYpp::initializeTarget("/");
     
     establish();
-    
+
     Table t;
     t.style(Ascii);
 
-    ZyppSearch search(God,options,arguments);
-    search.doSearch(FillTable(t, search.installedCache()));
+    ZyppSearch search( God, options,arguments );
+    FillTable callback( t, search.installedCache(), search.getQueryInstancePtr() );
+
+    search.doSearch( callback, callback );
 
     if (t.empty())
       cout << _("No resolvables found.") << endl;
index 189fbb3..aa42de2 100644 (file)
@@ -11,7 +11,7 @@
 # norootforbuild
 
 Name:           @PACKAGE@
-BuildRequires:  libzypp-devel >= 3.15.0 boost-devel >= 1.33.1 gettext-devel >= 0.15 readline-devel >= 5.1
+BuildRequires:  libzypp-devel >= 3.16.0 boost-devel >= 1.33.1 gettext-devel >= 0.15 readline-devel >= 5.1
 BuildRequires:  gcc-c++ >= 4.2 cmake >= 2.4.6 pkg-config >= 0.20
 Requires:      procps
 License:        GPL