优化删除逻辑
This commit is contained in:
parent
7f44800010
commit
e0a3406689
@ -186,16 +186,19 @@ void RecordManager::removeOldestHoursUntilSafe()
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
// 1) 检查磁盘
|
// 1) 检查磁盘使用率
|
||||||
struct statvfs vfs{};
|
struct statvfs vfs{};
|
||||||
if (statvfs(record_dir_.c_str(), &vfs) != 0) return;
|
if (statvfs(record_dir_.c_str(), &vfs) != 0) return;
|
||||||
|
|
||||||
double used = 1.0 - (double)vfs.f_bavail / (double)vfs.f_blocks;
|
double used = 1.0 - (double)vfs.f_bavail / (double)vfs.f_blocks;
|
||||||
if (used < usage_threshold_) break;
|
if (used < usage_threshold_) break;
|
||||||
|
|
||||||
// 2) 找全盘最旧的 day
|
// =====================================================
|
||||||
fs::path oldestDay;
|
// 2) 找“全盘最老日期字符串”(YYYY-MM-DD)
|
||||||
|
// =====================================================
|
||||||
|
std::string oldestDayName;
|
||||||
time_t oldestTime = 0;
|
time_t oldestTime = 0;
|
||||||
|
bool foundDay = false;
|
||||||
|
|
||||||
for (auto& streamDir : fs::directory_iterator(record_dir_))
|
for (auto& streamDir : fs::directory_iterator(record_dir_))
|
||||||
{
|
{
|
||||||
@ -213,55 +216,92 @@ void RecordManager::removeOldestHoursUntilSafe()
|
|||||||
tm.tm_mon -= 1;
|
tm.tm_mon -= 1;
|
||||||
time_t t = mktime(&tm);
|
time_t t = mktime(&tm);
|
||||||
|
|
||||||
if (oldestDay.empty() || t < oldestTime)
|
if (!foundDay || t < oldestTime)
|
||||||
{
|
{
|
||||||
|
foundDay = true;
|
||||||
oldestTime = t;
|
oldestTime = t;
|
||||||
oldestDay = dayDir.path();
|
oldestDayName = dayName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldestDay.empty())
|
if (!foundDay)
|
||||||
{
|
{
|
||||||
LOG_WARN("[RecordManager] No day folder found for hour cleanup.");
|
LOG_WARN("[RecordManager] No day folder found for hour cleanup.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 找该 day 下最旧的 hour ----
|
// =====================================================
|
||||||
fs::path oldestHour;
|
// 3) 在这个日期下,跨所有 stream 找最老 hour
|
||||||
|
// =====================================================
|
||||||
int oldestHourValue = -1;
|
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();
|
fs::path dayDir = streamDir.path() / oldestDayName;
|
||||||
int h = -1;
|
if (!fs::exists(dayDir) || !fs::is_directory(dayDir)) continue;
|
||||||
if (sscanf(hourName.c_str(), "%d", &h) != 1) continue;
|
|
||||||
|
|
||||||
if (oldestHourValue == -1 || h < oldestHourValue)
|
for (auto& hourDir : fs::directory_iterator(dayDir))
|
||||||
{
|
{
|
||||||
oldestHourValue = h;
|
if (!hourDir.is_directory()) continue;
|
||||||
oldestHour = hourDir.path();
|
|
||||||
|
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());
|
for (auto& streamDir : fs::directory_iterator(record_dir_))
|
||||||
fs::remove_all(oldestDay);
|
{
|
||||||
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 删除最旧 hour ----
|
// =====================================================
|
||||||
LOG_WARN("[RecordManager] Removing oldest hour: " + oldestHour.string());
|
// 5) 删除“同一天 + 同一小时”在所有路上的数据
|
||||||
fs::remove_all(oldestHour);
|
// =====================================================
|
||||||
|
char hourBuf[4];
|
||||||
|
snprintf(hourBuf, sizeof(hourBuf), "%02d", oldestHourValue);
|
||||||
|
std::string hourName(hourBuf);
|
||||||
|
|
||||||
// 如果此 day 已空 → 删除 day 目录
|
for (auto& streamDir : fs::directory_iterator(record_dir_))
|
||||||
if (fs::is_empty(oldestDay))
|
|
||||||
{
|
{
|
||||||
LOG_WARN("[RecordManager] Removing empty day: " + oldestDay.string());
|
if (!streamDir.is_directory()) continue;
|
||||||
fs::remove_all(oldestDay);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user