Prechádzať zdrojové kódy

Prune version history immediately when maxVersions is reduced in alterCollection

When a migration or direct call reduces maxVersions on a collection,
immediately prune all documents' version history to the new limit.
This prevents memory bloat from accumulated history entries.
Fszontagh 5 mesiacov pred
rodič
commit
42332ffaad
1 zmenil súbory, kde vykonal 22 pridanie a 3 odobranie
  1. 22 3
      service/src/memory_store.cpp

+ 22 - 3
service/src/memory_store.cpp

@@ -130,12 +130,31 @@ bool MemoryStore::alterCollection(const std::string& name, const CollectionOptio
 
     std::unique_lock<std::shared_mutex> collLock(it->second->mutex);
 
-    // Update collection options
+    // Check if maxVersions is being set/reduced - need to prune existing history
+    uint32_t oldMaxVersions = it->second->options.maxVersions;
+    uint32_t newMaxVersions = options.maxVersions;
+
+    // Update collection options first
     it->second->options = options;
     it->second->updatedAt = currentTimeMs();
 
-    spdlog::debug("Altered collection '{}': pinned={}, encrypted={}",
-                  name, options.pinned, options.encrypted);
+    // If maxVersions is now limited (and was unlimited or higher before), prune all history
+    if (newMaxVersions > 0 && (oldMaxVersions == 0 || newMaxVersions < oldMaxVersions)) {
+        uint64_t prunedVersions = 0;
+        for (auto& [docId, history] : it->second->versionHistory) {
+            while (history.size() > newMaxVersions) {
+                history.pop_front();
+                ++prunedVersions;
+            }
+        }
+        if (prunedVersions > 0) {
+            spdlog::info("Pruned {} version history entries from collection '{}' (maxVersions: {} -> {})",
+                         prunedVersions, name, oldMaxVersions, newMaxVersions);
+        }
+    }
+
+    spdlog::debug("Altered collection '{}': pinned={}, encrypted={}, maxVersions={}",
+                  name, options.pinned, options.encrypted, options.maxVersions);
 
     return true;
 }