Coverage Report

Created: 2024-11-18 12:21

/root/doris/be/src/olap/pb_helper.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
#pragma once
19
20
#include <fstream>
21
22
#include "common/status.h"
23
24
namespace doris {
25
template <typename PB>
26
0
Status read_pb(const std::string& pb_filename, PB* pb) {
27
0
    std::fstream pb_file_stream(pb_filename, std::ios::in | std::ios::binary);
28
0
    if (pb_file_stream.bad()) {
29
0
        auto err_msg = fmt::format("fail to open rowset binlog metas file. [path={}]", pb_filename);
30
0
        LOG(WARNING) << err_msg;
31
0
        return Status::InternalError(err_msg);
32
0
    }
33
34
0
    if (pb->ParseFromIstream(&pb_file_stream)) {
35
0
        return Status::OK();
36
0
    }
37
0
    return Status::InternalError("fail to parse rowset binlog metas file");
38
0
}
39
40
template <typename PB>
41
0
Status write_pb(const std::string& pb_filename, const PB& pb) {
42
0
    std::fstream pb_file_stream(pb_filename, std::ios::out | std::ios::trunc | std::ios::binary);
43
0
    if (pb_file_stream.bad()) {
44
0
        auto err_msg = fmt::format("fail to open rowset binlog metas file. [path={}]", pb_filename);
45
0
        LOG(WARNING) << err_msg;
46
0
        return Status::InternalError(err_msg);
47
0
    }
48
49
0
    if (!pb.SerializeToOstream(&pb_file_stream)) {
50
0
        auto err_msg =
51
0
                fmt::format("fail to save rowset binlog metas to file. [path={}]", pb_filename);
52
0
        LOG(WARNING) << err_msg;
53
0
        return Status::InternalError(err_msg);
54
0
    }
55
56
0
    if (!pb_file_stream.flush()) {
57
0
        auto err_msg =
58
0
                fmt::format("fail to flush rowset binlog metas to file. [path={}]", pb_filename);
59
0
        LOG(WARNING) << err_msg;
60
0
        return Status::InternalError(err_msg);
61
0
    }
62
63
0
    pb_file_stream.close();
64
0
    if (pb_file_stream.bad()) {
65
0
        auto err_msg =
66
0
                fmt::format("fail to close rowset binlog metas file. [path={}]", pb_filename);
67
0
        LOG(WARNING) << err_msg;
68
0
        return Status::InternalError(err_msg);
69
0
    }
70
71
0
    return Status::OK();
72
0
}
73
} // namespace doris