comparison rdf/RDFFeatureWriter.cpp @ 590:063da8955f65

* Try to avoid URI clashes with existing URIs in the file, when writing with --rdf-append
author Chris Cannam
date Thu, 14 May 2009 15:47:16 +0000
parents 94054c97c76a
children f3bb7a3ffd89
comparison
equal deleted inserted replaced
589:a03aafaacb5a 590:063da8955f65
217 << "@prefix vamp: <http://purl.org/ontology/vamp/> .\n" 217 << "@prefix vamp: <http://purl.org/ontology/vamp/> .\n"
218 << "@prefix : <#> .\n\n"; 218 << "@prefix : <#> .\n\n";
219 } 219 }
220 220
221 void 221 void
222 RDFFeatureWriter::reviewFileForAppending(QString filename)
223 {
224 // Appending to an RDF file is tricky, because we need to ensure
225 // that our URIs differ from any already in the file. This is a
226 // dirty grubby low-rent way of doing that. This function is
227 // called by FileFeatureWriter::getOutputFile when in append mode.
228
229 std::cerr << "reviewFileForAppending(" << filename.toStdString() << ")" << std::endl;
230
231 QFile file(filename);
232
233 // just return, don't report failure -- function that called us will do that
234 if (!file.open(QIODevice::ReadOnly)) return;
235
236 QTextStream in(&file);
237
238 QRegExp localObjectUriWithDigits(":[^ ]+_([0-9]+) a ");
239
240 while (!in.atEnd()) {
241 QString line = in.readLine();
242 if (line.length() > 120) { // probably data
243 continue;
244 }
245 if (localObjectUriWithDigits.indexIn(line) > -1) {
246 QString numeric = localObjectUriWithDigits.cap(1);
247 int number = numeric.toInt();
248 if (number >= m_count) m_count = number + 1;
249 }
250 }
251
252 file.close();
253 }
254
255 void
222 RDFFeatureWriter::writeSignalDescription(QTextStream *sptr, 256 RDFFeatureWriter::writeSignalDescription(QTextStream *sptr,
223 QString trackId) 257 QString trackId)
224 { 258 {
225 std::cerr << "RDFFeatureWriter::writeSignalDescription" << std::endl; 259 std::cerr << "RDFFeatureWriter::writeSignalDescription" << std::endl;
226 260
242 url.setScheme("file"); 276 url.setScheme("file");
243 url.setPath(scheme + ":" + url.path()); 277 url.setPath(scheme + ":" + url.path());
244 } 278 }
245 } 279 }
246 280
247 //!!! FIX: If we are appending, we need to start counting after 281 // Note reviewFileForAppending above (when opening in append mode)
248 //all of the existing counts that are already in the file!
249 282
250 uint64_t signalCount = m_count++; 283 uint64_t signalCount = m_count++;
251 284
252 if (m_trackSignalURIs.find(trackId) == m_trackSignalURIs.end()) { 285 if (m_trackSignalURIs.find(trackId) == m_trackSignalURIs.end()) {
253 m_trackSignalURIs[trackId] = QString(":signal_%1").arg(signalCount); 286 m_trackSignalURIs[trackId] = QString(":signal_%1").arg(signalCount);