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.h
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
// Classes used internally by pb_util.h.
33
// This header should not be included by anything but pb_util and its tests.
34
#ifndef YB_UTIL_PB_UTIL_INTERNAL_H
35
#define YB_UTIL_PB_UTIL_INTERNAL_H
36
37
#include <glog/logging.h>
38
#include <google/protobuf/io/zero_copy_stream.h>
39
40
#include "yb/util/env.h"
41
#include "yb/util/status.h"
42
43
namespace yb {
44
namespace pb_util {
45
namespace internal {
46
47
// Input Stream used by ParseFromSequentialFile()
48
class SequentialFileFileInputStream : public google::protobuf::io::ZeroCopyInputStream {
49
 public:
50
  explicit SequentialFileFileInputStream(SequentialFile *rfile,
51
                                         size_t buffer_size = kDefaultBufferSize)
52
    : buffer_used_(0), buffer_offset_(0),
53
      buffer_size_(buffer_size), buffer_(new uint8[buffer_size_]),
54
1.80k
      total_read_(0), rfile_(rfile) {
55
1.80k
    CHECK_GT(buffer_size, 0);
56
1.80k
  }
57
58
1.80k
  ~SequentialFileFileInputStream() {
59
1.80k
  }
60
61
  bool Next(const void **data, int *size) override;
62
  bool Skip(int count) override;
63
64
0
  void BackUp(int count) override {
65
0
    CHECK_GE(count, 0);
66
0
    CHECK_LE(count, buffer_offset_);
67
0
    buffer_offset_ -= count;
68
0
    total_read_ -= count;
69
0
  }
70
71
0
  int64 ByteCount() const override {
72
0
    return total_read_;
73
0
  }
74
75
 private:
76
  static const size_t kDefaultBufferSize = 8192;
77
78
  Status status_;
79
80
  size_t buffer_used_;
81
  size_t buffer_offset_;
82
  const size_t buffer_size_;
83
  std::unique_ptr<uint8_t[]> buffer_;
84
85
  size_t total_read_;
86
  SequentialFile *rfile_;
87
};
88
89
// Output Stream used by SerializeToWritableFile()
90
class WritableFileOutputStream : public google::protobuf::io::ZeroCopyOutputStream {
91
 public:
92
  explicit WritableFileOutputStream(WritableFile *wfile, size_t buffer_size = kDefaultBufferSize)
93
    : buffer_offset_(0), buffer_size_(buffer_size), buffer_(new uint8[buffer_size_]),
94
2.54k
      flushed_(0), wfile_(wfile) {
95
2.54k
    CHECK_GT(buffer_size, 0);
96
2.54k
  }
97
98
2.54k
  ~WritableFileOutputStream() {
99
2.54k
  }
100
101
2.54k
  bool Flush() {
102
2.54k
    if (buffer_offset_ > 0) {
103
2.54k
      Slice data(buffer_.get(), buffer_offset_);
104
2.54k
      status_ = wfile_->Append(data);
105
2.54k
      flushed_ += buffer_offset_;
106
2.54k
      buffer_offset_ = 0;
107
2.54k
    }
108
2.54k
    return status_.ok();
109
2.54k
  }
110
111
  bool Next(void **data, int *size) override;
112
113
2.54k
  void BackUp(int count) override {
114
2.54k
    CHECK_GE(count, 0);
115
2.54k
    CHECK_LE(count, buffer_offset_);
116
2.54k
    buffer_offset_ -= count;
117
2.54k
  }
118
119
7
  int64 ByteCount() const override {
120
7
    return flushed_ + buffer_offset_;
121
7
  }
122
123
 private:
124
  static const size_t kDefaultBufferSize = 8192;
125
126
  Status status_;
127
128
  size_t buffer_offset_;
129
  const size_t buffer_size_;
130
  std::unique_ptr<uint8_t[]> buffer_;
131
132
  size_t flushed_;
133
  WritableFile *wfile_;
134
};
135
136
} // namespace internal
137
} // namespace pb_util
138
} // namespace yb
139
140
#endif  // YB_UTIL_PB_UTIL_INTERNAL_H