Apache OpenDALâ„¢ C++ Binding
The C++ binding for Apache OpenDALâ„¢
Loading...
Searching...
No Matches
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
33namespace opendal {
34
40 FILE = 1,
41 DIR = 2,
43};
44
49struct 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
66struct Entry {
67 std::string path;
68
69 Entry(ffi::Entry &&);
70};
71
72class Reader;
73class Lister;
74
79class 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 [[deprecated("Use exists() instead.")]]
141 bool is_exist(std::string_view path);
142
149 bool exists(std::string_view path);
150
156 void create_dir(std::string_view path);
157
164 void copy(std::string_view src, std::string_view dst);
165
172 void rename(std::string_view src, std::string_view dst);
173
179 void remove(std::string_view path);
180
187 Metadata stat(std::string_view path);
188
196 std::vector<Entry> list(std::string_view path);
197
198 Lister lister(std::string_view path);
199
200 private:
201 std::optional<rust::Box<opendal::ffi::Operator>> operator_;
202};
203
214 : public boost::iostreams::device<boost::iostreams::input_seekable> {
215 public:
216 Reader(rust::Box<opendal::ffi::Reader> &&reader)
217 : raw_reader_(std::move(reader)) {}
218
219 std::streamsize read(void *s, std::streamsize n);
220 std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
221
222 private:
223 rust::Box<opendal::ffi::Reader> raw_reader_;
224};
225
226// Boost IOStreams requires it to be copyable. So we need to use
227// `reference_wrapper` in ReaderStream. More details can be seen at
228// https://lists.boost.org/Archives/boost/2005/10/95939.php
229
237 : public boost::iostreams::stream<boost::reference_wrapper<Reader>> {
238 public:
240 : boost::iostreams::stream<boost::reference_wrapper<Reader>>(
241 boost::ref(reader_)),
242 reader_(std::move(reader)) {}
243
244 private:
245 Reader reader_;
246};
247
260class Lister {
261 public:
262 Lister(rust::Box<opendal::ffi::Lister> &&lister)
263 : raw_lister_(std::move(lister)) {}
264
272 public:
273 using iterator_category = std::input_iterator_tag;
275 using difference_type = std::ptrdiff_t;
276 using pointer = Entry *;
277 using reference = Entry &;
278
279 ListerIterator(Lister &lister) : lister_(lister) {
280 current_entry_ = lister_.next();
281 }
282
283 Entry operator*() { return current_entry_.value(); }
284
286 if (current_entry_) {
287 current_entry_ = lister_.next();
288 }
289 return *this;
290 }
291
292 bool operator!=(const ListerIterator &other) const {
293 return current_entry_ != std::nullopt ||
294 other.current_entry_ != std::nullopt;
295 }
296
297 protected:
298 // Only used for end iterator
299 ListerIterator(Lister &lister, bool /*end*/) : lister_(lister) {}
300
301 private:
302 Lister &lister_;
303 std::optional<Entry> current_entry_;
304
305 friend class Lister;
306 };
307
313 std::optional<Entry> next();
314
316 ListerIterator end() { return ListerIterator(*this, true); }
317
318 private:
319 rust::Box<opendal::ffi::Lister> raw_lister_;
320};
321} // namespace opendal
ListerIterator is an iterator of Lister.
Definition opendal.hpp:271
ListerIterator & operator++()
Definition opendal.hpp:285
bool operator!=(const ListerIterator &other) const
Definition opendal.hpp:292
Entry operator*()
Definition opendal.hpp:283
ListerIterator(Lister &lister, bool)
Definition opendal.hpp:299
ListerIterator(Lister &lister)
Definition opendal.hpp:279
std::ptrdiff_t difference_type
Definition opendal.hpp:275
std::input_iterator_tag iterator_category
Definition opendal.hpp:273
Lister is designed to list the entries of a directory.
Definition opendal.hpp:260
Lister(rust::Box< opendal::ffi::Lister > &&lister)
Definition opendal.hpp:262
ListerIterator end()
Definition opendal.hpp:316
ListerIterator begin()
Definition opendal.hpp:315
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.
Operator & operator=(Operator &&)=default
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.
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 & operator=(const Operator &)=delete
~Operator()=default
bool exists(std::string_view path)
Check if the path exists.
std::vector< uint8_t > read(std::string_view path)
Read data from the operator.
Reader reader(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:237
ReaderStream(Reader &&reader)
Definition opendal.hpp:239
Reader is designed to read data from the operator.
Definition opendal.hpp:214
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:216
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