|
|
@@ -1,5 +1,7 @@
|
|
|
#include "memory_store.hpp"
|
|
|
|
|
|
+#include "config/collection_config_manager.hpp"
|
|
|
+
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
|
#include <algorithm>
|
|
|
@@ -237,7 +239,7 @@ std::string MemoryStore::insert(const std::string& collection, Document doc) {
|
|
|
// Set metadata
|
|
|
doc.collection = collection;
|
|
|
doc.version = 1;
|
|
|
- doc.createdAt = currentTimeMs();
|
|
|
+ doc.createdAt = currentTimeFor(collection);
|
|
|
doc.updatedAt = doc.createdAt;
|
|
|
doc.nodeId = config_.nodeId;
|
|
|
|
|
|
@@ -373,7 +375,7 @@ bool MemoryStore::update(const std::string& collection, const std::string& id, c
|
|
|
updated.version = it->second.version + 1;
|
|
|
updated.createdAt = it->second.createdAt;
|
|
|
updated.createdBy = it->second.createdBy; // Preserve original creator
|
|
|
- updated.updatedAt = currentTimeMs();
|
|
|
+ updated.updatedAt = currentTimeFor(collection);
|
|
|
updated.nodeId = config_.nodeId;
|
|
|
|
|
|
// Add to new expiration index
|
|
|
@@ -450,7 +452,7 @@ std::string MemoryStore::upsert(const std::string& collection, Document doc) {
|
|
|
isInsert = true;
|
|
|
doc.collection = collection;
|
|
|
doc.version = 1;
|
|
|
- doc.createdAt = currentTimeMs();
|
|
|
+ doc.createdAt = currentTimeFor(collection);
|
|
|
doc.updatedAt = doc.createdAt;
|
|
|
doc.nodeId = config_.nodeId;
|
|
|
|
|
|
@@ -476,7 +478,7 @@ std::string MemoryStore::upsert(const std::string& collection, Document doc) {
|
|
|
doc.version = it->second.version + 1;
|
|
|
doc.createdAt = it->second.createdAt;
|
|
|
doc.createdBy = it->second.createdBy; // Preserve original creator
|
|
|
- doc.updatedAt = currentTimeMs();
|
|
|
+ doc.updatedAt = currentTimeFor(collection);
|
|
|
doc.nodeId = config_.nodeId;
|
|
|
|
|
|
if (doc.expiresAt > 0) {
|
|
|
@@ -620,7 +622,7 @@ bool MemoryStore::updateIfVersion(const std::string& collection, const std::stri
|
|
|
updated.version = expectedVersion + 1;
|
|
|
updated.createdAt = it->second.createdAt;
|
|
|
updated.createdBy = it->second.createdBy; // Preserve original creator
|
|
|
- updated.updatedAt = currentTimeMs();
|
|
|
+ updated.updatedAt = currentTimeFor(collection);
|
|
|
updated.nodeId = config_.nodeId;
|
|
|
|
|
|
// Add to new expiration index
|
|
|
@@ -690,7 +692,7 @@ uint64_t MemoryStore::patchDocument(const std::string& collection, const std::st
|
|
|
|
|
|
// Update metadata
|
|
|
it->second.version++;
|
|
|
- it->second.updatedAt = currentTimeMs();
|
|
|
+ it->second.updatedAt = currentTimeFor(collection);
|
|
|
it->second.nodeId = config_.nodeId;
|
|
|
if (!actor.empty()) {
|
|
|
it->second.updatedBy = actor;
|
|
|
@@ -926,7 +928,7 @@ bool MemoryStore::setAdd(const std::string& collection, const std::string& setId
|
|
|
doc.data = nlohmann::json::object();
|
|
|
doc.data["members"] = nlohmann::json::array({member});
|
|
|
doc.version = 1;
|
|
|
- doc.createdAt = currentTimeMs();
|
|
|
+ doc.createdAt = currentTimeFor(collection);
|
|
|
doc.updatedAt = doc.createdAt;
|
|
|
doc.nodeId = config_.nodeId;
|
|
|
|
|
|
@@ -967,7 +969,7 @@ bool MemoryStore::setAdd(const std::string& collection, const std::string& setId
|
|
|
saveToHistory(*coll, it->second);
|
|
|
members.push_back(member);
|
|
|
it->second.version++;
|
|
|
- it->second.updatedAt = currentTimeMs();
|
|
|
+ it->second.updatedAt = currentTimeFor(collection);
|
|
|
it->second.nodeId = config_.nodeId;
|
|
|
coll->updatedAt = it->second.updatedAt;
|
|
|
|
|
|
@@ -1018,7 +1020,7 @@ bool MemoryStore::setRemove(const std::string& collection, const std::string& se
|
|
|
saveToHistory(*coll, it->second);
|
|
|
it->second.data["members"] = newMembers;
|
|
|
it->second.version++;
|
|
|
- it->second.updatedAt = currentTimeMs();
|
|
|
+ it->second.updatedAt = currentTimeFor(collection);
|
|
|
it->second.nodeId = config_.nodeId;
|
|
|
coll->updatedAt = it->second.updatedAt;
|
|
|
|
|
|
@@ -1594,7 +1596,7 @@ uint64_t MemoryStore::restoreToVersion(const std::string& collection, const std:
|
|
|
newVersion = docIt->second.version + 1;
|
|
|
docIt->second.data = targetVer->data;
|
|
|
docIt->second.version = newVersion;
|
|
|
- docIt->second.updatedAt = currentTimeMs();
|
|
|
+ docIt->second.updatedAt = currentTimeFor(collection);
|
|
|
docIt->second.updatedBy = actor;
|
|
|
docIt->second.encrypted = targetVer->encrypted;
|
|
|
docIt->second.encryptedFields = targetVer->encryptedFields;
|
|
|
@@ -1616,7 +1618,7 @@ uint64_t MemoryStore::restoreToVersion(const std::string& collection, const std:
|
|
|
restoredDoc.version = newVersion;
|
|
|
restoredDoc.createdAt = targetVer->createdAt;
|
|
|
restoredDoc.createdBy = targetVer->createdBy;
|
|
|
- restoredDoc.updatedAt = currentTimeMs();
|
|
|
+ restoredDoc.updatedAt = currentTimeFor(collection);
|
|
|
restoredDoc.updatedBy = actor;
|
|
|
restoredDoc.encrypted = targetVer->encrypted;
|
|
|
restoredDoc.encryptedFields = targetVer->encryptedFields;
|
|
|
@@ -1871,6 +1873,22 @@ uint64_t MemoryStore::currentTimeMs() {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+uint64_t MemoryStore::currentTimeFor(const std::string& collection) const {
|
|
|
+ // Fast path: no manager attached → ms precision (default)
|
|
|
+ std::string precision = "ms";
|
|
|
+ if (configManager_) {
|
|
|
+ precision = configManager_->configFor(collection).timestampPrecision;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto now = std::chrono::system_clock::now().time_since_epoch();
|
|
|
+ if (precision == "ns") {
|
|
|
+ return static_cast<uint64_t>(
|
|
|
+ std::chrono::duration_cast<std::chrono::nanoseconds>(now).count());
|
|
|
+ }
|
|
|
+ return static_cast<uint64_t>(
|
|
|
+ std::chrono::duration_cast<std::chrono::milliseconds>(now).count());
|
|
|
+}
|
|
|
+
|
|
|
bool MemoryStore::matchesFilters(const Document& doc, const std::vector<Filter>& filters) const {
|
|
|
for (const auto& filter : filters) {
|
|
|
auto value = getJsonPath(doc.data, filter.field);
|