47constexpr char const *gSQliteVfsName =
"ROOT-Davix-readonly";
56 VfsRootFile() =
default;
59 std::unique_ptr<ROOT::Internal::RRawFile> fRawFile;
66int VfsRdOnlyClose(sqlite3_file *pFile)
68 VfsRootFile *p =
reinterpret_cast<VfsRootFile *
>(pFile);
76int VfsRdOnlyRead(sqlite3_file *pFile,
void *zBuf,
int count, sqlite_int64 offset)
78 VfsRootFile *p =
reinterpret_cast<VfsRootFile *
>(pFile);
79 auto nbytes = p->fRawFile->ReadAt(zBuf, count, offset);
80 return (nbytes !=
static_cast<unsigned int>(count)) ? SQLITE_IOERR : SQLITE_OK;
85int VfsRdOnlyWrite(sqlite3_file * ,
const void * ,
int , sqlite_int64 )
87 return SQLITE_OPEN_READONLY;
92int VfsRdOnlyTruncate(sqlite3_file * , sqlite_int64 )
94 return SQLITE_OPEN_READONLY;
99int VfsRdOnlySync(sqlite3_file * ,
int )
106int VfsRdOnlyFileSize(sqlite3_file *pFile, sqlite_int64 *pSize)
108 VfsRootFile *p =
reinterpret_cast<VfsRootFile *
>(pFile);
109 *pSize = p->fRawFile->GetSize();
115int VfsRdOnlyLock(sqlite3_file * ,
int )
122int VfsRdOnlyUnlock(sqlite3_file * ,
int )
129int VfsRdOnlyCheckReservedLock(sqlite3_file * ,
int *pResOut)
137int VfsRdOnlyFileControl(sqlite3_file * ,
int ,
void * )
139 return SQLITE_NOTFOUND;
144int VfsRdOnlySectorSize(sqlite3_file * )
146 return SQLITE_OPEN_READONLY;
151int VfsRdOnlyDeviceCharacteristics(sqlite3_file * )
153 return SQLITE_OPEN_READONLY;
159static sqlite3_io_methods GetSqlite3IoMethods()
163 sqlite3_io_methods io_methods;
164 memset(&io_methods, 0,
sizeof(io_methods));
165 io_methods.iVersion = 1;
166 io_methods.xClose = VfsRdOnlyClose;
167 io_methods.xRead = VfsRdOnlyRead;
168 io_methods.xWrite = VfsRdOnlyWrite;
169 io_methods.xTruncate = VfsRdOnlyTruncate;
170 io_methods.xSync = VfsRdOnlySync;
171 io_methods.xFileSize = VfsRdOnlyFileSize;
172 io_methods.xLock = VfsRdOnlyLock;
173 io_methods.xUnlock = VfsRdOnlyUnlock;
174 io_methods.xCheckReservedLock = VfsRdOnlyCheckReservedLock;
175 io_methods.xFileControl = VfsRdOnlyFileControl;
176 io_methods.xSectorSize = VfsRdOnlySectorSize;
177 io_methods.xDeviceCharacteristics = VfsRdOnlyDeviceCharacteristics;
183int VfsRdOnlyOpen(sqlite3_vfs * ,
const char *zName, sqlite3_file *pFile,
int flags,
int * )
186 VfsRootFile *p =
new (pFile) VfsRootFile();
187 p->pFile.pMethods =
nullptr;
191 static const sqlite3_io_methods io_methods = GetSqlite3IoMethods();
193 if (flags & (SQLITE_OPEN_READWRITE | SQLITE_OPEN_DELETEONCLOSE | SQLITE_OPEN_EXCLUSIVE))
198 ::Error(
"VfsRdOnlyOpen",
"Cannot open %s\n", zName);
203 ::Error(
"VfsRdOnlyOpen",
"cannot determine file size of %s\n", zName);
207 p->pFile.pMethods = &io_methods;
217int VfsRdOnlyDelete(sqlite3_vfs * ,
const char * ,
int )
219 return SQLITE_IOERR_DELETE;
224int VfsRdOnlyAccess(sqlite3_vfs * ,
const char * ,
int flags,
int *pResOut)
227 if (flags == SQLITE_ACCESS_READWRITE) {
228 return SQLITE_OPEN_READONLY;
235int VfsRdOnlyFullPathname(sqlite3_vfs * ,
const char *zPath,
int nOut,
char *zOut)
237 zOut[nOut - 1] =
'\0';
238 sqlite3_snprintf(nOut, zOut,
"%s", zPath);
244int VfsRdOnlyRandomness(sqlite3_vfs * ,
int nBuf,
char *zBuf)
246 for (
int i = 0; i < nBuf; ++i) {
254int VfsRdOnlySleep(sqlite3_vfs * ,
int microseconds)
263int VfsRdOnlyGetLastError(sqlite3_vfs * ,
int ,
char * )
270int VfsRdOnlyCurrentTimeInt64(sqlite3_vfs * , sqlite3_int64 *piNow)
272 static constexpr sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
275 *piNow = ((sqlite3_int64)t) * 1000 + unixEpoch;
281int VfsRdOnlyCurrentTime(sqlite3_vfs *vfs,
double *prNow)
284 int rc = VfsRdOnlyCurrentTimeInt64(vfs, &i);
285 *prNow = i / 86400000.0;
292static sqlite3_vfs GetSqlite3Vfs()
297 memset(&vfs, 0,
sizeof(vfs));
299 vfs.szOsFile =
sizeof(VfsRootFile);
300 vfs.mxPathname = 2000;
301 vfs.zName = gSQliteVfsName;
302 vfs.xOpen = VfsRdOnlyOpen;
303 vfs.xDelete = VfsRdOnlyDelete;
304 vfs.xAccess = VfsRdOnlyAccess;
305 vfs.xFullPathname = VfsRdOnlyFullPathname;
306 vfs.xRandomness = VfsRdOnlyRandomness;
307 vfs.xSleep = VfsRdOnlySleep;
308 vfs.xCurrentTime = VfsRdOnlyCurrentTime;
309 vfs.xGetLastError = VfsRdOnlyGetLastError;
315static struct sqlite3_vfs kSqlite3Vfs = GetSqlite3Vfs();
317static bool RegisterSqliteVfs()
320 retval = sqlite3_vfs_register(&kSqlite3Vfs,
false);
321 return (retval == SQLITE_OK);
340 : fType(
type), fIsActive(false), fInteger(0), fReal(0.0), fText(), fBlob(), fNull(nullptr)
348 default:
throw std::runtime_error(
"Internal error");
363 static bool hasSqliteVfs = RegisterSqliteVfs();
365 throw std::runtime_error(
"Cannot register SQlite VFS in RSqliteDS");
369 retval = sqlite3_open_v2(fileName.c_str(), &
fDataSet->fDb, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX,
371 if (retval != SQLITE_OK)
379 retval = sqlite3_exec(
fDataSet->fDb,
"PRAGMA temp_store=2;",
nullptr,
nullptr,
nullptr);
380 if (retval != SQLITE_OK)
383 retval = sqlite3_prepare_v2(
fDataSet->fDb, query.c_str(), -1, &
fDataSet->fQuery,
nullptr);
384 if (retval != SQLITE_OK)
387 int colCount = sqlite3_column_count(
fDataSet->fQuery);
388 retval = sqlite3_step(
fDataSet->fQuery);
389 if ((retval != SQLITE_ROW) && (retval != SQLITE_DONE))
393 for (
int i = 0; i < colCount; ++i) {
395 int type = SQLITE_NULL;
398 const char *declTypeCstr = sqlite3_column_decltype(
fDataSet->fQuery, i);
399 if (declTypeCstr ==
nullptr) {
400 if (retval == SQLITE_ROW)
403 std::string declType(declTypeCstr);
404 std::transform(declType.begin(), declType.end(), declType.begin(), ::toupper);
405 if (declType ==
"INTEGER")
406 type = SQLITE_INTEGER;
407 else if (declType ==
"FLOAT")
409 else if (declType ==
"TEXT")
411 else if (declType ==
"BLOB")
414 throw std::runtime_error(
"Unexpected column decl type");
439 default:
throw std::runtime_error(
"Unhandled data type");
475 std::string errmsg =
"The type selected for column \"";
477 errmsg +=
"\" does not correspond to column type, which is ";
479 throw std::runtime_error(errmsg);
482 fValues[index].fIsActive =
true;
491 std::vector<std::pair<ULong64_t, ULong64_t>> entryRanges;
492 int retval = sqlite3_step(
fDataSet->fQuery);
494 case SQLITE_DONE:
return entryRanges;
512 for (
unsigned i = 0; i <
N; ++i) {
517 throw std::runtime_error(
"Unknown column: " + std::string(colName));
532 int retval = sqlite3_reset(
fDataSet->fQuery);
533 if (retval != SQLITE_OK)
534 throw std::runtime_error(
"SQlite error, reset");
548 ROOT::RDataFrame rdf(std::make_unique<RSqliteDS>(std::string(fileName), std::string(query)));
556 assert(entry + 1 ==
fNRow);
559 for (
unsigned i = 0; i <
N; ++i) {
568 nbytes = sqlite3_column_bytes(
fDataSet->fQuery, i);
572 fValues[i].fText =
reinterpret_cast<const char *
>(sqlite3_column_text(
fDataSet->fQuery, i));
576 nbytes = sqlite3_column_bytes(
fDataSet->fQuery, i);
577 fValues[i].fBlob.resize(nbytes);
579 std::memcpy(
fValues[i].fBlob.data(), sqlite3_column_blob(
fDataSet->fQuery, i), nbytes);
583 default:
throw std::runtime_error(
"Unhandled column type");
594 ::Warning(
"SetNSlots",
"Currently the SQlite data source faces performance degradation in multi-threaded mode. "
595 "Consider turning off IMT.");
604 std::string errmsg =
"SQlite error: ";
605#if SQLITE_VERSION_NUMBER < 3007015
606 errmsg += std::to_string(errcode);
608 errmsg += sqlite3_errstr(errcode);
610 throw std::runtime_error(errmsg);
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
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
void SetNSlots(unsigned int nSlots) final
Almost a no-op, many slots can in fact reduce the performance due to thread synchronization.
static constexpr char const * fgTypeNames[]
Corresponds to the types defined in ETypes.
std::string GetLabel() final
Return a string representation of the datasource type.
void Initialise() final
Resets the SQlite query engine at the beginning of the event loop.
std::vector< std::string > fColumnNames
~RSqliteDS()
Frees the sqlite resources and closes the file.
bool HasColumn(std::string_view colName) const final
A linear search through the columns for the given name.
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.
Record_t GetColumnReadersImpl(std::string_view name, const std::type_info &) final
Activates the given column's result value.
RSqliteDS(const std::string &fileName, const std::string &query)
Build the dataframe.
std::unique_ptr< Internal::RSqliteDSDataSet > fDataSet
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Returns a range of size 1 as long as more rows are available in the SQL result set.
const std::vector< std::string > & GetColumnNames() const final
Returns the SELECT queries names.
bool SetEntry(unsigned int slot, ULong64_t entry) final
Stores the result of the current active sqlite query row as a C++ value.
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 TTree,...
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...
The state of an open dataset in terms of the sqlite3 C library.
void * fPtr
Points to one of the values; an address to this pointer is returned by GetColumnReadersImpl.
std::vector< unsigned char > fBlob