Skip to content

Commit

Permalink
[wpiutil] DataLog: Move schema info to a separate map
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Jan 3, 2025
1 parent da90ffd commit ccba7f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
27 changes: 14 additions & 13 deletions wpiutil/src/main/native/cpp/DataLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ void DataLog::StartFile() {
AppendStartRecord(entryInfo.second.id, entryInfo.first,
entryInfo.second.type,
m_entryIds[entryInfo.second.id].metadata, 0);
if (!entryInfo.second.schemaData.empty()) {
StartRecord(entryInfo.second.id, 0, entryInfo.second.schemaData.size(),
0);
AppendImpl(entryInfo.second.schemaData);
}

// Existing schema data records
for (auto&& schemaInfo : m_schemas) {
if (schemaInfo.second.id != 0) {
StartRecord(schemaInfo.second.id, 0, schemaInfo.second.data.size(), 0);
AppendImpl(schemaInfo.second.data);
}
}

Expand Down Expand Up @@ -131,28 +134,26 @@ void DataLog::BufferHalfFull() {}

bool DataLog::HasSchema(std::string_view name) const {
std::scoped_lock lock{m_mutex};
wpi::SmallString<128> fullName{"/.schema/"};
fullName += name;
auto it = m_entries.find(fullName);
return it != m_entries.end();
return m_schemas.contains(name);
}

void DataLog::AddSchema(std::string_view name, std::string_view type,
std::span<const uint8_t> schema, int64_t timestamp) {
std::scoped_lock lock{m_mutex};
wpi::SmallString<128> fullName{"/.schema/"};
fullName += name;
auto& entryInfo = m_entries[fullName];
if (entryInfo.id != 0) {
auto& schemaInfo = m_schemas[name];
if (schemaInfo.id != 0) {
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);

// inline AppendRaw() without releasing lock
if (entry <= 0) {
[[unlikely]] return; // should never happen, but check anyway
}
schemaInfo.id = entry;
if (!m_active) {
[[unlikely]] return;
}
Expand Down
6 changes: 5 additions & 1 deletion wpiutil/src/main/native/include/wpi/DataLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,14 @@ class DataLog {
std::vector<Buffer> m_outgoing;
struct EntryInfo {
std::string type;
std::vector<uint8_t> schemaData; // only set for schema entries
int id{0};
};
wpi::StringMap<EntryInfo> m_entries;
struct SchemaInfo {
std::vector<uint8_t> data;
int id{0};
};
wpi::StringMap<SchemaInfo> m_schemas;
struct EntryInfo2 {
std::string metadata;
unsigned int count;
Expand Down

0 comments on commit ccba7f1

Please sign in to comment.