2016-05-12 23:43:17 +03:00
|
|
|
/*
|
2017-01-21 09:02:33 +03:00
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
2016-05-12 23:43:17 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "LocalStore.h"
|
2016-06-09 00:52:51 +03:00
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <folly/Format.h>
|
2016-12-13 04:48:46 +03:00
|
|
|
#include <folly/Optional.h>
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <folly/String.h>
|
2018-05-10 04:41:21 +03:00
|
|
|
#include <folly/futures/Future.h>
|
2016-06-16 00:23:24 +03:00
|
|
|
#include <folly/io/Cursor.h>
|
|
|
|
#include <folly/io/IOBuf.h>
|
2017-12-08 11:24:44 +03:00
|
|
|
#include <folly/lang/Bits.h>
|
2018-05-01 07:20:51 +03:00
|
|
|
#include <folly/logging/xlog.h>
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <array>
|
2017-06-22 23:39:57 +03:00
|
|
|
|
2016-06-09 04:59:56 +03:00
|
|
|
#include "eden/fs/model/Blob.h"
|
|
|
|
#include "eden/fs/model/Tree.h"
|
2016-05-12 23:43:17 +03:00
|
|
|
#include "eden/fs/model/git/GitBlob.h"
|
|
|
|
#include "eden/fs/model/git/GitTree.h"
|
2016-06-09 04:59:56 +03:00
|
|
|
#include "eden/fs/store/StoreResult.h"
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
using facebook::eden::Hash;
|
|
|
|
using folly::ByteRange;
|
2016-06-16 00:23:24 +03:00
|
|
|
using folly::IOBuf;
|
2016-12-13 04:48:46 +03:00
|
|
|
using folly::Optional;
|
2016-05-12 23:43:17 +03:00
|
|
|
using folly::StringPiece;
|
2017-11-04 01:58:04 +03:00
|
|
|
using folly::io::Cursor;
|
2016-05-12 23:43:17 +03:00
|
|
|
using std::string;
|
|
|
|
using std::unique_ptr;
|
|
|
|
|
2016-06-09 00:52:51 +03:00
|
|
|
namespace {
|
2016-12-13 04:48:46 +03:00
|
|
|
using namespace facebook::eden;
|
|
|
|
class SerializedBlobMetadata {
|
|
|
|
public:
|
|
|
|
explicit SerializedBlobMetadata(const BlobMetadata& metadata) {
|
|
|
|
serialize(metadata.sha1, metadata.size);
|
|
|
|
}
|
|
|
|
SerializedBlobMetadata(const Hash& contentsHash, uint64_t blobSize) {
|
|
|
|
serialize(contentsHash, blobSize);
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
ByteRange slice() const {
|
|
|
|
return ByteRange{data_};
|
2016-12-13 04:48:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static BlobMetadata parse(Hash blobID, const StoreResult& result) {
|
|
|
|
auto bytes = result.bytes();
|
|
|
|
if (bytes.size() != SIZE) {
|
|
|
|
throw std::invalid_argument(folly::sformat(
|
|
|
|
"Blob metadata for {} had unexpected size {}. Could not deserialize.",
|
|
|
|
blobID.toString(),
|
|
|
|
bytes.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t blobSizeBE;
|
|
|
|
memcpy(&blobSizeBE, bytes.data(), sizeof(uint64_t));
|
|
|
|
bytes.advance(sizeof(uint64_t));
|
|
|
|
auto contentsHash = Hash{bytes};
|
|
|
|
return BlobMetadata{contentsHash, folly::Endian::big(blobSizeBE)};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void serialize(const Hash& contentsHash, uint64_t blobSize) {
|
|
|
|
uint64_t blobSizeBE = folly::Endian::big(blobSize);
|
|
|
|
memcpy(data_.data(), &blobSizeBE, sizeof(uint64_t));
|
|
|
|
memcpy(
|
|
|
|
data_.data() + sizeof(uint64_t),
|
|
|
|
contentsHash.getBytes().data(),
|
|
|
|
Hash::RAW_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t SIZE = sizeof(uint64_t) + Hash::RAW_SIZE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The serialized data is stored as stored as:
|
|
|
|
* - size (8 bytes, big endian)
|
|
|
|
* - hash (20 bytes)
|
|
|
|
*/
|
|
|
|
std::array<uint8_t, SIZE> data_;
|
|
|
|
};
|
2017-11-04 01:58:04 +03:00
|
|
|
} // namespace
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
2018-05-31 20:39:49 +03:00
|
|
|
void LocalStore::clearCaches() {
|
|
|
|
clearKeySpace(BlobFamily);
|
|
|
|
clearKeySpace(BlobMetaDataFamily);
|
2018-05-31 20:39:53 +03:00
|
|
|
|
|
|
|
// If the trees were imported from a flatmanifest, we cannot delete them.
|
|
|
|
// See test_contents_are_the_same_if_handle_is_held_open when running against
|
|
|
|
// a flatmanifest repository.
|
|
|
|
// clearKeySpace(TreeFamily);
|
|
|
|
|
|
|
|
// Proxy hashes are required to fetch objects from hg from a hash. Deleting
|
|
|
|
// them breaks re-importing after an inode is unloaded.
|
|
|
|
// clearKeySpace(HgProxyHashFamily);
|
|
|
|
|
|
|
|
clearKeySpace(HgCommitToTreeFamily);
|
2018-05-31 20:39:49 +03:00
|
|
|
}
|
|
|
|
|
2017-09-08 00:39:47 +03:00
|
|
|
StoreResult LocalStore::get(KeySpace keySpace, const Hash& id) const {
|
|
|
|
return get(keySpace, id.getBytes());
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 04:41:21 +03:00
|
|
|
// This is the fallback implementation for stores that don't have any
|
|
|
|
// internal support for asynchronous fetches. This just performs the
|
|
|
|
// fetch and wraps it in a future
|
|
|
|
folly::Future<StoreResult> LocalStore::getFuture(
|
|
|
|
KeySpace keySpace,
|
|
|
|
folly::ByteRange key) const {
|
|
|
|
return folly::makeFutureWith(
|
|
|
|
[keySpace, key, this] { return get(keySpace, key); });
|
|
|
|
}
|
|
|
|
|
2018-05-25 23:47:54 +03:00
|
|
|
folly::Future<std::vector<StoreResult>> LocalStore::getBatch(
|
|
|
|
KeySpace keySpace,
|
|
|
|
const std::vector<folly::ByteRange>& keys) const {
|
|
|
|
return folly::makeFutureWith([keySpace, keys, this] {
|
|
|
|
std::vector<StoreResult> results;
|
|
|
|
for (auto& key : keys) {
|
|
|
|
results.emplace_back(get(keySpace, key));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
// TODO(mbolin): Currently, all objects in our RocksDB are Git objects. We
|
|
|
|
// probably want to namespace these by column family going forward, at which
|
|
|
|
// point we might want to have a GitLocalStore that delegates to an
|
|
|
|
// LocalStore so a vanilla LocalStore has no knowledge of deserializeGitTree()
|
|
|
|
// or deserializeGitBlob().
|
|
|
|
|
|
|
|
std::unique_ptr<Tree> LocalStore::getTree(const Hash& id) const {
|
2017-09-08 00:39:47 +03:00
|
|
|
auto result = get(KeySpace::TreeFamily, id.getBytes());
|
2016-06-09 04:59:56 +03:00
|
|
|
if (!result.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return deserializeGitTree(id, result.bytes());
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 04:41:21 +03:00
|
|
|
folly::Future<std::unique_ptr<Tree>> LocalStore::getTreeFuture(
|
|
|
|
const Hash& id) const {
|
|
|
|
return getFuture(KeySpace::TreeFamily, id.getBytes())
|
|
|
|
.then([id](folly::Try<StoreResult>&& dataTry) {
|
|
|
|
auto& data = dataTry.value();
|
|
|
|
if (!data.isValid()) {
|
|
|
|
return std::unique_ptr<Tree>(nullptr);
|
|
|
|
}
|
|
|
|
return deserializeGitTree(id, data.bytes());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
std::unique_ptr<Blob> LocalStore::getBlob(const Hash& id) const {
|
|
|
|
// We have to hold this string in scope while we deserialize and build
|
|
|
|
// the blob; otherwise, the results are undefined.
|
2017-09-08 00:39:47 +03:00
|
|
|
auto result = get(KeySpace::BlobFamily, id.getBytes());
|
2016-06-09 04:59:56 +03:00
|
|
|
if (!result.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-06-09 04:59:57 +03:00
|
|
|
auto buf = result.extractIOBuf();
|
|
|
|
return deserializeGitBlob(id, &buf);
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2018-05-10 04:41:21 +03:00
|
|
|
folly::Future<std::unique_ptr<Blob>> LocalStore::getBlobFuture(
|
|
|
|
const Hash& id) const {
|
|
|
|
return getFuture(KeySpace::BlobFamily, id.getBytes())
|
|
|
|
.then([id](folly::Try<StoreResult>&& dataTry) {
|
|
|
|
auto& data = dataTry.value();
|
|
|
|
if (!data.isValid()) {
|
|
|
|
return std::unique_ptr<Blob>(nullptr);
|
|
|
|
}
|
|
|
|
auto buf = data.extractIOBuf();
|
|
|
|
return deserializeGitBlob(id, &buf);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-13 04:48:46 +03:00
|
|
|
Optional<BlobMetadata> LocalStore::getBlobMetadata(const Hash& id) const {
|
2017-09-08 00:39:47 +03:00
|
|
|
auto result = get(KeySpace::BlobMetaDataFamily, id);
|
2016-06-09 04:59:56 +03:00
|
|
|
if (!result.isValid()) {
|
2016-12-13 04:48:49 +03:00
|
|
|
return folly::none;
|
2016-06-09 04:59:56 +03:00
|
|
|
}
|
2016-12-13 04:48:46 +03:00
|
|
|
return SerializedBlobMetadata::parse(id, result);
|
|
|
|
}
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2017-09-29 06:45:57 +03:00
|
|
|
std::pair<Hash, folly::IOBuf> LocalStore::serializeTree(const Tree* tree) {
|
|
|
|
GitTreeSerializer serializer;
|
|
|
|
for (auto& entry : tree->getTreeEntries()) {
|
|
|
|
serializer.addEntry(std::move(entry));
|
|
|
|
}
|
|
|
|
IOBuf treeBuf = serializer.finalize();
|
|
|
|
|
|
|
|
auto id = tree->getHash();
|
|
|
|
if (id == Hash()) {
|
|
|
|
id = Hash::sha1(&treeBuf);
|
|
|
|
}
|
|
|
|
return std::make_pair(id, treeBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalStore::hasKey(KeySpace keySpace, const Hash& id) const {
|
|
|
|
return hasKey(keySpace, id.getBytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
Hash LocalStore::putTree(const Tree* tree) {
|
|
|
|
auto serialized = LocalStore::serializeTree(tree);
|
|
|
|
ByteRange treeData = serialized.second.coalesce();
|
|
|
|
|
|
|
|
auto& id = serialized.first;
|
|
|
|
put(KeySpace::TreeFamily, id, treeData);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hash LocalStore::WriteBatch::putTree(const Tree* tree) {
|
|
|
|
auto serialized = LocalStore::serializeTree(tree);
|
|
|
|
ByteRange treeData = serialized.second.coalesce();
|
|
|
|
|
|
|
|
auto& id = serialized.first;
|
|
|
|
put(KeySpace::TreeFamily, id.getBytes(), treeData);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-12-13 04:48:46 +03:00
|
|
|
BlobMetadata LocalStore::putBlob(const Hash& id, const Blob* blob) {
|
2017-09-29 06:45:57 +03:00
|
|
|
// Since blob serialization is moderately complex, just delegate
|
|
|
|
// the immediate putBlob to the method on the WriteBatch.
|
|
|
|
// Pre-allocate a buffer of approximately the right size; it
|
|
|
|
// needs to hold the blob content plus have room for a couple of
|
|
|
|
// hashes for the keys, plus some padding.
|
|
|
|
auto batch = beginWrite(blob->getContents().computeChainDataLength() + 64);
|
2018-02-07 22:45:41 +03:00
|
|
|
auto result = batch->putBlob(id, blob);
|
|
|
|
batch->flush();
|
2017-09-29 06:45:57 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
void LocalStore::put(
|
|
|
|
LocalStore::KeySpace keySpace,
|
|
|
|
const Hash& id,
|
|
|
|
folly::ByteRange value) {
|
|
|
|
put(keySpace, id.getBytes(), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalStore::WriteBatch::put(
|
|
|
|
LocalStore::KeySpace keySpace,
|
|
|
|
const Hash& id,
|
|
|
|
folly::ByteRange value) {
|
|
|
|
put(keySpace, id.getBytes(), value);
|
|
|
|
}
|
|
|
|
|
2017-09-29 06:45:57 +03:00
|
|
|
BlobMetadata LocalStore::WriteBatch::putBlob(const Hash& id, const Blob* blob) {
|
2016-06-16 00:23:24 +03:00
|
|
|
const IOBuf& contents = blob->getContents();
|
|
|
|
|
2016-12-13 04:48:46 +03:00
|
|
|
BlobMetadata metadata{Hash::sha1(&contents),
|
|
|
|
contents.computeChainDataLength()};
|
2017-03-31 21:16:47 +03:00
|
|
|
|
2016-12-13 04:48:46 +03:00
|
|
|
SerializedBlobMetadata metadataBytes(metadata);
|
2016-06-16 00:23:24 +03:00
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
auto hashSlice = id.getBytes();
|
2016-06-16 00:23:24 +03:00
|
|
|
ByteRange bodyBytes;
|
|
|
|
|
|
|
|
// Add a git-style blob prefix
|
|
|
|
auto prefix = folly::to<string>("blob ", contents.computeChainDataLength());
|
|
|
|
prefix.push_back('\0');
|
2018-02-07 22:45:41 +03:00
|
|
|
std::vector<ByteRange> bodySlices;
|
|
|
|
bodySlices.emplace_back(StringPiece(prefix));
|
2016-06-16 00:23:24 +03:00
|
|
|
|
|
|
|
// Add all of the IOBuf chunks
|
|
|
|
Cursor cursor(&contents);
|
|
|
|
while (true) {
|
|
|
|
auto bytes = cursor.peekBytes();
|
|
|
|
if (bytes.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-07 22:45:41 +03:00
|
|
|
bodySlices.push_back(bytes);
|
2016-06-16 00:23:24 +03:00
|
|
|
cursor.skip(bytes.size());
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
put(LocalStore::KeySpace::BlobFamily, hashSlice, bodySlices);
|
|
|
|
put(LocalStore::KeySpace::BlobMetaDataFamily,
|
2017-09-29 06:45:57 +03:00
|
|
|
hashSlice,
|
|
|
|
metadataBytes.slice());
|
2016-12-13 04:48:46 +03:00
|
|
|
return metadata;
|
2016-05-12 23:43:17 +03:00
|
|
|
}
|
|
|
|
|
2018-02-07 22:45:41 +03:00
|
|
|
LocalStore::WriteBatch::~WriteBatch() {}
|
|
|
|
LocalStore::~LocalStore() {}
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2017-09-29 06:45:57 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|