Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RRawFileDavix.cxx
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Jakob Blomer
3
4/*************************************************************************
5 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
13#include "ROOT/RMakeUnique.hxx"
14
15#include <TError.h>
16
17#include <stdexcept>
18
19#include <davix.hpp>
20#include <sys/stat.h>
21
22namespace {
23constexpr int kDefaultBlockSize = 128 * 1024; // Read in relatively large 128k blocks for better network utilization
24} // anonymous namespace
25
26namespace ROOT {
27namespace Internal {
28
29struct RDavixFileDes {
30 RDavixFileDes() : fd(nullptr), pos(&ctx) {}
31 RDavixFileDes(const RDavixFileDes &) = delete;
32 RDavixFileDes &operator=(const RDavixFileDes &) = delete;
33 ~RDavixFileDes() = default;
34
35 DAVIX_FD *fd;
36 Davix::Context ctx;
37 Davix::DavPosix pos;
38};
39
40} // namespace Internal
41} // namespace ROOT
42
43
45 : RRawFile(url, options), fFileDes(new RDavixFileDes())
46{
47}
48
50{
51 if (fFileDes->fd != nullptr)
52 fFileDes->pos.close(fFileDes->fd, nullptr);
53}
54
55std::unique_ptr<ROOT::Internal::RRawFile> ROOT::Internal::RRawFileDavix::Clone() const
56{
57 return std::make_unique<RRawFileDavix>(fUrl, fOptions);
58}
59
61{
62 struct stat buf;
63 Davix::DavixError *err = nullptr;
64 if (fFileDes->pos.stat(nullptr, fUrl, &buf, &err) == -1) {
65 throw std::runtime_error("Cannot determine size of '" + fUrl + "', error: " + err->getErrMsg());
66 }
67 return buf.st_size;
68}
69
71{
72 Davix::DavixError *err = nullptr;
73 fFileDes->fd = fFileDes->pos.open(nullptr, fUrl, O_RDONLY, &err);
74 if (fFileDes->fd == nullptr) {
75 throw std::runtime_error("Cannot open '" + fUrl + "', error: " + err->getErrMsg());
76 }
77 if (fOptions.fBlockSize < 0)
78 fOptions.fBlockSize = kDefaultBlockSize;
79}
80
81size_t ROOT::Internal::RRawFileDavix::ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset)
82{
83 Davix::DavixError *err = nullptr;
84 auto retval = fFileDes->pos.pread(fFileDes->fd, buffer, nbytes, offset, &err);
85 if (retval < 0) {
86 throw std::runtime_error("Cannot read from '" + fUrl + "', error: " + err->getErrMsg());
87 }
88 return static_cast<size_t>(retval);
89}
90
91void ROOT::Internal::RRawFileDavix::ReadVImpl(RIOVec *ioVec, unsigned int nReq)
92{
93 Davix::DavixError *davixErr = NULL;
94 Davix::DavIOVecInput in[nReq];
95 Davix::DavIOVecOuput out[nReq];
96
97 for (unsigned int i = 0; i < nReq; ++i) {
98 in[i].diov_buffer = ioVec[i].fBuffer;
99 in[i].diov_offset = ioVec[i].fOffset;
100 in[i].diov_size = ioVec[i].fSize;
101 R__ASSERT(ioVec[i].fSize > 0);
102 }
103
104 auto ret = fFileDes->pos.preadVec(fFileDes->fd, in, out, nReq, &davixErr);
105 if (ret < 0) {
106 throw std::runtime_error("Cannot do vector read from '" + fUrl + "', error: " + davixErr->getErrMsg());
107 }
108
109 for (unsigned int i = 0; i < nReq; ++i) {
110 ioVec[i].fOutBytes = out[i].diov_size;
111 }
112}
size_t fSize
#define R__ASSERT(e)
Definition TError.h:120
Binding & operator=(OUT(*fun)(void))
void ReadVImpl(RIOVec *ioVec, unsigned int nReq) final
By default implemented as a loop of ReadAt calls but can be overwritten, e.g. XRootD or DAVIX impleme...
size_t ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset) final
Derived classes should implement low-level reading without buffering.
std::unique_ptr< RRawFile > Clone() const final
Create a new RawFile that accesses the same resource. The file pointer is reset to zero.
RRawFileDavix(std::string_view url, RRawFile::ROptions options)
std::uint64_t GetSizeImpl() final
Derived classes should return the file size or kUnknownFileSize.
void OpenImpl() final
OpenImpl() is called at most once and before any call to either DoReadAt or DoGetSize.
The RRawFile provides read-only access to local and remote files.
Definition RRawFile.hxx:43
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Used for vector reads from multiple offsets into multiple buffers.
Definition RRawFile.hxx:71
std::size_t fOutBytes
The number of actually read bytes, set by ReadV()
Definition RRawFile.hxx:79
std::size_t fSize
The number of desired bytes.
Definition RRawFile.hxx:77
void * fBuffer
The destination for reading.
Definition RRawFile.hxx:73
std::uint64_t fOffset
The file offset.
Definition RRawFile.hxx:75
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition RRawFile.hxx:59