55constexpr char const *gSQliteVfsName =
"ROOT-Davix-readonly";
64 VfsRootFile() =
default;
67 std::unique_ptr<ROOT::Internal::RRawFile> fRawFile;
74int VfsRdOnlyClose(sqlite3_file *pFile)
76 VfsRootFile *p =
reinterpret_cast<VfsRootFile *
>(pFile);
84int VfsRdOnlyRead(sqlite3_file *pFile,
void *zBuf,
int count, sqlite_int64 offset)
86 VfsRootFile *p =
reinterpret_cast<VfsRootFile *
>(pFile);
87 auto nbytes = p->fRawFile->ReadAt(zBuf, count, offset);
88 return (nbytes !=
static_cast<unsigned int>(count)) ? SQLITE_IOERR : SQLITE_OK;
93int VfsRdOnlyWrite(sqlite3_file * ,
const void * ,
int , sqlite_int64 )
95 return SQLITE_OPEN_READONLY;
100int VfsRdOnlyTruncate(sqlite3_file * , sqlite_int64 )
102 return SQLITE_OPEN_READONLY;
107int VfsRdOnlySync(sqlite3_file * ,
int )
114int VfsRdOnlyFileSize(sqlite3_file *pFile, sqlite_int64 *pSize)
116 VfsRootFile *p =
reinterpret_cast<VfsRootFile *
>(pFile);
117 *pSize = p->fRawFile->GetSize();
123int VfsRdOnlyLock(sqlite3_file * ,
int )
130int VfsRdOnlyUnlock(sqlite3_file * ,
int )
137int VfsRdOnlyCheckReservedLock(sqlite3_file * ,
int *pResOut)
145int VfsRdOnlyFileControl(sqlite3_file * ,
int ,
void * )
147 return SQLITE_NOTFOUND;
152int VfsRdOnlySectorSize(sqlite3_file * )
154 return SQLITE_OPEN_READONLY;
159int VfsRdOnlyDeviceCharacteristics(sqlite3_file * )
161 return SQLITE_OPEN_READONLY;
167static sqlite3_io_methods GetSqlite3IoMethods()
171 sqlite3_io_methods io_methods;
172 memset(&io_methods, 0,
sizeof(io_methods));
173 io_methods.iVersion = 1;
174 io_methods.xClose = VfsRdOnlyClose;
175 io_methods.xRead = VfsRdOnlyRead;
176 io_methods.xWrite = VfsRdOnlyWrite;
177 io_methods.xTruncate = VfsRdOnlyTruncate;
178 io_methods.xSync = VfsRdOnlySync;
179 io_methods.xFileSize = VfsRdOnlyFileSize;
180 io_methods.xLock = VfsRdOnlyLock;
181 io_methods.xUnlock = VfsRdOnlyUnlock;
182 io_methods.xCheckReservedLock = VfsRdOnlyCheckReservedLock;
183 io_methods.xFileControl = VfsRdOnlyFileControl;
184 io_methods.xSectorSize = VfsRdOnlySectorSize;
185 io_methods.xDeviceCharacteristics = VfsRdOnlyDeviceCharacteristics;
191int VfsRdOnlyOpen(sqlite3_vfs * ,
const char *zName, sqlite3_file *pFile,
int flags,
int * )
194 VfsRootFile *p =
new (pFile) VfsRootFile();
195 p->pFile.pMethods =
nullptr;
199 static const sqlite3_io_methods io_methods = GetSqlite3IoMethods();
201 if (flags & (SQLITE_OPEN_READWRITE | SQLITE_OPEN_DELETEONCLOSE | SQLITE_OPEN_EXCLUSIVE))
206 ::Error(
"VfsRdOnlyOpen",
"Cannot open %s\n", zName);
211 ::Error(
"VfsRdOnlyOpen",
"cannot determine file size of %s\n", zName);
215 p->pFile.pMethods = &io_methods;
225int VfsRdOnlyDelete(sqlite3_vfs * ,
const char * ,
int )
227 return SQLITE_IOERR_DELETE;
232int VfsRdOnlyAccess(sqlite3_vfs * ,
const char * ,
int flags,
int *pResOut)
235 if (flags == SQLITE_ACCESS_READWRITE) {
236 return SQLITE_OPEN_READONLY;
243int VfsRdOnlyFullPathname(sqlite3_vfs * ,
const char *zPath,
int nOut,
char *zOut)
245 zOut[nOut - 1] =
'\0';
246 sqlite3_snprintf(nOut, zOut,
"%s", zPath);
252int VfsRdOnlyRandomness(sqlite3_vfs * ,
int nBuf,
char *zBuf)
254 for (
int i = 0; i < nBuf; ++i) {
262int VfsRdOnlySleep(sqlite3_vfs * ,
int microseconds)
271int VfsRdOnlyGetLastError(sqlite3_vfs * ,
int ,
char * )
278int VfsRdOnlyCurrentTimeInt64(sqlite3_vfs * , sqlite3_int64 *piNow)
280 static constexpr sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
283 *piNow = ((sqlite3_int64)t) * 1000 + unixEpoch;
289int VfsRdOnlyCurrentTime(sqlite3_vfs *vfs,
double *prNow)
292 int rc = VfsRdOnlyCurrentTimeInt64(vfs, &i);
293 *prNow = i / 86400000.0;
300static sqlite3_vfs GetSqlite3Vfs()
305 memset(&vfs, 0,
sizeof(vfs));
307 vfs.szOsFile =
sizeof(VfsRootFile);
308 vfs.mxPathname = 2000;
309 vfs.zName = gSQliteVfsName;
310 vfs.xOpen = VfsRdOnlyOpen;
311 vfs.xDelete = VfsRdOnlyDelete;
312 vfs.xAccess = VfsRdOnlyAccess;
313 vfs.xFullPathname = VfsRdOnlyFullPathname;
314 vfs.xRandomness = VfsRdOnlyRandomness;
315 vfs.xSleep = VfsRdOnlySleep;
316 vfs.xCurrentTime = VfsRdOnlyCurrentTime;
317 vfs.xGetLastError = VfsRdOnlyGetLastError;
323static struct sqlite3_vfs kSqlite3Vfs = GetSqlite3Vfs();
325static bool RegisterSqliteVfs()
328 retval = sqlite3_vfs_register(&kSqlite3Vfs,
false);
329 return (retval == SQLITE_OK);
341struct RSqliteDSDataSet {
342 sqlite3 *fDb =
nullptr;
343 sqlite3_stmt *fQuery =
nullptr;
348 : fType(
type), fIsActive(false), fInteger(0), fReal(0.0), fText(), fBlob(), fNull(nullptr)
356 default:
throw std::runtime_error(
"Internal error");
371 static bool hasSqliteVfs = RegisterSqliteVfs();
373 throw std::runtime_error(
"Cannot register SQlite VFS in RSqliteDS");
377 retval = sqlite3_open_v2(fileName.c_str(), &
fDataSet->fDb, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX,
379 if (retval != SQLITE_OK)
382 retval = sqlite3_prepare_v2(
fDataSet->fDb, query.c_str(), -1, &
fDataSet->fQuery,
nullptr);
383 if (retval != SQLITE_OK)
386 int colCount = sqlite3_column_count(
fDataSet->fQuery);
387 retval = sqlite3_step(
fDataSet->fQuery);
388 if ((retval != SQLITE_ROW) && (retval != SQLITE_DONE))
392 for (
int i = 0; i < colCount; ++i) {
394 int type = SQLITE_NULL;
397 const char *declTypeCstr = sqlite3_column_decltype(
fDataSet->fQuery, i);
398 if (declTypeCstr ==
nullptr) {
399 if (retval == SQLITE_ROW)
402 std::string declType(declTypeCstr);
403 std::transform(declType.begin(), declType.end(), declType.begin(), ::toupper);
404 if (declType ==
"INTEGER")
405 type = SQLITE_INTEGER;
406 else if (declType ==
"FLOAT")
408 else if (declType ==
"TEXT")
410 else if (declType ==
"BLOB")
413 throw std::runtime_error(
"Unexpected column decl type");
438 default:
throw std::runtime_error(
"Unhandled data type");
445RSqliteDS::~RSqliteDS()
458const std::vector<std::string> &RSqliteDS::GetColumnNames()
const
474 std::string errmsg =
"The type selected for column \"";
476 errmsg +=
"\" does not correspond to column type, which is ";
478 throw std::runtime_error(errmsg);
481 fValues[index].fIsActive =
true;
488std::vector<std::pair<ULong64_t, ULong64_t>> RSqliteDS::GetEntryRanges()
490 std::vector<std::pair<ULong64_t, ULong64_t>> entryRanges;
491 int retval = sqlite3_step(
fDataSet->fQuery);
493 case SQLITE_DONE:
return entryRanges;
507std::string RSqliteDS::GetTypeName(std::string_view colName)
const
511 for (
unsigned i = 0; i <
N; ++i) {
516 throw std::runtime_error(
"Unknown column: " + std::string(colName));
521bool RSqliteDS::HasColumn(std::string_view colName)
const
528void RSqliteDS::Initialise()
531 int retval = sqlite3_reset(
fDataSet->fQuery);
532 if (retval != SQLITE_OK)
533 throw std::runtime_error(
"SQlite error, reset");
536std::string RSqliteDS::GetLabel()
547 ROOT::RDataFrame rdf(std::make_unique<RSqliteDS>(std::string(fileName), std::string(query)));
557 for (
unsigned i = 0; i <
N; ++i) {
566 nbytes = sqlite3_column_bytes(
fDataSet->fQuery, i);
570 fValues[i].fText =
reinterpret_cast<const char *
>(sqlite3_column_text(
fDataSet->fQuery, i));
574 nbytes = sqlite3_column_bytes(
fDataSet->fQuery, i);
575 fValues[i].fBlob.resize(nbytes);
577 std::memcpy(
fValues[i].fBlob.data(), sqlite3_column_blob(
fDataSet->fQuery, i), nbytes);
581 default:
throw std::runtime_error(
"Unhandled column type");
589void RSqliteDS::SetNSlots(
unsigned int nSlots)
592 ::Warning(
"SetNSlots",
"Currently the SQlite data source faces performance degradation in multi-threaded mode. "
593 "Consider turning off IMT.");
600void RSqliteDS::SqliteError(
int errcode)
602 std::string errmsg =
"SQlite error: ";
603#if SQLITE_VERSION_NUMBER < 3007015
604 errmsg += std::to_string(errcode);
606 errmsg += sqlite3_errstr(errcode);
608 throw std::runtime_error(errmsg);
unsigned long long ULong64_t
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
R__EXTERN TRandom * gRandom
R__EXTERN TSystem * gSystem
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.
static constexpr int kFeatureHasSize
GetSize() does not return kUnknownFileSize.
std::vector< void * > Record_t
static constexpr char const * fgTypeNames[]
Corresponds to the types defined in ETypes.
std::vector< std::string > fColumnNames
std::vector< ETypes > fColumnTypes
std::string GetTypeName(std::string_view colName) const final
Returns the C++ type for a given column name, implemented as a linear search through all the columns.
ETypes
All the types known to SQlite. Changes require changing fgTypeNames, too.
RSqliteDS(const std::string &fileName, const std::string &query)
Build the dataframe.
std::unique_ptr< Internal::RSqliteDSDataSet > fDataSet
void SqliteError(int errcode)
Helper function to throw an exception if there is a fatal sqlite error, e.g. an I/O error.
std::vector< Value_t > fValues
The data source is inherently single-threaded and returns only one row at a time. This vector holds t...
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
RDataFrame MakeSqliteDataFrame(std::string_view fileName, std::string_view query)
Factory method to create a SQlite RDataFrame.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
void * fPtr
Points to one of the values; an address to this pointer is returned by GetColumnReadersImpl.
std::vector< unsigned char > fBlob