sapling/eden/fs/store/SerializedBlobMetadata.cpp
Andrey Chursin ae684f3993 explicit Hash20 instead of Hash [proxy hash removal 2/n]
Summary:
This is fairly mechanical diff that finalizes split of Hash into ObjectId and Hash20.

More specifically this diff does two things:
* Replaces `Hash` with `Hash20`
* Removes alias `using Hash = Hash20`

Reviewed By: chadaustin

Differential Revision: D31324202

fbshipit-source-id: 780b6d2a422ddf6d0f3cfc91e3e70ad10ebaa8b4
2021-10-01 12:43:26 -07:00

67 lines
1.7 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "SerializedBlobMetadata.h"
#include <folly/Format.h>
#include <folly/Range.h>
#include <folly/lang/Bits.h>
#include <eden/fs/model/Hash.h>
#include <eden/fs/store/BlobMetadata.h>
namespace facebook::eden {
SerializedBlobMetadata::SerializedBlobMetadata(const BlobMetadata& metadata) {
serialize(metadata.sha1, metadata.size);
}
SerializedBlobMetadata::SerializedBlobMetadata(
const Hash20& contentsHash,
uint64_t blobSize) {
serialize(contentsHash, blobSize);
}
folly::ByteRange SerializedBlobMetadata::slice() const {
return folly::ByteRange{data_};
}
BlobMetadata SerializedBlobMetadata::parse(
ObjectId blobID,
const StoreResult& result) {
auto bytes = result.bytes();
if (bytes.size() != SIZE) {
throw std::invalid_argument(fmt::format(
"Blob metadata for {} had unexpected size {}. Could not deserialize.",
blobID.toString(),
bytes.size()));
}
return unslice(bytes);
}
BlobMetadata SerializedBlobMetadata::unslice(folly::ByteRange bytes) {
uint64_t blobSizeBE;
memcpy(&blobSizeBE, bytes.data(), sizeof(uint64_t));
bytes.advance(sizeof(uint64_t));
auto contentsHash = Hash20{bytes};
return BlobMetadata{contentsHash, folly::Endian::big(blobSizeBE)};
}
void SerializedBlobMetadata::serialize(
const Hash20& contentsHash,
uint64_t blobSize) {
uint64_t blobSizeBE = folly::Endian::big(blobSize);
memcpy(data_.data(), &blobSizeBE, sizeof(uint64_t));
memcpy(
data_.data() + sizeof(uint64_t),
contentsHash.getBytes().data(),
Hash20::RAW_SIZE);
}
} // namespace facebook::eden