rtlcommon.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef ECLCOMMON_HPP
  2. #define ECLCOMMON_HPP
  3. #include "jiface.hpp"
  4. #include "jfile.hpp"
  5. #include "eclrtl.hpp"
  6. #include "eclhelper.hpp"
  7. //The CContiguousRowBuffer is a buffer used for reading ahead into a file, and unsuring there is a contiguous
  8. //block of data available to the reader. Fixed size files could use this directly.
  9. class ECLRTL_API CContiguousRowBuffer
  10. {
  11. public:
  12. CContiguousRowBuffer() = default;
  13. CContiguousRowBuffer(ISerialStream * _in);
  14. void setStream(ISerialStream *_in);
  15. const byte * peekBytes(size32_t maxSize);
  16. const byte * peekFirstByte();
  17. void skipBytes(size32_t size)
  18. {
  19. cur += size;
  20. available -= size;
  21. // call eos() to ensure stream->eos() is true if this class has got to the end of the stream
  22. eos();
  23. }
  24. inline bool eos()
  25. {
  26. if (likely(available))
  27. return false;
  28. return checkInputEos();
  29. }
  30. inline offset_t tell() const { return in->tell() + (cur - buffer); }
  31. inline const byte * queryRow() const { return cur; }
  32. inline size_t maxAvailable() const { return available; }
  33. inline void clearStream() { setStream(nullptr); }
  34. inline void reset(offset_t offset, offset_t flen = (offset_t)-1)
  35. {
  36. in->reset(offset, flen);
  37. clearBuffer();
  38. }
  39. protected:
  40. void peekBytesDirect(size32_t size); // skip any consumed data and directly peek bytes from the input
  41. private:
  42. bool checkInputEos();
  43. void clearBuffer()
  44. {
  45. buffer = nullptr;
  46. cur = nullptr;
  47. available = 0;
  48. }
  49. protected:
  50. const byte * cur = nullptr;
  51. private:
  52. ISerialStream* in = nullptr;
  53. const byte * buffer = nullptr;
  54. size32_t available = 0;
  55. };
  56. //The CThorContiguousRowBuffer is the source for a readAhead call to ensure the entire row
  57. //is in a contiguous block of memory. The read() and skip() functions must be implemented
  58. class ECLRTL_API CThorContiguousRowBuffer : public CContiguousRowBuffer, implements IRowPrefetcherSource
  59. {
  60. public:
  61. CThorContiguousRowBuffer() = default;
  62. CThorContiguousRowBuffer(ISerialStream * _in);
  63. inline void setStream(ISerialStream *_in)
  64. {
  65. CContiguousRowBuffer::setStream(_in);
  66. readOffset = 0;
  67. }
  68. virtual const byte * peek(size32_t maxSize) override;
  69. virtual offset_t beginNested() override;
  70. virtual bool finishedNested(offset_t & len) override;
  71. virtual size32_t read(size32_t len, void * ptr) override;
  72. virtual size32_t readSize() override;
  73. virtual size32_t readPackedInt(void * ptr) override;
  74. virtual size32_t readUtf8(ARowBuilder & target, size32_t offset, size32_t fixedSize, size32_t len) override;
  75. virtual size32_t readVStr(ARowBuilder & target, size32_t offset, size32_t fixedSize) override;
  76. virtual size32_t readVUni(ARowBuilder & target, size32_t offset, size32_t fixedSize) override;
  77. //The following functions should only really be called when used by the readAhead() function
  78. virtual void skip(size32_t size) override;
  79. virtual void skipPackedInt() override;
  80. virtual void skipUtf8(size32_t len) override;
  81. virtual void skipVStr() override;
  82. virtual void skipVUni() override;
  83. virtual const byte * querySelf() override; // Dubious - used from ifblocks
  84. virtual void noteStartChild() override;
  85. virtual void noteFinishChild() override;
  86. inline const byte * queryRow() const { return cur; }
  87. inline size32_t queryRowSize() const { return readOffset; }
  88. inline void finishedRow()
  89. {
  90. skipBytes(readOffset);
  91. readOffset = 0;
  92. }
  93. inline void reset(offset_t offset, offset_t flen = (offset_t)-1)
  94. {
  95. CContiguousRowBuffer::reset(offset, flen);
  96. readOffset = 0;
  97. }
  98. protected:
  99. size32_t sizePackedInt();
  100. size32_t sizeUtf8(size32_t len);
  101. size32_t sizeVStr();
  102. size32_t sizeVUni();
  103. void reportReadFail();
  104. private:
  105. void doRead(size32_t len, void * ptr);
  106. inline void ensureAccessible(size32_t required)
  107. {
  108. if (required > maxAvailable())
  109. {
  110. peekBytesDirect(required);
  111. assertex(required <= maxAvailable());
  112. }
  113. }
  114. protected:
  115. size32_t readOffset = 0; // Offset within the current row
  116. UnsignedArray childStartOffsets;
  117. };
  118. #endif