From a63dc510c62830382f243e965be45b705d396879 Mon Sep 17 00:00:00 2001 From: Per Larsson Date: Thu, 21 Oct 2021 08:21:45 +0200 Subject: Compressed oplog attachments --- zencore/compactbinarypackage.cpp | 46 ++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'zencore/compactbinarypackage.cpp') diff --git a/zencore/compactbinarypackage.cpp b/zencore/compactbinarypackage.cpp index 88757d47f..f7ce371c8 100644 --- a/zencore/compactbinarypackage.cpp +++ b/zencore/compactbinarypackage.cpp @@ -635,6 +635,11 @@ namespace legacy { Writer.AddBinary(Attachment.AsBinary()); Writer.AddBinaryAttachment(Attachment.GetHash()); } + else if (Attachment.IsCompressedBinary()) + { + Writer.AddBinary(Attachment.AsCompressedBinary().GetCompressed()); + Writer.AddBinaryAttachment(Attachment.GetHash()); + } else if (Attachment.IsNull()) { Writer.AddBinary(MemoryView()); @@ -695,17 +700,32 @@ namespace legacy { SharedBuffer Buffer = SharedBuffer::MakeView(View, ValueField.GetOuterBuffer()).MakeOwned(); CbField HashField = LoadCompactBinary(Reader, Allocator); const IoHash& Hash = HashField.AsAttachment(); - if (HashField.HasError() || IoHash::HashBuffer(Buffer) != Hash) + if (HashField.HasError()) { return false; } - if (HashField.IsObjectAttachment()) + if (CompressedBuffer Compressed = CompressedBuffer::FromCompressed(Buffer)) { - Package.AddAttachment(CbAttachment(CbObject(std::move(Buffer)), Hash)); + if (IoHash::FromBLAKE3(Compressed.GetRawHash()) != Hash) + { + return false; + } + Package.AddAttachment(CbAttachment(Compressed)); } else { - Package.AddAttachment(CbAttachment(CompositeBuffer(std::move(Buffer)), Hash)); + if (IoHash::HashBuffer(Buffer) != Hash) + { + return false; + } + if (HashField.IsObjectAttachment()) + { + Package.AddAttachment(CbAttachment(CbObject(std::move(Buffer)), Hash)); + } + else + { + Package.AddAttachment(CbAttachment(CompositeBuffer(std::move(Buffer)), Hash)); + } } } } @@ -714,8 +734,22 @@ namespace legacy { const IoHash Hash = ValueField.AsHash(); ZEN_ASSERT(Mapper); - - Package.AddAttachment(CbAttachment((*Mapper)(Hash), Hash)); + if (SharedBuffer AttachmentData = (*Mapper)(Hash)) + { + if (CompressedBuffer Compressed = CompressedBuffer::FromCompressed(AttachmentData)) + { + Package.AddAttachment(CbAttachment(Compressed)); + } + else + { + const CbValidateError ValidationResult = ValidateCompactBinary(AttachmentData.GetView(), CbValidateMode::All); + if (ValidationResult != CbValidateError::None) + { + return false; + } + Package.AddAttachment(CbAttachment(CbObject(std::move(AttachmentData)), Hash)); + } + } } else { -- cgit v1.2.3