优化删除逻辑

This commit is contained in:
cxh 2025-12-02 15:12:36 +08:00
parent 7f44800010
commit e0a3406689

View File

@ -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);
}
}
}
}