Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_file][improve]do some improvement #510

Merged
merged 9 commits into from
Dec 1, 2023

Conversation

qicosmos
Copy link
Collaborator

@qicosmos qicosmos commented Nov 30, 2023

Why

allow use fread and pread at the same time. support random read for uring.

open file with read_type:

  • read_type::uring : io_uring stream file;
  • read_type::uring_random : io_uring random file;
  • read_type::fread : FILE;
  • read_type::pread : fd;
enum class read_type {
#if defined(YLT_ENABLE_FILE_IO_URING)
  uring,
  uring_random,
#else
  fread,
#endif
#if defined(__GNUC__)
  pread,
#endif
};

pread

{
    coro_io::coro_file file{};
    co_await file.async_open(
            filename.data(), coro_io::flags::read_only, coro_io::read_type::pread);
    
    char buf[100];
    co_await file.async_pread(10, buf, 100);
}

{
    coro_io::coro_file file{};
    co_await file.async_open(
            filename.data(), coro_io::flags::read_write, coro_io::read_type::pread);
    
    std::string buf = "cccccccccc";
    co_await file.async_pwrite(10, buf.data(), buf.size());
}

uring random read

{
  coro_io::coro_file file{};
  co_await file.async_open(filename.data(), coro_io::flags::read_only,
                        coro_io::read_type::uring_random);

  char buf[100];
  file.async_read_some_at(10, buf, 100);
}

{
  coro_io::coro_file file{};
  co_await file.async_open(filename.data(), coro_io::flags::read_write,
                        coro_io::read_type::uring_random);

  std::string buf = "cccccccccc";
  file.async_write_some_at(10, buf.data(), buf.size());
}

uring read

{
  coro_io::coro_file file{};
  co_await file.async_open(filename.data(), coro_io::flags::read_only,
                        coro_io::read_type::uring);

  char buf[100];
  file.async_read(buf, 100);
}

{
  coro_io::coro_file file{};
  co_await file.async_open(filename.data(), coro_io::flags::read_write,
                        coro_io::read_type::uring);

  std::string buf = "cccccccccc";
  file.async_write(buf.data(), buf.size());
}

fread

{
  coro_io::coro_file file{};
  co_await file.async_open(filename.data(), coro_io::flags::read_only,
                        coro_io::read_type::fread);

  char buf[100];
  file.async_read(buf, 100);
}

{
  coro_io::coro_file file{};
  co_await file.async_open(filename.data(), coro_io::flags::read_write,
                        coro_io::read_type::fread);

  std::string buf = "cccccccccc";
  file.async_write(buf.data(), buf.size());
}

What is changing

Example

@qicosmos qicosmos merged commit 95c4f33 into alibaba:main Dec 1, 2023
30 checks passed
@qicosmos qicosmos deleted the improve_coro_file branch December 1, 2023 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant