From e0a3406689cba6763e34268219318bc6252dee11 Mon Sep 17 00:00:00 2001 From: cxh Date: Tue, 2 Dec 2025 15:12:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=A0=E9=99=A4=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/record_manager.cpp | 92 ++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/src/record_manager.cpp b/src/record_manager.cpp index 7217023..02e866a 100644 --- a/src/record_manager.cpp +++ b/src/record_manager.cpp @@ -186,16 +186,19 @@ void RecordManager::removeOldestHoursUntilSafe() while (true) { - // 1) 检查磁盘 + // 1) 检查磁盘使用率 struct statvfs vfs{}; if (statvfs(record_dir_.c_str(), &vfs) != 0) return; double used = 1.0 - (double)vfs.f_bavail / (double)vfs.f_blocks; if (used < usage_threshold_) break; - // 2) 找全盘最旧的 day - fs::path oldestDay; + // ===================================================== + // 2) 找“全盘最老日期字符串”(YYYY-MM-DD) + // ===================================================== + std::string oldestDayName; time_t oldestTime = 0; + bool foundDay = false; for (auto& streamDir : fs::directory_iterator(record_dir_)) { @@ -213,55 +216,92 @@ void RecordManager::removeOldestHoursUntilSafe() tm.tm_mon -= 1; time_t t = mktime(&tm); - if (oldestDay.empty() || t < oldestTime) + if (!foundDay || t < oldestTime) { + foundDay = true; oldestTime = t; - oldestDay = dayDir.path(); + oldestDayName = dayName; } } } - if (oldestDay.empty()) + if (!foundDay) { LOG_WARN("[RecordManager] No day folder found for hour cleanup."); break; } - // ---- 找该 day 下最旧的 hour ---- - fs::path oldestHour; + // ===================================================== + // 3) 在这个日期下,跨所有 stream 找最老 hour + // ===================================================== int oldestHourValue = -1; + bool foundHour = false; - for (auto& hourDir : fs::directory_iterator(oldestDay)) + for (auto& streamDir : fs::directory_iterator(record_dir_)) { - if (!hourDir.is_directory()) continue; + if (!streamDir.is_directory()) continue; - std::string hourName = hourDir.path().filename().string(); - int h = -1; - if (sscanf(hourName.c_str(), "%d", &h) != 1) continue; + fs::path dayDir = streamDir.path() / oldestDayName; + if (!fs::exists(dayDir) || !fs::is_directory(dayDir)) continue; - if (oldestHourValue == -1 || h < oldestHourValue) + for (auto& hourDir : fs::directory_iterator(dayDir)) { - oldestHourValue = h; - oldestHour = hourDir.path(); + if (!hourDir.is_directory()) continue; + + std::string hourName = hourDir.path().filename().string(); + int h = -1; + if (sscanf(hourName.c_str(), "%d", &h) != 1) continue; + + if (!foundHour || h < oldestHourValue) + { + foundHour = true; + oldestHourValue = h; + } } } - if (oldestHour.empty()) + // ===================================================== + // 4) 如果这个日期在所有 stream 下都没有 hour → 删整天 + // ===================================================== + if (!foundHour) { - LOG_WARN("[RecordManager] Day empty, remove whole day: " + oldestDay.string()); - fs::remove_all(oldestDay); + for (auto& streamDir : fs::directory_iterator(record_dir_)) + { + fs::path dayDir = streamDir.path() / oldestDayName; + if (fs::exists(dayDir)) + { + LOG_WARN("[RecordManager] Day empty, remove whole day: " + dayDir.string()); + fs::remove_all(dayDir); + } + } continue; } - // ---- 删除最旧 hour ---- - LOG_WARN("[RecordManager] Removing oldest hour: " + oldestHour.string()); - fs::remove_all(oldestHour); + // ===================================================== + // 5) 删除“同一天 + 同一小时”在所有路上的数据 + // ===================================================== + char hourBuf[4]; + snprintf(hourBuf, sizeof(hourBuf), "%02d", oldestHourValue); + std::string hourName(hourBuf); - // 如果此 day 已空 → 删除 day 目录 - if (fs::is_empty(oldestDay)) + for (auto& streamDir : fs::directory_iterator(record_dir_)) { - LOG_WARN("[RecordManager] Removing empty day: " + oldestDay.string()); - fs::remove_all(oldestDay); + if (!streamDir.is_directory()) continue; + + fs::path hourDir = streamDir.path() / oldestDayName / hourName; + if (fs::exists(hourDir) && fs::is_directory(hourDir)) + { + LOG_WARN("[RecordManager] Removing oldest hour: " + hourDir.string()); + fs::remove_all(hourDir); + } + + // 顺便清掉空 day + fs::path dayDir = streamDir.path() / oldestDayName; + if (fs::exists(dayDir) && fs::is_directory(dayDir) && fs::is_empty(dayDir)) + { + LOG_WARN("[RecordManager] Removing empty day: " + dayDir.string()); + fs::remove_all(dayDir); + } } } }