]> git.tdb.fi Git - libs/datafile.git/commitdiff
Cache filenames in DirectorySource
authorMikko Rasa <tdb@tdb.fi>
Thu, 15 Nov 2012 21:25:59 +0000 (23:25 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 15 Nov 2012 21:25:59 +0000 (23:25 +0200)
source/directorysource.cpp
source/directorysource.h

index b1b19a1faa74e829d2a35dba9adbd39e29170350..5d70bb5deb3f9202a5f2aa0686b2ca7e2c2f25a5 100644 (file)
@@ -10,25 +10,22 @@ namespace DataFile {
 
 void DirectorySource::add_directory(const FS::Path &d)
 {
-       dirs.push_back(d);
+       list<string> files = FS::list_files(d);
+       for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
+               objects[*i] = d / *i;
 }
 
 bool DirectorySource::is_loadable(const CollectionItemTypeBase &, const string &name) const
 {
-       FS::Path path;
-       return lookup_file(name, path);
+       return objects.count(name);
 }
 
 CollectionSource::NameList DirectorySource::get_names(const CollectionItemTypeBase &type) const
 {
        NameList names;
-       for(list<FS::Path>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
-       {
-               list<string> files = FS::list_files(*i);
-               for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
-                       if(type.match_name(*j))
-                               names.push_back(*j);
-       }
+       for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+               if(type.match_name(i->first))
+                       names.push_back(i->first);
        return names;
 }
 
@@ -45,17 +42,12 @@ void DirectorySource::load(Collection &coll, const CollectionItemTypeBase &type,
 
 bool DirectorySource::lookup_file(const string &name, FS::Path &result) const
 {
-       for(list<FS::Path>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
-       {
-               FS::Path file_path = *i/name;
-               if(FS::exists(file_path))
-               {
-                       result = file_path;
-                       return true;
-               }
-       }
+       ObjectMap::const_iterator i = objects.find(name);
+       if(i==objects.end())
+               return false;
 
-       return false;
+       result = i->second;
+       return true;
 }
 
 } // namespace DataFile
index 0710225cc519a6f8eb87c01101c563136b904899..dfce833562abb0cd3fd34e57d366740d1615da16 100644 (file)
@@ -13,7 +13,9 @@ A source that load items from files in a directory.
 class DirectorySource: public CollectionSource
 {
 private:
-       std::list<FS::Path> dirs;
+       typedef std::map<std::string, FS::Path> ObjectMap;
+
+       ObjectMap objects;
 
 public:
        void add_directory(const FS::Path &);