reader.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <util/generic/array_ref.h>
  3. #include <util/generic/vector.h>
  4. #include <util/generic/yexception.h>
  5. class TBlob;
  6. class TChunkedDataReader {
  7. public:
  8. TChunkedDataReader(const TBlob& blob);
  9. inline const void* GetBlock(size_t index) const {
  10. CheckIndex(index);
  11. return Offsets[index];
  12. }
  13. inline size_t GetBlockLen(size_t index) const {
  14. CheckIndex(index);
  15. if (Version == 0) {
  16. if (index + 1 < Offsets.size()) {
  17. return Offsets[index + 1] - Offsets[index];
  18. }
  19. return Size - (Offsets.back() - Offsets.front());
  20. }
  21. return Lengths[index];
  22. }
  23. TBlob GetBlob(size_t index) const;
  24. template <typename T>
  25. TArrayRef<const T> GetRegion(size_t index) const {
  26. size_t len = GetBlockLen(index);
  27. Y_ENSURE(len % sizeof(T) == 0, "wrong data padding");
  28. return TArrayRef<const T>(reinterpret_cast<const T*>(GetBlock(index)), len / sizeof(T));
  29. }
  30. inline size_t GetBlocksCount() const {
  31. return Offsets.size();
  32. }
  33. private:
  34. inline void CheckIndex(size_t index) const {
  35. if (index >= GetBlocksCount()) {
  36. ythrow yexception() << "requested block " << index << " of " << GetBlocksCount() << " blocks";
  37. }
  38. }
  39. private:
  40. ui64 Version = 0;
  41. TVector<const char*> Offsets;
  42. TVector<size_t> Lengths;
  43. size_t Size = 0;
  44. };