-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsession_pin_store.cpp
More file actions
249 lines (219 loc) · 7.36 KB
/
Copy pathsession_pin_store.cpp
File metadata and controls
249 lines (219 loc) · 7.36 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "session_pin_store.hpp"
#include <algorithm>
#include <fstream>
#include <system_error>
#include <unordered_set>
#include <nlohmann/json.hpp>
namespace acecode::session_pins {
namespace fs = std::filesystem;
using nlohmann::json;
namespace {
std::string pinned_order_key(const PinnedSessionOrderItem& item) {
return item.workspace_hash + '\0' + item.session_id;
}
} // namespace
bool operator==(const PinnedSessionOrderItem& lhs,
const PinnedSessionOrderItem& rhs) {
return lhs.workspace_hash == rhs.workspace_hash &&
lhs.session_id == rhs.session_id;
}
bool operator!=(const PinnedSessionOrderItem& lhs,
const PinnedSessionOrderItem& rhs) {
return !(lhs == rhs);
}
std::vector<std::string> normalize_pinned_session_ids(
const std::vector<std::string>& ids) {
std::vector<std::string> out;
std::unordered_set<std::string> seen;
out.reserve(ids.size());
for (const auto& id : ids) {
if (id.empty() || seen.count(id)) continue;
seen.insert(id);
out.push_back(id);
}
return out;
}
std::vector<std::string> pin_session_id(
const std::vector<std::string>& ids,
const std::string& session_id) {
if (session_id.empty()) return normalize_pinned_session_ids(ids);
auto out = normalize_pinned_session_ids(ids);
out.erase(std::remove(out.begin(), out.end(), session_id), out.end());
out.insert(out.begin(), session_id);
return out;
}
std::vector<std::string> unpin_session_id(
const std::vector<std::string>& ids,
const std::string& session_id) {
auto out = normalize_pinned_session_ids(ids);
if (session_id.empty()) return out;
out.erase(std::remove(out.begin(), out.end(), session_id), out.end());
return out;
}
std::vector<std::string> prune_pinned_session_ids(
const std::vector<std::string>& ids,
const std::vector<std::string>& available_session_ids) {
std::unordered_set<std::string> available;
for (const auto& id : available_session_ids) {
if (!id.empty()) available.insert(id);
}
std::vector<std::string> out;
for (const auto& id : normalize_pinned_session_ids(ids)) {
if (available.count(id)) out.push_back(id);
}
return out;
}
PinnedSessionsState read_pinned_sessions_state(const fs::path& path) {
std::ifstream in(path);
if (!in) return {};
try {
auto root = json::parse(in, nullptr, true, true);
if (!root.is_object() || !root.contains("session_ids") ||
!root["session_ids"].is_array()) {
return {};
}
std::vector<std::string> ids;
for (const auto& item : root["session_ids"]) {
if (item.is_string()) ids.push_back(item.get<std::string>());
}
return {normalize_pinned_session_ids(ids)};
} catch (...) {
return {};
}
}
bool write_pinned_sessions_state(const fs::path& path,
const PinnedSessionsState& state,
std::string* error) {
std::error_code ec;
if (!path.parent_path().empty()) {
fs::create_directories(path.parent_path(), ec);
if (ec) {
if (error) *error = ec.message();
return false;
}
}
json root;
root["session_ids"] = normalize_pinned_session_ids(state.session_ids);
auto tmp = path;
tmp += ".tmp";
{
std::ofstream out(tmp, std::ios::binary | std::ios::trunc);
if (!out) {
if (error) *error = "cannot open state file for write";
return false;
}
out << root.dump(2) << '\n';
if (!out) {
if (error) *error = "failed to write state file";
return false;
}
}
fs::remove(path, ec);
ec.clear();
fs::rename(tmp, path, ec);
if (ec) {
fs::remove(tmp);
if (error) *error = ec.message();
return false;
}
return true;
}
std::vector<PinnedSessionOrderItem> normalize_pinned_session_order_items(
const std::vector<PinnedSessionOrderItem>& items) {
std::vector<PinnedSessionOrderItem> out;
std::unordered_set<std::string> seen;
out.reserve(items.size());
for (const auto& item : items) {
if (item.workspace_hash.empty() || item.session_id.empty()) continue;
const auto key = pinned_order_key(item);
if (seen.count(key)) continue;
seen.insert(key);
out.push_back(item);
}
return out;
}
std::vector<PinnedSessionOrderItem> prune_pinned_session_order_items(
const std::vector<PinnedSessionOrderItem>& items,
const std::vector<PinnedSessionOrderItem>& available_items) {
std::unordered_set<std::string> available;
for (const auto& item : available_items) {
if (item.workspace_hash.empty() || item.session_id.empty()) continue;
available.insert(pinned_order_key(item));
}
std::vector<PinnedSessionOrderItem> out;
for (const auto& item : normalize_pinned_session_order_items(items)) {
if (available.count(pinned_order_key(item))) out.push_back(item);
}
return out;
}
PinnedSessionOrderState read_pinned_session_order_state(const fs::path& path) {
std::ifstream in(path);
if (!in) return {};
try {
auto root = json::parse(in, nullptr, true, true);
if (!root.is_object() || !root.contains("items") ||
!root["items"].is_array()) {
return {};
}
std::vector<PinnedSessionOrderItem> items;
for (const auto& raw : root["items"]) {
if (!raw.is_object()) continue;
PinnedSessionOrderItem item;
if (raw.contains("workspace_hash") &&
raw["workspace_hash"].is_string()) {
item.workspace_hash = raw["workspace_hash"].get<std::string>();
}
if (raw.contains("session_id") && raw["session_id"].is_string()) {
item.session_id = raw["session_id"].get<std::string>();
}
items.push_back(std::move(item));
}
return {normalize_pinned_session_order_items(items)};
} catch (...) {
return {};
}
}
bool write_pinned_session_order_state(const fs::path& path,
const PinnedSessionOrderState& state,
std::string* error) {
std::error_code ec;
if (!path.parent_path().empty()) {
fs::create_directories(path.parent_path(), ec);
if (ec) {
if (error) *error = ec.message();
return false;
}
}
json root;
root["items"] = json::array();
for (const auto& item : normalize_pinned_session_order_items(state.items)) {
root["items"].push_back(json{
{"workspace_hash", item.workspace_hash},
{"session_id", item.session_id},
});
}
auto tmp = path;
tmp += ".tmp";
{
std::ofstream out(tmp, std::ios::binary | std::ios::trunc);
if (!out) {
if (error) *error = "cannot open state file for write";
return false;
}
out << root.dump(2) << '\n';
if (!out) {
if (error) *error = "failed to write state file";
return false;
}
}
fs::remove(path, ec);
ec.clear();
fs::rename(tmp, path, ec);
if (ec) {
fs::remove(tmp);
if (error) *error = ec.message();
return false;
}
return true;
}
} // namespace acecode::session_pins