Revision 45:0033259c6772 common/FeatureFileIndex.cpp
| common/FeatureFileIndex.cpp | ||
|---|---|---|
| 22 | 22 |
} |
| 23 | 23 |
|
| 24 | 24 |
FeatureFileIndex::FeatureFileIndex() : |
| 25 |
m_bs(0), |
|
| 25 | 26 |
m_index(0) |
| 26 | 27 |
{
|
| 27 | 28 |
try {
|
| ... | ... | |
| 31 | 32 |
return; |
| 32 | 33 |
} |
| 33 | 34 |
|
| 34 |
m_index = new BasicStore; |
|
| 35 |
m_bs = new BasicStore; |
|
| 36 |
m_index = new TransactionalStore(m_bs); |
|
| 35 | 37 |
|
| 36 |
TypeRegistrar::addMappings(m_index, 0);
|
|
| 38 |
TypeRegistrar::addMappings(m_bs, 0);
|
|
| 37 | 39 |
|
| 38 | 40 |
if (QFile(m_indexFileName).exists()) {
|
| 39 |
m_index->import(QUrl::fromLocalFile(m_indexFileName),
|
|
| 40 |
BasicStore::ImportIgnoreDuplicates);
|
|
| 41 |
m_bs->import(QUrl::fromLocalFile(m_indexFileName),
|
|
| 42 |
BasicStore::ImportIgnoreDuplicates);
|
|
| 41 | 43 |
//!!! catch |
| 42 | 44 |
} |
| 43 | 45 |
} |
| 44 | 46 |
|
| 45 | 47 |
FeatureFileIndex::~FeatureFileIndex() |
| 46 | 48 |
{
|
| 49 |
delete m_index; |
|
| 50 |
delete m_bs; |
|
| 47 | 51 |
} |
| 48 | 52 |
|
| 49 | 53 |
QString |
| ... | ... | |
| 77 | 81 |
} |
| 78 | 82 |
|
| 79 | 83 |
void |
| 80 |
FeatureFileIndex::loadFor(TrackFile *tf, BasicStore *store)
|
|
| 84 |
FeatureFileIndex::loadFor(AudioFile *tf, Store *store)
|
|
| 81 | 85 |
{
|
| 82 | 86 |
if (!m_index) {
|
| 83 | 87 |
std::cerr << "FeatureFileIndex::loadFor: No index!" << std::endl; |
| ... | ... | |
| 85 | 89 |
} |
| 86 | 90 |
updateIndex(); |
| 87 | 91 |
|
| 88 |
//... |
|
| 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 |
|
|
| 89 | 114 |
} |
| 90 | 115 |
|
| 91 | 116 |
void |
| 117 |
FeatureFileIndex::featureFileAdded(QString filepath) |
|
| 118 |
{
|
|
| 119 |
index(QUrl::fromLocalFile(filepath)); |
|
| 120 |
} |
|
| 121 |
|
|
| 122 |
void |
|
| 92 | 123 |
FeatureFileIndex::updateIndex() |
| 93 | 124 |
{
|
| 94 | 125 |
QMutexLocker locker(&m_mutex); |
| ... | ... | |
| 106 | 137 |
featureDir.setFilter(QDir::Files); |
| 107 | 138 |
|
| 108 | 139 |
for (unsigned int i = 0; i < featureDir.count(); ++i) {
|
| 109 |
|
|
| 110 | 140 |
QFileInfo fi(featureDir.filePath(featureDir[i])); |
| 111 |
|
|
| 112 | 141 |
if (fi.isFile() && fi.isReadable()) {
|
| 113 |
QUrl fileUrl(QUrl::fromLocalFile(fi.filePath())); |
|
| 114 |
try {
|
|
| 115 |
BasicStore *b = BasicStore::load(fileUrl); |
|
| 116 |
Triples ts = b->match |
|
| 117 |
(Triple(Node(), "a", m_index->expand("mo:AudioFile")));
|
|
| 118 |
foreach (Triple t, ts) {
|
|
| 119 |
m_index->add(Triple(Uri(fileUrl), "a", m_index->expand("foaf:Document")));
|
|
| 120 |
m_index->add(Triple(Uri(fileUrl), "foaf:primaryTopic", t.a));; |
|
| 121 |
} |
|
| 122 |
} catch (...) { }
|
|
| 142 |
index(QUrl::fromLocalFile(fi.filePath())); |
|
| 123 | 143 |
} |
| 124 | 144 |
} |
| 125 | 145 |
|
| 126 | 146 |
//!!! remove triples from index that refer to nonexistent files? |
| 127 | 147 |
|
| 128 | 148 |
std::cerr << "Saving index to " << m_indexFileName.toStdString() << std::endl; |
| 129 |
m_index->save(m_indexFileName); |
|
| 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; |
|
| 130 | 174 |
} |
| 131 | 175 |
|
| 132 | 176 |
|
Also available in: Unified diff