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 <optional>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27#include "boost/iostreams/concepts.hpp"
28#include "boost/iostreams/stream.hpp"
29#include "data_structure.hpp"
30
31namespace opendal {
32
33namespace ffi {
34class Operator;
35class Reader;
36class Lister;
37} // namespace ffi
38
39class Reader;
40class Lister;
41
46class Operator {
47 public:
48 Operator() noexcept;
49
56 Operator(std::string_view scheme,
57 const std::unordered_map<std::string, std::string> &config = {});
58
59 // Disable copy and assign
60 Operator(const Operator &) = delete;
61 Operator &operator=(const Operator &) = delete;
62
63 // Enable move
64 Operator(Operator &&other) noexcept;
65 Operator &operator=(Operator &&other) noexcept;
66
67 ~Operator() noexcept;
68
74 bool available() const;
75
84 std::string read(std::string_view path);
85
92 void write(std::string_view path, std::string_view data);
93
100 Reader reader(std::string_view path);
101
108 [[deprecated("Use exists() instead.")]]
109 bool is_exist(std::string_view path);
110
117 bool exists(std::string_view path);
118
124 void create_dir(std::string_view path);
125
132 void copy(std::string_view src, std::string_view dst);
133
140 void rename(std::string_view src, std::string_view dst);
141
147 void remove(std::string_view path);
148
155 Metadata stat(std::string_view path);
156
164 std::vector<Entry> list(std::string_view path);
165
166 Lister lister(std::string_view path);
167
168 private:
169 void destroy() noexcept;
170
171 ffi::Operator *operator_{nullptr};
172};
173
184 : public boost::iostreams::device<boost::iostreams::input_seekable> {
185 public:
186 Reader(Reader &&other) noexcept;
187
188 ~Reader() noexcept;
189
190 std::streamsize read(void *s, std::streamsize n);
191
192 std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
193
194 private:
195 friend class Operator;
196
197 Reader(ffi::Reader *pointer) noexcept;
198
199 void destroy() noexcept;
200
201 ffi::Reader *reader_{nullptr};
202};
203
204// Boost IOStreams requires it to be copyable. So we need to use
205// `reference_wrapper` in ReaderStream. More details can be seen at
206// https://lists.boost.org/Archives/boost/2005/10/95939.php
207
215 : public boost::iostreams::stream<boost::reference_wrapper<Reader>> {
216 public:
218 : boost::iostreams::stream<boost::reference_wrapper<Reader>>(
219 boost::ref(reader_)),
220 reader_(std::move(reader)) {}
221
222 private:
223 Reader reader_;
224};
225
238class Lister {
239 public:
240 Lister(Lister &&other) noexcept;
241
242 ~Lister() noexcept;
243
250 class Iterator {
251 public:
252 using iterator_category = std::input_iterator_tag;
254 using difference_type = std::ptrdiff_t;
255 using pointer = Entry *;
256 using reference = Entry &;
257
258 Iterator(Lister &lister) : lister_{lister} {
259 current_entry_ = lister_.next();
260 }
261
262 Entry operator*() { return current_entry_.value(); }
263
265 if (current_entry_) {
266 current_entry_ = lister_.next();
267 }
268 return *this;
269 }
270
271 bool operator!=(const Iterator &other) const {
272 return current_entry_ != std::nullopt ||
273 other.current_entry_ != std::nullopt;
274 }
275
276 protected:
277 // Only used for end iterator
278 Iterator(Lister &lister, bool /*end*/) noexcept : lister_(lister) {}
279
280 private:
281 friend class Lister;
282
283 Lister &lister_;
284 std::optional<Entry> current_entry_;
285 };
286
292 std::optional<Entry> next();
293
294 Iterator begin() { return Iterator(*this); }
295 Iterator end() { return Iterator(*this, true); }
296
297 private:
298 friend class Operator;
299
300 Lister(ffi::Lister *pointer) noexcept;
301
302 void destroy() noexcept;
303
304 ffi::Lister *lister_{nullptr};
305};
306
307} // namespace opendal
The entry of a file or directory.
Definition data_structure.hpp:60
Definition opendal.hpp:250
std::ptrdiff_t difference_type
Definition opendal.hpp:254
Entry operator*()
Definition opendal.hpp:262
Iterator(Lister &lister)
Definition opendal.hpp:258
std::input_iterator_tag iterator_category
Definition opendal.hpp:252
Iterator(Lister &lister, bool) noexcept
Definition opendal.hpp:278
Iterator & operator++()
Definition opendal.hpp:264
bool operator!=(const Iterator &other) const
Definition opendal.hpp:271
Lister is designed to list the entries of a directory.
Definition opendal.hpp:238
std::optional< Entry > next()
Get the next entry of the lister.
~Lister() noexcept
Lister(Lister &&other) noexcept
Iterator begin()
Definition opendal.hpp:294
Iterator end()
Definition opendal.hpp:295
The metadata of a file or directory.
Definition data_structure.hpp:44
Operator is the entry for all public APIs.
Definition opendal.hpp:46
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.
Operator() noexcept
std::vector< Entry > list(std::string_view path)
List the entries of a directory.
Operator(Operator &&other) noexcept
~Operator() noexcept
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=(const Operator &)=delete
bool exists(std::string_view path)
Check if the path exists.
Operator & operator=(Operator &&other) noexcept
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:215
ReaderStream(Reader &&reader)
Definition opendal.hpp:217
Reader is designed to read data from the operator.
Definition opendal.hpp:184
Reader(Reader &&other) noexcept
std::streampos seek(std::streamoff off, std::ios_base::seekdir way)
std::streamsize read(void *s, std::streamsize n)
~Reader() noexcept
Definition data_structure.hpp:28