comparison 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
comparison
equal deleted inserted replaced
480:3ffce691c9bf 481:a82645e788fc
26 #include <QVariant> 26 #include <QVariant>
27 #include <QMap> 27 #include <QMap>
28 #include <QDir> 28 #include <QDir>
29 #include <QCryptographicHash> 29 #include <QCryptographicHash>
30 30
31 #include "base/Profiler.h"
32
31 #include <iostream> 33 #include <iostream>
34
35 CachedFile::OriginLocalFilenameMap
36 CachedFile::m_knownGoodCaches;
32 37
33 QString 38 QString
34 CachedFile::getLocalFilenameFor(QUrl url) 39 CachedFile::getLocalFilenameFor(QUrl url)
35 { 40 {
41 Profiler p("CachedFile::getLocalFilenameFor");
42
36 QDir dir(getCacheDirectory()); 43 QDir dir(getCacheDirectory());
37 44
38 QString filename = 45 QString filename =
39 QString::fromLocal8Bit 46 QString::fromLocal8Bit
40 (QCryptographicHash::hash(url.toString().toLocal8Bit(), 47 (QCryptographicHash::hash(url.toString().toLocal8Bit(),
61 return fi.filePath(); 68 return fi.filePath();
62 } 69 }
63 70
64 CachedFile::CachedFile(QString origin, ProgressReporter *reporter) : 71 CachedFile::CachedFile(QString origin, ProgressReporter *reporter) :
65 m_origin(origin), 72 m_origin(origin),
66 m_localFilename(getLocalFilenameFor(m_origin)),
67 m_reporter(reporter), 73 m_reporter(reporter),
68 m_ok(false) 74 m_ok(false)
69 { 75 {
76 Profiler p("CachedFile::CachedFile[1]");
77
70 std::cerr << "CachedFile::CachedFile: origin is \"" 78 std::cerr << "CachedFile::CachedFile: origin is \""
71 << origin.toStdString() << "\"" << std::endl; 79 << origin.toStdString() << "\"" << std::endl;
72 check(); 80 check();
73 } 81 }
74 82
75 CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) : 83 CachedFile::CachedFile(QUrl url, ProgressReporter *reporter) :
76 m_origin(url.toString()), 84 m_origin(url.toString()),
77 m_localFilename(getLocalFilenameFor(m_origin)),
78 m_reporter(reporter), 85 m_reporter(reporter),
79 m_ok(false) 86 m_ok(false)
80 { 87 {
88 Profiler p("CachedFile::CachedFile[2]");
89
81 std::cerr << "CachedFile::CachedFile: url is \"" 90 std::cerr << "CachedFile::CachedFile: url is \""
82 << url.toString().toStdString() << "\"" << std::endl; 91 << url.toString().toStdString() << "\"" << std::endl;
83 check(); 92 check();
84 } 93 }
85 94
103 CachedFile::check() 112 CachedFile::check()
104 { 113 {
105 //!!! n.b. obvious race condition here if different CachedFile 114 //!!! n.b. obvious race condition here if different CachedFile
106 // objects for same url used in more than one thread -- need to 115 // objects for same url used in more than one thread -- need to
107 // lock appropriately. also consider race condition between 116 // lock appropriately. also consider race condition between
108 // separate instances of the program 117 // separate instances of the program!
118
119 OriginLocalFilenameMap::const_iterator i = m_knownGoodCaches.find(m_origin);
120 if (i != m_knownGoodCaches.end()) {
121 m_ok = true;
122 m_localFilename = i->second;
123 return;
124 }
125
126 m_localFilename = getLocalFilenameFor(m_origin);
109 127
110 if (!QFileInfo(m_localFilename).exists()) { 128 if (!QFileInfo(m_localFilename).exists()) {
111 std::cerr << "CachedFile::check: Local file does not exist, making a note that it hasn't been retrieved" << std::endl; 129 std::cerr << "CachedFile::check: Local file does not exist, making a note that it hasn't been retrieved" << std::endl;
112 updateLastRetrieval(false); // empirically! 130 updateLastRetrieval(false); // empirically!
113 } 131 }
147 std::cerr << "CachedFile::check: Retrieval failed, remaining in invalid state" << std::endl; 165 std::cerr << "CachedFile::check: Retrieval failed, remaining in invalid state" << std::endl;
148 // again, we don't need to do anything here -- the last 166 // again, we don't need to do anything here -- the last
149 // retrieval timestamp is already invalid 167 // retrieval timestamp is already invalid
150 } 168 }
151 } 169 }
170
171 if (m_ok) {
172 m_knownGoodCaches[m_origin] = m_localFilename;
173 }
152 } 174 }
153 175
154 bool 176 bool
155 CachedFile::retrieve() 177 CachedFile::retrieve()
156 { 178 {