rewrite map loader
This commit is contained in:
+18
-17
@@ -11,27 +11,26 @@ using namespace std;
|
||||
|
||||
FileContentsCache::FileContentsCache(uint64_t ttl_usecs) : ttl_usecs(ttl_usecs) { }
|
||||
|
||||
FileContentsCache::File::File(const string& name, shared_ptr<const string> contents,
|
||||
uint64_t load_time) : name(name), contents(contents), load_time(load_time) { }
|
||||
FileContentsCache::File::File(
|
||||
const string& name,
|
||||
string&& data,
|
||||
uint64_t load_time)
|
||||
: name(name), data(move(data)), load_time(load_time) { }
|
||||
|
||||
shared_ptr<const string> FileContentsCache::replace(
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::replace(
|
||||
const string& name, string&& data, uint64_t t) {
|
||||
if (t == 0) {
|
||||
t = now();
|
||||
}
|
||||
shared_ptr<const string> contents(new string(move(data)));
|
||||
auto emplace_ret = this->name_to_file.emplace(
|
||||
piecewise_construct,
|
||||
forward_as_tuple(name),
|
||||
forward_as_tuple(name, contents, t));
|
||||
shared_ptr<File> new_file(new File(name, move(data), t));
|
||||
auto emplace_ret = this->name_to_file.emplace(name, new_file);
|
||||
if (!emplace_ret.second) {
|
||||
emplace_ret.first->second.contents = contents;
|
||||
emplace_ret.first->second.load_time = t;
|
||||
emplace_ret.first->second = new_file;
|
||||
}
|
||||
return contents;
|
||||
return new_file;
|
||||
}
|
||||
|
||||
shared_ptr<const string> FileContentsCache::replace(
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::replace(
|
||||
const string& name, const void* data, size_t size, uint64_t t) {
|
||||
string s(reinterpret_cast<const char*>(data), size);
|
||||
return this->replace(name, move(s), t);
|
||||
@@ -45,14 +44,16 @@ FileContentsCache::GetResult FileContentsCache::get_or_load(const char* name) {
|
||||
return this->get_or_load(string(name));
|
||||
}
|
||||
|
||||
shared_ptr<const string> FileContentsCache::get_or_throw(const std::string& name) {
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::get_or_throw(
|
||||
const std::string& name) {
|
||||
auto throw_fn = +[](const std::string&) -> string {
|
||||
throw out_of_range("file missing from cache");
|
||||
};
|
||||
return this->get(name, throw_fn).data;
|
||||
return this->get(name, throw_fn).file;
|
||||
}
|
||||
|
||||
shared_ptr<const string> FileContentsCache::get_or_throw(const char* name) {
|
||||
shared_ptr<const FileContentsCache::File> FileContentsCache::get_or_throw(
|
||||
const char* name) {
|
||||
return this->get_or_throw(string(name));
|
||||
}
|
||||
|
||||
@@ -61,8 +62,8 @@ FileContentsCache::GetResult FileContentsCache::get(const std::string& name,
|
||||
uint64_t t = now();
|
||||
try {
|
||||
auto& entry = this->name_to_file.at(name);
|
||||
if (this->ttl_usecs && (t - entry.load_time < this->ttl_usecs)) {
|
||||
return {entry.contents, false};
|
||||
if (this->ttl_usecs && (t - entry->load_time < this->ttl_usecs)) {
|
||||
return {entry, false};
|
||||
}
|
||||
} catch (const out_of_range& e) { }
|
||||
return {this->replace(name, generate(name)), true};
|
||||
|
||||
Reference in New Issue
Block a user