1616
1717#include " paimon/common/memory/memory_slice_output.h"
1818
19+ #include " paimon/common/utils/math.h"
1920namespace paimon {
2021
2122MemorySliceOutput::MemorySliceOutput (int32_t estimated_size, MemoryPool* pool) {
@@ -37,49 +38,38 @@ std::unique_ptr<MemorySlice> MemorySliceOutput::ToSlice() {
3738 return std::make_unique<MemorySlice>(segment, 0 , size_);
3839}
3940
40- void MemorySliceOutput::WriteByte (int8_t value) {
41- EnsureSize (size_ + 1 );
42- segment_.Put (size_++, static_cast <char >(value));
43- }
44-
45- void MemorySliceOutput::WriteShort (int16_t value) {
46- EnsureSize (size_ + 2 );
47- segment_.PutValue (size_, value);
48- size_ += 2 ;
49- }
50-
51- void MemorySliceOutput::WriteInt (int32_t value) {
52- EnsureSize (size_ + 4 );
53- segment_.PutValue (size_, value);
54- size_ += 4 ;
55- }
56-
57- void MemorySliceOutput::WriteLong (int64_t value) {
58- EnsureSize (size_ + 8 );
59- segment_.PutValue (size_, value);
60- size_ += 8 ;
41+ template <typename T>
42+ void MemorySliceOutput::WriteValue (T value) {
43+ int32_t write_length = sizeof (T);
44+ EnsureSize (size_ + write_length);
45+ T write_value = value;
46+ if (NeedSwap ()) {
47+ write_value = EndianSwapValue (value);
48+ }
49+ segment_.PutValue (size_, write_value);
50+ size_ += write_length;
6151}
6252
6353void MemorySliceOutput::WriteVarLenInt (int32_t value) {
6454 if (value < 0 ) {
6555 throw std::invalid_argument (" negative value: v=" + std::to_string (value));
6656 }
6757 while ((value & ~0x7F ) != 0 ) {
68- WriteByte ( ((value & 0x7F ) | 0x80 ));
58+ WriteValue ( static_cast < char > ((value & 0x7F ) | 0x80 ));
6959 value >>= 7 ;
7060 }
71- WriteByte (static_cast <int8_t >(value));
61+ WriteValue (static_cast <char >(value));
7262}
7363
7464void MemorySliceOutput::WriteVarLenLong (int64_t value) {
7565 if (value < 0 ) {
7666 throw std::invalid_argument (" negative value: v=" + std::to_string (value));
7767 }
7868 while ((value & ~0x7F ) != 0 ) {
79- WriteByte ( ((value & 0x7F ) | 0x80 ));
69+ WriteValue ( static_cast < char > ((value & 0x7F ) | 0x80 ));
8070 value >>= 7 ;
8171 }
82- WriteByte (static_cast <int8_t >(value));
72+ WriteValue (static_cast <char >(value));
8373}
8474
8575void MemorySliceOutput::WriteBytes (const std::shared_ptr<Bytes>& source) {
@@ -94,6 +84,14 @@ void MemorySliceOutput::WriteBytes(const std::shared_ptr<Bytes>& source, int sou
9484 size_ += length;
9585}
9686
87+ void MemorySliceOutput::SetOrder (ByteOrder order) {
88+ byte_order_ = order;
89+ }
90+
91+ bool MemorySliceOutput::NeedSwap () const {
92+ return SystemByteOrder () != byte_order_;
93+ }
94+
9795void MemorySliceOutput::EnsureSize (int size) {
9896 if (size <= segment_.Size ()) {
9997 return ;
@@ -111,4 +109,11 @@ void MemorySliceOutput::EnsureSize(int size) {
111109 segment_ = new_segment;
112110}
113111
112+ template void MemorySliceOutput::WriteValue (bool );
113+ template void MemorySliceOutput::WriteValue (char );
114+ template void MemorySliceOutput::WriteValue (int8_t );
115+ template void MemorySliceOutput::WriteValue (int16_t );
116+ template void MemorySliceOutput::WriteValue (int32_t );
117+ template void MemorySliceOutput::WriteValue (int64_t );
118+
114119} // namespace paimon
0 commit comments