aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2024-12-13 11:59:13 +0100
committerStefan Boberg <[email protected]>2024-12-13 11:59:13 +0100
commit6512c8214676662180a62d89a1aebb7b53df5e9b (patch)
treedd40a36dd89564d7f93df8a50fb880fb4438e559 /src
parentadded xxhash unit tests (which currently fail) (diff)
downloadzen-6512c8214676662180a62d89a1aebb7b53df5e9b.tar.xz
zen-6512c8214676662180a62d89a1aebb7b53df5e9b.zip
added ComputeOpKey so all instances of mapping key -> Oid is in a single place
Diffstat (limited to 'src')
-rw-r--r--src/zenserver-test/zenserver-test.cpp18
-rw-r--r--src/zenserver/projectstore/projectstore.cpp39
2 files changed, 33 insertions, 24 deletions
diff --git a/src/zenserver-test/zenserver-test.cpp b/src/zenserver-test/zenserver-test.cpp
index 1eba769d2..33c4f99cf 100644
--- a/src/zenserver-test/zenserver-test.cpp
+++ b/src/zenserver-test/zenserver-test.cpp
@@ -2864,12 +2864,22 @@ TEST_CASE("project.remote")
ZEN_ASSERT(It == AttachmentSizes.end());
}
- auto AddOp = [](const CbObject& Op, std::unordered_map<Oid, uint32_t, Oid::Hasher>& Ops) {
+ // Note: This is a clone of the function in projectstore.cpp
+ auto ComputeOpKey = [](const CbObjectView& Op) -> Oid {
+ using namespace std::literals;
+
XXH3_128Stream KeyHasher;
Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); });
- XXH3_128 KeyHash = KeyHasher.GetHash();
- Oid Id;
- memcpy(Id.OidBits, &KeyHash, sizeof Id.OidBits);
+ XXH3_128 KeyHash128 = KeyHasher.GetHash();
+
+ Oid KeyHash;
+ memcpy(&KeyHash, KeyHash128.Hash, sizeof KeyHash);
+
+ return KeyHash;
+ };
+
+ auto AddOp = [ComputeOpKey](const CbObject& Op, std::unordered_map<Oid, uint32_t, Oid::Hasher>& Ops) {
+ const Oid Id = ComputeOpKey(Op);
IoBuffer Buffer = Op.GetBuffer().AsIoBuffer();
const uint32_t OpCoreHash = uint32_t(XXH3_64bits(Buffer.GetData(), Buffer.GetSize()) & 0xffffFFFF);
Ops.insert({Id, OpCoreHash});
diff --git a/src/zenserver/projectstore/projectstore.cpp b/src/zenserver/projectstore/projectstore.cpp
index 63a80fbd8..d153e7274 100644
--- a/src/zenserver/projectstore/projectstore.cpp
+++ b/src/zenserver/projectstore/projectstore.cpp
@@ -407,6 +407,21 @@ namespace {
//////////////////////////////////////////////////////////////////////////
+Oid
+ComputeOpKey(const CbObjectView& Op)
+{
+ using namespace std::literals;
+
+ XXH3_128Stream KeyHasher;
+ Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); });
+ XXH3_128 KeyHash128 = KeyHasher.GetHash();
+
+ Oid KeyHash;
+ memcpy(&KeyHash, KeyHash128.Hash, sizeof KeyHash);
+
+ return KeyHash;
+}
+
struct ProjectStore::OplogStorage : public RefCounted
{
OplogStorage(ProjectStore::Oplog* OwnerOplog, std::filesystem::path BasePath) : m_OwnerOplog(OwnerOplog), m_OplogStoragePath(BasePath)
@@ -900,10 +915,7 @@ struct ProjectStore::OplogStorage : public RefCounted
ZEN_ASSERT(WriteSize != 0);
- XXH3_128Stream KeyHasher;
- Core["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); });
- XXH3_128 KeyHash128 = KeyHasher.GetHash();
- memcpy(&OpData.KeyHash, KeyHash128.Hash, sizeof OpData.KeyHash);
+ OpData.KeyHash = ComputeOpKey(Core);
return OpData;
}
@@ -1107,11 +1119,7 @@ ProjectStore::Oplog::ScrubStorage(ScrubContext& Ctx)
Op.IterateAttachments([&](CbFieldView Visitor) { Cids.emplace_back(Visitor.AsAttachment()); });
{
- XXH3_128Stream KeyHasher;
- Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); });
- XXH3_128 KeyHash128 = KeyHasher.GetHash();
- Oid KeyHash;
- memcpy(&KeyHash, KeyHash128.Hash, sizeof KeyHash);
+ const Oid KeyHash = ComputeOpKey(Op);
ZEN_ASSERT_FORMAT(KeyHash == Key, "oplog data does not match information from index (op:{} != index:{})", KeyHash, Key);
}
@@ -2366,11 +2374,7 @@ ProjectStore::Oplog::GetAttachmentsLocked(std::vector<IoHash>& OutAttachments, b
Op.IterateAttachments([&](CbFieldView Visitor) { OutAttachments.emplace_back(Visitor.AsAttachment()); });
if (StoreMetaDataOnDisk)
{
- XXH3_128Stream KeyHasher;
- Op["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); });
- XXH3_128 KeyHash128 = KeyHasher.GetHash();
- Oid KeyHash;
- memcpy(&KeyHash, KeyHash128.Hash, sizeof KeyHash);
+ const Oid KeyHash = ComputeOpKey(Op);
Keys.push_back(KeyHash);
AttachmentCounts.push_back(gsl::narrow<uint32_t>(OutAttachments.size() - CurrentAttachmentCount));
}
@@ -6541,12 +6545,7 @@ OpKeyStringAsOid(std::string_view OpKey)
CbObjectWriter Writer;
Writer << "key"sv << OpKey;
- XXH3_128Stream KeyHasher;
- Writer.Save()["key"sv].WriteToStream([&](const void* Data, size_t Size) { KeyHasher.Append(Data, Size); });
- XXH3_128 KeyHash = KeyHasher.GetHash();
-
- Oid OpId;
- memcpy(OpId.OidBits, &KeyHash, sizeof(OpId.OidBits));
+ const Oid OpId = ComputeOpKey(Writer.Save());
return OpId;
}