To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / common / FeatureFileIndex.cpp @ 44:ed2befdf1e98

History | View | Annotate | Download (3.16 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_index(0)
26
{
27
    try {
28
        m_indexFileName = getIndexFileName();
29
    } catch (DirectoryCreationFailed f) {
30
        std::cerr << "FeatureFileIndex: ERROR: Failed to find or create index directory: " << f.what() << std::endl;
31
        return;
32
    }
33

    
34
    m_index = new BasicStore;
35

    
36
    TypeRegistrar::addMappings(m_index, 0);
37

    
38
    if (QFile(m_indexFileName).exists()) {
39
        m_index->import(QUrl::fromLocalFile(m_indexFileName),
40
                        BasicStore::ImportIgnoreDuplicates);
41
        //!!! catch
42
    }
43
}
44

    
45
FeatureFileIndex::~FeatureFileIndex()
46
{
47
}
48

    
49
QString
50
FeatureFileIndex::getIndexFileName()
51
{
52
    QDir d = TempDirectory::getInstance()->getContainingPath();
53
    QString n("index");
54
    QFileInfo fi(d.filePath(n));
55

    
56
    if ((fi.exists() && !fi.isDir()) ||
57
        (!fi.exists() && !d.mkdir(n))) {
58
        throw DirectoryCreationFailed(fi.filePath());
59
    }
60

    
61
    return QDir(fi.filePath()).filePath("features.ttl");
62
}
63

    
64
QString
65
FeatureFileIndex::getFeatureDirectoryName()
66
{
67
    QDir d = TempDirectory::getInstance()->getContainingPath();
68
    QString n("features");
69
    QFileInfo fi(d.filePath(n));
70

    
71
    if ((fi.exists() && !fi.isDir()) ||
72
        (!fi.exists() && !d.mkdir(n))) {
73
        throw DirectoryCreationFailed(fi.filePath());
74
    }
75

    
76
    return fi.filePath();
77
}
78

    
79
void
80
FeatureFileIndex::loadFor(TrackFile *tf, BasicStore *store)
81
{
82
    if (!m_index) {
83
        std::cerr << "FeatureFileIndex::loadFor: No index!" << std::endl;
84
        return;
85
    }
86
    updateIndex();
87

    
88
    //...
89
}
90

    
91
void
92
FeatureFileIndex::updateIndex()
93
{
94
    QMutexLocker locker(&m_mutex);
95
    if (!m_index) return;
96

    
97
    QDir featureDir;
98
    try {
99
        QString s = getFeatureDirectoryName();
100
        featureDir = QDir(s);
101
    } catch (DirectoryCreationFailed f) {
102
        std::cerr << "FeatureFileIndex::updateIndex: ERROR: Failed to find or create feature directory: " << f.what() << std::endl;
103
        return;
104
    }
105

    
106
    featureDir.setFilter(QDir::Files);
107

    
108
    for (unsigned int i = 0; i < featureDir.count(); ++i) {
109

    
110
        QFileInfo fi(featureDir.filePath(featureDir[i]));
111

    
112
        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 (...) { }
123
        }
124
    }
125

    
126
    //!!! remove triples from index that refer to nonexistent files?
127

    
128
    std::cerr << "Saving index to " << m_indexFileName.toStdString() << std::endl;
129
    m_index->save(m_indexFileName);
130
}
131

    
132

    
133
}
134