Apollo  6.0
Open source self driving car software
record_file_reader.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_RECORD_FILE_RECORD_FILE_READER_H_
18 #define CYBER_RECORD_FILE_RECORD_FILE_READER_H_
19 
20 #include <fstream>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <utility>
25 
26 #include <limits>
27 #include "google/protobuf/io/coded_stream.h"
28 #include "google/protobuf/io/zero_copy_stream_impl.h"
29 #include "google/protobuf/message.h"
30 #include "google/protobuf/text_format.h"
31 
32 #include "cyber/common/log.h"
35 #include "cyber/time/time.h"
36 
37 namespace apollo {
38 namespace cyber {
39 namespace record {
40 
41 using google::protobuf::io::CodedInputStream;
42 using google::protobuf::io::FileInputStream;
43 using google::protobuf::io::ZeroCopyInputStream;
44 
46  public:
47  RecordFileReader() = default;
48  virtual ~RecordFileReader() = default;
49  bool Open(const std::string& path) override;
50  void Close() override;
51  bool Reset();
52  bool ReadSection(Section* section);
53  bool SkipSection(int64_t size);
54  template <typename T>
55  bool ReadSection(int64_t size, T* message);
56  bool ReadIndex();
57  bool EndOfFile() { return end_of_file_; }
58 
59  private:
60  bool ReadHeader();
61  bool end_of_file_ = false;
62 };
63 
64 template <typename T>
65 bool RecordFileReader::ReadSection(int64_t size, T* message) {
66  if (size < std::numeric_limits<int>::min() ||
67  size > std::numeric_limits<int>::max()) {
68  AERROR << "Size value greater than the range of int value.";
69  return false;
70  }
71  FileInputStream raw_input(fd_, static_cast<int>(size));
72  CodedInputStream coded_input(&raw_input);
73  CodedInputStream::Limit limit = coded_input.PushLimit(static_cast<int>(size));
74  if (!message->ParseFromCodedStream(&coded_input)) {
75  AERROR << "Parse section message failed.";
76  end_of_file_ = coded_input.ExpectAtEnd();
77  return false;
78  }
79  if (!coded_input.ConsumedEntireMessage()) {
80  AERROR << "Do not consumed entire message.";
81  return false;
82  }
83  coded_input.PopLimit(limit);
84  if (static_cast<int64_t>(message->ByteSizeLong()) != size) {
85  AERROR << "Message size is not consistent in section header"
86  << ", expect: " << size << ", actual: " << message->ByteSizeLong();
87  return false;
88  }
89  return true;
90 }
91 
92 } // namespace record
93 } // namespace cyber
94 } // namespace apollo
95 
96 #endif // CYBER_RECORD_FILE_RECORD_FILE_READER_H_
int fd_
Definition: record_file_base.h:48
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: section.h:24
Definition: record_file_base.h:31
Definition: record_file_reader.h:45
#define AERROR
Definition: log.h:44
bool Open(const std::string &path) override
bool EndOfFile()
Definition: record_file_reader.h:57