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 <cstring>
24#include <iostream>
25#include <memory>
26#include <optional>
27#include <string>
28#include <unordered_map>
29#include <vector>
30
31#include "data_structure.hpp"
32#include "layer.hpp"
33
34namespace opendal {
35
36namespace ffi {
37class Operator;
38class Reader;
39class Lister;
40class Writer;
41} // namespace ffi
42
43class Reader;
44class Lister;
45class Writer;
46
51class Operator {
52 public:
53 Operator() noexcept;
54
62 Operator(std::string_view scheme,
63 const std::unordered_map<std::string, std::string> &config = {},
64 std::vector<std::unique_ptr<OperatorOption>> options = {});
65
66 // Disable copy and assign
67 Operator(const Operator &) = delete;
68 Operator &operator=(const Operator &) = delete;
69
70 // Enable move
71 Operator(Operator &&other) noexcept;
72 Operator &operator=(Operator &&other) noexcept;
73
74 ~Operator() noexcept;
75
81 bool Available() const;
82
91 std::string Read(std::string_view path);
92
100 std::string Read(std::string_view path, const ReadOptions &options);
101
108 void Write(std::string_view path, std::string_view data);
109
117 void Write(std::string_view path, std::string_view data,
118 const WriteOptions &options);
119
126 Reader GetReader(std::string_view path);
127 Reader GetReader(std::string_view path, const ReaderOptions &options);
128 Writer GetWriter(std::string_view path);
129 Writer GetWriter(std::string_view path, const WriteOptions &options);
130
137 [[deprecated("Use Exists() instead.")]]
138 bool IsExist(std::string_view path);
139
146 bool Exists(std::string_view path);
147
153 void CreateDir(std::string_view path);
154
161 void Copy(std::string_view src, std::string_view dst);
162 void Copy(std::string_view src, std::string_view dst,
163 const CopyOptions &options);
164
171 void Rename(std::string_view src, std::string_view dst);
172 void Rename(std::string_view src, std::string_view dst,
173 const RenameOptions &options);
174
180 void Remove(std::string_view path);
181 void Remove(std::string_view path, const DeleteOptions &options);
182 void RemoveAll(const std::vector<std::string> &paths);
183
190 Metadata Stat(std::string_view path);
191 Metadata Stat(std::string_view path, const StatOptions &options);
192
200 std::vector<Entry> List(std::string_view path);
201 std::vector<Entry> List(std::string_view path, const ListOptions &options);
202
203 Lister GetLister(std::string_view path);
204 Lister GetLister(std::string_view path, const ListOptions &options);
206
207 private:
208 void Destroy() noexcept;
209
210 ffi::Operator *operator_{nullptr};
211 };
212
222class Reader {
223 public:
224 Reader(Reader &&other) noexcept;
225
226 ~Reader() noexcept;
227
228 std::streamsize Read(void *s, std::streamsize n);
229
230 std::streamsize ReadAt(void *s, std::streamsize n, uint64_t offset);
231
232 std::streampos Seek(std::streamoff off, std::ios_base::seekdir way);
233
234 private:
235 friend class Operator;
236
237 Reader(ffi::Reader *pointer) noexcept;
238
239 void Destroy() noexcept;
240
241 ffi::Reader *reader_{nullptr};
242};
243
251class Writer {
252 public:
253 Writer(Writer &&other) noexcept;
254
255 ~Writer() noexcept;
256
257 void Write(std::string_view data);
258
259 void Flush();
260
261 void Close();
262
263 private:
264 friend class Operator;
265
266 Writer(ffi::Writer *pointer) noexcept;
267
268 void Destroy() noexcept;
269
270 ffi::Writer *writer_{nullptr};
271};
272
279class ReaderStream : public std::istream {
280 public:
281 class ReaderStreamBuf : public std::streambuf {
282 public:
284 : reader_(std::move(reader)), buffer_start_pos_(0) {
285 setg(buffer_, buffer_, buffer_);
286 }
287
288 protected:
289 std::streamsize xsgetn(char *s, std::streamsize count) override {
290 std::streamsize total_read = 0;
291
292 while (total_read < count) {
293 if (gptr() < egptr()) {
294 std::streamsize available_bytes = egptr() - gptr();
295 std::streamsize to_copy =
296 std::min(available_bytes, count - total_read);
297 std::memcpy(s + total_read, gptr(), to_copy);
298 gbump(to_copy);
299 total_read += to_copy;
300 continue;
301 }
302
303 if (underflow() == traits_type::eof()) break;
304 }
305
306 return total_read;
307 }
308
309 int_type underflow() override {
310 if (gptr() < egptr()) {
311 return traits_type::to_int_type(*gptr());
312 }
313
314 // Update the buffer start position to current reader position
315 buffer_start_pos_ += static_cast<std::streamoff>(egptr() - eback());
316
317 std::streamsize n = reader_.Read(buffer_, sizeof(buffer_));
318 if (n <= 0) {
319 return traits_type::eof();
320 }
321
322 setg(buffer_, buffer_, buffer_ + n);
323 return traits_type::to_int_type(*gptr());
324 }
325
326 int_type uflow() override {
327 int_type result = underflow();
328 if (result != traits_type::eof()) {
329 gbump(1);
330 }
331 return result;
332 }
333
334 std::streampos seekoff(std::streamoff off, std::ios_base::seekdir dir,
335 std::ios_base::openmode which) override {
336 if (dir == std::ios_base::cur && off == 0) {
337 // tellg() case - return current position
338 return buffer_start_pos_ + static_cast<std::streamoff>(gptr() - eback());
339 }
340
341 // Actual seek operation
342 std::streampos new_pos = reader_.Seek(off, dir);
343 if (new_pos != std::streampos(-1)) {
344 buffer_start_pos_ = new_pos;
345 setg(buffer_, buffer_, buffer_);
346 }
347 return new_pos;
348 }
349
350 std::streampos seekpos(std::streampos pos,
351 std::ios_base::openmode which) override {
352 return seekoff(pos, std::ios_base::beg, which);
353 }
354
355 private:
356 Reader reader_;
357 char buffer_[8192];
358 std::streampos buffer_start_pos_;
359 };
360
362 : std::istream(&buf_), buf_(std::move(reader)) {}
363
364 private:
365 ReaderStreamBuf buf_;
366};
367
380class Lister {
381 public:
382 Lister(Lister &&other) noexcept;
383
384 ~Lister() noexcept;
385
392 class Iterator {
393 public:
394 using iterator_category = std::input_iterator_tag;
396 using difference_type = std::ptrdiff_t;
397 using pointer = Entry *;
398 using reference = Entry &;
399
400 Iterator(Lister &lister) : lister_{lister} {
401 current_entry_ = lister_.Next();
402 }
403
404 Entry operator*() { return current_entry_.value(); }
405
407 if (current_entry_) {
408 current_entry_ = lister_.Next();
409 }
410 return *this;
411 }
412
413 bool operator!=(const Iterator &other) const {
414 return current_entry_ != std::nullopt ||
415 other.current_entry_ != std::nullopt;
416 }
417
418 protected:
419 // Only used for end iterator
420 Iterator(Lister &lister, bool /*end*/) noexcept : lister_(lister) {}
421
422 private:
423 friend class Lister;
424
425 Lister &lister_;
426 std::optional<Entry> current_entry_;
427 };
428
434 std::optional<Entry> Next();
435
436 Iterator begin() { return Iterator(*this); }
437 Iterator end() { return Iterator(*this, true); }
438
439 private:
440 friend class Operator;
441
442 Lister(ffi::Lister *pointer) noexcept;
443
444 void Destroy() noexcept;
445
446 ffi::Lister *lister_{nullptr};
447};
448
449} // namespace opendal
Definition data_structure.hpp:340
The entry of a file or directory.
Definition data_structure.hpp:190
Definition opendal.hpp:392
std::ptrdiff_t difference_type
Definition opendal.hpp:396
Entry operator*()
Definition opendal.hpp:404
Iterator(Lister &lister)
Definition opendal.hpp:400
std::input_iterator_tag iterator_category
Definition opendal.hpp:394
Iterator(Lister &lister, bool) noexcept
Definition opendal.hpp:420
Iterator & operator++()
Definition opendal.hpp:406
bool operator!=(const Iterator &other) const
Definition opendal.hpp:413
Lister is designed to list the entries of a directory.
Definition opendal.hpp:380
std::optional< Entry > Next()
Get the next entry of the lister.
~Lister() noexcept
Lister(Lister &&other) noexcept
Iterator begin()
Definition opendal.hpp:436
Iterator end()
Definition opendal.hpp:437
The metadata of a file or directory.
Definition data_structure.hpp:51
Operator is the entry for all public APIs.
Definition opendal.hpp:51
void RemoveAll(const std::vector< std::string > &paths)
void Remove(std::string_view path)
Remove a file or directory.
Reader GetReader(std::string_view path)
Read data from the operator.
Operator() noexcept
Capability Info()
void Rename(std::string_view src, std::string_view dst)
Rename a file from src to dst.
Operator(Operator &&other) noexcept
bool Exists(std::string_view path)
Check if the path exists.
bool Available() const
Check if the operator is available.
~Operator() noexcept
std::string Read(std::string_view path)
Read data from the operator.
Operator(const Operator &)=delete
bool IsExist(std::string_view path)
Check if the path exists.
Operator & operator=(const Operator &)=delete
Lister GetLister(std::string_view path)
void CreateDir(std::string_view path)
Create a directory.
Operator & operator=(Operator &&other) noexcept
void Copy(std::string_view src, std::string_view dst)
Copy a file from src to dst.
Writer GetWriter(std::string_view path)
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.
Metadata Stat(std::string_view path)
Get the metadata of a file or directory.
Definition opendal.hpp:281
int_type uflow() override
Definition opendal.hpp:326
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir dir, std::ios_base::openmode which) override
Definition opendal.hpp:334
std::streamsize xsgetn(char *s, std::streamsize count) override
Definition opendal.hpp:289
int_type underflow() override
Definition opendal.hpp:309
std::streampos seekpos(std::streampos pos, std::ios_base::openmode which) override
Definition opendal.hpp:350
ReaderStreamBuf(Reader &&reader)
Definition opendal.hpp:283
ReaderStream is a stream wrapper of Reader which provides iostream interface. It wraps the Reader to ...
Definition opendal.hpp:279
ReaderStream(Reader &&reader)
Definition opendal.hpp:361
Reader is designed to read data from the operator.
Definition opendal.hpp:222
Reader(Reader &&other) noexcept
std::streamsize ReadAt(void *s, std::streamsize n, uint64_t offset)
std::streamsize Read(void *s, std::streamsize n)
std::streampos Seek(std::streamoff off, std::ios_base::seekdir way)
~Reader() noexcept
Writer is designed to write a sequence of byte buffers to an object.
Definition opendal.hpp:251
~Writer() noexcept
void Write(std::string_view data)
Writer(Writer &&other) noexcept
Definition data_structure.hpp:29
Options for copy operations.
Definition data_structure.hpp:300
Options for delete operations.
Definition data_structure.hpp:321
Options for list and lister operations.
Definition data_structure.hpp:330
Options for read operations.
Definition data_structure.hpp:245
Options for creating readers.
Definition data_structure.hpp:265
Options for rename operations.
Definition data_structure.hpp:313
Options for stat operations.
Definition data_structure.hpp:230
Options for write and writer operations.
Definition data_structure.hpp:282