Cache tag list, directories, and file status

This commit is contained in:
Les De Ridder 2016-10-24 03:06:09 +02:00
parent 24b869d0f6
commit dd21635050
No known key found for this signature in database
GPG Key ID: 5EC132DFA85DB372
1 changed files with 32 additions and 13 deletions

View File

@ -20,10 +20,14 @@ class FileSystem : Operations
private string _source; private string _source;
private TagProvider[] _tagProviders; private TagProvider[] _tagProviders;
private string[][string] _tagCache;
private stat_t _sourceStat; private stat_t _sourceStat;
private string[][string] _tagCache;
private stat_t[string] _statCache;
private string[][string] _dirCache;
private string[] _tagList;
this(string source, TagProvider[] tagProviders) this(string source, TagProvider[] tagProviders)
{ {
_source = source; _source = source;
@ -49,6 +53,13 @@ class FileSystem : Operations
_tagCache[file] ~= tagProvider.getTags(file); _tagCache[file] ~= tagProvider.getTags(file);
} }
} }
_tagList = _tagCache.byValue()
.joiner
.array
.sort()
.uniq
.array;
} }
override void getattr(const(char)[] path, ref stat_t stat) override void getattr(const(char)[] path, ref stat_t stat)
@ -62,8 +73,16 @@ class FileSystem : Operations
} }
else if(isFile(path.baseName)) else if(isFile(path.baseName))
{ {
auto file = findFile(path.baseName); if(path.baseName in _statCache)
lstat(toStringz(file), &stat); {
stat = _statCache[path.baseName];
}
else
{
auto file = findFile(path.baseName);
lstat(toStringz(file), &stat);
_statCache[path.baseName] = stat;
}
} }
else else
{ {
@ -73,7 +92,7 @@ class FileSystem : Operations
bool isTag(const(char)[] name) bool isTag(const(char)[] name)
{ {
return _tagCache.values.any!(a => a.canFind(name)); return _tagList.canFind(name);
} }
bool isFile(const(char)[] name) bool isFile(const(char)[] name)
@ -95,12 +114,7 @@ class FileSystem : Operations
{ {
if(path == "/") if(path == "/")
{ {
return _tagCache.byValue() return _tagList;
.joiner
.array
.sort()
.uniq
.array;
} }
else else
{ {
@ -138,14 +152,19 @@ class FileSystem : Operations
override string[] readdir(const(char)[] path) override string[] readdir(const(char)[] path)
{ {
if(path in _dirCache)
{
return _dirCache[path];
}
//TODO: Don't return tags if only one file (or files with exactly the same set of tags) files? //TODO: Don't return tags if only one file (or files with exactly the same set of tags) files?
if (path == "/") if (path == "/")
{ {
return getTags(path) ~ getFiles(path); return _dirCache[path] = getTags(path) ~ getFiles(path);
} }
else else
{ {
return getTags(path) ~ getFiles(path) ~ [".", ".."]; return _dirCache[path] = getTags(path) ~ getFiles(path) ~ [".", ".."];
} }
} }