sapling/eden/fs/store/BlobCache.h
Andrey Chursin 0af2511a3f separate out ObjectId [proxy hash removal 1/n]
Summary:
The goal of this stack is to remove Proxy Hash type, but to achieve that we need first to address some tech debt in Eden codebase.

For the long time EdenFs had single Hash type that was used for many different use cases.

One of major uses for Hash type is identifies internal EdenFs objects such as blobs, trees, and others.

We seem to reach agreement that we need a different type for those identifiers, so we introduce separate ObjectId type in this diff to denote new identifier type and replace _some_ usage of Hash with ObjectId.

We still retain original Hash type for other use cases.

Roughly speaking, this is how this diff separates between Hash and ObjectId:

**ObjectId**:
* Everything that is stored in local store(blobs, trees, commits)

**Hash20**:
* Explicit hashes(Sha1 of the blob)
* Hg identifiers: manifest id and blob hg ig

For now, in this diff ObjectId has exactly same content as Hash, but this will change in the future diffs. Doing this way allows to keep diff size manageable, while migrating to new ObjectId right away would produce insanely large diff that would be both hard to make and review.

There are few more things that needs to be done before we can get to the meat of removing proxy hashes:

1) Replace include Hash.h with ObjectId.h where needed
2) Remove Hash type, explicitly rename rest of Hash usages to Hash20
3) Modify content of ObjectId to support new use cases
4) Modify serialized metadata and possibly other places that assume ObjectId size is fixed and equal to Hash20 size

Reviewed By: chadaustin

Differential Revision: D31316477

fbshipit-source-id: 0d5e4460a461bcaac6b9fd884517e129aeaf4baf
2021-10-01 10:25:46 -07:00

80 lines
2.6 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/Blob.h"
#include "eden/fs/store/ObjectCache.h"
namespace facebook::eden {
using BlobInterestHandle = ObjectInterestHandle<Blob>;
/**
* An in-memory LRU cache for loaded blobs. It is parameterized by both a
* maximum cache size and a minimum entry count. The cache tries to evict
* entries when the total number of loaded blobs exceeds the maximum cache size,
* except that it always keeps the minimum entry count around.
*
* The intent of the minimum entry count is to avoid having to reload
* frequently-accessed large blobs when they are larger than the maximum cache
* size.
*
* It is safe to use this object from arbitrary threads.
*/
class BlobCache : public ObjectCache<Blob, ObjectCacheFlavor::InterestHandle> {
public:
static std::shared_ptr<BlobCache> create(
size_t maximumCacheSizeBytes,
size_t minimumEntryCount) {
struct BC : BlobCache {
BC(size_t x, size_t y) : BlobCache{x, y} {}
};
return std::make_shared<BC>(maximumCacheSizeBytes, minimumEntryCount);
}
~BlobCache() = default;
/**
* If a blob for the given hash is in cache, return it. If the blob is not in
* cache, return nullptr (and an empty interest handle).
*
* If a blob is returned and interest is WantHandle, then a movable handle
* object is also returned. When the interest handle is destroyed, the cached
* blob may be evicted.
*
* After fetching a blob, prefer calling getBlob() on the returned
* BlobInterestHandle first. It can avoid some overhead or return a blob if
* it still exists in memory and the BlobCache has evicted its reference.
*/
GetResult get(
const ObjectId& hash,
Interest interest = Interest::LikelyNeededAgain) {
return getInterestHandle(hash, interest);
}
/**
* Inserts a blob into the cache for future lookup. If the new total size
* exceeds the maximum cache size and the minimum entry count, old entries are
* evicted.
*
* Optionally returns an interest handle that, when dropped, evicts the
* inserted blob.
*/
BlobInterestHandle insert(
ObjectPtr blob,
Interest interest = Interest::LikelyNeededAgain) {
return insertInterestHandle(blob, interest);
}
private:
explicit BlobCache(size_t maximumCacheSizeBytes, size_t minimumEntryCount)
: ObjectCache<Blob, ObjectCacheFlavor::InterestHandle>{
maximumCacheSizeBytes,
minimumEntryCount} {}
};
} // namespace facebook::eden