diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/on_disk/chunks/reader.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/on_disk/chunks/reader.h')
-rw-r--r-- | library/cpp/on_disk/chunks/reader.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/library/cpp/on_disk/chunks/reader.h b/library/cpp/on_disk/chunks/reader.h new file mode 100644 index 0000000000..c5fe783319 --- /dev/null +++ b/library/cpp/on_disk/chunks/reader.h @@ -0,0 +1,57 @@ +#pragma once + +#include <util/generic/array_ref.h> +#include <util/generic/vector.h> +#include <util/generic/yexception.h> + +class TBlob; + +class TChunkedDataReader { +public: + TChunkedDataReader(const TBlob& blob); + + inline const void* GetBlock(size_t index) const { + CheckIndex(index); + return Offsets[index]; + } + + inline size_t GetBlockLen(size_t index) const { + CheckIndex(index); + + if (Version == 0) { + if (index + 1 < Offsets.size()) { + return Offsets[index + 1] - Offsets[index]; + } + + return Size - (Offsets.back() - Offsets.front()); + } + + return Lengths[index]; + } + + TBlob GetBlob(size_t index) const; + + template <typename T> + TArrayRef<const T> GetRegion(size_t index) const { + size_t len = GetBlockLen(index); + Y_ENSURE(len % sizeof(T) == 0, "wrong data padding"); + return TArrayRef<const T>(reinterpret_cast<const T*>(GetBlock(index)), len / sizeof(T)); + } + + inline size_t GetBlocksCount() const { + return Offsets.size(); + } + +private: + inline void CheckIndex(size_t index) const { + if (index >= GetBlocksCount()) { + ythrow yexception() << "requested block " << index << " of " << GetBlocksCount() << " blocks"; + } + } + +private: + ui64 Version = 0; + TVector<const char*> Offsets; + TVector<size_t> Lengths; + size_t Size = 0; +}; |