add mirrorlist caching:
authorDominik Heidler <dheidler@suse.de>
Tue, 30 Nov 2010 16:06:38 +0000 (17:06 +0100)
committerDominik Heidler <dheidler@suse.de>
Tue, 30 Nov 2010 16:09:33 +0000 (17:09 +0100)
the mirrorlist will be cached until the repo_refresh_delay time is over

zypp/RepoInfo.cc
zypp/repo/RepoMirrorList.cc
zypp/repo/RepoMirrorList.h

index c86b19f..4574595 100644 (file)
@@ -80,8 +80,16 @@ namespace zypp
       if ( _baseUrls.empty() && ! (getmirrorListUrl().asString().empty()) )
       {
         emptybaseurls = true;
-        repo::RepoMirrorList rmirrorlist (getmirrorListUrl());
-        std::vector<Url> rmurls = rmirrorlist.getUrls();
+        repo::RepoMirrorList *rmirrorlist = NULL;
+
+        if( metadatapath.empty() )
+          rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl() );
+        else
+          rmirrorlist = new repo::RepoMirrorList (getmirrorListUrl(), metadatapath );
+
+        std::vector<Url> rmurls = rmirrorlist->getUrls();
+        delete rmirrorlist;
+        rmirrorlist = NULL;
         _baseUrls.insert(rmurls.begin(), rmurls.end());
       }
       return _baseUrls;
index 750b607..e212edf 100644 (file)
 
 #include <iostream>
 #include <vector>
+#include <time.h>
 #include "zypp/repo/RepoMirrorList.h"
 #include "zypp/media/MetaLinkParser.h"
 #include "zypp/MediaSetAccess.h"
 #include "zypp/base/LogTools.h"
+#include "zypp/ZConfig.h"
+#include "zypp/PathInfo.h"
 
 using namespace std;
 
@@ -26,11 +29,54 @@ namespace zypp
   namespace repo
   { /////////////////////////////////////////////////////////////////
 
+    RepoMirrorList::RepoMirrorList( const Url &url, const Pathname &metadatapath )
+    {
+      std::vector<Url> my_urls;
+      Pathname tmpfile, cachefile;
+
+      if ( url.asString().find("/metalink") != string::npos )
+        cachefile = metadatapath / "mirrorlist.xml";
+      else
+        cachefile = metadatapath / "mirrorlist.txt";
+        //cachefile = ZConfig::instance().repoMetadataPath() / Pathname(escaped_alias) / "mirrorlist.txt";
+
+      zypp::filesystem::PathInfo cacheinfo (cachefile);
+
+      if ( !cacheinfo.isFile() || cacheinfo.mtime() < time(NULL) - (long) ZConfig::instance().repo_refresh_delay() * 60 )
+      {
+        Pathname filepath (url.getPathName());
+        Url abs_url (url);
+
+        DBG << "Getting MirrorList from URL: " << abs_url << endl;
+
+        abs_url.setPathName("");
+        abs_url.setQueryParam("mediahandler", "curl");
+
+        MediaSetAccess access (abs_url);
+        tmpfile = access.provideFile(filepath);
+
+        zypp::filesystem::copy(tmpfile, cachefile);
+      }
+
+      if ( url.asString().find("/metalink") != string::npos )
+      {
+        my_urls = parseXML(cachefile);
+      }
+      else
+      {
+        my_urls = parseTXT(cachefile);
+      }
+
+      setUrls( my_urls );
+    }
+
     RepoMirrorList::RepoMirrorList( const Url &url )
     {
+      std::vector<Url> my_urls;
+      Pathname tmpfile;
+
       Pathname filepath (url.getPathName());
       Url abs_url (url);
-      std::vector<Url> my_urls;
 
       DBG << "Getting MirrorList from URL: " << abs_url << endl;
 
@@ -38,25 +84,22 @@ namespace zypp
       abs_url.setQueryParam("mediahandler", "curl");
 
       MediaSetAccess access (abs_url);
-      Pathname tmpfile = access.provideFile(filepath);
-
-      InputStream tmpfstream (tmpfile);
+      tmpfile = access.provideFile(filepath);
 
       if ( url.asString().find("/metalink") != string::npos )
       {
-        media::MetaLinkParser metalink;
-        metalink.parse(tmpfstream);
-        my_urls = metalink.getUrls();
+        my_urls = parseXML(tmpfile);
       }
       else
       {
-        string tmpurl;
-        while (getline(tmpfstream.stream(), tmpurl))
-        {
-          my_urls.push_back(Url(tmpurl));
-        }
+        my_urls = parseTXT(tmpfile);
       }
 
+      setUrls( my_urls );
+    }
+
+    void RepoMirrorList::setUrls( std::vector<Url> my_urls )
+    {
       int valid_urls = 0;
       for (std::vector<Url>::iterator it = my_urls.begin() ; it != my_urls.end() and valid_urls < 4 ; ++it)
       {
@@ -72,6 +115,26 @@ namespace zypp
         }
       }
     }
+
+    std::vector<Url> RepoMirrorList::parseXML( const Pathname &tmpfile ) const
+    {
+      InputStream tmpfstream (tmpfile);
+      media::MetaLinkParser metalink;
+      metalink.parse(tmpfstream);
+      return metalink.getUrls();
+    }
+
+    std::vector<Url> RepoMirrorList::parseTXT( const Pathname &tmpfile ) const
+    {
+      InputStream tmpfstream (tmpfile);
+      std::vector<Url> my_urls;
+      string tmpurl;
+      while (getline(tmpfstream.stream(), tmpurl))
+      {
+        my_urls.push_back(Url(tmpurl));
+      }
+      return my_urls;
+    }
     
     std::vector<Url> RepoMirrorList::getUrls() const
     {
index 49a0efd..dfc2ca6 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <vector>
 #include "zypp/Url.h"
+#include "zypp/Pathname.h"
 
 namespace zypp
 {
@@ -21,12 +22,16 @@ namespace zypp
     {
       public:
         RepoMirrorList( const Url &url );
+        RepoMirrorList( const Url &url, const Pathname &metadatapath );
         virtual ~RepoMirrorList();
         
         std::vector<Url> getUrls() const;
 
       private:
         std::vector<Url> urls;
+        void setUrls( std::vector<Url> my_urls );
+        std::vector<Url> parseXML( const Pathname &tmpfile ) const;
+        std::vector<Url> parseTXT( const Pathname &tmpfile ) const;
     };
 
   } // ns repo