| 89 |
89 |
}
|
| 90 |
90 |
updateIndex();
|
| 91 |
91 |
|
|
92 |
QSet<Uri> fileUris;
|
|
93 |
|
|
94 |
// The same file may be referred to with more than one URI; we
|
|
95 |
// want to load any or all of: the URI in our file object; encoded
|
|
96 |
// version of same; and any other file that is recorded as having
|
|
97 |
// the same hash (i.e. it is the same file).
|
|
98 |
|
|
99 |
fileUris.insert(tf->uri());
|
|
100 |
|
|
101 |
// and again with encoded version of file URI
|
|
102 |
QByteArray enc = QUrl(tf->uri().toString()).toEncoded();
|
|
103 |
fileUris.insert(Uri(QString::fromUtf8(enc)));
|
|
104 |
|
|
105 |
// and again with anything else having the same hash
|
|
106 |
if (tf->hash() != "") {
|
|
107 |
Triple pattern(Node(), "foaf:sha1", Node(Node::Literal, tf->hash()));
|
|
108 |
Triples results = m_index->match(pattern);
|
|
109 |
std::cerr << "FeatureFileIndex::loadFor: " << results.size() << " audio file(s) found with hash " << tf->hash().toStdString() << std::endl;
|
|
110 |
foreach (Triple t, results) {
|
|
111 |
fileUris.insert(Uri(t.a.value));
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
foreach (Uri u, fileUris) {
|
|
116 |
loadFor(tf->uri(), u, tf->hash(), store);
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
bool
|
|
121 |
FeatureFileIndex::loadFor(Uri canonicalUri, Uri afuri,
|
|
122 |
QString hash, Store *store)
|
|
123 |
{
|
| 92 |
124 |
// The AudioFile object has a URI and a hash. Feature files
|
| 93 |
125 |
// generated for this AudioFile should ideally have a matching
|
| 94 |
126 |
// hash; if they have no hash, then the URI should match. If the
|
| 95 |
127 |
// hash is present in the feature file but does not match, then it
|
| 96 |
128 |
// cannot be the right track even if the URI matches.
|
| 97 |
129 |
|
| 98 |
|
Triple t(Node(), "foaf:primaryTopic", tf->uri());
|
| 99 |
|
Triples results = m_index->match(t);
|
| 100 |
|
std::cerr << "FeatureFileIndex::loadFor: " << results.size() << " feature file(s) for audio file " << tf->uri() << std::endl;
|
|
130 |
Triple pattern(Node(), "foaf:primaryTopic", afuri);
|
|
131 |
Triples results = m_index->match(pattern);
|
|
132 |
std::cerr << "FeatureFileIndex::loadFor: " << results.size() << " feature file(s) for audio file " << afuri << std::endl;
|
| 101 |
133 |
|
| 102 |
|
t = Triple(Node(), "foaf:primaryTopic",
|
| 103 |
|
Uri(QString::fromUtf8(QUrl(tf->uri().toString()).toEncoded())));
|
| 104 |
|
Triples moreResults = m_index->match(t);
|
| 105 |
|
std::cerr << "FeatureFileIndex::loadFor: " << moreResults.size() << " feature file(s) for audio file " << t.c << std::endl;
|
|
134 |
bool loadedSomething = false;
|
| 106 |
135 |
|
| 107 |
|
//!!! what's the right approach here?
|
| 108 |
|
|
| 109 |
|
if (results.empty() && moreResults.empty()) {
|
| 110 |
|
return;
|
|
136 |
foreach (Triple t, results) {
|
|
137 |
try {
|
|
138 |
BasicStore *b = BasicStore::load(QUrl(t.a.value));
|
|
139 |
Triples ts = b->match
|
|
140 |
(Triple(afuri, "a", m_index->expand("mo:AudioFile")));
|
|
141 |
std::cerr << "FeatureFileIndex::loadFor: feature file "
|
|
142 |
<< t.a << " has " << ts.size() << " type node(s) for this audio file" << std::endl;
|
|
143 |
bool someGood = false;
|
|
144 |
foreach (Triple t, ts) {
|
|
145 |
bool good = true;
|
|
146 |
if (hash != "") {
|
|
147 |
Triples hashts = b->match
|
|
148 |
(Triple(afuri, m_index->expand("foaf:sha1"), Node()));
|
|
149 |
std::cerr << "FeatureFileIndex::loadFor: feature file "
|
|
150 |
<< t.a << " has " << hashts.size() << " hashes for this file" << std::endl;
|
|
151 |
if (!hashts.empty()) {
|
|
152 |
good = false;
|
|
153 |
foreach (Triple hasht, hashts) {
|
|
154 |
if (hasht.c.value == hash) {
|
|
155 |
std::cerr << "Hash " << hasht.c << " matches our hash " << hash.toStdString() << std::endl;
|
|
156 |
good = true;
|
|
157 |
break;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
if (!good) {
|
|
161 |
std::cerr << "(no hash matches, eliminating file)" << std::endl;
|
|
162 |
}
|
|
163 |
} else {
|
|
164 |
std::cerr << "(so cannot eliminate file via hash)" << std::endl;
|
|
165 |
}
|
|
166 |
}
|
|
167 |
if (good) {
|
|
168 |
std::cerr << "...going to import this one" << std::endl;
|
|
169 |
someGood = true;
|
|
170 |
}
|
|
171 |
}
|
|
172 |
if (someGood) {
|
|
173 |
Triples all = b->match(Triple());
|
|
174 |
std::cerr << "Importing " << all.size() << " triple(s) into store" << std::endl;
|
|
175 |
// Replace instances of the audio file URI with our
|
|
176 |
// canonical URI (we want to make sure we're
|
|
177 |
// associating these facts with our own URI for this
|
|
178 |
// file, even if they originated from a different URI
|
|
179 |
// with the same hash)
|
|
180 |
Node from = Node(Node::URI, afuri.toString());
|
|
181 |
Node to = Node(Node::URI, canonicalUri.toString());
|
|
182 |
foreach (Triple t, all) {
|
|
183 |
if (t.a == from) t.a = to;
|
|
184 |
if (t.c == from) t.c = to;
|
|
185 |
store->add(t);
|
|
186 |
}
|
|
187 |
loadedSomething = true;
|
|
188 |
}
|
|
189 |
} catch (...) { }
|
| 111 |
190 |
}
|
| 112 |
191 |
|
| 113 |
|
|
|
192 |
return loadedSomething;
|
| 114 |
193 |
}
|
| 115 |
194 |
|
| 116 |
195 |
void
|
| ... | ... | |
| 125 |
204 |
QMutexLocker locker(&m_mutex);
|
| 126 |
205 |
if (!m_index) return;
|
| 127 |
206 |
|
|
207 |
std::cerr << "Generating index..." << std::endl;
|
|
208 |
|
| 128 |
209 |
QDir featureDir;
|
| 129 |
210 |
try {
|
| 130 |
211 |
QString s = getFeatureDirectoryName();
|
| ... | ... | |
| 147 |
228 |
|
| 148 |
229 |
std::cerr << "Saving index to " << m_indexFileName.toStdString() << std::endl;
|
| 149 |
230 |
m_bs->save(m_indexFileName);
|
|
231 |
|
|
232 |
std::cerr << "Done" << std::endl;
|
| 150 |
233 |
}
|
| 151 |
234 |
|
| 152 |
235 |
void
|
| ... | ... | |
| 167 |
250 |
(Triple(Node(), "a", m_index->expand("mo:AudioFile")));
|
| 168 |
251 |
foreach (Triple t, ts) {
|
| 169 |
252 |
tx->add(Triple(Uri(fileUrl), "foaf:primaryTopic", t.a));;
|
|
253 |
Triples hashts = b->match
|
|
254 |
(Triple(t.a, m_index->expand("foaf:sha1"), Node()));
|
|
255 |
foreach (Triple hasht, hashts) {
|
|
256 |
tx->add(hasht);
|
|
257 |
}
|
| 170 |
258 |
}
|
| 171 |
|
} catch (...) { }
|
|
259 |
} catch (std::exception &e) {
|
|
260 |
std::cerr << "Caught exception: \"" << e.what() << "\" while indexing "
|
|
261 |
<< Uri(fileUrl) << ", skipping" << std::endl;
|
|
262 |
}
|
| 172 |
263 |
|
| 173 |
264 |
delete tx;
|
| 174 |
265 |
}
|