Apache OpenDALâ„¢ C++ Binding
The C++ binding for Apache OpenDALâ„¢
opendal.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include "boost/date_time/posix_time/posix_time.hpp"
29 #include "boost/iostreams/concepts.hpp"
30 #include "boost/iostreams/stream.hpp"
31 #include "lib.rs.h"
32 
33 namespace opendal {
34 
39 enum EntryMode {
40  FILE = 1,
41  DIR = 2,
42  UNKNOWN = 0,
43 };
44 
49 struct Metadata {
51  std::uint64_t content_length;
52  std::optional<std::string> cache_control;
53  std::optional<std::string> content_disposition;
54  std::optional<std::string> content_md5;
55  std::optional<std::string> content_type;
56  std::optional<std::string> etag;
57  std::optional<boost::posix_time::ptime> last_modified;
58 
59  Metadata(ffi::Metadata &&);
60 };
61 
66 struct Entry {
67  std::string path;
68 
69  Entry(ffi::Entry &&);
70 };
71 
72 class Reader;
73 class Lister;
74 
79 class Operator {
80  public:
81  Operator() = default;
82 
89  Operator(std::string_view scheme,
90  const std::unordered_map<std::string, std::string> &config = {});
91 
92  // Disable copy and assign
93  Operator(const Operator &) = delete;
94  Operator &operator=(const Operator &) = delete;
95 
96  // Enable move
97  Operator(Operator &&) = default;
98  Operator &operator=(Operator &&) = default;
99  ~Operator() = default;
100 
106  bool available() const;
107 
116  std::vector<uint8_t> read(std::string_view path);
117 
124  void write(std::string_view path, const std::vector<uint8_t> &data);
125 
132  Reader reader(std::string_view path);
133 
140  bool is_exist(std::string_view path);
141 
147  void create_dir(std::string_view path);
148 
155  void copy(std::string_view src, std::string_view dst);
156 
163  void rename(std::string_view src, std::string_view dst);
164 
170  void remove(std::string_view path);
171 
178  Metadata stat(std::string_view path);
179 
187  std::vector<Entry> list(std::string_view path);
188 
189  Lister lister(std::string_view path);
190 
191  private:
192  std::optional<rust::Box<opendal::ffi::Operator>> operator_;
193 };
194 
204 class Reader
205  : public boost::iostreams::device<boost::iostreams::input_seekable> {
206  public:
207  Reader(rust::Box<opendal::ffi::Reader> &&reader)
208  : raw_reader_(std::move(reader)) {}
209 
210  std::streamsize read(void *s, std::streamsize n);
211  std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
212 
213  private:
214  rust::Box<opendal::ffi::Reader> raw_reader_;
215 };
216 
217 // Boost IOStreams requires it to be copyable. So we need to use
218 // `reference_wrapper` in ReaderStream. More details can be seen at
219 // https://lists.boost.org/Archives/boost/2005/10/95939.php
220 
228  : public boost::iostreams::stream<boost::reference_wrapper<Reader>> {
229  public:
231  : boost::iostreams::stream<boost::reference_wrapper<Reader>>(
232  boost::ref(reader_)),
233  reader_(std::move(reader)) {}
234 
235  private:
236  Reader reader_;
237 };
238 
251 class Lister {
252  public:
253  Lister(rust::Box<opendal::ffi::Lister> &&lister)
254  : raw_lister_(std::move(lister)) {}
255 
263  public:
264  using iterator_category = std::input_iterator_tag;
265  using value_type = Entry;
266  using difference_type = std::ptrdiff_t;
267  using pointer = Entry *;
268  using reference = Entry &;
269 
270  ListerIterator(Lister &lister) : lister_(lister) {
271  current_entry_ = lister_.next();
272  }
273 
274  Entry operator*() { return current_entry_.value(); }
275 
277  if (current_entry_) {
278  current_entry_ = lister_.next();
279  }
280  return *this;
281  }
282 
283  bool operator!=(const ListerIterator &other) const {
284  return current_entry_ != std::nullopt ||
285  other.current_entry_ != std::nullopt;
286  }
287 
288  protected:
289  // Only used for end iterator
290  ListerIterator(Lister &lister, bool /*end*/) : lister_(lister) {}
291 
292  private:
293  Lister &lister_;
294  std::optional<Entry> current_entry_;
295 
296  friend class Lister;
297  };
298 
304  std::optional<Entry> next();
305 
306  ListerIterator begin() { return ListerIterator(*this); }
307  ListerIterator end() { return ListerIterator(*this, true); }
308 
309  private:
310  rust::Box<opendal::ffi::Lister> raw_lister_;
311 };
312 } // namespace opendal
ListerIterator is an iterator of Lister.
Definition: opendal.hpp:262
bool operator!=(const ListerIterator &other) const
Definition: opendal.hpp:283
ListerIterator & operator++()
Definition: opendal.hpp:276
Entry operator*()
Definition: opendal.hpp:274
ListerIterator(Lister &lister, bool)
Definition: opendal.hpp:290
ListerIterator(Lister &lister)
Definition: opendal.hpp:270
std::ptrdiff_t difference_type
Definition: opendal.hpp:266
std::input_iterator_tag iterator_category
Definition: opendal.hpp:264
Lister is designed to list the entries of a directory.
Definition: opendal.hpp:251
Lister(rust::Box< opendal::ffi::Lister > &&lister)
Definition: opendal.hpp:253
ListerIterator end()
Definition: opendal.hpp:307
ListerIterator begin()
Definition: opendal.hpp:306
std::optional< Entry > next()
Get the next entry of the lister.
Operator is the entry for all public APIs.
Definition: opendal.hpp:79
void rename(std::string_view src, std::string_view dst)
Rename a file from src to dst.
Lister lister(std::string_view path)
void create_dir(std::string_view path)
Create a directory.
Operator(Operator &&)=default
void remove(std::string_view path)
Remove a file or directory.
Operator & operator=(Operator &&)=default
Metadata stat(std::string_view path)
Get the metadata of a file or directory.
void write(std::string_view path, const std::vector< uint8_t > &data)
Write data to the operator.
std::vector< Entry > list(std::string_view path)
List the entries of a directory.
Operator()=default
Operator(const Operator &)=delete
bool is_exist(std::string_view path)
Check if the path exists.
Operator(std::string_view scheme, const std::unordered_map< std::string, std::string > &config={})
Construct a new Operator object.
~Operator()=default
Operator & operator=(const Operator &)=delete
Reader reader(std::string_view path)
Read data from the operator.
std::vector< uint8_t > read(std::string_view path)
Read data from the operator.
void copy(std::string_view src, std::string_view dst)
Copy a file from src to dst.
bool available() const
Check if the operator is available.
ReaderStream is a stream wrapper of Reader which can provide iostream interface. It will keep a Reade...
Definition: opendal.hpp:228
ReaderStream(Reader &&reader)
Definition: opendal.hpp:230
Reader is designed to read data from the operator.
Definition: opendal.hpp:205
std::streampos seek(std::streamoff off, std::ios_base::seekdir way)
std::streamsize read(void *s, std::streamsize n)
Reader(rust::Box< opendal::ffi::Reader > &&reader)
Definition: opendal.hpp:207
Definition: opendal.hpp:33
EntryMode
The mode of the entry.
Definition: opendal.hpp:39
@ FILE
Definition: opendal.hpp:40
@ UNKNOWN
Definition: opendal.hpp:42
@ DIR
Definition: opendal.hpp:41
The entry of a file or directory.
Definition: opendal.hpp:66
std::string path
Definition: opendal.hpp:67
Entry(ffi::Entry &&)
The metadata of a file or directory.
Definition: opendal.hpp:49
std::optional< std::string > content_disposition
Definition: opendal.hpp:53
EntryMode type
Definition: opendal.hpp:50
Metadata(ffi::Metadata &&)
std::optional< std::string > etag
Definition: opendal.hpp:56
std::optional< std::string > cache_control
Definition: opendal.hpp:52
std::optional< std::string > content_type
Definition: opendal.hpp:55
std::uint64_t content_length
Definition: opendal.hpp:51
std::optional< std::string > content_md5
Definition: opendal.hpp:54
std::optional< boost::posix_time::ptime > last_modified
Definition: opendal.hpp:57