To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / common / FeatureFileIndex.cpp @ 45:0033259c6772
History | View | Annotate | Download (4.41 KB)
| 1 |
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|---|---|
| 2 |
|
| 3 |
#include "FeatureFileIndex.h" |
| 4 |
#include "TypeRegistrar.h" |
| 5 |
|
| 6 |
#include <QMutexLocker> |
| 7 |
#include <QDir> |
| 8 |
|
| 9 |
#include "base/TempDirectory.h" |
| 10 |
#include "base/Exceptions.h" |
| 11 |
|
| 12 |
using namespace Dataquay; |
| 13 |
|
| 14 |
|
| 15 |
namespace ClassicalData {
|
| 16 |
|
| 17 |
FeatureFileIndex * |
| 18 |
FeatureFileIndex::getInstance() |
| 19 |
{
|
| 20 |
static FeatureFileIndex instance;
|
| 21 |
return &instance;
|
| 22 |
} |
| 23 |
|
| 24 |
FeatureFileIndex::FeatureFileIndex() : |
| 25 |
m_bs(0),
|
| 26 |
m_index(0)
|
| 27 |
{
|
| 28 |
try {
|
| 29 |
m_indexFileName = getIndexFileName(); |
| 30 |
} catch (DirectoryCreationFailed f) {
|
| 31 |
std::cerr << "FeatureFileIndex: ERROR: Failed to find or create index directory: " << f.what() << std::endl;
|
| 32 |
return;
|
| 33 |
} |
| 34 |
|
| 35 |
m_bs = new BasicStore;
|
| 36 |
m_index = new TransactionalStore(m_bs);
|
| 37 |
|
| 38 |
TypeRegistrar::addMappings(m_bs, 0);
|
| 39 |
|
| 40 |
if (QFile(m_indexFileName).exists()) {
|
| 41 |
m_bs->import(QUrl::fromLocalFile(m_indexFileName), |
| 42 |
BasicStore::ImportIgnoreDuplicates); |
| 43 |
//!!! catch
|
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
FeatureFileIndex::~FeatureFileIndex() |
| 48 |
{
|
| 49 |
delete m_index;
|
| 50 |
delete m_bs;
|
| 51 |
} |
| 52 |
|
| 53 |
QString |
| 54 |
FeatureFileIndex::getIndexFileName() |
| 55 |
{
|
| 56 |
QDir d = TempDirectory::getInstance()->getContainingPath(); |
| 57 |
QString n("index");
|
| 58 |
QFileInfo fi(d.filePath(n)); |
| 59 |
|
| 60 |
if ((fi.exists() && !fi.isDir()) ||
|
| 61 |
(!fi.exists() && !d.mkdir(n))) {
|
| 62 |
throw DirectoryCreationFailed(fi.filePath());
|
| 63 |
} |
| 64 |
|
| 65 |
return QDir(fi.filePath()).filePath("features.ttl"); |
| 66 |
} |
| 67 |
|
| 68 |
QString |
| 69 |
FeatureFileIndex::getFeatureDirectoryName() |
| 70 |
{
|
| 71 |
QDir d = TempDirectory::getInstance()->getContainingPath(); |
| 72 |
QString n("features");
|
| 73 |
QFileInfo fi(d.filePath(n)); |
| 74 |
|
| 75 |
if ((fi.exists() && !fi.isDir()) ||
|
| 76 |
(!fi.exists() && !d.mkdir(n))) {
|
| 77 |
throw DirectoryCreationFailed(fi.filePath());
|
| 78 |
} |
| 79 |
|
| 80 |
return fi.filePath();
|
| 81 |
} |
| 82 |
|
| 83 |
void
|
| 84 |
FeatureFileIndex::loadFor(AudioFile *tf, Store *store) |
| 85 |
{
|
| 86 |
if (!m_index) {
|
| 87 |
std::cerr << "FeatureFileIndex::loadFor: No index!" << std::endl;
|
| 88 |
return;
|
| 89 |
} |
| 90 |
updateIndex(); |
| 91 |
|
| 92 |
// The AudioFile object has a URI and a hash. Feature files
|
| 93 |
// generated for this AudioFile should ideally have a matching
|
| 94 |
// hash; if they have no hash, then the URI should match. If the
|
| 95 |
// hash is present in the feature file but does not match, then it
|
| 96 |
// cannot be the right track even if the URI matches.
|
| 97 |
|
| 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; |
| 101 |
|
| 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; |
| 106 |
|
| 107 |
//!!! what's the right approach here?
|
| 108 |
|
| 109 |
if (results.empty() && moreResults.empty()) {
|
| 110 |
return;
|
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
void
|
| 117 |
FeatureFileIndex::featureFileAdded(QString filepath) |
| 118 |
{
|
| 119 |
index(QUrl::fromLocalFile(filepath)); |
| 120 |
} |
| 121 |
|
| 122 |
void
|
| 123 |
FeatureFileIndex::updateIndex() |
| 124 |
{
|
| 125 |
QMutexLocker locker(&m_mutex); |
| 126 |
if (!m_index) return; |
| 127 |
|
| 128 |
QDir featureDir; |
| 129 |
try {
|
| 130 |
QString s = getFeatureDirectoryName(); |
| 131 |
featureDir = QDir(s); |
| 132 |
} catch (DirectoryCreationFailed f) {
|
| 133 |
std::cerr << "FeatureFileIndex::updateIndex: ERROR: Failed to find or create feature directory: " << f.what() << std::endl;
|
| 134 |
return;
|
| 135 |
} |
| 136 |
|
| 137 |
featureDir.setFilter(QDir::Files); |
| 138 |
|
| 139 |
for (unsigned int i = 0; i < featureDir.count(); ++i) { |
| 140 |
QFileInfo fi(featureDir.filePath(featureDir[i])); |
| 141 |
if (fi.isFile() && fi.isReadable()) {
|
| 142 |
index(QUrl::fromLocalFile(fi.filePath())); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
//!!! remove triples from index that refer to nonexistent files?
|
| 147 |
|
| 148 |
std::cerr << "Saving index to " << m_indexFileName.toStdString() << std::endl;
|
| 149 |
m_bs->save(m_indexFileName); |
| 150 |
} |
| 151 |
|
| 152 |
void
|
| 153 |
FeatureFileIndex::index(QUrl fileUrl) |
| 154 |
{
|
| 155 |
Triple typeTriple(Uri(fileUrl), "a", m_index->expand("foaf:Document")); |
| 156 |
|
| 157 |
if (m_index->contains(typeTriple)) {
|
| 158 |
return;
|
| 159 |
} |
| 160 |
|
| 161 |
Transaction *tx = m_index->startTransaction(); |
| 162 |
tx->add(typeTriple); |
| 163 |
|
| 164 |
try {
|
| 165 |
BasicStore *b = BasicStore::load(fileUrl); |
| 166 |
Triples ts = b->match |
| 167 |
(Triple(Node(), "a", m_index->expand("mo:AudioFile"))); |
| 168 |
foreach (Triple t, ts) {
|
| 169 |
tx->add(Triple(Uri(fileUrl), "foaf:primaryTopic", t.a));;
|
| 170 |
} |
| 171 |
} catch (...) { }
|
| 172 |
|
| 173 |
delete tx;
|
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
} |
| 178 |
|