[wpiutil] DataLog: Move schema info to a separate map (#7626)

This commit is contained in:
Peter Johnson
2025-01-03 13:28:50 -08:00
committed by GitHub
parent 93521420c8
commit 148fcdca85
2 changed files with 19 additions and 14 deletions

View File

@@ -86,10 +86,13 @@ void DataLog::StartFile() {
AppendStartRecord(entryInfo.second.id, entryInfo.first, AppendStartRecord(entryInfo.second.id, entryInfo.first,
entryInfo.second.type, entryInfo.second.type,
m_entryIds[entryInfo.second.id].metadata, 0); m_entryIds[entryInfo.second.id].metadata, 0);
if (!entryInfo.second.schemaData.empty()) { }
StartRecord(entryInfo.second.id, 0, entryInfo.second.schemaData.size(),
0); // Existing schema data records
AppendImpl(entryInfo.second.schemaData); for (auto&& schemaInfo : m_schemas) {
if (schemaInfo.second.id != 0) {
StartRecord(schemaInfo.second.id, 0, schemaInfo.second.data.size(), 0);
AppendImpl(schemaInfo.second.data);
} }
} }
@@ -131,28 +134,26 @@ void DataLog::BufferHalfFull() {}
bool DataLog::HasSchema(std::string_view name) const { bool DataLog::HasSchema(std::string_view name) const {
std::scoped_lock lock{m_mutex}; std::scoped_lock lock{m_mutex};
wpi::SmallString<128> fullName{"/.schema/"}; return m_schemas.contains(name);
fullName += name;
auto it = m_entries.find(fullName);
return it != m_entries.end();
} }
void DataLog::AddSchema(std::string_view name, std::string_view type, void DataLog::AddSchema(std::string_view name, std::string_view type,
std::span<const uint8_t> schema, int64_t timestamp) { std::span<const uint8_t> schema, int64_t timestamp) {
std::scoped_lock lock{m_mutex}; std::scoped_lock lock{m_mutex};
wpi::SmallString<128> fullName{"/.schema/"}; auto& schemaInfo = m_schemas[name];
fullName += name; if (schemaInfo.id != 0) {
auto& entryInfo = m_entries[fullName];
if (entryInfo.id != 0) {
return; // don't add duplicates return; // don't add duplicates
} }
entryInfo.schemaData.assign(schema.begin(), schema.end()); schemaInfo.data.assign(schema.begin(), schema.end());
wpi::SmallString<128> fullName{"/.schema/"};
fullName += name;
int entry = StartImpl(fullName, type, {}, timestamp); int entry = StartImpl(fullName, type, {}, timestamp);
// inline AppendRaw() without releasing lock // inline AppendRaw() without releasing lock
if (entry <= 0) { if (entry <= 0) {
[[unlikely]] return; // should never happen, but check anyway [[unlikely]] return; // should never happen, but check anyway
} }
schemaInfo.id = entry;
if (!m_active) { if (!m_active) {
[[unlikely]] return; [[unlikely]] return;
} }

View File

@@ -514,10 +514,14 @@ class DataLog {
std::vector<Buffer> m_outgoing; std::vector<Buffer> m_outgoing;
struct EntryInfo { struct EntryInfo {
std::string type; std::string type;
std::vector<uint8_t> schemaData; // only set for schema entries
int id{0}; int id{0};
}; };
wpi::StringMap<EntryInfo> m_entries; wpi::StringMap<EntryInfo> m_entries;
struct SchemaInfo {
std::vector<uint8_t> data;
int id{0};
};
wpi::StringMap<SchemaInfo> m_schemas;
struct EntryInfo2 { struct EntryInfo2 {
std::string metadata; std::string metadata;
unsigned int count; unsigned int count;