Support PoolQuery for sub-structures attributes. (fate #305503)
authorMichael Andres <ma@suse.de>
Wed, 1 Jul 2009 16:21:52 +0000 (18:21 +0200)
committerMichael Andres <ma@suse.de>
Wed, 1 Jul 2009 16:21:52 +0000 (18:21 +0200)
tests/sat/LookupAttr_test.cc
zypp/sat/LookupAttr.cc
zypp/sat/LookupAttr.h
zypp/sat/SolvAttr.cc
zypp/sat/SolvAttr.h

index 6ee003d..8b7ce75 100644 (file)
@@ -190,9 +190,9 @@ BOOST_AUTO_TEST_CASE(LookupAttr_solvable_attribute_substructure)
     // BOOST_CHECK_EQUAL( res.subFind( sat::SolvAttr::updateReferenceType ).subBegin(),  res.subBegin() );
   }
 
-  // search substructure id without parent-structure won't work:
-  q = sat::LookupAttr( sat::SolvAttr::updateReferenceId );
-  BOOST_CHECK_EQUAL( q.size(), 0 );
+  // search substructure id without parent-structure works for wellknown structures:
+  //   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId );
+  BOOST_CHECK_EQUAL( q.size(), 303 );
 
   // search id in parent-structure:
   q = sat::LookupAttr( sat::SolvAttr::updateReferenceId, sat::SolvAttr::updateReference );
index 71acfab..6f4cf80 100644 (file)
@@ -56,15 +56,16 @@ namespace zypp
     {
       public:
         Impl()
+        : _parent( SolvAttr::noAttr )
         {}
         Impl( SolvAttr attr_r, Location loc_r )
-        : _attr( attr_r ), _parent( SolvAttr::noAttr ), _solv( loc_r == REPO_ATTR ? SOLVID_META : noSolvableId )
+        : _attr( attr_r ), _parent( attr_r.parent() ), _solv( loc_r == REPO_ATTR ? SOLVID_META : noSolvableId )
         {}
         Impl( SolvAttr attr_r, Repository repo_r, Location loc_r )
-        : _attr( attr_r ), _parent( SolvAttr::noAttr ), _repo( repo_r ), _solv( loc_r == REPO_ATTR ? SOLVID_META : noSolvableId )
+        : _attr( attr_r ), _parent( attr_r.parent() ), _repo( repo_r ), _solv( loc_r == REPO_ATTR ? SOLVID_META : noSolvableId )
         {}
         Impl( SolvAttr attr_r, Solvable solv_r )
-        : _attr( attr_r ), _parent( SolvAttr::noAttr ), _solv( solv_r )
+        : _attr( attr_r ), _parent( attr_r.parent() ), _solv( solv_r )
         {}
 
       public:
@@ -72,7 +73,12 @@ namespace zypp
         { return _attr; }
 
         void setAttr( SolvAttr attr_r )
-        { _attr = attr_r; }
+        {
+          _attr = attr_r;
+          SolvAttr p( _attr.parent() );
+          if ( p != SolvAttr::noAttr )
+            _parent = p;
+        }
 
         const AttrMatcher & attrMatcher() const
         { return _attrMatcher; }
@@ -132,6 +138,7 @@ namespace zypp
           detail::DIWrap dip( whichRepo, _solv.id(), _attr.id(), _attrMatcher.searchstring(), _attrMatcher.flags().get() );
           if ( _parent != SolvAttr::noAttr )
             ::dataiterator_prepend_keyname( dip.get(), _parent.id() );
+
           return iterator( dip ); // iterator takes over ownership!
         }
 
