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

Commit 9d0f317

Browse files
committed
Limit in-memory unsaved contents to 128MiB
Activate working cache reads...
1 parent db4dae6 commit 9d0f317

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/cache_manager.cc

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void UnqliteHandleResult(std::string operation, unqlite* database, int ret) {
6767
// Storing index+content in an unqlite database (possibly shared between
6868
// multiple cquery caches, since it could be a user-setting)
6969
struct UnqliteCacheDriver : public ICacheStore {
70-
UnqliteCacheDriver(unqlite* database) : database_(database) {}
70+
UnqliteCacheDriver(unqlite* database) : database_(database), bytesSinceCommit_(0) {}
7171

7272
UnqliteCacheDriver(UnqliteCacheDriver&) = delete;
7373

@@ -84,9 +84,15 @@ struct UnqliteCacheDriver : public ICacheStore {
8484
}
8585

8686
if (ret == UNQLITE_OK)
87+
{
88+
LOG_S(INFO) << "unqlite: Handing out cache for key \"" << key << "\"";
8789
return std::move(result);
90+
}
8891
else
92+
{
93+
LOG_S(WARNING) << "unqlite: No data for key \"" << key << "\"";
8994
return {};
95+
}
9096
}
9197

9298
void Write(const std::string& key, const std::string& value) override {
@@ -97,6 +103,16 @@ struct UnqliteCacheDriver : public ICacheStore {
97103
if (ret != UNQLITE_OK) {
98104
UnqliteHandleResult("unqlite_kv_store", database_, ret);
99105
}
106+
else
107+
{
108+
bytesSinceCommit_ += value.size();
109+
110+
if (bytesSinceCommit_ > 32*1024*1024)
111+
{
112+
ret = unqlite_commit(database_);
113+
if (ret == UNQLITE_OK) bytesSinceCommit_ = 0u;
114+
}
115+
}
100116
}
101117

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

113129
~UnqliteCacheDriver() override {}
114130

131+
size_t bytesSinceCommit_;
115132
unqlite* database_;
116133
};
117134

@@ -141,6 +158,10 @@ IndexFile* IndexCache::TryLoad(const NormalizedPath& path) {
141158
result = ptr.get();
142159
caches_.emplace(path.path, std::move(ptr));
143160
}
161+
else
162+
{
163+
LOG_S(WARNING) << "IndexCache::TryLoad: Cannot serve cache request for \"" << path.path << "\"";
164+
}
144165

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

160181
std::unique_ptr<IndexFile> IndexCache::LoadIndexFileFromCache(
161182
const NormalizedPath& file) {
162-
optional<std::string> file_content = ReadContent(file.path);
163-
optional<std::string> serialized_indexed_content = ReadContent(
183+
optional<std::string> file_content = driver_->Read(file.path);
184+
optional<std::string> serialized_indexed_content = driver_->Read(
164185
file.path + SerializationFormatToSuffix(g_config->cacheFormat));
165186

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

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

225-
ret = unqlite_config(database, UNQLITE_CONFIG_MAX_PAGE_CACHE, 64*1024);
226-
227249
// if (ret == UNQLITE_OK) return
228250
// std::make_shared<UnqliteCacheDriver>(database);
229251
if (ret == UNQLITE_OK)

0 commit comments

Comments
 (0)