YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
3.61k
bool SequentialFileFileInputStream::Next(const void **data, int *size) {
45
3.61k
  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
3.61k
  size_t available = buffer_used_ - buffer_offset_;
51
3.61k
  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
3.61k
  Slice result;
60
3.61k
  status_ = rfile_->Read(buffer_size_, &result, buffer_.get());
61
3.61k
  if (!status_.ok()) {
62
0
    LOG(WARNING) << "Read at " << buffer_offset_ << " failed: " << status_.ToString();
63
0
    return false;
64
0
  }
65
66
3.61k
  if (result.data() != buffer_.get()) {
67
0
    memcpy(buffer_.get(), result.data(), result.size());
68
0
  }
69
70
3.61k
  buffer_used_ = result.size();
71
3.61k
  buffer_offset_ = buffer_used_;
72
3.61k
  total_read_ += buffer_used_;
73
3.61k
  *data = buffer_.get();
74
3.61k
  *size = narrow_cast<int>(buffer_used_);
75
3.61k
  return buffer_used_ > 0;
76
3.61k
}
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
2.54k
bool WritableFileOutputStream::Next(void **data, int *size) {
99
2.54k
  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
2.54k
  size_t available = buffer_size_ - buffer_offset_;
105
2.54k
  if (available > 0) {
106
2.54k
    *data = buffer_.get() + buffer_offset_;
107
2.54k
    *size = narrow_cast<int>(available);
108
2.54k
    buffer_offset_ += available;
109
2.54k
    return true;
110
2.54k
  }
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