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 <cstdint>
23#include <optional>
24#include <string>
25#include <unordered_map>
26#include <vector>
27
28#include "boost/date_time/posix_time/ptime.hpp"
29#include "boost/iostreams/concepts.hpp"
30#include "boost/iostreams/stream.hpp"
31
32namespace opendal {
33
34namespace details {
35class Operator;
36class Reader;
37class Lister;
38} // namespace details
39
44enum class EntryMode : int {
45 FILE = 1,
46 DIR = 2,
47 UNKNOWN = 0,
48};
49
54struct Metadata {
56 std::uint64_t content_length;
57 std::optional<std::string> cache_control;
58 std::optional<std::string> content_disposition;
59 std::optional<std::string> content_md5;
60 std::optional<std::string> content_type;
61 std::optional<std::string> etag;
62 std::optional<boost::posix_time::ptime> last_modified;
63};
64
69struct Entry {
70 std::string path;
71};
72
73class Reader;
74class Lister;
75
80class Operator {
81 public:
83
90 Operator(std::string_view scheme,
91 const std::unordered_map<std::string, std::string> &config = {});
92
93 // Disable copy and assign
94 Operator(const Operator &) = delete;
95 Operator &operator=(const Operator &) = delete;
96
97 // Enable move
100
102
108 bool available() const;
109
118 std::string read(std::string_view path);
119
126 void write(std::string_view path, std::string_view data);
127
134 Reader reader(std::string_view path);
135
142 [[deprecated("Use exists() instead.")]]
143 bool is_exist(std::string_view path);
144
151 bool exists(std::string_view path);
152
158 void create_dir(std::string_view path);
159
166 void copy(std::string_view src, std::string_view dst);
167
174 void rename(std::string_view src, std::string_view dst);
175
181 void remove(std::string_view path);
182
189 Metadata stat(std::string_view path);
190
198 std::vector<Entry> list(std::string_view path);
199
200 Lister lister(std::string_view path);
201
202 private:
203 std::unique_ptr<details::Operator> operator_;
204};
205
216 : public boost::iostreams::device<boost::iostreams::input_seekable> {
217 public:
218 Reader(std::unique_ptr<details::Reader> &&reader);
219
221
223
224 std::streamsize read(void *s, std::streamsize n);
225
226 std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
227
228 private:
229 std::unique_ptr<details::Reader> raw_reader_;
230};
231
232// Boost IOStreams requires it to be copyable. So we need to use
233// `reference_wrapper` in ReaderStream. More details can be seen at
234// https://lists.boost.org/Archives/boost/2005/10/95939.php
235
243 : public boost::iostreams::stream<boost::reference_wrapper<Reader>> {
244 public:
246 : boost::iostreams::stream<boost::reference_wrapper<Reader>>(
247 boost::ref(reader_)),
248 reader_(std::move(reader)) {}
249
250 private:
251 Reader reader_;
252};
253
266class Lister {
267 public:
268 Lister(std::unique_ptr<details::Lister> lister);
269
271
273
280 class Iterator {
281 public:
282 using iterator_category = std::input_iterator_tag;
284 using difference_type = std::ptrdiff_t;
285 using pointer = Entry *;
286 using reference = Entry &;
287
288 Iterator(Lister &lister) : lister_{lister} {
289 current_entry_ = lister_.next();
290 }
291
292 Entry operator*() { return current_entry_.value(); }
293
295 if (current_entry_) {
296 current_entry_ = lister_.next();
297 }
298 return *this;
299 }
300
301 bool operator!=(const Iterator &other) const {
302 return current_entry_ != std::nullopt ||
303 other.current_entry_ != std::nullopt;
304 }
305
306 protected:
307 // Only used for end iterator
308 Iterator(Lister &lister, bool /*end*/) : lister_(lister) {}
309
310 private:
311 Lister &lister_;
312 std::optional<Entry> current_entry_;
313
314 friend class Lister;
315 };
316
322 std::optional<Entry> next();
323
324 Iterator begin() { return Iterator(*this); }
325 Iterator end() { return Iterator(*this, true); }
326
327 private:
328 std::unique_ptr<details::Lister> raw_lister_;
329};
330
331} // namespace opendal
Definition opendal.hpp:280
std::ptrdiff_t difference_type
Definition opendal.hpp:284
Entry operator*()
Definition opendal.hpp:292
Iterator(Lister &lister, bool)
Definition opendal.hpp:308
Iterator(Lister &lister)
Definition opendal.hpp:288
std::input_iterator_tag iterator_category
Definition opendal.hpp:282
Iterator & operator++()
Definition opendal.hpp:294
bool operator!=(const Iterator &other) const
Definition opendal.hpp:301
Lister is designed to list the entries of a directory.
Definition opendal.hpp:266
Lister(std::unique_ptr< details::Lister > lister)
Lister(Lister &&)
std::optional< Entry > next()
Get the next entry of the lister.
Iterator begin()
Definition opendal.hpp:324
Iterator end()
Definition opendal.hpp:325
Operator is the entry for all public APIs.
Definition opendal.hpp:80
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.
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, std::string_view data)
Write data to the operator.
std::vector< Entry > list(std::string_view path)
List the entries of a directory.
std::string read(std::string_view path)
Read data from the operator.
Operator(const Operator &)=delete
bool is_exist(std::string_view path)
Check if the path exists.
Operator(Operator &&)
Operator(std::string_view scheme, const std::unordered_map< std::string, std::string > &config={})
Construct a new Operator object.
Operator & operator=(const Operator &)=delete
bool exists(std::string_view path)
Check if the path exists.
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.
Operator & operator=(Operator &&)
ReaderStream is a stream wrapper of Reader which can provide iostream interface. It will keep a Reade...
Definition opendal.hpp:243
ReaderStream(Reader &&reader)
Definition opendal.hpp:245
Reader is designed to read data from the operator.
Definition opendal.hpp:216
Reader(std::unique_ptr< details::Reader > &&reader)
Reader(Reader &&)
std::streampos seek(std::streamoff off, std::ios_base::seekdir way)
std::streamsize read(void *s, std::streamsize n)
Definition opendal.hpp:32
EntryMode
The mode of the entry.
Definition opendal.hpp:44
The entry of a file or directory.
Definition opendal.hpp:69
std::string path
Definition opendal.hpp:70
The metadata of a file or directory.
Definition opendal.hpp:54
std::optional< std::string > content_disposition
Definition opendal.hpp:58
EntryMode type
Definition opendal.hpp:55
std::optional< std::string > etag
Definition opendal.hpp:61
std::optional< std::string > cache_control
Definition opendal.hpp:57
std::optional< std::string > content_type
Definition opendal.hpp:60
std::uint64_t content_length
Definition opendal.hpp:56
std::optional< std::string > content_md5
Definition opendal.hpp:59
std::optional< boost::posix_time::ptime > last_modified
Definition opendal.hpp:62