- enable per-attribute string setting (same value)
authorJan Kupec <jkupec@suse.cz>
Thu, 10 Apr 2008 11:12:26 +0000 (11:12 +0000)
committerJan Kupec <jkupec@suse.cz>
Thu, 10 Apr 2008 11:12:26 +0000 (11:12 +0000)
devel/devel.jkupec/CMakeLists.txt
devel/devel.jkupec/poolquery.cc [new file with mode: 0644]
tests/zypp/PoolQuery_test.cc
zypp/PoolQuery.cc

index 8345b99..01c33b8 100644 (file)
@@ -1,9 +1,7 @@
-ADD_DEFINITIONS(-DSRC_DIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}\\\")
-
-#ADD_EXECUTABLE(sourcedownload sourcedownload.cc)
-#TARGET_LINK_LIBRARIES(sourcedownload zypp zypp2)
-
-
+ADD_DEFINITIONS(
+  -DSRC_DIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}\\\"
+  -DTESTS_SRC_DIR=\\\"${CMAKE_SOURCE_DIR}/tests\\\"
+)
 
 SET( bridge_SRCS
   bridge/bridge.cc
@@ -17,8 +15,11 @@ SET( bridge_HEADERS
   bridge/Derived.h
 )
 
-ADD_EXECUTABLE( yumparsertest YUMParser_test.cc )
-TARGET_LINK_LIBRARIES(yumparsertest zypp zypp2)
+#ADD_EXECUTABLE( yumparsertest YUMParser_test.cc )
+#TARGET_LINK_LIBRARIES( yumparsertest zypp )
 
 ADD_EXECUTABLE( bridge ${bridge_SRCS} )
-TARGET_LINK_LIBRARIES( bridge zypp zypp2)
+TARGET_LINK_LIBRARIES( bridge zypp )
+
+ADD_EXECUTABLE( poolquery poolquery.cc )
+TARGET_LINK_LIBRARIES( poolquery zypp )
diff --git a/devel/devel.jkupec/poolquery.cc b/devel/devel.jkupec/poolquery.cc
new file mode 100644 (file)
index 0000000..50a6d04
--- /dev/null
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <iostream>
+#include <iterator>
+#include <list>
+
+#include "zypp/ZYppFactory.h"
+#include "zypp/PoolQuery.h"
+#include "zypp/PoolQueryUtil.tcc"
+#include "zypp/RepoInfo.h"
+#include "zypp/Arch.h"
+#include "zypp/Pathname.h"
+
+using std::cout;
+using std::endl;
+using std::string;
+using namespace zypp;
+
+
+bool result_cb( const sat::Solvable & solvable )
+{
+  zypp::PoolItem pi( zypp::ResPool::instance().find( solvable ) );
+  cout << pi.resolvable() << endl;
+  // name: yast2-sound 2.16.2-9 i586
+  return true;
+}
+
+
+static void init_pool()
+{
+  Pathname dir(TESTS_SRC_DIR);
+  dir += "/zypp/data/PoolQuery";
+
+  ZYpp::Ptr z = getZYpp();
+  ZConfig::instance().setSystemArchitecture(Arch("i586"));
+
+  RepoInfo i1; i1.setAlias("factory");
+  sat::Pool::instance().addRepoSolv(dir / "factory.solv", i1);
+  RepoInfo i2; i2.setAlias("factory-nonoss");
+  sat::Pool::instance().addRepoSolv(dir / "factory-nonoss.solv", i2);
+  RepoInfo i3; i3.setAlias("zypp_svn");
+  sat::Pool::instance().addRepoSolv(dir / "zypp_svn.solv", i3);
+  RepoInfo i4; i4.setAlias("@System");
+  sat::Pool::instance().addRepoSolv(dir / "@System.solv", i4);
+}
+
+
+int main (int argc, const char ** argv)
+{
+  init_pool();
+
+  PoolQuery q;
+  //q.addString("novell");
+  q.addAttribute(sat::SolvAttr::name, "novell");
+  q.addAttribute(sat::SolvAttr::summary, "novell");
+
+  std::for_each(q.begin(), q.end(), &result_cb);
+
+  cout << q.size() << endl;
+}
index 5df250d..895cb99 100644 (file)
@@ -247,6 +247,7 @@ BOOST_AUTO_TEST_CASE(pool_query_050)
   BOOST_CHECK(q1.size() == 5);
 }
 
+
 /////////////////////////////////////////////////////////////////////////////
 //  1xx multiple attribute queries
 /////////////////////////////////////////////////////////////////////////////
@@ -279,6 +280,7 @@ BOOST_AUTO_TEST_CASE(pool_query_100)
   BOOST_CHECK(q1.size() == 7);
 }
 
+
 // multi attr (same value) substring matching (case sensitive and insensitive)
 BOOST_AUTO_TEST_CASE(pool_query_101)
 {
@@ -322,6 +324,30 @@ BOOST_AUTO_TEST_CASE(pool_query_102)
 }
 
 
+// multi attr (same value via addAttribute())
+BOOST_AUTO_TEST_CASE(pool_query_103)
+{
+  cout << "****103.1****"  << endl;
+  PoolQuery q;
+  q.addAttribute(sat::SolvAttr::name, "novell");
+  q.addAttribute(sat::SolvAttr::summary, "novell");
+
+//  std::for_each(q.begin(), q.end(), &result_cb);
+  BOOST_CHECK(q.size() == 42);
+
+  cout << "****103.2****"  << endl;
+
+  PoolQuery q1;
+  q1.addString("novell");
+  q1.addAttribute(sat::SolvAttr::name);
+  q1.addAttribute(sat::SolvAttr::summary);
+
+//  std::for_each(q1.begin(), q1.end(), &result_cb);
+  BOOST_CHECK(q1.size() == 42);
+
+  cout << endl;
+}
+
 /////////////////////////////////////////////////////////////////////////////
 //  3xx repo filter queries (addRepo(alias_str))
 /////////////////////////////////////////////////////////////////////////////
