Skip to content

Commit ae8d4d6

Browse files
vilattofacebook-github-bot
authored andcommitted
Move helper methods and enums into a common util file
Summary: Extract helper functions for serializing/deserializing the hash value of aux data. We put this into a utility file so we can reuse these helpers when we need to serialize the Tree aux data so we can store it into the local storage. ## Context Currently we fetch TreeAuxData directly from the backing store without caching. We want to add cache support. The cache will be consist of the in-memory cache (in ObjectStore) as well as the local store. Reviewed By: jdelliot Differential Revision: D72340694 fbshipit-source-id: 3351f158872304bb7bb0b929199593414a2f927a
1 parent e03114d commit ae8d4d6

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

eden/common/utils/Hash.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <folly/Range.h>
11+
12+
#include "eden/common/utils/Throw.h"
13+
#include "eden/fs/model/Hash.h"
14+
15+
namespace facebook::eden {
16+
17+
// bit enum representing possible hash types that could be used
18+
// 8 should be more than enough for now
19+
// but still this enum is represented as a variant
20+
enum class HashType : uint8_t {
21+
SHA1 = (1 << 0),
22+
BLAKE3 = (1 << 1),
23+
};
24+
25+
FOLLY_ALWAYS_INLINE void
26+
write(const uint8_t* src, size_t len, uint8_t* dest, size_t& off) {
27+
memcpy(dest + off, src, len);
28+
off += len;
29+
}
30+
31+
/**
32+
* Read the hash from a byte range to the destination Hash buffer.
33+
*
34+
* This is currently used by BlobAuxData and TreeAuxData.
35+
*/
36+
template <size_t SIZE>
37+
void readAuxDataHash(
38+
const ObjectId& id,
39+
folly::ByteRange& bytes,
40+
Hash<SIZE>& hash) {
41+
if (bytes.size() < SIZE) {
42+
throwf<std::invalid_argument>(
43+
"auxData for {} had unexpected size {}. Could not deserialize the hash of size {}.",
44+
id,
45+
bytes.size(),
46+
SIZE);
47+
}
48+
49+
auto mutableBytes = hash.mutableBytes();
50+
memcpy(mutableBytes.data(), bytes.data(), mutableBytes.size());
51+
bytes.advance(mutableBytes.size());
52+
}
53+
54+
} // namespace facebook::eden

0 commit comments

Comments
 (0)