be/src/storage/index/snii/common/slice.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <cassert> |
21 | | #include <cstddef> |
22 | | #include <cstdint> |
23 | | #include <string_view> |
24 | | #include <vector> |
25 | | |
26 | | namespace doris::snii { |
27 | | |
28 | | // Read-only byte view (does not own memory). Lifetime is managed by the underlying buffer. |
29 | | class Slice { |
30 | | public: |
31 | 10.7M | Slice() = default; |
32 | 20.4M | Slice(const uint8_t* d, size_t n) : data_(d), size_(n) {} |
33 | 2.48M | explicit Slice(const std::vector<uint8_t>& v) : data_(v.data()), size_(v.size()) {} |
34 | | explicit Slice(std::string_view sv) |
35 | 1.38M | : data_(reinterpret_cast<const uint8_t*>(sv.data())), size_(sv.size()) {} |
36 | | |
37 | 66.9M | const uint8_t* data() const { return data_; } |
38 | 41.7M | size_t size() const { return size_; } |
39 | 3.10M | bool empty() const { return size_ == 0; } |
40 | | |
41 | 6.96M | uint8_t operator[](size_t i) const { |
42 | 6.96M | assert(i < size_); |
43 | 6.96M | return data_[i]; |
44 | 6.96M | } |
45 | | |
46 | 18.3M | Slice subslice(size_t off, size_t n) const { |
47 | 18.3M | assert(off + n <= size_); |
48 | 18.3M | return Slice(data_ + off, n); |
49 | 18.3M | } |
50 | | |
51 | | private: |
52 | | const uint8_t* data_ = nullptr; |
53 | | size_t size_ = 0; |
54 | | }; |
55 | | |
56 | | } // namespace doris::snii |