comparison transform/TransformFactory.cpp @ 460:93fb1ebff76b

* Add persistent cache file support to FileSource (e.g. for RDF descriptions) * Query RDF plugin data in a background thread on startup
author Chris Cannam
date Fri, 17 Oct 2008 13:32:55 +0000
parents ef14acd6d102
children 2019d89ebcf9
comparison
equal deleted inserted replaced
459:6441b31b37ac 460:93fb1ebff76b
33 #include <set> 33 #include <set>
34 34
35 #include <QRegExp> 35 #include <QRegExp>
36 #include <QTextStream> 36 #include <QTextStream>
37 37
38 #include "base/Thread.h"
39
38 using std::cerr; 40 using std::cerr;
39 using std::endl; 41 using std::endl;
40 42
41 TransformFactory * 43 TransformFactory *
42 TransformFactory::m_instance = new TransformFactory; 44 TransformFactory::m_instance = new TransformFactory;
49 51
50 TransformFactory::TransformFactory() : 52 TransformFactory::TransformFactory() :
51 m_transformsPopulated(false), 53 m_transformsPopulated(false),
52 m_uninstalledTransformsPopulated(false) 54 m_uninstalledTransformsPopulated(false)
53 { 55 {
56 UninstalledTransformsPopulateThread *thread =
57 new UninstalledTransformsPopulateThread(this);
58 thread->start();
54 } 59 }
55 60
56 TransformFactory::~TransformFactory() 61 TransformFactory::~TransformFactory()
57 { 62 {
58 } 63 }
59 64
60 TransformList 65 TransformList
61 TransformFactory::getAllTransformDescriptions() 66 TransformFactory::getAllTransformDescriptions()
62 { 67 {
63 if (!m_transformsPopulated) populateTransforms(); 68 populateTransforms();
64 69
65 std::set<TransformDescription> dset; 70 std::set<TransformDescription> dset;
66 for (TransformDescriptionMap::const_iterator i = m_transforms.begin(); 71 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
67 i != m_transforms.end(); ++i) { 72 i != m_transforms.end(); ++i) {
68 // cerr << "inserting transform into set: id = " << i->second.identifier.toStdString() << endl; 73 // cerr << "inserting transform into set: id = " << i->second.identifier.toStdString() << endl;
80 } 85 }
81 86
82 TransformDescription 87 TransformDescription
83 TransformFactory::getTransformDescription(TransformId id) 88 TransformFactory::getTransformDescription(TransformId id)
84 { 89 {
85 if (!m_transformsPopulated) populateTransforms(); 90 populateTransforms();
86 91
87 if (m_transforms.find(id) == m_transforms.end()) { 92 if (m_transforms.find(id) == m_transforms.end()) {
88 return TransformDescription(); 93 return TransformDescription();
89 } 94 }
90 95
92 } 97 }
93 98
94 TransformList 99 TransformList
95 TransformFactory::getUninstalledTransformDescriptions() 100 TransformFactory::getUninstalledTransformDescriptions()
96 { 101 {
97 if (!m_uninstalledTransformsPopulated) populateUninstalledTransforms(); 102 populateUninstalledTransforms();
98 103
99 std::set<TransformDescription> dset; 104 std::set<TransformDescription> dset;
100 for (TransformDescriptionMap::const_iterator i = m_uninstalledTransforms.begin(); 105 for (TransformDescriptionMap::const_iterator i = m_uninstalledTransforms.begin();
101 i != m_uninstalledTransforms.end(); ++i) { 106 i != m_uninstalledTransforms.end(); ++i) {
102 // cerr << "inserting transform into set: id = " << i->second.identifier.toStdString() << endl; 107 // cerr << "inserting transform into set: id = " << i->second.identifier.toStdString() << endl;
104 } 109 }
105 110
106 TransformList list; 111 TransformList list;
107 for (std::set<TransformDescription>::const_iterator i = dset.begin(); 112 for (std::set<TransformDescription>::const_iterator i = dset.begin();
108 i != dset.end(); ++i) { 113 i != dset.end(); ++i) {
109 // cerr << "inserting transform into list: id = " << i->identifier.toStdString() << endl; 114 // cerr << "inserting transform into uninstalled list: id = " << i->identifier.toStdString() << endl;
110 list.push_back(*i); 115 list.push_back(*i);
111 } 116 }
112 117
113 return list; 118 return list;
114 } 119 }
115 120
116 TransformDescription 121 TransformDescription
117 TransformFactory::getUninstalledTransformDescription(TransformId id) 122 TransformFactory::getUninstalledTransformDescription(TransformId id)
118 { 123 {
119 if (!m_uninstalledTransformsPopulated) populateUninstalledTransforms(); 124 populateUninstalledTransforms();
120 125
121 if (m_uninstalledTransforms.find(id) == m_uninstalledTransforms.end()) { 126 if (m_uninstalledTransforms.find(id) == m_uninstalledTransforms.end()) {
122 return TransformDescription(); 127 return TransformDescription();
123 } 128 }
124 129
126 } 131 }
127 132
128 TransformFactory::TransformInstallStatus 133 TransformFactory::TransformInstallStatus
129 TransformFactory::getTransformInstallStatus(TransformId id) 134 TransformFactory::getTransformInstallStatus(TransformId id)
130 { 135 {
131 if (!m_transformsPopulated) populateTransforms(); 136 populateTransforms();
132 if (!m_uninstalledTransformsPopulated) populateUninstalledTransforms(); 137 populateUninstalledTransforms();
133 138
134 if (m_transforms.find(id) != m_transforms.end()) { 139 if (m_transforms.find(id) != m_transforms.end()) {
135 return TransformInstalled; 140 return TransformInstalled;
136 } 141 }
137 if (m_uninstalledTransforms.find(id) != m_uninstalledTransforms.end()) { 142 if (m_uninstalledTransforms.find(id) != m_uninstalledTransforms.end()) {
142 147
143 148
144 std::vector<QString> 149 std::vector<QString>
145 TransformFactory::getAllTransformTypes() 150 TransformFactory::getAllTransformTypes()
146 { 151 {
147 if (!m_transformsPopulated) populateTransforms(); 152 populateTransforms();
148 153
149 std::set<QString> types; 154 std::set<QString> types;
150 for (TransformDescriptionMap::const_iterator i = m_transforms.begin(); 155 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
151 i != m_transforms.end(); ++i) { 156 i != m_transforms.end(); ++i) {
152 types.insert(i->second.type); 157 types.insert(i->second.type);
161 } 166 }
162 167
163 std::vector<QString> 168 std::vector<QString>
164 TransformFactory::getTransformCategories(QString transformType) 169 TransformFactory::getTransformCategories(QString transformType)
165 { 170 {
166 if (!m_transformsPopulated) populateTransforms(); 171 populateTransforms();
167 172
168 std::set<QString> categories; 173 std::set<QString> categories;
169 for (TransformDescriptionMap::const_iterator i = m_transforms.begin(); 174 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
170 i != m_transforms.end(); ++i) { 175 i != m_transforms.end(); ++i) {
171 if (i->second.type == transformType) { 176 if (i->second.type == transformType) {
188 } 193 }
189 194
190 std::vector<QString> 195 std::vector<QString>
191 TransformFactory::getTransformMakers(QString transformType) 196 TransformFactory::getTransformMakers(QString transformType)
192 { 197 {
193 if (!m_transformsPopulated) populateTransforms(); 198 populateTransforms();
194 199
195 std::set<QString> makers; 200 std::set<QString> makers;
196 for (TransformDescriptionMap::const_iterator i = m_transforms.begin(); 201 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
197 i != m_transforms.end(); ++i) { 202 i != m_transforms.end(); ++i) {
198 if (i->second.type == transformType) { 203 if (i->second.type == transformType) {
215 } 220 }
216 221
217 void 222 void
218 TransformFactory::populateTransforms() 223 TransformFactory::populateTransforms()
219 { 224 {
225 MutexLocker locker(&m_transformsMutex,
226 "TransformFactory::populateTransforms");
227 if (m_transformsPopulated) {
228 return;
229 }
230
220 TransformDescriptionMap transforms; 231 TransformDescriptionMap transforms;
221 232
222 populateFeatureExtractionPlugins(transforms); 233 populateFeatureExtractionPlugins(transforms);
223 populateRealTimePlugins(transforms); 234 populateRealTimePlugins(transforms);
224 235
510 } 521 }
511 522
512 void 523 void
513 TransformFactory::populateUninstalledTransforms() 524 TransformFactory::populateUninstalledTransforms()
514 { 525 {
515 if (!m_uninstalledTransforms.empty()) return; 526 populateTransforms();
516 if (m_transforms.empty()) populateTransforms(); 527
528 MutexLocker locker(&m_uninstalledTransformsMutex,
529 "TransformFactory::populateUninstalledTransforms");
530 if (m_uninstalledTransformsPopulated) return;
531
532 PluginRDFIndexer::getInstance()->indexURL
533 ("http://www.vamp-plugins.org/rdf/plugins/vamp-example-plugins");
517 534
518 //!!! This will be amazingly slow 535 //!!! This will be amazingly slow
519 536
520 QStringList ids = PluginRDFIndexer::getInstance()->getIndexedPluginIds(); 537 QStringList ids = PluginRDFIndexer::getInstance()->getIndexedPluginIds();
521 538
573 m_uninstalledTransforms[tid] = td; 590 m_uninstalledTransforms[tid] = td;
574 } 591 }
575 } 592 }
576 593
577 m_uninstalledTransformsPopulated = true; 594 m_uninstalledTransformsPopulated = true;
595
596 std::cerr << "populateUninstalledTransforms exiting" << std::endl;
578 } 597 }
579 598
580 Transform 599 Transform
581 TransformFactory::getDefaultTransformFor(TransformId id, size_t rate) 600 TransformFactory::getDefaultTransformFor(TransformId id, size_t rate)
582 { 601 {
658 } 677 }
659 678
660 bool 679 bool
661 TransformFactory::haveTransform(TransformId identifier) 680 TransformFactory::haveTransform(TransformId identifier)
662 { 681 {
663 if (m_transforms.empty()) populateTransforms(); 682 populateTransforms();
664 return (m_transforms.find(identifier) != m_transforms.end()); 683 return (m_transforms.find(identifier) != m_transforms.end());
665 } 684 }
666 685
667 QString 686 QString
668 TransformFactory::getTransformName(TransformId identifier) 687 TransformFactory::getTransformName(TransformId identifier)
925 } 944 }
926 945
927 TransformFactory::SearchResults 946 TransformFactory::SearchResults
928 TransformFactory::search(QStringList keywords) 947 TransformFactory::search(QStringList keywords)
929 { 948 {
930 if (m_transforms.empty()) populateTransforms(); 949 populateTransforms();
931 950
932 if (keywords.size() > 1) { 951 if (keywords.size() > 1) {
933 // Additional score for all keywords in a row 952 // Additional score for all keywords in a row
934 keywords.push_back(keywords.join(" ")); 953 keywords.push_back(keywords.join(" "));
935 } 954 }
953 matcher.test(match, keywords, i->second.units, tr("Units"), 10); 972 matcher.test(match, keywords, i->second.units, tr("Units"), 10);
954 973
955 if (match.score > 0) results[i->first] = match; 974 if (match.score > 0) results[i->first] = match;
956 } 975 }
957 976
958 if (m_uninstalledTransforms.empty()) populateUninstalledTransforms(); 977 if (!m_uninstalledTransformsMutex.tryLock()) {
978 // uninstalled transforms are being populated; this may take some time,
979 // and they aren't critical
980 std::cerr << "TransformFactory::search: Uninstalled transforms mutex is held, skipping" << std::endl;
981 return results;
982 }
983
984 if (!m_uninstalledTransformsPopulated) {
985 std::cerr << "WARNING: TransformFactory::search: Uninstalled transforms are not populated yet" << endl
986 << "and are not being populated either -- was the thread not started correctly?" << endl;
987 m_uninstalledTransformsMutex.unlock();
988 return results;
989 }
990
991 m_uninstalledTransformsMutex.unlock();
959 992
960 for (TransformDescriptionMap::const_iterator i = m_uninstalledTransforms.begin(); 993 for (TransformDescriptionMap::const_iterator i = m_uninstalledTransforms.begin();
961 i != m_uninstalledTransforms.end(); ++i) { 994 i != m_uninstalledTransforms.end(); ++i) {
962 995
963 TextMatcher::Match match; 996 TextMatcher::Match match;