Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Limit in-memory unsaved contents to 128MiB
Browse files Browse the repository at this point in the history
Activate working cache reads...
  • Loading branch information
Michael Steinberg committed Apr 19, 2018
1 parent db4dae6 commit 9d0f317
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/cache_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void UnqliteHandleResult(std::string operation, unqlite* database, int ret) {
// Storing index+content in an unqlite database (possibly shared between
// multiple cquery caches, since it could be a user-setting)
struct UnqliteCacheDriver : public ICacheStore {
UnqliteCacheDriver(unqlite* database) : database_(database) {}
UnqliteCacheDriver(unqlite* database) : database_(database), bytesSinceCommit_(0) {}

UnqliteCacheDriver(UnqliteCacheDriver&) = delete;

Expand All @@ -84,9 +84,15 @@ struct UnqliteCacheDriver : public ICacheStore {
}

if (ret == UNQLITE_OK)
{
LOG_S(INFO) << "unqlite: Handing out cache for key \"" << key << "\"";
return std::move(result);
}
else
{
LOG_S(WARNING) << "unqlite: No data for key \"" << key << "\"";
return {};
}
}

void Write(const std::string& key, const std::string& value) override {
Expand All @@ -97,6 +103,16 @@ struct UnqliteCacheDriver : public ICacheStore {
if (ret != UNQLITE_OK) {
UnqliteHandleResult("unqlite_kv_store", database_, ret);
}
else
{
bytesSinceCommit_ += value.size();

if (bytesSinceCommit_ > 32*1024*1024)
{
ret = unqlite_commit(database_);
if (ret == UNQLITE_OK) bytesSinceCommit_ = 0u;
}
}
}

void Close() override {
Expand All @@ -112,6 +128,7 @@ struct UnqliteCacheDriver : public ICacheStore {

~UnqliteCacheDriver() override {}

size_t bytesSinceCommit_;
unqlite* database_;
};

Expand Down Expand Up @@ -141,6 +158,10 @@ IndexFile* IndexCache::TryLoad(const NormalizedPath& path) {
result = ptr.get();
caches_.emplace(path.path, std::move(ptr));
}
else
{
LOG_S(WARNING) << "IndexCache::TryLoad: Cannot serve cache request for \"" << path.path << "\"";
}

return result;
}
Expand All @@ -159,12 +180,15 @@ optional<std::string> IndexCache::TryLoadContent(const NormalizedPath& path) {

std::unique_ptr<IndexFile> IndexCache::LoadIndexFileFromCache(
const NormalizedPath& file) {
optional<std::string> file_content = ReadContent(file.path);
optional<std::string> serialized_indexed_content = ReadContent(
optional<std::string> file_content = driver_->Read(file.path);
optional<std::string> serialized_indexed_content = driver_->Read(
file.path + SerializationFormatToSuffix(g_config->cacheFormat));

if (!file_content || !serialized_indexed_content)
{
LOG_S(WARNING) << "IndexCache::LoadIndexFileFromCache: Cannot serve cache request for \"" << file.path << "\"";
return nullptr;
}

return Deserialize(g_config->cacheFormat, file.path,
*serialized_indexed_content, *file_content,
Expand Down Expand Up @@ -222,8 +246,6 @@ std::shared_ptr<ICacheStore> OpenOrConnectUnqliteStore(
LOG_S(WARNING) << "Unqlite: unqlite_open reported error condition " << ret
<< ".";

ret = unqlite_config(database, UNQLITE_CONFIG_MAX_PAGE_CACHE, 64*1024);

// if (ret == UNQLITE_OK) return
// std::make_shared<UnqliteCacheDriver>(database);
if (ret == UNQLITE_OK)
Expand Down

0 comments on commit 9d0f317

Please sign in to comment.