#include "blob.h" #include "addstorage.h" #include #include #include #include #include #include #include #include #include #include template class TDynamicBlobBase: public TBlob::TBase, public TRefCounted, TCounter>, public TAdditionalStorage> { using TRefBase = TRefCounted; public: inline TDynamicBlobBase() = default; ~TDynamicBlobBase() override = default; void Ref() noexcept override { TRefBase::Ref(); } void UnRef() noexcept override { TRefBase::UnRef(); } inline void* Data() const noexcept { return this->AdditionalData(); } inline size_t Length() const noexcept { return this->AdditionalDataLength(); } }; template class TBufferBlobBase: public TBlob::TBase, public TRefCounted, TCounter> { using TRefBase = TRefCounted; public: inline TBufferBlobBase(TBuffer& buf) { Buf_.Swap(buf); } ~TBufferBlobBase() override = default; void Ref() noexcept override { TRefBase::Ref(); } void UnRef() noexcept override { TRefBase::UnRef(); } inline const TBuffer& Buffer() const noexcept { return Buf_; } private: TBuffer Buf_; }; template class TStringBlobBase: public TBlob::TBase, public TRefCounted, TCounter> { using TRefBase = TRefCounted; public: inline TStringBlobBase(const TString& s) : S_(s) { } TStringBlobBase(TString&& s) noexcept : S_(std::move(s)) { } ~TStringBlobBase() override = default; void Ref() noexcept override { TRefBase::Ref(); } void UnRef() noexcept override { TRefBase::UnRef(); } inline const TString& String() const noexcept { return S_; } private: const TString S_; }; template class TMappedBlobBase: public TBlob::TBase, public TRefCounted, TCounter> { using TRefBase = TRefCounted, TCounter>; public: inline TMappedBlobBase(const TMemoryMap& map, ui64 offset, size_t len, EMappingMode mode) : Map_(map) , Mode_(mode) { Y_ENSURE(Map_.IsOpen(), TStringBuf("memory map not open")); Map_.Map(offset, len); if (len && !Map_.Ptr()) { // Ptr is 0 for blob of size 0 ythrow yexception() << "can not map(" << offset << ", " << len << ")"; } if (Mode_ == EMappingMode::Locked) { LockMemory(Data(), Length()); } } ~TMappedBlobBase() override { if (Mode_ == EMappingMode::Locked && Length()) { UnlockMemory(Data(), Length()); } } void Ref() noexcept override { TRefBase::Ref(); } void UnRef() noexcept override { TRefBase::UnRef(); } inline const void* Data() const noexcept { return Map_.Ptr(); } inline size_t Length() const noexcept { return Map_.MappedSize(); } private: TFileMap Map_; EMappingMode Mode_; }; TBlob TBlob::SubBlob(size_t len) const { /* * may be slightly optimized */ return SubBlob(0, len); } TBlob TBlob::SubBlob(size_t begin, size_t end) const { if (begin > Length() || end > Length() || begin > end) { ythrow yexception() << "incorrect subblob (" << begin << ", " << end << ", outer length = " << Length() << ")"; } return TBlob(Begin() + begin, end - begin, S_.Base); } TBlob TBlob::DeepCopy() const { return TBlob::Copy(Data(), Length()); } template static inline TBlob CopyConstruct(const void* data, size_t len) { using Base = TDynamicBlobBase; THolder base(new (len) Base); Y_ASSERT(base->Length() == len); memcpy(base->Data(), data, len); TBlob ret(base->Data(), len, base.Get()); Y_UNUSED(base.Release()); return ret; } TBlob TBlob::CopySingleThreaded(const void* data, size_t length) { return CopyConstruct(data, length); } TBlob TBlob::Copy(const void* data, size_t length) { return CopyConstruct(data, length); } TBlob TBlob::NoCopy(const void* data, size_t length) { return TBlob(data, length, nullptr); } template static inline TBlob ConstructFromMap(const TMemoryMap& map, ui64 offset, size_t length, EMappingMode mode) { using TBase = TMappedBlobBase; THolder base(new TBase(map, offset, length, mode)); TBlob ret(base->Data(), base->Length(), base.Get()); Y_UNUSED(base.Release()); return ret; } template static inline TBlob ConstructAsMap(const T& t, EMappingMode mode) { TMemoryMap::EOpenMode openMode = (mode == EMappingMode::Precharged) ? (TMemoryMap::oRdOnly | TMemoryMap::oPrecharge) : TMemoryMap::oRdOnly; TMemoryMap map(t, openMode); const ui64 toMap = map.Length(); if (toMap > Max()) { ythrow yexception() << "can not map whole file(length = " << toMap << ")"; } return ConstructFromMap(map, 0, static_cast(toMap), mode); } TBlob TBlob::FromFileSingleThreaded(const TString& path, EMappingMode mode) { return ConstructAsMap(path, mode); } TBlob TBlob::FromFile(const TString& path, EMappingMode mode) { return ConstructAsMap(path, mode); } TBlob TBlob::FromFileSingleThreaded(const TFile& file, EMappingMode mode) { return ConstructAsMap(file, mode); } TBlob TBlob::FromFile(const TFile& file, EMappingMode mode) { return ConstructAsMap(file, mode); } TBlob TBlob::FromFileSingleThreaded(const TString& path) { return ConstructAsMap(path, EMappingMode::Standard); } TBlob TBlob::FromFile(const TString& path) { return ConstructAsMap(path, EMappingMode::Standard); } TBlob TBlob::FromFileSingleThreaded(const TFile& file) { return ConstructAsMap(file, EMappingMode::Standard); } TBlob TBlob::FromFile(const TFile& file) { return ConstructAsMap(file, EMappingMode::Standard); } TBlob TBlob::PrechargedFromFileSingleThreaded(const TString& path) { return ConstructAsMap(path, EMappingMode::Precharged); } TBlob TBlob::PrechargedFromFile(const TString& path) { return ConstructAsMap(path, EMappingMode::Precharged); } TBlob TBlob::PrechargedFromFileSingleThreaded(const TFile& file) { return ConstructAsMap(file, EMappingMode::Precharged); } TBlob TBlob::PrechargedFromFile(const TFile& file) { return ConstructAsMap(file, EMappingMode::Precharged); } TBlob TBlob::LockedFromFileSingleThreaded(const TString& path) { return ConstructAsMap(path, EMappingMode::Locked); } TBlob TBlob::LockedFromFile(const TString& path) { return ConstructAsMap(path, EMappingMode::Locked); } TBlob TBlob::LockedFromFileSingleThreaded(const TFile& file) { return ConstructAsMap(file, EMappingMode::Locked); } TBlob TBlob::LockedFromFile(const TFile& file) { return ConstructAsMap(file, EMappingMode::Locked); } TBlob TBlob::LockedFromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap(map, offset, length, EMappingMode::Locked); } TBlob TBlob::LockedFromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap(map, offset, length, EMappingMode::Locked); } TBlob TBlob::FromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap(map, offset, length, EMappingMode::Standard); } TBlob TBlob::FromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap(map, offset, length, EMappingMode::Standard); } template static inline TBlob ReadFromFile(const TFile& file, ui64 offset, size_t length) { using TBase = TDynamicBlobBase; THolder base(new (length) TBase); Y_ASSERT(base->Length() == length); file.Pload(base->Data(), length, offset); TBlob ret(base->Data(), length, base.Get()); Y_UNUSED(base.Release()); return ret; } template static inline TBlob ConstructFromFileContent(const TFile& file, ui64 offset, ui64 length) { if (length > Max()) { ythrow yexception() << "can not read whole file(length = " << length << ")"; } return ReadFromFile(file, offset, static_cast(length)); } TBlob TBlob::FromFileContentSingleThreaded(const TString& path) { TFile file(path, RdOnly); return ConstructFromFileContent(file, 0, file.GetLength()); } TBlob TBlob::FromFileContent(const TString& path) { TFile file(path, RdOnly); return ConstructFromFileContent(file, 0, file.GetLength()); } TBlob TBlob::FromFileContentSingleThreaded(const TFile& file) { return ConstructFromFileContent(file, 0, file.GetLength()); } TBlob TBlob::FromFileContent(const TFile& file) { return ConstructFromFileContent(file, 0, file.GetLength()); } TBlob TBlob::FromFileContentSingleThreaded(const TFile& file, ui64 offset, size_t length) { return ConstructFromFileContent(file, offset, length); } TBlob TBlob::FromFileContent(const TFile& file, ui64 offset, size_t length) { return ConstructFromFileContent(file, offset, length); } template static inline TBlob ConstructFromBuffer(TBuffer& in) { using TBase = TBufferBlobBase; THolder base(new TBase(in)); TBlob ret(base->Buffer().Data(), base->Buffer().Size(), base.Get()); Y_UNUSED(base.Release()); return ret; } template static inline TBlob ConstructFromStream(IInputStream& in) { TBuffer buf; { TBufferOutput out(buf); TransferData(&in, &out); } return ConstructFromBuffer(buf); } TBlob TBlob::FromStreamSingleThreaded(IInputStream& in) { return ConstructFromStream(in); } TBlob TBlob::FromStream(IInputStream& in) { return ConstructFromStream(in); } TBlob TBlob::FromBufferSingleThreaded(TBuffer& in) { return ConstructFromBuffer(in); } TBlob TBlob::FromBuffer(TBuffer& in) { return ConstructFromBuffer(in); } template TBlob ConstructFromString(S&& s) { using TBase = TStringBlobBase; auto base = MakeHolder(std::forward(s)); TBlob ret(base->String().data(), base->String().size(), base.Get()); Y_UNUSED(base.Release()); return ret; } TBlob TBlob::FromStringSingleThreaded(const TString& s) { return ConstructFromString(s); } TBlob TBlob::FromStringSingleThreaded(TString&& s) { return ConstructFromString(std::move(s)); } TBlob TBlob::FromString(const TString& s) { return ConstructFromString(s); } TBlob TBlob::FromString(TString&& s) { return ConstructFromString(std::move(s)); }