diff data/fileio/CachedFile.cpp @ 481:a82645e788fc

* Auto-select RDF datastore/parsing backend; use trees datastore if available * Make CachedFile remember whether a file has already been successfully located locally (avoiding system call out to look at filesystem)
author Chris Cannam
date Fri, 14 Nov 2008 10:10:05 +0000
parents 70b333085952
children e340b2fb9471
line wrap: on
line diff
--- a/data/fileio/CachedFile.cpp	Thu Nov 13 14:23:23 2008 +0000
+++ b/data/fileio/CachedFile.cpp	Fri Nov 14 10:10:05 2008 +0000
@@ -28,11 +28,18 @@
 #include <QDir>
 #include <QCryptographicHash>
 
+#include "base/Profiler.h"
+
 #include <iostream>
 
+CachedFile::OriginLocalFilenameMap
+CachedFile::m_knownGoodCaches;
+
 QString
 CachedFile::getLocalFilenameFor(QUrl url)
 {
+    Profiler p("CachedFile::getLocalFilenameFor");
+
     QDir dir(getCacheDirectory());
 
     QString filename =
@@ -63,10 +70,11 @@
 
 CachedFile::CachedFile(QString origin, ProgressReporter *reporter) :
     m_origin(origin),
-    m_localFilename(getLocalFilenameFor(m_origin)),
     m_reporter(reporter),
     m_ok(false)
 {
+    Profiler p("CachedFile::CachedFile[1]");
+
     std::cerr << "CachedFile::CachedFile: origin is \""
               << origin.toStdString() << "\"" << std::endl;
     check();
@@ -74,10 +82,11 @@
 
 CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) :
     m_origin(url.toString()),
-    m_localFilename(getLocalFilenameFor(m_origin)),
     m_reporter(reporter),
     m_ok(false)
 {
+    Profiler p("CachedFile::CachedFile[2]");
+
     std::cerr << "CachedFile::CachedFile: url is \""
               << url.toString().toStdString() << "\"" << std::endl;
     check();
@@ -105,7 +114,16 @@
     //!!! n.b. obvious race condition here if different CachedFile
     // objects for same url used in more than one thread -- need to
     // lock appropriately.  also consider race condition between
-    // separate instances of the program
+    // separate instances of the program!
+
+    OriginLocalFilenameMap::const_iterator i = m_knownGoodCaches.find(m_origin);
+    if (i != m_knownGoodCaches.end()) {
+        m_ok = true;
+        m_localFilename = i->second;
+        return;
+    }
+
+    m_localFilename = getLocalFilenameFor(m_origin);
 
     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;
@@ -149,6 +167,10 @@
             // retrieval timestamp is already invalid
         }
     }
+
+    if (m_ok) {
+        m_knownGoodCaches[m_origin] = m_localFilename;
+    }
 }
 
 bool