/Users/deen/code/yugabyte-db/src/yb/util/pb_util-internal.cc
Line | Count | Source (jump to first uncovered line) |
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 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #include "yb/util/pb_util-internal.h" |
33 | | |
34 | | #include "yb/gutil/casts.h" |
35 | | |
36 | | namespace yb { |
37 | | namespace pb_util { |
38 | | namespace internal { |
39 | | |
40 | | //////////////////////////////////////////// |
41 | | // SequentialFileFileInputStream |
42 | | //////////////////////////////////////////// |
43 | | |
44 | 6.69k | bool SequentialFileFileInputStream::Next(const void **data, int *size) { |
45 | 6.69k | if (PREDICT_FALSE(!status_.ok())) { |
46 | 0 | LOG(WARNING) << "Already failed on a previous read: " << status_.ToString(); |
47 | 0 | return false; |
48 | 0 | } |
49 | | |
50 | 6.69k | size_t available = buffer_used_ - buffer_offset_; |
51 | 6.69k | if (available > 0) { |
52 | 0 | *data = buffer_.get() + buffer_offset_; |
53 | 0 | *size = narrow_cast<int>(available); |
54 | 0 | buffer_offset_ += available; |
55 | 0 | total_read_ += available; |
56 | 0 | return true; |
57 | 0 | } |
58 | | |
59 | 6.69k | Slice result; |
60 | 6.69k | status_ = rfile_->Read(buffer_size_, &result, buffer_.get()); |
61 | 6.69k | if (!status_.ok()) { |
62 | 0 | LOG(WARNING) << "Read at " << buffer_offset_ << " failed: " << status_.ToString(); |
63 | 0 | return false; |
64 | 0 | } |
65 | | |
66 | 6.69k | if (result.data() != buffer_.get()) { |
67 | 0 | memcpy(buffer_.get(), result.data(), result.size()); |
68 | 0 | } |
69 | | |
70 | 6.69k | buffer_used_ = result.size(); |
71 | 6.69k | buffer_offset_ = buffer_used_; |
72 | 6.69k | total_read_ += buffer_used_; |
73 | 6.69k | *data = buffer_.get(); |
74 | 6.69k | *size = narrow_cast<int>(buffer_used_); |
75 | 6.69k | return buffer_used_ > 0; |
76 | 6.69k | } |
77 | | |
78 | 0 | bool SequentialFileFileInputStream::Skip(int signed_count) { |
79 | 0 | CHECK_GT(signed_count, 0); |
80 | 0 | size_t count = signed_count; |
81 | 0 | auto avail = buffer_used_ - buffer_offset_; |
82 | 0 | if (avail > count) { |
83 | 0 | buffer_offset_ += count; |
84 | 0 | total_read_ += count; |
85 | 0 | } else { |
86 | 0 | buffer_used_ = 0; |
87 | 0 | buffer_offset_ = 0; |
88 | 0 | status_ = rfile_->Skip(count - avail); |
89 | 0 | total_read_ += count - avail; |
90 | 0 | } |
91 | 0 | return status_.ok(); |
92 | 0 | } |
93 | | |
94 | | //////////////////////////////////////////// |
95 | | // WritableFileOutputStream |
96 | | //////////////////////////////////////////// |
97 | | |
98 | 4.70k | bool WritableFileOutputStream::Next(void **data, int *size) { |
99 | 4.70k | if (PREDICT_FALSE(!status_.ok())) { |
100 | 0 | LOG(WARNING) << "Already failed on a previous write: " << status_.ToString(); |
101 | 0 | return false; |
102 | 0 | } |
103 | | |
104 | 4.70k | size_t available = buffer_size_ - buffer_offset_; |
105 | 4.70k | if (available > 0) { |
106 | 4.70k | *data = buffer_.get() + buffer_offset_; |
107 | 4.70k | *size = narrow_cast<int>(available); |
108 | 4.70k | buffer_offset_ += available; |
109 | 4.70k | return true; |
110 | 4.70k | } |
111 | | |
112 | 1 | if (!Flush()) { |
113 | 0 | return false; |
114 | 0 | } |
115 | | |
116 | 1 | buffer_offset_ = buffer_size_; |
117 | 1 | *data = buffer_.get(); |
118 | 1 | *size = narrow_cast<int>(buffer_size_); |
119 | 1 | return true; |
120 | 1 | } |
121 | | |
122 | | } // namespace internal |
123 | | } // namespace pb_util |
124 | | } // namespace yb |