mkql_optional_usage_mask.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include "mkql_computation_node_pack_impl.h"
  3. #include <util/generic/bitmap.h>
  4. #include <util/generic/buffer.h>
  5. #include <cstring>
  6. namespace NKikimr {
  7. namespace NMiniKQL {
  8. namespace NDetails {
  9. class TOptionalUsageMask {
  10. public:
  11. TOptionalUsageMask() = default;
  12. void Reset() {
  13. CountOfOptional = 0U;
  14. Mask.Clear();
  15. }
  16. void Reset(TChunkedInputBuffer& buf) {
  17. Reset();
  18. ui64 bytes = UnpackUInt64(buf);
  19. if (bytes) {
  20. Mask.Reserve(bytes << 3ULL);
  21. buf.CopyTo(reinterpret_cast<char*>(const_cast<ui8*>(Mask.GetChunks())), bytes);
  22. }
  23. }
  24. void SetNextEmptyOptional(bool empty) {
  25. if (empty) {
  26. Mask.Set(CountOfOptional);
  27. }
  28. ++CountOfOptional;
  29. }
  30. bool IsNextEmptyOptional() {
  31. return Mask.Test(CountOfOptional++);
  32. }
  33. bool IsEmptyMask() const {
  34. return Mask.Empty();
  35. }
  36. size_t CalcSerializedSize() {
  37. if (!CountOfOptional || Mask.Empty()) {
  38. return 1ULL; // One byte for size=0
  39. }
  40. const size_t usedBits = Mask.ValueBitCount();
  41. const size_t usedBytes = (usedBits + 7ULL) >> 3ULL;
  42. return GetPack64Length(usedBytes) + usedBytes;
  43. }
  44. template<typename TBuf>
  45. void Serialize(TBuf& buf) const {
  46. if (!CountOfOptional || Mask.Empty()) {
  47. return buf.Append(0);
  48. }
  49. const size_t usedBits = Mask.ValueBitCount();
  50. const size_t usedBytes = (usedBits + 7ULL) >> 3ULL;
  51. buf.Advance(MAX_PACKED64_SIZE);
  52. // Note: usage of Pack64() is safe here - it won't overwrite useful data for small values of usedBytes
  53. buf.EraseBack(MAX_PACKED64_SIZE - Pack64(usedBytes, buf.Pos() - MAX_PACKED64_SIZE));
  54. buf.Append(reinterpret_cast<const char*>(Mask.GetChunks()), usedBytes);
  55. }
  56. private:
  57. ui32 CountOfOptional = 0U;
  58. TBitMapOps<TDynamicBitMapTraits<ui8>> Mask;
  59. };
  60. } // NDetails
  61. }
  62. }