cord_rep_btree_reader.cc
Go to the documentation of this file.
1 // Copyright 2021 The Abseil Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <cassert>
18 
19 #include "absl/base/config.h"
21 #include "absl/strings/internal/cord_internal.h"
24 #include "absl/strings/internal/cord_rep_flat.h"
25 
26 namespace absl {
28 namespace cord_internal {
29 
30 absl::string_view CordRepBtreeReader::Read(size_t n, size_t chunk_size,
31  CordRep*& tree) {
32  assert(chunk_size <= navigator_.Current()->length);
33 
34  // If chunk_size is non-zero, we need to start inside last returned edge.
35  // Else we start reading at the next data edge of the tree.
36  CordRep* edge = chunk_size ? navigator_.Current() : navigator_.Next();
37  const size_t offset = chunk_size ? edge->length - chunk_size : 0;
38 
39  // Read the sub tree and verify we got what we wanted.
41  tree = result.tree;
42 
43  // If the data returned in `tree` was covered entirely by `chunk_size`, i.e.,
44  // read from the 'previous' edge, we did not consume any additional data, and
45  // can directly return the substring into the current data edge as the next
46  // chunk. We can easily establish from the above code that `navigator_.Next()`
47  // has not been called as that requires `chunk_size` to be zero.
48  if (n < chunk_size) return EdgeData(edge).substr(result.n);
49 
50  // The amount of data taken from the last edge is `chunk_size` and `result.n`
51  // contains the offset into the current edge trailing the read data (which can
52  // be 0). As the call to `navigator_.Read()` could have consumed all remaining
53  // data, calling `navigator_.Current()` is not safe before checking if we
54  // already consumed all remaining data.
55  const size_t consumed_by_read = n - chunk_size - result.n;
56  if (consumed_by_read >= remaining_) {
57  remaining_ = 0;
58  return {};
59  }
60 
61  // We did not read all data, return remaining data from current edge.
62  edge = navigator_.Current();
63  remaining_ -= consumed_by_read + edge->length;
64  return EdgeData(edge).substr(result.n);
65 }
66 
67 } // namespace cord_internal
69 } // namespace absl
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
cord_data_edge.h
absl::cord_internal::EdgeData
absl::string_view EdgeData(const CordRep *edge)
Definition: cord_data_edge.h:45
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
absl::cord_internal::CordRepBtreeReader::navigator_
CordRepBtreeNavigator navigator_
Definition: cord_rep_btree_reader.h:159
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::cord_internal::CordRep
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:209
absl::cord_internal::CordRepBtreeNavigator::Current
CordRep * Current() const
Definition: cord_rep_btree_navigator.h:157
cord_rep_btree_reader.h
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
cord_rep_btree.h
absl::cord_internal::CordRepBtreeNavigator::Read
ReadResult Read(size_t edge_offset, size_t n)
Definition: cord_rep_btree_navigator.cc:108
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::cord_internal::CordRep::length
size_t length
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:228
absl::cord_internal::CordRepBtreeReader::remaining_
size_t remaining_
Definition: cord_rep_btree_reader.h:158
absl::cord_internal::CordRepBtreeNavigator::Next
CordRep * Next()
Definition: cord_rep_btree_navigator.h:217
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
cord_rep_btree_navigator.h
absl::cord_internal::CordRepBtreeReader::Read
absl::string_view Read(size_t n, size_t chunk_size, CordRep *&tree)
Definition: cord_rep_btree_reader.cc:30
absl::string_view::substr
constexpr string_view substr(size_type pos=0, size_type n=npos) const
Definition: abseil-cpp/absl/strings/string_view.h:399
absl::cord_internal::CordRepBtreeNavigator::ReadResult
Definition: cord_rep_btree_navigator.h:61
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:03