/* * 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 #include using folly::Future; using folly::makeFuture; using std::make_shared; using std::shared_ptr; namespace facebook { namespace eden { FakeObjectStore::FakeObjectStore() {} FakeObjectStore::~FakeObjectStore() {} 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 Hash& 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( "tree already added for commit with id ", commitID)); } } Future> FakeObjectStore::getTree( const Hash& id, ObjectFetchContext&) const { ++accessCounts_[id]; auto iter = trees_.find(id); if (iter == trees_.end()) { return makeFuture>( std::domain_error("tree " + id.toString() + " not found")); } return makeFuture(make_shared(iter->second)); } Future> FakeObjectStore::getBlob( const Hash& id, ObjectFetchContext&) const { ++accessCounts_[id]; auto iter = blobs_.find(id); if (iter == blobs_.end()) { return makeFuture>( std::domain_error("blob " + id.toString() + " not found")); } return makeFuture(make_shared(iter->second)); } Future> FakeObjectStore::getTreeForCommit( const Hash& commitID, ObjectFetchContext&) const { ++accessCounts_[commitID]; auto iter = commits_.find(commitID); if (iter == commits_.end()) { return makeFuture>(std::domain_error( "tree data for commit " + commitID.toString() + " not found")); } return makeFuture(make_shared(iter->second)); } folly::Future> FakeObjectStore::getTreeForManifest( const Hash& commitID, const Hash& /* manifestID */, ObjectFetchContext&) const { return getTreeForCommit(commitID); } folly::Future FakeObjectStore::prefetchBlobs( const std::vector&, 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 eden } // namespace facebook