index 605ff55..a9b3f67 100644 (file)
@@ -68,10 +68,12 @@ namespace zypp
     StrContainer _strings;
     /** Regex-compiled search strings. */
     mutable string _rcstrings;
+    mutable regex_t _regex;
     /** Raw attributes */
     AttrMap _attrs;
     /** Regex-compiled attributes */
     mutable CompiledAttrMap _rcattrs;
+    mutable map<sat::SolvAttr, regex_t> _rattrs;
 
     /** Repos to search. */
     StrContainer _repos;
@@ -104,25 +106,31 @@ namespace zypp
     Impl * clone() const
     { return new Impl( *this ); }
   };
-/*
-  template <class _OutputIterator>
-  struct CollectNonEmpty
-  {
-    CollectNonEmpty( _OutputIterator iter_r ) : _iter( iter_r ) {}
 
-    template<class _Tp>
-    bool operator()( const _Tp & value_r ) const
+  
+  struct MyInserter
+  {
+    MyInserter(PoolQuery::StrContainer & cont) : _cont(cont) {}
+    
+    bool operator()(const string & str)
     {
-      if (value_r.empty())
-        return true;
-      *_iter++ = value_r;
+      _cont.insert(str);
       return true;
     }
+    
+    PoolQuery::StrContainer & _cont;
+  };
 
-    private:
-      mutable _OutputIterator _iter;
+  
+  struct EmptyFilter
+  {
+    bool operator()(const string & str)
+    {
+      return !str.empty();
+    }
   };
-*/
+
+
   void PoolQuery::Impl::compile() const
   {
     _cflags = _flags;
@@ -150,20 +158,16 @@ namespace zypp
     else if (_attrs.size() == 1)
     {
       StrContainer joined;
-      for(StrContainer::const_iterator it = _strings.begin(); it != _strings.end(); ++it)
-        if (!it->empty())
-          joined.insert(*it);
-      for(StrContainer::const_iterator it = _attrs.begin()->second.begin(); it != _attrs.begin()->second.end(); ++it)
-        if (!it->empty())
-          joined.insert(*it);
+      invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
+      invokeOnEach(_attrs.begin()->second.begin(), _attrs.begin()->second.end(), EmptyFilter(), MyInserter(joined));
       _rcstrings = createRegex(joined);
       _rcattrs.insert(pair<sat::SolvAttr, string>(_attrs.begin()->first, string()));
     }
 
-
     // // MULTIPLE ATTRIBUTES
     else
     {
+      // check whether there are any per-attribute strings 
       bool attrvals_empty = true;
       for (AttrMap::const_iterator ai = _attrs.begin(); ai != _attrs.end(); ++ai)
         if (!ai->second.empty())
@@ -177,10 +181,24 @@ namespace zypp
 
 attremptycheckend:
 
+      // chceck whether the per-attribute strings are all the same
       bool attrvals_thesame = true;
-      for (AttrMap::const_iterator ai = _attrs.begin(); ai != _attrs.end(); ++ai)
+      AttrMap::const_iterator ai = _attrs.begin();
+      const StrContainer & set1 = ai->second;
+      ++ai;
+      for (; ai != _attrs.end(); ++ai)
       {
-        
+        StrContainer result;
+        set_difference(
+          set1.begin(), set1.end(),
+          ai->second.begin(), ai->second.end(),
+          inserter(result, result.begin())/*, ltstr()*/);
+        if (!result.empty())
+        {
+          copy(result.begin(), result.end(), ostream_iterator<string>(cout, " "));
+          attrvals_thesame = false;
+          break;
+        }
       }
 
       // // THE SAME STRINGS FOR DIFFERENT ATTRS
@@ -189,19 +207,21 @@ attremptycheckend:
       //     create regex; store in _rcattrs and _rcstrings; flag 'same'; if more strings flag regex;
       if (attrvals_empty || attrvals_thesame)
       {
+        StrContainer joined;
         if (attrvals_empty)
         {
-          // compile the search string
-          StrContainer joined;
-          for(StrContainer::const_iterator it = _strings.begin(); it != _strings.end(); ++it)
-            if (!it->empty())
-              joined.insert(*it);
+          invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
+          _rcstrings = createRegex(joined);
+        }
+        else
+        {
+          invokeOnEach(_strings.begin(), _strings.end(), EmptyFilter(), MyInserter(joined));
+          invokeOnEach(_attrs.begin()->second.begin(), _attrs.begin()->second.end(), EmptyFilter(), MyInserter(joined));
           _rcstrings = createRegex(joined);
-
-          // copy the _attrs keys to _rcattrs
-          for (AttrMap::const_iterator ai = _attrs.begin(); ai != _attrs.end(); ++ai)
-            _rcattrs.insert(pair<sat::SolvAttr, string>(ai->first, string()));
         }
+        // copy the _attrs keys to _rcattrs
+        for_(ai, _attrs.begin(), _attrs.end())
+          _rcattrs.insert(pair<sat::SolvAttr, string>(ai->first, string()));
       }
       // // DIFFERENT STRINGS FOR DIFFERENT ATTRS
       // if _attrs is not empty and it contains non-empty vectors with non-empty strings
@@ -209,7 +229,9 @@ attremptycheckend:
       //     create regex; store in _rcattrs; flag 'different'; if more strings flag regex;
       else
       {
-        
+        /*          set_union(A.begin(), A.end(), B.begin(), B.end(),
+                             ostream_iterator<const char*>(cout, " "),
+                             ltstr());*/
       }
     }