Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
message.h
Go to the documentation of this file.
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 // C++ object model and user API for interprocess schema messaging
19 
20 #ifndef ARROW_IPC_MESSAGE_H
21 #define ARROW_IPC_MESSAGE_H
22 
23 #include <cstdint>
24 #include <memory>
25 #include <string>
26 
27 #include "arrow/status.h"
28 #include "arrow/util/macros.h"
29 #include "arrow/util/visibility.h"
30 
31 namespace arrow {
32 
33 class Buffer;
34 
35 namespace io {
36 
37 class InputStream;
38 class OutputStream;
39 class RandomAccessFile;
40 
41 } // namespace io
42 
43 namespace ipc {
44 
45 enum class MetadataVersion : char { V1, V2, V3 };
46 
47 // ARROW-109: We set this number arbitrarily to help catch user mistakes. For
48 // deeply nested schemas, it is expected the user will indicate explicitly the
49 // maximum allowed recursion depth
50 constexpr int kMaxNestingDepth = 64;
51 
52 // Read interface classes. We do not fully deserialize the flatbuffers so that
53 // individual fields metadata can be retrieved from very large schema without
54 //
55 
58 class ARROW_EXPORT Message {
59  public:
60  enum Type { NONE, SCHEMA, DICTIONARY_BATCH, RECORD_BATCH, TENSOR };
61 
65  Message(const std::shared_ptr<Buffer>& metadata, const std::shared_ptr<Buffer>& body);
66 
67  ~Message();
68 
75  static Status Open(const std::shared_ptr<Buffer>& metadata,
76  const std::shared_ptr<Buffer>& body, std::unique_ptr<Message>* out);
77 
85  static Status ReadFrom(const std::shared_ptr<Buffer>& metadata, io::InputStream* stream,
86  std::unique_ptr<Message>* out);
87 
92  bool Equals(const Message& other) const;
93 
97  std::shared_ptr<Buffer> metadata() const;
98 
102  std::shared_ptr<Buffer> body() const;
103 
105  Type type() const;
106 
108  MetadataVersion metadata_version() const;
109 
110  const void* header() const;
111 
117  Status SerializeTo(io::OutputStream* file, int64_t* output_length) const;
118 
119  private:
120  // Hide serialization details from user API
121  class MessageImpl;
122  std::unique_ptr<MessageImpl> impl_;
123 
125 };
126 
127 ARROW_EXPORT std::string FormatMessageType(Message::Type type);
128 
131 class ARROW_EXPORT MessageReader {
132  public:
133  virtual ~MessageReader() = default;
134 
139  virtual Status ReadNextMessage(std::unique_ptr<Message>* message) = 0;
140 };
141 
144 class ARROW_EXPORT InputStreamMessageReader : public MessageReader {
145  public:
146  explicit InputStreamMessageReader(io::InputStream* stream) : stream_(stream) {}
147 
148  explicit InputStreamMessageReader(const std::shared_ptr<io::InputStream>& owned_stream)
149  : InputStreamMessageReader(owned_stream.get()) {
150  owned_stream_ = owned_stream;
151  }
152 
154 
155  Status ReadNextMessage(std::unique_ptr<Message>* message) override;
156 
157  private:
158  io::InputStream* stream_;
159  std::shared_ptr<io::InputStream> owned_stream_;
160 };
161 
176 ARROW_EXPORT
177 Status ReadMessage(const int64_t offset, const int32_t metadata_length,
178  io::RandomAccessFile* file, std::unique_ptr<Message>* message);
179 
185 ARROW_EXPORT
186 Status ReadMessage(io::InputStream* stream, std::unique_ptr<Message>* message);
187 
188 } // namespace ipc
189 } // namespace arrow
190 
191 #endif // ARROW_IPC_MESSAGE_H
Status ReadMessage(io::InputStream *stream, std::unique_ptr< Message > *message)
Read encapulated RPC message (metadata and body) from InputStream.
Definition: interfaces.h:111
std::string FormatMessageType(Message::Type type)
An IPC message including metadata and body.
Definition: message.h:58
Definition: interfaces.h:116
Definition: interfaces.h:121
Definition: status.h:106
Type
Definition: message.h:60
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
MetadataVersion
Definition: message.h:45
constexpr int kMaxNestingDepth
Definition: message.h:50
InputStreamMessageReader(io::InputStream *stream)
Definition: message.h:146
Abstract interface for a sequence of messages.
Definition: message.h:131
Implementation of MessageReader that reads from InputStream.
Definition: message.h:144
InputStreamMessageReader(const std::shared_ptr< io::InputStream > &owned_stream)
Definition: message.h:148
#define ARROW_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:23