mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-08 03:49:44 -04:00
Add asynchronous readv
method for filesystem_v2
This commit is contained in:
parent
5c13287c1a
commit
12b949525d
@ -24,6 +24,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@ -47,6 +48,7 @@ struct filesystem_options;
|
||||
struct rewrite_options;
|
||||
struct iovec_read_buf;
|
||||
|
||||
class block_range;
|
||||
class filesystem_writer;
|
||||
class logger;
|
||||
class mmif;
|
||||
@ -146,6 +148,11 @@ class filesystem_v2 {
|
||||
return impl_->readv(inode, buf, size, offset);
|
||||
}
|
||||
|
||||
folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
readv(uint32_t inode, size_t size, off_t offset) const {
|
||||
return impl_->readv(inode, size, offset);
|
||||
}
|
||||
|
||||
class impl {
|
||||
public:
|
||||
virtual ~impl() = default;
|
||||
@ -180,6 +187,8 @@ class filesystem_v2 {
|
||||
read(uint32_t inode, char* buf, size_t size, off_t offset) const = 0;
|
||||
virtual ssize_t readv(uint32_t inode, iovec_read_buf& buf, size_t size,
|
||||
off_t offset) const = 0;
|
||||
virtual folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
readv(uint32_t inode, size_t size, off_t offset) const = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
|
@ -28,6 +28,8 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <folly/Expected.h>
|
||||
|
||||
#include "dwarfs/metadata_types.h"
|
||||
|
||||
namespace dwarfs {
|
||||
@ -53,6 +55,11 @@ class inode_reader_v2 {
|
||||
return impl_->readv(buf, size, offset, chunks);
|
||||
}
|
||||
|
||||
folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
readv(size_t size, off_t offset, chunk_range chunks) const {
|
||||
return impl_->readv(size, offset, chunks);
|
||||
}
|
||||
|
||||
void
|
||||
dump(std::ostream& os, const std::string& indent, chunk_range chunks) const {
|
||||
impl_->dump(os, indent, chunks);
|
||||
@ -66,6 +73,8 @@ class inode_reader_v2 {
|
||||
read(char* buf, size_t size, off_t offset, chunk_range chunks) const = 0;
|
||||
virtual ssize_t readv(iovec_read_buf& buf, size_t size, off_t offset,
|
||||
chunk_range chunks) const = 0;
|
||||
virtual folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
readv(size_t size, off_t offset, chunk_range chunks) const = 0;
|
||||
virtual void dump(std::ostream& os, const std::string& indent,
|
||||
chunk_range chunks) const = 0;
|
||||
};
|
||||
|
@ -21,7 +21,6 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
@ -209,6 +208,8 @@ class filesystem_ : public filesystem_v2::impl {
|
||||
read(uint32_t inode, char* buf, size_t size, off_t offset) const override;
|
||||
ssize_t readv(uint32_t inode, iovec_read_buf& buf, size_t size,
|
||||
off_t offset) const override;
|
||||
folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
readv(uint32_t inode, size_t size, off_t offset) const override;
|
||||
|
||||
private:
|
||||
LOG_PROXY_DECL(LoggerPolicy);
|
||||
@ -382,7 +383,7 @@ ssize_t filesystem_<LoggerPolicy>::read(uint32_t inode, char* buf, size_t size,
|
||||
if (auto chunks = meta_.get_chunks(inode)) {
|
||||
return ir_.read(buf, size, offset, *chunks);
|
||||
}
|
||||
return -1;
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
@ -391,7 +392,17 @@ ssize_t filesystem_<LoggerPolicy>::readv(uint32_t inode, iovec_read_buf& buf,
|
||||
if (auto chunks = meta_.get_chunks(inode)) {
|
||||
return ir_.readv(buf, size, offset, *chunks);
|
||||
}
|
||||
return -1;
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
filesystem_<LoggerPolicy>::readv(uint32_t inode, size_t size,
|
||||
off_t offset) const {
|
||||
if (auto chunks = meta_.get_chunks(inode)) {
|
||||
return ir_.readv(size, offset, *chunks);
|
||||
}
|
||||
return folly::makeUnexpected(-EBADF);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -64,10 +64,15 @@ class inode_reader_ : public inode_reader_v2::impl {
|
||||
read(char* buf, size_t size, off_t offset, chunk_range chunks) const override;
|
||||
ssize_t readv(iovec_read_buf& buf, size_t size, off_t offset,
|
||||
chunk_range chunks) const override;
|
||||
folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
readv(size_t size, off_t offset, chunk_range chunks) const override;
|
||||
void dump(std::ostream& os, const std::string& indent,
|
||||
chunk_range chunks) const override;
|
||||
|
||||
private:
|
||||
folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
read(size_t size, off_t offset, chunk_range chunks) const;
|
||||
|
||||
template <typename StoreFunc>
|
||||
ssize_t read(size_t size, off_t offset, chunk_range chunks,
|
||||
const StoreFunc& store) const;
|
||||
@ -89,16 +94,18 @@ void inode_reader_<LoggerPolicy>::dump(std::ostream& os,
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
template <typename StoreFunc>
|
||||
ssize_t
|
||||
inode_reader_<LoggerPolicy>::read(size_t size, off_t offset, chunk_range chunks,
|
||||
const StoreFunc& store) const {
|
||||
folly::Expected<std::vector<std::future<block_range>>, int>
|
||||
inode_reader_<LoggerPolicy>::readv(size_t size, off_t offset,
|
||||
chunk_range chunks) const {
|
||||
if (offset < 0) {
|
||||
return -EINVAL;
|
||||
return folly::makeUnexpected(-EINVAL);
|
||||
}
|
||||
|
||||
// request ranges from block cache
|
||||
std::vector<std::future<block_range>> ranges;
|
||||
|
||||
if (size == 0 || chunks.empty()) {
|
||||
return 0;
|
||||
return ranges;
|
||||
}
|
||||
|
||||
auto it = chunks.begin();
|
||||
@ -118,19 +125,16 @@ inode_reader_<LoggerPolicy>::read(size_t size, off_t offset, chunk_range chunks,
|
||||
|
||||
if (it == end) {
|
||||
// offset beyond EOF; TODO: check if this should rather be -EINVAL
|
||||
return 0;
|
||||
return ranges;
|
||||
}
|
||||
|
||||
// request ranges from block cache
|
||||
std::vector<std::future<block_range>> ranges;
|
||||
|
||||
for (size_t num_read = 0; it != end && num_read < size; ++it) {
|
||||
size_t chunksize = it->size() - offset;
|
||||
size_t chunkoff = it->offset() + offset;
|
||||
|
||||
if (chunksize == 0) {
|
||||
LOG_ERROR << "invalid zero-sized chunk";
|
||||
return -EIO;
|
||||
return folly::makeUnexpected(-EIO);
|
||||
}
|
||||
|
||||
if (num_read + chunksize > size) {
|
||||
@ -143,10 +147,24 @@ inode_reader_<LoggerPolicy>::read(size_t size, off_t offset, chunk_range chunks,
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
return ranges;
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
template <typename StoreFunc>
|
||||
ssize_t
|
||||
inode_reader_<LoggerPolicy>::read(size_t size, off_t offset, chunk_range chunks,
|
||||
const StoreFunc& store) const {
|
||||
auto ranges = readv(size, offset, chunks);
|
||||
|
||||
if (!ranges) {
|
||||
return ranges.error();
|
||||
}
|
||||
|
||||
try {
|
||||
// now fill the buffer
|
||||
size_t num_read = 0;
|
||||
for (auto& r : ranges) {
|
||||
for (auto& r : ranges.value()) {
|
||||
auto br = r.get();
|
||||
store(num_read, br);
|
||||
num_read += br.size();
|
||||
|
Loading…
x
Reference in New Issue
Block a user