This commit is contained in:
cxh 2025-11-14 17:47:35 +08:00
parent 7eab4eb1a2
commit 98f84845be

View File

@ -322,20 +322,63 @@ std::vector<RecordSegment> RecordManager::querySegments(const std::string& strea
//
RecordSegment RecordManager::getSegment(const std::string& segmentId)
{
auto pos1 = segmentId.find('_');
auto pos2 = segmentId.find('_', pos1 + 1);
if (pos1 == std::string::npos || pos2 == std::string::npos) return {};
// 1) 找最后一个 "_"
size_t pos_last = segmentId.rfind('_');
if (pos_last == std::string::npos) return {};
std::string stream = segmentId.substr(0, pos1);
int64_t start_ms = std::stoll(segmentId.substr(pos1 + 1, pos2 - pos1 - 1));
int64_t end_ms = std::stoll(segmentId.substr(pos2 + 1));
// 2) 再往前找一个 "_"
size_t pos_mid = segmentId.rfind('_', pos_last - 1);
if (pos_mid == std::string::npos) return {};
auto segs = querySegments(stream, start_ms, end_ms);
for (auto& s : segs)
// 3) 三段拆分
std::string stream = segmentId.substr(0, pos_mid);
std::string startStr = segmentId.substr(pos_mid + 1, pos_last - pos_mid - 1);
std::string endStr = segmentId.substr(pos_last + 1);
int64_t start_ms = 0;
int64_t end_ms = 0;
try
{
if (s.start_ms == start_ms && s.end_ms == end_ms) return s;
start_ms = std::stoll(startStr);
end_ms = std::stoll(endStr);
}
return {};
catch (...)
{
LOG_ERROR("[RecordManager] segmentId parse failed: " + segmentId);
return {};
}
// 确认 stream 在 index_ 里
if (!index_.count(stream))
{
LOG_WARN("[RecordManager] stream not exist in index: " + stream);
return {};
}
// 4) 根据时间区间收集属于该 segment 的所有文件
const auto& files = index_[stream];
RecordSegment seg;
seg.segment_id = segmentId;
seg.index = 1;
seg.start_ms = start_ms;
seg.end_ms = end_ms;
for (const auto& f : files)
{
// 与 segment 时间区间有重叠的文件都属于这个 segment
bool overlap = !(f.end_ms <= start_ms || f.start_ms >= end_ms);
if (overlap) seg.files.push_back(f);
}
if (seg.files.empty())
{
LOG_WARN("[RecordManager] No files found for segmentId: " + segmentId);
return {};
}
return seg;
}
int64_t RecordManager::toMsTimestamp(const std::string& s)