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));
55 memcpy(buffer, fBuffer + offsetInBuffer, bytesInBuffer);
56 copiedBytes = bytesInBuffer;
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>
64ROOT::Internal::RRawFile::Create(std::string_view url, ROptions options)
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" ||
75 transport == "root" || transport == "roots" ) {
76 std::string plgclass = transport.compare( 0, 4, "http" ) == 0 ?
77 "RRawFileDavix" : "RRawFileNetXNG";
78 if (TPluginHandler *h = gROOT->GetPluginManager()->
79 FindHandler("ROOT::Internal::RRawFile", std::string(url).c_str())) {
80 if (h->LoadPlugin() == 0) {
81 return std::unique_ptr<RRawFile>(reinterpret_cast<RRawFile *>(h->ExecPlugin(2, &url, &options)));
82 }
83 throw std::runtime_error("Cannot load plugin handler for " + plgclass);
84 }
85 throw std::runtime_error("Cannot find plugin handler for " + plgclass);
86 }
87 throw std::runtime_error("Unsupported transport protocol: " + transport);
88}
89
91{
92 if (fIsOpen)
93 return;
94
95 OpenImpl();
96 fIsOpen = true;
97}
98
99void ROOT::Internal::RRawFile::ReadVImpl(RIOVec *ioVec, unsigned int nReq)
100{
101 for (unsigned i = 0; i < nReq; ++i) {
102 ioVec[i].fOutBytes = ReadAt(ioVec[i].fBuffer, ioVec[i].fSize, ioVec[i].fOffset);
103 }
104}
105
106std::string ROOT::Internal::RRawFile::GetLocation(std::string_view url)
107{
108 auto idx = url.find(kTransportSeparator);
109 if (idx == std::string_view::npos)
110 return std::string(url);
111 return std::string(url.substr(idx + strlen(kTransportSeparator)));
112}
113
115{
116 if (fFileSize != kUnknownFileSize)
117 return fFileSize;
118
119 EnsureOpen();
120 fFileSize = GetSizeImpl();
121 return fFileSize;
122}
123
125 return fUrl;
126}
127
128std::string ROOT::Internal::RRawFile::GetTransport(std::string_view url)
129{
130 auto idx = url.find(kTransportSeparator);
131 if (idx == std::string_view::npos)
132 return "file";
133 std::string transport(url.substr(0, idx));
134 std::transform(transport.begin(), transport.end(), transport.begin(), ::tolower);
135 return transport;
136}
137
138size_t ROOT::Internal::RRawFile::Read(void *buffer, size_t nbytes)
139{
140 size_t res = ReadAt(buffer, nbytes, fFilePos);
141 fFilePos += res;
142 return res;
143}
144
145size_t ROOT::Internal::RRawFile::ReadAt(void *buffer, size_t nbytes, std::uint64_t offset)
146{
147 EnsureOpen();
148
149 // Early return for empty requests
150 if (nbytes == 0)
151 return 0;
152
153 // "Large" reads are served directly, bypassing the cache; since nbytes > 0, fBlockSize == 0 is also handled here
154 if (!fIsBuffering || nbytes > static_cast<unsigned int>(fOptions.fBlockSize))
155 return ReadAtImpl(buffer, nbytes, offset);
156
157 if (!fBufferSpace) {
158 fBufferSpace.reset(new unsigned char[kNumBlockBuffers * fOptions.fBlockSize]);
159 for (unsigned int i = 0; i < kNumBlockBuffers; ++i) {
160 fBlockBuffers[i].fBuffer = fBufferSpace.get() + i * fOptions.fBlockSize;
161 fBlockBuffers[i].fBufferSize = 0;
162 }
163 }
164
165 size_t totalBytes = 0;
166 size_t copiedBytes = 0;
167 /// Try to serve as many bytes as possible from the block buffers
168 for (unsigned int idx = fBlockBufferIdx; idx < fBlockBufferIdx + kNumBlockBuffers; ++idx) {
169 copiedBytes = fBlockBuffers[idx % kNumBlockBuffers].CopyTo(buffer, nbytes, offset);
170 buffer = reinterpret_cast<unsigned char *>(buffer) + copiedBytes;
171 nbytes -= copiedBytes;
172 offset += copiedBytes;
173 totalBytes += copiedBytes;
174 if (copiedBytes > 0)
175 fBlockBufferIdx = idx;
176 if (nbytes == 0)
177 return totalBytes;
178 }
179 fBlockBufferIdx++;
180
181 /// The request was not fully satisfied and fBlockBufferIdx now points to the previous shadow buffer
182
183 /// The remaining bytes populate the newly promoted main buffer
184 RBlockBuffer *thisBuffer = &fBlockBuffers[fBlockBufferIdx % kNumBlockBuffers];
185 size_t res = ReadAtImpl(thisBuffer->fBuffer, fOptions.fBlockSize, offset);
186 thisBuffer->fBufferOffset = offset;
187 thisBuffer->fBufferSize = res;
188 size_t remainingBytes = std::min(res, nbytes);
189 memcpy(buffer, thisBuffer->fBuffer, remainingBytes);
190 totalBytes += remainingBytes;
191 return totalBytes;
192}
193
194void ROOT::Internal::RRawFile::ReadV(RIOVec *ioVec, unsigned int nReq)
195{
196 EnsureOpen();
197 ReadVImpl(ioVec, nReq);
198}
199
201{
202 fIsBuffering = value;
203 if (!fIsBuffering)
204 fBufferSpace.reset();
205}
206
208{
209 if (fOptions.fLineBreak == ELineBreaks::kAuto) {
210 // Auto-detect line breaks according to the break discovered in the first line
211 fOptions.fLineBreak = ELineBreaks::kUnix;
212 bool res = Readln(line);
213 if ((line.length() > 0) && (*line.rbegin() == '\r')) {
214 fOptions.fLineBreak = ELineBreaks::kWindows;
215 line.resize(line.length() - 1);
216 }
217 return res;
218 }
219
220 line.clear();
221 char buffer[kLineBuffer];
222 size_t nbytes;
223 do {
224 nbytes = Read(buffer, sizeof(buffer));
225 std::string_view bufferView(buffer, nbytes);
226 auto idx = bufferView.find(kLineBreakTokens[static_cast<int>(fOptions.fLineBreak)]);
227 if (idx != std::string_view::npos) {
228 // Line break found, return the string and skip the linebreak itself
229 line.append(buffer, idx);
230 fFilePos -= nbytes - idx;
231 fFilePos += kLineBreakTokenSizes[static_cast<int>(fOptions.fLineBreak)];
232 return true;
233 }
234 line.append(buffer, nbytes);
235 } while (nbytes > 0);
236
237 return !line.empty();
238}
239
241{
242 fFilePos = offset;
243}
fBuffer
dim_t fSize
#define h(i)
Definition RSha256.hxx:106
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:407
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:106
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:99
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:128
std::uint64_t GetSize()
Returns the size of the file.
Definition RRawFile.cxx:114
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:240
size_t ReadAt(void *buffer, size_t nbytes, std::uint64_t offset)
Buffered read from a random position.
Definition RRawFile.cxx:145
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:207
void EnsureOpen()
Open the file if not already open. Otherwise noop.
Definition RRawFile.cxx:90
void ReadV(RIOVec *ioVec, unsigned int nReq)
Opens the file if necessary and calls ReadVImpl.
Definition RRawFile.cxx:194
size_t Read(void *buffer, size_t nbytes)
Read from fFilePos offset. Returns the actual number of bytes read.
Definition RRawFile.cxx:138
void SetBuffering(bool value)
Turn off buffered reads; all scalar read requests go directly to the implementation.
Definition RRawFile.cxx:200
std::string GetUrl() const
Returns the url of the file.
Definition RRawFile.cxx:124
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
std::size_t fOutBytes
The number of actually read bytes, set by ReadV()
Definition RRawFile.hxx:69
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition RRawFile.hxx:49