Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RRawFile.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
12#include <ROOT/RConfig.h>
13#include <ROOT/RRawFile.hxx>
14#ifdef _WIN32
15#include <ROOT/RRawFileWin.hxx>
16#else
17#include <ROOT/RRawFileUnix.hxx>
18#endif
19
20#include "TError.h"
21#include "TPluginManager.h"
22#include "TROOT.h"
23
24#include <algorithm>
25#include <cctype> // for towlower
26#include <cerrno>
27#include <cstddef>
28#include <cstdint>
29#include <cstring>
30#include <stdexcept>
31#include <string>
32
33namespace {
34const char *kTransportSeparator = "://";
35// Corresponds to ELineBreaks
36#ifdef _WIN32
37const char *kLineBreakTokens[] = {"", "\r\n", "\n", "\r\n"};
38constexpr unsigned int kLineBreakTokenSizes[] = {0, 2, 1, 2};
39#else
40const char *kLineBreakTokens[] = {"", "\n", "\n", "\r\n"};
41constexpr unsigned int kLineBreakTokenSizes[] = {0, 1, 1, 2};
42#endif
43constexpr unsigned int kLineBuffer = 128; // On Readln, look for line-breaks in chunks of 128 bytes
44} // anonymous namespace
45
46size_t ROOT::Internal::RRawFile::RBlockBuffer::CopyTo(void *buffer, size_t nbytes, std::uint64_t offset)
47{
49 return 0;
50
51 size_t copiedBytes = 0;
52 std::uint64_t offsetInBuffer = offset - fBufferOffset;
53 if (offsetInBuffer < static_cast<std::uint64_t>(fBufferSize)) {
54 size_t bytesInBuffer = std::min(nbytes, static_cast<size_t>(fBufferSize - offsetInBuffer));
57 }
58 return copiedBytes;
59}
60
61ROOT::Internal::RRawFile::RRawFile(std::string_view url, ROptions options) : fUrl(url), fOptions(options) {}
62
63std::unique_ptr<ROOT::Internal::RRawFile>
65{
66 std::string transport = GetTransport(url);
67 if (transport == "file") {
68#ifdef _WIN32
69 return std::unique_ptr<RRawFile>(new RRawFileWin(url, options));
70#else
71 return std::unique_ptr<RRawFile>(new RRawFileUnix(url, options));
72#endif
73 }
74 if (transport == "http" || transport == "https" || transport == "root" || transport == "roots") {
75 std::string plgclass = transport.compare(0, 4, "http") == 0 ? "RRawFileDavix" : "RRawFileNetXNG";
76 if (TPluginHandler *h =
77 gROOT->GetPluginManager()->FindHandler("ROOT::Internal::RRawFile", std::string(url).c_str())) {
78 if (h->LoadPlugin() == 0) {
79 return std::unique_ptr<RRawFile>(reinterpret_cast<RRawFile *>(h->ExecPlugin(2, &url, &options)));
80 }
81 throw std::runtime_error("Cannot load plugin handler for " + plgclass);
82 }
83 throw std::runtime_error("Cannot find plugin handler for " + plgclass);
84 }
85 throw std::runtime_error("Unsupported transport protocol: " + transport);
86}
87
89{
90 if (fIsOpen)
91 return;
92
93 OpenImpl();
94 fIsOpen = true;
95}
96
98{
99 for (unsigned i = 0; i < nReq; ++i) {
100 ioVec[i].fOutBytes = ReadAt(ioVec[i].fBuffer, ioVec[i].fSize, ioVec[i].fOffset);
101 }
102}
103
104std::string ROOT::Internal::RRawFile::GetLocation(std::string_view url)
105{
106 auto idx = url.find(kTransportSeparator);
107 if (idx == std::string_view::npos)
108 return std::string(url);
109 return std::string(url.substr(idx + strlen(kTransportSeparator)));
110}
111
113{
114 if (fFileSize != kUnknownFileSize)
115 return fFileSize;
116
117 EnsureOpen();
118 fFileSize = GetSizeImpl();
119 return fFileSize;
120}
121
123 return fUrl;
124}
125
126std::string ROOT::Internal::RRawFile::GetTransport(std::string_view url)
127{
128 auto idx = url.find(kTransportSeparator);
129 if (idx == std::string_view::npos)
130 return "file";
131 std::string transport(url.substr(0, idx));
132 std::transform(transport.begin(), transport.end(), transport.begin(), ::tolower);
133 return transport;
134}
135
136size_t ROOT::Internal::RRawFile::Read(void *buffer, size_t nbytes)
137{
138 size_t res = ReadAt(buffer, nbytes, fFilePos);
139 fFilePos += res;
140 return res;
141}
142
143size_t ROOT::Internal::RRawFile::ReadAt(void *buffer, size_t nbytes, std::uint64_t offset)
144{
145 EnsureOpen();
146
147 // Early return for empty requests
148 if (nbytes == 0)
149 return 0;
150
151 // "Large" reads are served directly, bypassing the cache; since nbytes > 0, fBlockSize == 0 is also handled here
152 if (!fIsBuffering || nbytes > static_cast<unsigned int>(fOptions.fBlockSize))
153 return ReadAtImpl(buffer, nbytes, offset);
154
155 if (!fBufferSpace) {
156 fBufferSpace.reset(new unsigned char[kNumBlockBuffers * fOptions.fBlockSize]);
157 for (unsigned int i = 0; i < kNumBlockBuffers; ++i) {
158 fBlockBuffers[i].fBuffer = fBufferSpace.get() + i * fOptions.fBlockSize;
159 fBlockBuffers[i].fBufferSize = 0;
160 }
161 }
162
163 size_t totalBytes = 0;
164 size_t copiedBytes = 0;
165 /// Try to serve as many bytes as possible from the block buffers
166 for (unsigned int idx = fBlockBufferIdx; idx < fBlockBufferIdx + kNumBlockBuffers; ++idx) {
167 copiedBytes = fBlockBuffers[idx % kNumBlockBuffers].CopyTo(buffer, nbytes, offset);
168 buffer = reinterpret_cast<unsigned char *>(buffer) + copiedBytes;
172 if (copiedBytes > 0)
173 fBlockBufferIdx = idx;
174 if (nbytes == 0)
175 return totalBytes;
176 }
177 fBlockBufferIdx++;
178
179 /// The request was not fully satisfied and fBlockBufferIdx now points to the previous shadow buffer
180
181 /// The remaining bytes populate the newly promoted main buffer
182 RBlockBuffer *thisBuffer = &fBlockBuffers[fBlockBufferIdx % kNumBlockBuffers];
183 size_t res = ReadAtImpl(thisBuffer->fBuffer, fOptions.fBlockSize, offset);
184 thisBuffer->fBufferOffset = offset;
185 thisBuffer->fBufferSize = res;
186 size_t remainingBytes = std::min(res, nbytes);
187 memcpy(buffer, thisBuffer->fBuffer, remainingBytes);
189 return totalBytes;
190}
191
193{
194 EnsureOpen();
195 ReadVImpl(ioVec, nReq);
196}
197
199{
200 fIsBuffering = value;
201 if (!fIsBuffering)
202 fBufferSpace.reset();
203}
204
206{
207 if (fOptions.fLineBreak == ELineBreaks::kAuto) {
208 // Auto-detect line breaks according to the break discovered in the first line
209 fOptions.fLineBreak = ELineBreaks::kUnix;
210 bool res = Readln(line);
211 if ((line.length() > 0) && (*line.rbegin() == '\r')) {
212 fOptions.fLineBreak = ELineBreaks::kWindows;
213 line.resize(line.length() - 1);
214 }
215 return res;
216 }
217
218 line.clear();
219 char buffer[kLineBuffer];
220 size_t nbytes;
221 do {
222 nbytes = Read(buffer, sizeof(buffer));
223 std::string_view bufferView(buffer, nbytes);
224 auto idx = bufferView.find(kLineBreakTokens[static_cast<int>(fOptions.fLineBreak)]);
225 if (idx != std::string_view::npos) {
226 // Line break found, return the string and skip the linebreak itself
227 line.append(buffer, idx);
228 fFilePos -= nbytes - idx;
229 fFilePos += kLineBreakTokenSizes[static_cast<int>(fOptions.fLineBreak)];
230 return true;
231 }
232 line.append(buffer, nbytes);
233 } while (nbytes > 0);
234
235 return !line.empty();
236}
237
239{
240 fFilePos = offset;
241}
fBuffer
dim_t fSize
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
#define gROOT
Definition TROOT.h:414
The RRawFileUnix class uses POSIX calls to read from a mounted file system.
The RRawFileWin class uses portable C I/O calls to read from a drive.
The RRawFile provides read-only access to local and remote files.
Definition RRawFile.hxx:43
static std::string GetLocation(std::string_view url)
Returns only the file location, e.g. "server/file" for http://server/file.
Definition RRawFile.cxx:104
RRawFile(std::string_view url, ROptions options)
Definition RRawFile.cxx:61
virtual void ReadVImpl(RIOVec *ioVec, unsigned int nReq)
By default implemented as a loop of ReadAt calls but can be overwritten, e.g. XRootD or DAVIX impleme...
Definition RRawFile.cxx:97
static std::string GetTransport(std::string_view url)
Returns only the transport protocol in lower case, e.g. "http" for HTTP://server/file.
Definition RRawFile.cxx:126
std::uint64_t GetSize()
Returns the size of the file.
Definition RRawFile.cxx:112
static std::unique_ptr< RRawFile > Create(std::string_view url, ROptions options=ROptions())
Factory method that returns a suitable concrete implementation according to the transport in the url.
Definition RRawFile.cxx:64
void Seek(std::uint64_t offset)
Change the cursor fFilePos.
Definition RRawFile.cxx:238
size_t ReadAt(void *buffer, size_t nbytes, std::uint64_t offset)
Buffered read from a random position.
Definition RRawFile.cxx:143
bool Readln(std::string &line)
Read the next line starting from the current value of fFilePos. Returns false if the end of the file ...
Definition RRawFile.cxx:205
void EnsureOpen()
Open the file if not already open. Otherwise noop.
Definition RRawFile.cxx:88
void ReadV(RIOVec *ioVec, unsigned int nReq)
Opens the file if necessary and calls ReadVImpl.
Definition RRawFile.cxx:192
size_t Read(void *buffer, size_t nbytes)
Read from fFilePos offset. Returns the actual number of bytes read.
Definition RRawFile.cxx:136
void SetBuffering(bool value)
Turn off buffered reads; all scalar read requests go directly to the implementation.
Definition RRawFile.cxx:198
std::string GetUrl() const
Returns the url of the file.
Definition RRawFile.cxx:122
const_iterator begin() const
const_iterator end() const
TLine * line
std::uint64_t fBufferOffset
Where in the open file does fBuffer start.
Definition RRawFile.hxx:95
unsigned char * fBuffer
Points into the I/O buffer with data from the file, not owned.
Definition RRawFile.hxx:99
size_t CopyTo(void *buffer, size_t nbytes, std::uint64_t offset)
Tries to copy up to nbytes starting at offset from fBuffer into buffer. Returns number of bytes copie...
Definition RRawFile.cxx:46
size_t fBufferSize
The number of currently buffered bytes in fBuffer.
Definition RRawFile.hxx:97
Used for vector reads from multiple offsets into multiple buffers.
Definition RRawFile.hxx:61
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition RRawFile.hxx:49