changeset 467:c9b055f84326

* make use of CachedFile (untested)
author Chris Cannam
date Mon, 27 Oct 2008 18:15:20 +0000
parents f35bfa88f0b5
children 70b333085952
files data/fileio/CachedFile.cpp data/fileio/CachedFile.h rdf/PluginRDFIndexer.cpp rdf/PluginRDFIndexer.h
diffstat 4 files changed, 59 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/CachedFile.cpp	Mon Oct 27 15:07:35 2008 +0000
+++ b/data/fileio/CachedFile.cpp	Mon Oct 27 18:15:20 2008 +0000
@@ -61,13 +61,30 @@
     return fi.filePath();
 }
 
-CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) :
+CachedFile::CachedFile(QString url, ProgressReporter *reporter) :
     m_url(url),
-    m_localFilename(getLocalFilenameFor(url)),
+    m_localFilename(getLocalFilenameFor(m_url)),
     m_reporter(reporter),
     m_ok(false)
 {
-    refresh();
+    std::cerr << "CachedFile::CachedFile: url is \""
+              << url.toStdString() << "\"" << std::endl;
+    check();
+}
+
+CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) :
+    m_url(url),
+    m_localFilename(getLocalFilenameFor(m_url)),
+    m_reporter(reporter),
+    m_ok(false)
+{
+    std::cerr << "CachedFile::CachedFile: url is \""
+              << url.toString().toStdString() << "\"" << std::endl;
+    check();
+}
+
+CachedFile::~CachedFile()
+{
 }
 
 bool
@@ -83,7 +100,7 @@
 }
 
 void
