sapling/eden/fs/model/Blob.h
Chad Austin bfd539c9c2 stop constructing blobs with their IDs
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
2023-07-24 16:29:25 -07:00

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