-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.cc
More file actions
102 lines (87 loc) · 2.94 KB
/
cache.cc
File metadata and controls
102 lines (87 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <chrono>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
namespace gitcode::cache {
struct CachedDocument {
std::string path;
std::string language;
std::string sha;
std::chrono::system_clock::time_point opened_at;
int line_count = 0;
};
class WorkspaceCacheStore {
public:
bool Put(std::string workspace_id, CachedDocument document) {
auto& bucket = documents_[std::move(workspace_id)];
bucket.emplace_back(std::move(document));
return true;
}
std::optional<CachedDocument> FindByPath(
std::string_view workspace_id,
std::string_view path) const {
const auto it = documents_.find(std::string(workspace_id));
if (it == documents_.end()) {
return std::nullopt;
}
for (const auto& document : it->second) {
if (document.path == path) {
return document;
}
}
return std::nullopt;
}
size_t RemoveOlderThan(std::string_view workspace_id,
std::chrono::system_clock::time_point threshold) {
const auto it = documents_.find(std::string(workspace_id));
if (it == documents_.end()) {
return 0;
}
auto& bucket = it->second;
const auto original_size = bucket.size();
bucket.erase(
std::remove_if(bucket.begin(), bucket.end(), [&](const CachedDocument& doc) {
return doc.opened_at < threshold;
}),
bucket.end());
return original_size - bucket.size();
}
std::vector<std::string> ListLanguages(std::string_view workspace_id) const {
std::vector<std::string> languages;
const auto it = documents_.find(std::string(workspace_id));
if (it == documents_.end()) {
return languages;
}
for (const auto& document : it->second) {
if (std::find(languages.begin(), languages.end(), document.language) == languages.end()) {
languages.push_back(document.language);
}
}
return languages;
}
size_t Size(std::string_view workspace_id) const {
const auto it = documents_.find(std::string(workspace_id));
return it == documents_.end() ? 0u : it->second.size();
}
private:
std::unordered_map<std::string, std::vector<CachedDocument>> documents_;
};
} // namespace gitcode::cache
int main() {
using gitcode::cache::CachedDocument;
using gitcode::cache::WorkspaceCacheStore;
WorkspaceCacheStore store;
const auto now = std::chrono::system_clock::now();
store.Put("workspace-core", {"src/utils/string.ts", "typescript", "2c1f7a1", now, 146});
store.Put("workspace-core", {"web/codemirror/core.ts", "typescript", "71a1ce4", now, 288});
store.Put("workspace-core", {"docs/LanguageSupport.md", "markdown", "2eb3140", now, 164});
const auto doc = store.FindByPath("workspace-core", "src/utils/string.ts");
const auto languages = store.ListLanguages("workspace-core");
if (doc) {
return static_cast<int>(store.Size("workspace-core") + languages.size());
}
return 1;
}