mirror of
https://github.com/facebook/sapling.git
synced 2024-10-05 14:28:17 +03:00
bfd539c9c2
Summary: It is a minor historical mistake that Blob knew its own ID. There can be multiple ID schemes that reference a single blob, and sometimes we want to construct a blob without an ID available. Now that blob IDs are never read directly, remove the field and simplify a variety of construction sites. Reviewed By: kmancini Differential Revision: D39188561 fbshipit-source-id: 72b59b744aac42c312816d568fa563629575267a
59 lines
1.3 KiB
C++
59 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This software may be used and distributed according to the terms of the
|
|
* GNU General Public License version 2.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <folly/io/IOBuf.h>
|
|
#include <string>
|
|
#include "eden/fs/model/BlobFwd.h"
|
|
#include "eden/fs/model/Hash.h"
|
|
#include "eden/fs/model/ObjectId.h"
|
|
|
|
namespace facebook::eden {
|
|
|
|
class Blob {
|
|
public:
|
|
Blob(folly::IOBuf&& contents)
|
|
: contents_{std::move(contents)},
|
|
size_{contents_.computeChainDataLength()} {}
|
|
|
|
Blob(const folly::IOBuf& contents)
|
|
: contents_{contents}, size_{contents_.computeChainDataLength()} {}
|
|
|
|
/**
|
|
* Convenience constructor for unit tests. Always copies the given
|
|
* StringPiece.
|
|
*/
|
|
Blob(folly::StringPiece contents)
|
|
: contents_{folly::IOBuf::COPY_BUFFER, contents.data(), contents.size()},
|
|
size_{contents.size()} {}
|
|
|
|
const folly::IOBuf& getContents() const {
|
|
return contents_;
|
|
}
|
|
|
|
const std::string asString() const {
|
|
auto dataBuf = contents_.cloneCoalescedAsValue();
|
|
return std::string{
|
|
reinterpret_cast<const char*>(dataBuf.data()), dataBuf.length()};
|
|
}
|
|
|
|
size_t getSize() const {
|
|
return size_;
|
|
}
|
|
|
|
size_t getSizeBytes() const {
|
|
return size_;
|
|
}
|
|
|
|
private:
|
|
const folly::IOBuf contents_;
|
|
const size_t size_;
|
|
};
|
|
|
|
} // namespace facebook::eden
|