sapling/eden/fs/model/TreeEntry.h
Katie Mancini 1a02401df9 create a custom in memory tree cache
Summary:
Chad first noted that deserializing trees from the local store can be expensive.
From the thrift side EdenFS does not have a copy of trees in memory. This
means for glob files each of the trees that have not been materialized will be
read from the local store. Since reading an deserializing trees from the local
store can be expensive lets add an in memory cache so that some of these
reads can be satisfied from here instead.

This introduces the class for the in memory cache and is based on the existing
BlobCache. note that we keep the minimum number of entries functionality from
the blob cache. This is unlikely to be needed as trees are much less likely
than blobs to exceed a reasonable cache size limit, but kept since we already
have it.

Reviewed By: chadaustin

Differential Revision: D27050285

fbshipit-source-id: 9dd46419761d32387b6f55ff508b60105edae3af
2021-04-27 17:38:39 -07:00

137 lines
3.1 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 "eden/fs/model/Hash.h"
#include "eden/fs/utils/DirType.h"
#include "eden/fs/utils/PathFuncs.h"
#include <folly/String.h>
#include <iosfwd>
#include <optional>
namespace facebook {
namespace eden {
class Hash;
/**
* Represents the allowed types of entries in version control trees.
*
* Currently missing from this list: git submodules.
*/
enum class TreeEntryType : uint8_t {
TREE,
REGULAR_FILE,
EXECUTABLE_FILE,
SYMLINK,
};
/**
* Computes an initial mode_t, including permission bits, from a FileType.
*/
mode_t modeFromTreeEntryType(TreeEntryType ft);
/**
* Converts an arbitrary mode_t to the appropriate TreeEntryType if the file
* can be tracked by version control. If not, returns std::nullopt.
*/
std::optional<TreeEntryType> treeEntryTypeFromMode(mode_t mode);
class TreeEntry {
public:
explicit TreeEntry(const Hash& hash, PathComponent name, TreeEntryType type)
: type_(type), hash_(hash), name_(std::move(name)) {}
explicit TreeEntry(
const Hash& hash,
PathComponent name,
TreeEntryType type,
std::optional<uint64_t> size,
std::optional<Hash> contentSha1)
: type_(type),
hash_(hash),
name_(std::move(name)),
size_(size),
contentSha1_(contentSha1) {}
const Hash& getHash() const {
return hash_;
}
const PathComponent& getName() const {
return name_;
}
bool isTree() const {
return type_ == TreeEntryType::TREE;
}
TreeEntryType getType() const {
#ifdef _WIN32
// XXX(T66590035): instead of doing this here, this should be done in the
// Windows specific code that interpret these.
switch (type_) {
case TreeEntryType::REGULAR_FILE:
case TreeEntryType::EXECUTABLE_FILE:
case TreeEntryType::SYMLINK:
return TreeEntryType::REGULAR_FILE;
default:
return type_;
}
#else
return type_;
#endif
}
dtype_t getDType() const {
switch (type_) {
case TreeEntryType::TREE:
return dtype_t::Dir;
case TreeEntryType::REGULAR_FILE:
case TreeEntryType::EXECUTABLE_FILE:
return dtype_t::Regular;
#ifndef _WIN32
case TreeEntryType::SYMLINK:
return dtype_t::Symlink;
#endif
default:
return dtype_t::Unknown;
}
}
std::string toLogString() const;
const std::optional<uint64_t>& getSize() const {
return size_;
}
const std::optional<Hash>& getContentSha1() const {
return contentSha1_;
}
/**
* An estimate of the memory footprint of this treeEntry outside of the data
* directly stored in this object.
*/
size_t getIndirectSizeBytes() const;
private:
TreeEntryType type_;
Hash hash_;
PathComponent name_;
std::optional<uint64_t> size_;
std::optional<Hash> contentSha1_;
};
std::ostream& operator<<(std::ostream& os, TreeEntryType type);
bool operator==(const TreeEntry& entry1, const TreeEntry& entry2);
bool operator!=(const TreeEntry& entry1, const TreeEntry& entry2);
} // namespace eden
} // namespace facebook