-CachedFile::refresh()
+CachedFile::check()
 {
     //!!! n.b. obvious race condition here if different CachedFile
     // objects for same url used in more than one thread -- need to
@@ -91,16 +108,20 @@
     // separate instances of the program
 
     if (!QFileInfo(m_localFilename).exists()) {
+        std::cerr << "CachedFile::check: Local file does not exist, making a note that it hasn't been retrieved" << std::endl;
         updateLastRetrieval(false); // empirically!
     }
 
     QDateTime lastRetrieval = getLastRetrieval();
 
     if (lastRetrieval.isValid()) {
+        std::cerr << "CachedFile::check: Valid last retrieval at "
+                  << lastRetrieval.toString().toStdString() << std::endl;
         // this will not be the case if the file is missing, after
         // updateLastRetrieval(false) was called above
         m_ok = true;
         if (lastRetrieval.addDays(2) < QDateTime::currentDateTime()) { //!!!
+            std::cerr << "CachedFile::check: Out of date; trying to retrieve again" << std::endl;
             // doesn't matter if retrieval fails -- we just don't
             // update the last retrieval time
 
@@ -109,15 +130,21 @@
             // retrieval every single time if it isn't working
 
             if (retrieve()) {
+                std::cerr << "CachedFile::check: Retrieval succeeded" << std::endl;
                 updateLastRetrieval(true);
-            }
+            } else {
+                std::cerr << "CachedFile::check: Retrieval failed, will try again later (using existing file for now)" << std::endl;
+            }                
         }
     } else {
+        std::cerr << "CachedFile::check: No valid last retrieval" << std::endl;
         // there is no acceptable file
         if (retrieve()) {
+            std::cerr << "CachedFile::check: Retrieval succeeded" << std::endl;
             m_ok = true;
             updateLastRetrieval(true);
         } else {
+            std::cerr << "CachedFile::check: Retrieval failed, remaining in invalid state" << std::endl;
             // again, we don't need to do anything here -- the last
             // retrieval timestamp is already invalid
         }
--- a/data/fileio/CachedFile.h	Mon Oct 27 15:07:35 2008 +0000
+++ b/data/fileio/CachedFile.h	Mon Oct 27 18:15:20 2008 +0000
@@ -25,6 +25,7 @@
 class CachedFile
 {
 public:
+    CachedFile(QString fileOrUrl, ProgressReporter *reporter = 0);
     CachedFile(QUrl url, ProgressReporter *reporter = 0);
 
     virtual ~CachedFile();
@@ -39,7 +40,7 @@
     ProgressReporter *m_reporter;
     bool m_ok;
 
-    void refresh();
+    void check();
     bool retrieve();
 
     QDateTime getLastRetrieval();
--- a/rdf/PluginRDFIndexer.cpp	Mon Oct 27 15:07:35 2008 +0000
+++ b/rdf/PluginRDFIndexer.cpp	Mon Oct 27 18:15:20 2008 +0000
@@ -17,7 +17,8 @@
 
 #include "SimpleSPARQLQuery.h"
 
-#include "data/fileio/FileSource.h"
+//!!!#include "data/fileio/FileSource.h"
+#include "data/fileio/CachedFile.h"
 #include "data/fileio/PlaylistFileReader.h"
 #include "plugin/PluginIdentifier.h"
 
@@ -100,11 +101,12 @@
 PluginRDFIndexer::~PluginRDFIndexer()
 {
     QMutexLocker locker(&m_mutex);
-
+/*!!!
     while (!m_sources.empty()) {
         delete *m_sources.begin();
         m_sources.erase(m_sources.begin());
     }
+*/
 }
 
 bool
@@ -125,11 +127,18 @@
         std::cerr << "PluginRDFIndexer::indexConfiguredURLs: index url is "
                   << index.toStdString() << std::endl;
 
+/*!!!
         expireCacheMaybe(index);
 
         FileSource indexSource(index, 0, FileSource::PersistentCache);
         if (!indexSource.isAvailable()) continue;
         indexSource.waitForData();
+*/
+
+        CachedFile cf(index);
+        if (!cf.isOK()) continue;
+
+        FileSource indexSource(cf.getLocalFilename());
 
         PlaylistFileReader reader(indexSource);
         if (!reader.isOK()) continue;
@@ -230,7 +239,7 @@
     QString urlString = url.toString();
     return indexURL(urlString);
 }
-
+/*!!!
 void
 PluginRDFIndexer::expireCacheMaybe(QString urlString)
 {
@@ -266,7 +275,7 @@
 
     settings.endGroup();
 }
-
+*/
 bool
 PluginRDFIndexer::indexURL(QString urlString)
 {
@@ -284,6 +293,13 @@
         //!!! how do we avoid hammering the server if it doesn't have
         //!!! the file, and/or the network if it can't get through?
 
+        CachedFile cf(urlString);
+        if (!cf.isOK()) {
+            return false;
+        }
+
+        localString = cf.getLocalFilename();
+/*!!!
         expireCacheMaybe(urlString);
 
         FileSource *source = new FileSource
@@ -295,6 +311,7 @@
         source->waitForData();
         localString = QUrl::fromLocalFile(source->getLocalFilename()).toString();
         m_sources.insert(source);
+*/
     }
 
 //    cerr << "PluginRDFIndexer::indexURL: url = <" << urlString.toStdString() << ">" << endl;
--- a/rdf/PluginRDFIndexer.h	Mon Oct 27 15:07:35 2008 +0000
+++ b/rdf/PluginRDFIndexer.h	Mon Oct 27 18:15:20 2008 +0000
@@ -22,7 +22,7 @@
 #include <map>
 #include <set>
 
-class FileSource;
+//!!!class FileSource;
 
 class PluginRDFIndexer
 {
@@ -55,14 +55,16 @@
 protected:
     PluginRDFIndexer();
     QMutex m_mutex;
+/*!!!
     std::set<FileSource *> m_sources;
+*/
     typedef std::map<QString, QString> StringMap;
     StringMap m_uriToIdMap;
     StringMap m_idToUriMap;
     StringMap m_idToDescriptionMap;
     bool indexFile(QString path);
     static PluginRDFIndexer *m_instance;
-    void expireCacheMaybe(QString);
+//!!!    void expireCacheMaybe(QString);
 };
 
 #endif