sapling/eden/fs/service/ThriftUtil.h

90 lines
2.5 KiB
C
Raw Normal View History

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#pragma once
#include <folly/Range.h>
#include <string>
#include "eden/fs/model/Hash.h"
#include "eden/fs/model/RootId.h"
#include "eden/fs/utils/EdenError.h"
namespace facebook::eden {
/**
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
* Convert a Hash to a std::string to be returned via thrift as a thrift
* BinaryHash data type.
*/
inline std::string thriftHash20(const Hash20& hash) {
return folly::StringPiece{hash.getBytes()}.str();
}
/**
* Convert thrift BinaryHash data type into a Hash20 object.
*
* This allows the input to be either a 20-byte binary string, or a 40-byte
* hexadecimal string.
*/
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
inline Hash20 hash20FromThrift(folly::StringPiece commitID) {
if (commitID.size() == Hash20::RAW_SIZE) {
// This looks like 20 bytes of binary data.
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
return Hash20(folly::ByteRange(folly::StringPiece(commitID)));
} else if (commitID.size() == 2 * Hash20::RAW_SIZE) {
// This looks like 40 bytes of hexadecimal data.
separate out ObjectId [proxy hash removal 1/n] Summary: The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase. For the long time EdenFs had single Hash type that was used for many different use cases. One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others. We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId. We still retain original Hash type for other use cases. Roughly speaking, this is how this diff separates between Hash and ObjectId: **ObjectId**: * Everything that is stored in local store(blobs, trees, commits) **Hash20**: * Explicit hashes(Sha1 of the blob) * Hg identifiers: manifest id and blob hg ig For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review. There are few more things that needs to be done before we can get to the meat of removing proxy hashes: 1) Replace include Hash.h with ObjectId.h where needed 2) Remove Hash type, explicitly rename rest of Hash usages to Hash20 3) Modify content of ObjectId to support new use cases 4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size Reviewed By: chadaustin Differential Revision: D31316477 fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 20:24:21 +03:00
return Hash20(commitID);
} else {
throw newEdenError(
EINVAL,
EdenErrorType::ARGUMENT_ERROR,
"expected argument to be a 20-byte binary hash or "
"40-byte hexadecimal hash; got \"",
commitID,
"\"");
}
}
inline std::string thriftHash32(const Hash32& hash) {
return folly::StringPiece{hash.getBytes()}.str();
}
/**
* Convert thrift BinaryHash data type into a Hash20 object.
*
* This allows the input to be either a 32-byte binary string, or a 64-byte
* hexadecimal string.
*/
inline Hash32 hash32FromThrift(folly::StringPiece commitID) {
if (commitID.size() == Hash32::RAW_SIZE) {
// This looks like 32 bytes of binary data.
return Hash32(folly::ByteRange(folly::StringPiece(commitID)));
} else if (commitID.size() == 2 * Hash32::RAW_SIZE) {
// This looks like 64 bytes of hexadecimal data.
return Hash32(commitID);
} else {
throw newEdenError(
EINVAL,
EdenErrorType::ARGUMENT_ERROR,
"expected argument to be a 32-byte binary hash or "
"64-byte hexadecimal hash; got \"",
commitID,
"\"");
}
}
/**
* A RootId codec suitable for BackingStores that use 20-byte hashes for
* RootIds, like Git and Hg.
*/
class HashRootIdCodec : public RootIdCodec {
public:
RootId parseRootId(folly::StringPiece piece) override;
std::string renderRootId(const RootId& rootId) override;
};
} // namespace facebook::eden