sapling/eden/fs/store/TreeMetadata.h
Katie Mancini 550400364d introduce tree metadata storage in local store
Summary:
This introduces a class to manipulate the metadata for all the entries in a
tree. This adds serialization and deserialization to this class so that it can
be written to the local store.

Why do we need this? We need some way to easily check when we have already
fetched metadata for a tree and do not need to refetch this from the server to
avoid expensive network requests. Later diffs add functionally to store the metadata
for tree entries in the local store under the tree hash using this class.

Reviewed By: chadaustin

Differential Revision: D21959015

fbshipit-source-id: 0c0e8750737f3076c1f9604d0319cab7f2658656
2020-07-10 16:03:32 -07:00

82 lines
2.3 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.
*/
#pragma once
#include <optional>
#include <variant>
#include <vector>
#include <folly/io/IOBuf.h>
#include "eden/fs/model/Hash.h"
#include "eden/fs/store/SerializedBlobMetadata.h"
namespace facebook {
namespace eden {
class BlobMetadata;
class StoreResult;
/**
* This is to help manipulate and store the metadata for the blob entries
* a tree. Currently "metadata" means the size and the SHA-1 hash of a Blob's
* contents.
*/
class TreeMetadata {
public:
/** Used to prepare the tree metadata for storage and when tree metadata is
* read out of the local store. --
* Storing tree metadata indexed by hashes instead of names removes the
* complexity of storing variable length names. It also allows us to easily
* Store BlobMetadata from stored TreeMetadata since BlobMetadata is stored
* under the eden hash for a blob.
*/
using HashIndexedEntryMetadata = std::vector<std::pair<Hash, BlobMetadata>>;
/** Used when TreeMetdata was just fethed from the server --
* the server is unaware of the eden specific hashes we use in eden, so
* tree metdata from the server will use names to index the metdata for the
* entries in the tree.
*/
using NameIndexedEntryMetadata =
std::vector<std::pair<std::string, BlobMetadata>>;
using EntryMetadata =
std::variant<HashIndexedEntryMetadata, NameIndexedEntryMetadata>;
explicit TreeMetadata(EntryMetadata entryMetadata);
/**
* Serializes the metadata for all of the blob entries in the tree.
*
* note: hashes of each of the entries are used in serialization, so each of
* the EntryIdentifier for the entries must contain the hash of the entry
* before calling this method. Otherwise this raises a std::domain_error.
*/
folly::IOBuf serialize() const;
static TreeMetadata deserialize(const StoreResult& result);
const EntryMetadata& entries() const {
return entryMetadata_;
}
private:
size_t getSerializedSize() const;
size_t getNumberOfEntries() const;
static constexpr size_t ENTRY_SIZE =
Hash::RAW_SIZE + SerializedBlobMetadata::SIZE;
EntryMetadata entryMetadata_;
};
} // namespace eden
} // namespace facebook