@@ -314,6 +321,14 @@ namespace zypp
         {
           _dip = new ::Dataiterator;
           *_dip = *rhs._dip;
+          if ( _dip->nparents )
+          {
+            for ( int i = 1; i < _dip->nparents; ++i )
+            {
+              _dip->parents[i].kv.parent = &_dip->parents[i-1].kv;
+            }
+            _dip->kv.parent = &_dip->parents[_dip->nparents-1].kv;
+          }
           // now we have to manually clone any allocated regex data matcher.
           ::Datamatcher & matcher( _dip->matcher );
           if ( matcher.match && ( matcher.flags & SEARCH_STRINGMASK ) == SEARCH_REGEX )
index 5af9688..9b2ed28 100644 (file)
@@ -63,7 +63,9 @@ namespace zypp
      * To search for attributes located in a sub-structure (flexarray)
      * you also have to pass the sub-structures attribute as parent.
      * Passing \ref SolvAttr::allAttr a parent will lookup the attribute
-     * in \c any sub-structure.
+     * in \c any sub-structure. Few attributes are known to have a parent
+     * (\see \ref SolvAttr::parent). Setting those attributes will automatically
+     * initialize their parent value.
      *
      * \code
      *    // Lookup all 'name' attributes:
index 83a98d3..f43e4fb 100644 (file)
@@ -136,6 +136,30 @@ namespace sat
   const SolvAttr SolvAttr::repositoryRpmDbCookie      ( REPOSITORY_RPMDBCOOKIE );
   const SolvAttr SolvAttr::repositoryDeltaInfo        ( REPOSITORY_DELTAINFO );
 
+  /////////////////////////////////////////////////////////////////
+
+  SolvAttr SolvAttr::parent() const
+  {
+    switch( id() )
+    {
+      case UPDATE_COLLECTION_NAME:
+      case UPDATE_COLLECTION_EVR:
+      case UPDATE_COLLECTION_ARCH:
+      case UPDATE_COLLECTION_FILENAME:
+      case UPDATE_COLLECTION_FLAGS:
+        return updateCollection;
+        break;
+
+      case UPDATE_REFERENCE_TYPE:
+      case UPDATE_REFERENCE_HREF:
+      case UPDATE_REFERENCE_ID:
+      case UPDATE_REFERENCE_TITLE:
+        return updateReference;
+        break;
+    }
+    return noAttr;
+  }
+
 } // namespace sat
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
index a8d59f9..4e51594 100644 (file)
@@ -33,6 +33,8 @@ namespace sat
    * Attributes associated with individual solvables,
    * or with the repository as a whole.
    *
+   * \note If you add well known subsructure attributes, update \ref parent.
+   *
    * \see \ref LookupAttr
    */
   class SolvAttr : public IdStringType<SolvAttr>
@@ -109,17 +111,17 @@ namespace sat
       static const SolvAttr restartSuggested;
       static const SolvAttr reloginSuggested;
       static const SolvAttr message;
-      static const SolvAttr updateCollection;
-      static const SolvAttr updateCollectionName;
-      static const SolvAttr updateCollectionEvr;
-      static const SolvAttr updateCollectionArch;
-      static const SolvAttr updateCollectionFilename;
-      static const SolvAttr updateCollectionFlags;
-      static const SolvAttr updateReference;
-      static const SolvAttr updateReferenceType;
-      static const SolvAttr updateReferenceHref;
-      static const SolvAttr updateReferenceId;
-      static const SolvAttr updateReferenceTitle;
+      static const SolvAttr updateCollection;          // SUB-STRUCTURE:
+      static const SolvAttr updateCollectionName;      // name
+      static const SolvAttr updateCollectionEvr;       // evr
+      static const SolvAttr updateCollectionArch;      // arch
+      static const SolvAttr updateCollectionFilename;  // filename
+      static const SolvAttr updateCollectionFlags;     // flags
+      static const SolvAttr updateReference;           // SUB-STRUCTURE:
+      static const SolvAttr updateReferenceType;       // type
+      static const SolvAttr updateReferenceHref;       // href
+      static const SolvAttr updateReferenceId;         // id
+      static const SolvAttr updateReferenceTitle;      // title
       //@}
 
       /** \name pattern */
@@ -176,6 +178,16 @@ namespace sat
       explicit SolvAttr( const std::string & str_r ) : _str( str_r ) {}
       explicit SolvAttr( const char * cstr_r )       : _str( cstr_r ) {}
 
+      /** Return the parent of well know sub-structure attributes (\ref SolvAttr::noAttr if none).
+       * \li \ref updateCollection
+       * \li \ref updateReference
+       */
+      SolvAttr parent() const;
+
+      /** Whether this is a well know sub-structure attribute. */
+      bool hasParent() const
+      { return parent() != noAttr; }
+
     private:
       friend class IdStringType<SolvAttr>;
       IdString _str;