-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsession_trajectory.cpp
More file actions
244 lines (217 loc) · 7.94 KB
/
Copy pathsession_trajectory.cpp
File metadata and controls
244 lines (217 loc) · 7.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
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
#include "session_trajectory.hpp"
#include "../utils/utf8_path.hpp"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <limits>
#include <mutex>
#include <string_view>
namespace fs = std::filesystem;
namespace acecode {
namespace {
std::mutex& trajectory_append_mutex() {
static std::mutex mu;
return mu;
}
bool parse_record_line(std::string_view line,
SessionTrajectoryRecord* record) {
if (!record || line.empty()) return false;
try {
return session_trajectory_record_from_json(
nlohmann::json::parse(line.begin(), line.end()), record);
} catch (...) {
return false;
}
}
template <typename Visitor>
SessionTrajectoryLoadDiagnostics visit_records(const std::string& path,
Visitor&& visitor) {
SessionTrajectoryLoadDiagnostics diagnostics;
std::ifstream input(path_from_utf8(path), std::ios::binary);
if (!input.is_open()) return diagnostics;
const std::string content((std::istreambuf_iterator<char>(input)),
std::istreambuf_iterator<char>());
std::size_t start = 0;
while (start < content.size()) {
const std::size_t newline = content.find('\n', start);
const bool is_tail = newline == std::string::npos;
const std::size_t end = is_tail ? content.size() : newline;
std::string_view line(content.data() + start, end - start);
if (!line.empty() && line.back() == '\r') line.remove_suffix(1);
if (!line.empty()) {
SessionTrajectoryRecord record;
if (parse_record_line(line, &record)) {
if (is_tail) diagnostics.recovered_unterminated_record = true;
if (!visitor(record)) return diagnostics;
} else if (is_tail) {
diagnostics.ignored_partial_tail = true;
} else {
++diagnostics.malformed_complete_records;
}
}
if (is_tail) break;
start = newline + 1;
}
return diagnostics;
}
} // namespace
nlohmann::json session_trajectory_record_to_json(
const SessionTrajectoryRecord& record) {
return nlohmann::json{
{"schema_version", record.schema_version},
{"sequence", record.sequence},
{"timestamp_ms", record.timestamp_ms},
{"type", record.type},
{"payload", record.payload.is_null()
? nlohmann::json::object()
: record.payload},
};
}
bool session_trajectory_record_from_json(
const nlohmann::json& value,
SessionTrajectoryRecord* record) {
if (!record || !value.is_object()) return false;
if (!value.contains("schema_version") ||
!value["schema_version"].is_number_integer() ||
!value.contains("sequence") ||
(!value["sequence"].is_number_unsigned() &&
!value["sequence"].is_number_integer()) ||
!value.contains("timestamp_ms") ||
(!value["timestamp_ms"].is_number_integer() &&
!value["timestamp_ms"].is_number_unsigned()) ||
!value.contains("type") || !value["type"].is_string() ||
value["type"].get_ref<const std::string&>().empty()) {
return false;
}
const auto schema_version = value["schema_version"].get<int>();
if (schema_version <= 0) return false;
std::uint64_t sequence = 0;
if (value["sequence"].is_number_unsigned()) {
sequence = value["sequence"].get<std::uint64_t>();
} else {
const auto signed_sequence = value["sequence"].get<std::int64_t>();
if (signed_sequence <= 0) return false;
sequence = static_cast<std::uint64_t>(signed_sequence);
}
if (sequence == 0) return false;
std::int64_t timestamp_ms = 0;
if (value["timestamp_ms"].is_number_unsigned()) {
const auto raw = value["timestamp_ms"].get<std::uint64_t>();
if (raw > static_cast<std::uint64_t>(
std::numeric_limits<std::int64_t>::max())) {
return false;
}
timestamp_ms = static_cast<std::int64_t>(raw);
} else {
timestamp_ms = value["timestamp_ms"].get<std::int64_t>();
}
if (timestamp_ms < 0) return false;
record->schema_version = schema_version;
record->sequence = sequence;
record->timestamp_ms = timestamp_ms;
record->type = value["type"].get<std::string>();
record->payload = value.value("payload", nlohmann::json::object());
return true;
}
std::string SessionTrajectoryStorage::file_path(
const std::string& project_dir,
const std::string& session_id) {
if (project_dir.empty() || session_id.empty()) return {};
return path_to_utf8(path_from_utf8(project_dir) /
path_from_utf8(session_id) /
"trajectory.jsonl");
}
bool SessionTrajectoryStorage::append(
const std::string& path,
const SessionTrajectoryRecord& record) {
if (path.empty() || record.sequence == 0 || record.type.empty() ||
record.schema_version <= 0 || record.timestamp_ms < 0) {
return false;
}
std::string encoded;
try {
encoded = session_trajectory_record_to_json(record).dump(-1);
} catch (...) {
return false;
}
std::lock_guard<std::mutex> lock(trajectory_append_mutex());
const fs::path native_path = path_from_utf8(path);
std::error_code ec;
const fs::path parent = native_path.parent_path();
if (!parent.empty()) {
fs::create_directories(parent, ec);
if (ec) return false;
}
bool isolate_existing_tail = false;
if (fs::exists(native_path, ec)) {
if (ec || !fs::is_regular_file(native_path, ec) || ec) return false;
const auto size = fs::file_size(native_path, ec);
if (ec) return false;
if (size > 0) {
std::ifstream tail(native_path, std::ios::binary);
if (!tail.is_open()) return false;
tail.seekg(-1, std::ios::end);
char last = '\0';
tail.read(&last, 1);
if (!tail.good()) return false;
isolate_existing_tail = last != '\n';
}
} else if (ec) {
return false;
}
std::ofstream output(native_path, std::ios::binary | std::ios::app);
if (!output.is_open()) return false;
if (isolate_existing_tail) output.put('\n');
output.write(encoded.data(), static_cast<std::streamsize>(encoded.size()));
output.put('\n');
output.flush();
return output.good();
}
SessionTrajectoryPage SessionTrajectoryStorage::load_page(
const std::string& path,
std::uint64_t after,
std::size_t limit) {
SessionTrajectoryPage page;
limit = std::clamp<std::size_t>(
limit == 0 ? kSessionTrajectoryDefaultPageSize : limit,
1,
kSessionTrajectoryMaxPageSize);
bool saw_extra = false;
page.diagnostics = visit_records(path, [&](const SessionTrajectoryRecord& record) {
if (record.sequence <= after) return true;
if (page.records.size() >= limit) {
saw_extra = true;
return false;
}
page.records.push_back(record);
return true;
});
page.has_more = saw_extra;
page.next_after = page.records.empty()
? after
: page.records.back().sequence;
return page;
}
std::vector<SessionTrajectoryRecord> SessionTrajectoryStorage::load_all(
const std::string& path,
SessionTrajectoryLoadDiagnostics* diagnostics) {
std::vector<SessionTrajectoryRecord> records;
auto loaded_diagnostics = visit_records(
path, [&records](const SessionTrajectoryRecord& record) {
records.push_back(record);
return true;
});
if (diagnostics) *diagnostics = loaded_diagnostics;
return records;
}
std::uint64_t SessionTrajectoryStorage::last_sequence(
const std::string& path) {
std::uint64_t last = 0;
visit_records(path, [&](const SessionTrajectoryRecord& record) {
last = std::max(last, record.sequence);
return true;
});
return last;
}
} // namespace acecode