Refactor of cache code, and fixed a crash generating Json from an empty cache object.

This commit is contained in:
Jonathan Thomas
2022-09-15 18:29:43 -05:00
parent 7c2a205d5e
commit 8a3e2eb2d0
6 changed files with 75 additions and 143 deletions

View File

@@ -42,66 +42,6 @@ CacheMemory::~CacheMemory()
delete cacheMutex;
}
// Calculate ranges of frames
void CacheMemory::CalculateRanges() {
// Only calculate when something has changed
if (needs_range_processing) {
// Create a scoped lock, to protect the cache from multiple threads
const std::lock_guard<std::recursive_mutex> lock(*cacheMutex);
// Sort ordered frame #s, and calculate JSON ranges
std::sort(ordered_frame_numbers.begin(), ordered_frame_numbers.end());
// Clear existing JSON variable
Json::Value ranges = Json::Value(Json::arrayValue);
// Increment range version
range_version++;
std::vector<int64_t>::iterator itr_ordered;
int64_t starting_frame = *ordered_frame_numbers.begin();
int64_t ending_frame = *ordered_frame_numbers.begin();
// Loop through all known frames (in sequential order)
for (itr_ordered = ordered_frame_numbers.begin(); itr_ordered != ordered_frame_numbers.end(); ++itr_ordered) {
int64_t frame_number = *itr_ordered;
if (frame_number - ending_frame > 1) {
// End of range detected
Json::Value range;
// Add JSON object with start/end attributes
// Use strings, since int64_ts are supported in JSON
range["start"] = std::to_string(starting_frame);
range["end"] = std::to_string(ending_frame);
ranges.append(range);
// Set new starting range
starting_frame = frame_number;
}
// Set current frame as end of range, and keep looping
ending_frame = frame_number;
}
// APPEND FINAL VALUE
Json::Value range;
// Add JSON object with start/end attributes
// Use strings, since int64_ts are not supported in JSON
range["start"] = std::to_string(starting_frame);
range["end"] = std::to_string(ending_frame);
ranges.append(range);
// Cache range JSON as string
json_ranges = ranges.toStyledString();
// Reset needs_range_processing
needs_range_processing = false;
}
}
// Add a Frame to the cache
void CacheMemory::Add(std::shared_ptr<Frame> frame)
{