sapling/eden/fs/testharness/FakeObjectStore.cpp
Chad Austin bb1cccac89 introduce a variable-width RootId type that identifies the root of an EdenFS checkout's contents
Summary:
Backing stores differentiate between individual tree objects and the
root of a checkout. For example, Git and Mercurial roots are commit
hashes. Allow EdenFS to track variable-width roots to better support
arbitrary backing stores.

Reviewed By: genevievehelsel

Differential Revision: D28619584

fbshipit-source-id: d94f1ecd21a0c416c1b4933341c70deabf386496
2021-06-07 17:25:31 -07:00

98 lines
2.8 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 "FakeObjectStore.h"
#include <folly/String.h>
#include <folly/futures/Future.h>
#include "eden/fs/service/ThriftUtil.h"
using folly::Future;
using folly::makeFuture;
using std::make_shared;
using std::shared_ptr;
namespace facebook::eden {
FakeObjectStore::FakeObjectStore() = default;
FakeObjectStore::~FakeObjectStore() = default;
void FakeObjectStore::addTree(Tree&& tree) {
auto treeHash = tree.getHash();
trees_.emplace(std::move(treeHash), std::move(tree));
}
void FakeObjectStore::addBlob(Blob&& blob) {
auto blobHash = blob.getHash();
blobs_.emplace(blobHash, std::move(blob));
}
void FakeObjectStore::setTreeForCommit(const RootId& commitID, Tree&& tree) {
auto ret = commits_.emplace(commitID, std::move(tree));
if (!ret.second) {
// Warn the caller that a Tree has already been specified for this commit,
// which is likely a logical error. If this turns out to be something that
// we want to do in a test, then we can change this behavior.
throw std::runtime_error(folly::to<std::string>(
"tree already added for commit with id ", commitID));
}
}
Future<shared_ptr<const Tree>> FakeObjectStore::getRootTree(
const RootId& commitID,
ObjectFetchContext&) const {
++commitAccessCounts_[commitID];
auto iter = commits_.find(commitID);
if (iter == commits_.end()) {
return makeFuture<shared_ptr<const Tree>>(
std::domain_error(folly::to<std::string>(
"tree data for commit ", commitID, " not found")));
}
return makeFuture(make_shared<Tree>(iter->second));
}
Future<std::shared_ptr<const Tree>> FakeObjectStore::getTree(
const Hash& id,
ObjectFetchContext&) const {
++accessCounts_[id];
auto iter = trees_.find(id);
if (iter == trees_.end()) {
return makeFuture<shared_ptr<const Tree>>(
std::domain_error("tree " + id.toString() + " not found"));
}
return makeFuture(make_shared<Tree>(iter->second));
}
Future<std::shared_ptr<const Blob>> FakeObjectStore::getBlob(
const Hash& id,
ObjectFetchContext&) const {
++accessCounts_[id];
auto iter = blobs_.find(id);
if (iter == blobs_.end()) {
return makeFuture<shared_ptr<const Blob>>(
std::domain_error("blob " + id.toString() + " not found"));
}
return makeFuture(make_shared<Blob>(iter->second));
}
folly::Future<folly::Unit> FakeObjectStore::prefetchBlobs(
const std::vector<Hash>&,
ObjectFetchContext&) const {
return folly::unit;
}
size_t FakeObjectStore::getAccessCount(const Hash& hash) const {
if (auto* item = folly::get_ptr(accessCounts_, hash)) {
return *item;
} else {
return 0;
}
}
} // namespace facebook::eden