forked from alibaba/paimon-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_slice.cpp
More file actions
120 lines (101 loc) · 3.76 KB
/
memory_slice.cpp
File metadata and controls
120 lines (101 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright 2026-present Alibaba Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "paimon/common/memory/memory_slice.h"
#include "paimon/common/memory/memory_slice_input.h"
namespace paimon {
std::shared_ptr<MemorySlice> MemorySlice::Wrap(const std::shared_ptr<Bytes>& bytes) {
auto segment = MemorySegment::Wrap(bytes);
return std::make_shared<MemorySlice>(segment, 0, segment.Size());
}
std::shared_ptr<MemorySlice> MemorySlice::Wrap(const MemorySegment& segment) {
return std::make_shared<MemorySlice>(segment, 0, segment.Size());
}
MemorySlice::MemorySlice(const MemorySegment& segment, int32_t offset, int32_t length)
: segment_(segment), offset_(offset), length_(length) {}
std::shared_ptr<MemorySlice> MemorySlice::Slice(int32_t index, int32_t length) {
if (index == 0 && length == length_) {
return shared_from_this();
}
return std::make_shared<MemorySlice>(segment_, offset_ + index, length);
}
int32_t MemorySlice::Length() const {
return length_;
}
int32_t MemorySlice::Offset() const {
return offset_;
}
std::shared_ptr<Bytes> MemorySlice::GetHeapMemory() const {
return segment_.GetHeapMemory();
}
const MemorySegment& MemorySlice::GetSegment() const {
return segment_;
}
int8_t MemorySlice::ReadByte(int32_t position) {
return segment_.GetValue<int8_t>(offset_ + position);
}
int32_t MemorySlice::ReadInt(int32_t position) {
return segment_.GetValue<int32_t>(offset_ + position);
}
int16_t MemorySlice::ReadShort(int32_t position) {
return segment_.GetValue<int16_t>(offset_ + position);
}
int64_t MemorySlice::ReadLong(int32_t position) {
return segment_.GetValue<int64_t>(offset_ + position);
}
std::string_view MemorySlice::ReadStringView() {
auto array = segment_.GetArray();
return {array->data() + offset_, static_cast<size_t>(length_)};
}
std::shared_ptr<Bytes> MemorySlice::CopyBytes(MemoryPool* pool) {
auto bytes = std::make_shared<Bytes>(length_, pool);
auto target = MemorySegment::Wrap(bytes);
segment_.CopyTo(offset_, &target, 0, length_);
return bytes;
}
bool MemorySlice::operator<(const MemorySlice& other) const {
return Compare(other) < 0;
}
bool MemorySlice::operator>(const MemorySlice& other) const {
return Compare(other) > 0;
}
bool MemorySlice::operator==(const MemorySlice& other) const {
return Compare(other) == 0;
}
bool MemorySlice::operator!=(const MemorySlice& other) const {
return !(*this == other);
}
bool MemorySlice::operator<=(const MemorySlice& other) const {
return Compare(other) <= 0;
}
bool MemorySlice::operator>=(const MemorySlice& other) const {
return Compare(other) >= 0;
}
std::shared_ptr<MemorySliceInput> MemorySlice::ToInput() {
auto self = shared_from_this();
return std::make_shared<MemorySliceInput>(self);
}
int32_t MemorySlice::Compare(const MemorySlice& other) const {
int32_t len = std::min(length_, other.length_);
for (int32_t i = 0; i < len; ++i) {
auto byte1 = static_cast<unsigned char>(segment_.Get(offset_ + i));
auto byte2 = static_cast<unsigned char>(other.segment_.Get(other.offset_ + i));
if (byte1 != byte2) {
return static_cast<int>(byte1) - static_cast<int>(byte2);
}
}
return length_ - other.length_;
}
} // namespace paimon