Repository navigation
feat: optional HTTP/HTTPS VFS for read-only remote databases - #680
Conversation
|
Thanks! This PR comes with a few unrelated changes, some useful, some undesired. In particular, I'd like to understand the |
There was a problem hiding this comment.
Pull request overview
Adds an optional, experimental HTTP/HTTPS SQLite VFS to RSQLite, enabling read-only access to remote immutable databases over HTTP(S) when libcurl is available at build time, plus R helpers, configuration, and tests.
Changes:
- Introduces a minimal HTTP/HTTPS VFS implementation (range-first with optional full-download fallback) and wires it into the extension loading path and build system (configure/Makevars).
- Adds R-level helpers for capability probing, remote connection convenience, configuration via environment variables, and runtime stats, along with httpuv-backed tests to validate behavior.
- Updates documentation (README, NEWS, Rd files) to describe the experimental HTTP VFS feature, its configuration, limitations, and build-time requirements.
Reviewed changes
Copilot reviewed 31 out of 33 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/testthat/test-httpvfs.R | New httpuv-based integration tests validating basic remote read, Range usage, stats, and failure when Range is unsupported and full-download fallback is disabled. |
| tests/testthat/test-httpvfs-compiled.R | Test ensuring sqliteHttpVfsCompiled() agrees with the internal sqlite_httpvfs_compiled() indicator. |
| src/vendor/extensions/http.c | Core HTTP/HTTPS read-only VFS implementation using libcurl, HTTP Range requests, in-memory page cache, optional full-download fallback, and global stats. |
| src/httpvfs_stats.cpp | C++/cpp11 wrapper exposing sqlite_http_stats() as an R list (sqliteHttpStats() back-end). |
| src/httpvfs_stats.dd | Dependency file for httpvfs_stats.o, ensuring it is rebuilt when relevant headers/config change. |
| src/has_http_vfs.cpp | C++ helpers to query whether an HTTP-related VFS is registered and whether HTTP VFS was compiled in (sqlite_has_http_vfs(), sqlite_httpvfs_compiled()). |
| src/has_http_vfs.dd | Dependency file for has_http_vfs.o. |
| src/ext-http.c | Glue file that pulls in vendor/extensions/http.c and exports sqlite3_http_init from the package shared library. |
| src/ext-http.dd | Dependency file for ext-http.o. |
| src/deps.mk | Switches from PKG_LIBS to PKG_OBJECTS for dependency includes and improves sed filtering to drop absolute Windows/system paths from generated .dd files. |
| src/cpp11.cpp | Registers new C-callable wrappers _RSQLite_sqlite_has_http_vfs, _RSQLite_sqlite_http_stats, _RSQLite_sqlite_httpvfs_compiled and adds them to the CallEntries table. |
| src/Makevars | Refactors build flags: introduces HTTPVFS_CPPFLAGS, CURL_CFLAGS, CURL_LIBS, PKG_OBJECTS, and wires libcurl/httpvfs support and optional warning-suppression flags in a portable way. |
| src/DbConnection.cpp | Ensures all connections are opened with SQLITE_OPEN_URI so URI options such as vfs= and immutable= work reliably, enabling HTTP VFS URIs. |
| R/zzz.R | On-load hook now memoizes warning_once and eagerly registers the HTTP VFS via initExtension(con, "http") only when compiled with HTTPVFS support, swallowing errors to keep behavior conservative. |
| R/initExtension.R | Adds "http" to the supported extension names and maps it to the sqlite3_http_init symbol; documents the HTTP extension in the initExtension help. |
| R/httpvfs_config.R | New sqlite_http_config() helper to configure HTTP VFS behavior process-wide via environment variables for cache size, prefetch pages, and fallback behavior. |
| R/httpvfs.R | New sqlite_remote() convenience function to open remote SQLite databases over HTTP/HTTPS, ensuring the HTTP VFS is registered and appending vfs=http and optionally immutable=1 to the URI. |
| R/cpp11.R | Adds R wrappers for the new cpp11-registered C++ functions: sqlite_has_http_vfs(), sqlite_httpvfs_compiled(), and sqlite_http_stats(). |
| R/SQLite.R | Adds sqliteHasHttpVFS(), sqliteHttpStats(), and sqliteHttpVfsCompiled() user-facing helpers, plus documentation and examples for HTTP VFS capabilities and stats. |
| man/sqlite_remote.Rd | New docs for sqlite_remote() and sqliteHasHttpVFS(), describing remote HTTP/HTTPS usage, immutability, and related HTTP VFS APIs. |
| man/sqlite_http_config.Rd | Docs for sqlite_http_config(), including details of the backing environment variables and example usage. |
| man/sqliteHttpVfsCompiled.Rd | Docs for sqliteHttpVfsCompiled(), clarifying its role as a compile-time capability indicator. |
| man/sqliteHttpStats.Rd | Docs for sqliteHttpStats(), describing the exposed counters and their meaning, with example usage. |
| man/initExtension.Rd | Extends initExtension docs to describe the "http" extension as registering an HTTP/HTTPS VFS. |
| inst/WORDLIST | Adds HTTP-related and libcurl terms to the spelling whitelist to keep spell checks passing. |
| configure.win | New Windows configure script to detect libcurl via pkg-config or curl-config, update src/Makevars.local with HTTPVFS and curl flags/libs, and optionally set warning-suppression flags. |
| configure | New Unix configure script mirroring the Windows behavior: detects libcurl, updates Makevars.local, enables HTTPVFS macro and curl flags/libs, and supports optional warning suppression. |
| README.Rmd | Source README updated with an “Experimental: HTTP/HTTPS VFS” section, configuration and introspection notes, build flags, and example usage. |
| README.md | Generated README reflecting the new HTTP/HTTPS VFS documentation, code examples, and advanced build flags. |
| NEWS.md | Adds a feature entry describing the experimental HTTP/HTTPS VFS, its range-based behavior, configuration, capability probes, and stats API. |
| NAMESPACE | Exports new public functions: sqliteHasHttpVFS, sqliteHttpStats, sqliteHttpVfsCompiled, sqlite_http_config, and sqlite_remote. |
| DESCRIPTION | Updates the package description to mention the optional HTTP/HTTPS VFS, adds memoise to Imports and httpuv to Suggests, and collates the new R source files. |
| .gitignore | Ignores src/Makevars.local, which is generated/updated by the new configure scripts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| is experimental and not an official SQLite extension; it currently downloads | ||
| the entire remote file into memory and serves reads from that buffer. | ||
| It is intended for read-only access to small, immutable databases. | ||
| Building this extension may require libcurl and is optional in RSQLite. |
There was a problem hiding this comment.
The "http" extension description in the Rd file still claims it "currently downloads the entire remote file into memory", but the implementation now fetches pages on demand via HTTP Range requests with an in-memory page cache and only optional full-download fallback. To avoid confusing users, this section should be updated to match the current design described in the README and sqlite_http_config()/sqlite_remote() documentation.
| is experimental and not an official SQLite extension; it currently downloads | |
| the entire remote file into memory and serves reads from that buffer. | |
| It is intended for read-only access to small, immutable databases. | |
| Building this extension may require libcurl and is optional in RSQLite. | |
| is experimental and not an official SQLite extension; it fetches pages on demand | |
| using HTTP Range requests and serves reads from an in-memory page cache, with an | |
| optional full-download fallback depending on server support and configuration. | |
| It is primarily intended for read-only access to small, immutable databases; see | |
| \code{\link{sqlite_http_config}()} and \code{\link{sqlite_remote}()} for configuration | |
| options and usage examples. Building this extension may require libcurl and is | |
| optional in RSQLite. |
| sqlite_remote <- function(url, immutable = TRUE, ...) { | ||
| stopifnot(is.character(url), length(url) == 1L) | ||
| if (!grepl("^https?://", url)) stop("url must begin with http:// or https://", call. = FALSE) | ||
| # Ensure the HTTP VFS is registered in this process; try to lazily init if needed | ||
| if (!sqliteHasHttpVFS()) { | ||
| # Register via initExtension() using a throwaway in-memory connection | ||
| tmp <- DBI::dbConnect(SQLite(), ":memory:") | ||
| on.exit(try(DBI::dbDisconnect(tmp), silent = TRUE), add = TRUE) | ||
| try(initExtension(tmp, "http"), silent = TRUE) | ||
| } | ||
| if (!sqliteHasHttpVFS()) stop("HTTP VFS not available in this build", call. = FALSE) | ||
| sep <- if (grepl("\\?", url)) "&" else "?" | ||
| if (!grepl("[?&]vfs=", url)) url <- paste0(url, sep, "vfs=http") | ||
| if (isTRUE(immutable) && !grepl("[?&]immutable=", url)) { | ||
| sep <- if (grepl("\\?", url)) "&" else "?" | ||
| url <- paste0(url, sep, "immutable=1") | ||
| } | ||
| DBI::dbConnect(SQLite(), url, flags = SQLITE_RO, ...) |
There was a problem hiding this comment.
sqlite_remote() introduces a user-facing helper for constructing HTTP/HTTPS URIs and ensuring the HTTP VFS is registered, but there are currently no tests that exercise this function directly (e.g., validating URI rewriting for vfs=/immutable= parameters and behavior when the VFS is unavailable). Given the rest of the package’s testthat coverage, it would be good to add targeted tests for sqlite_remote() (and possibly its error paths) to guard against regressions in this helper.
| sqlite_http_config <- function(cache_size_mb = NULL, prefetch_pages = NULL, fallback_full_download = NULL) { | ||
| stopifnot(is.null(cache_size_mb) || (is.numeric(cache_size_mb) && length(cache_size_mb) == 1L)) | ||
| stopifnot(is.null(prefetch_pages) || (is.numeric(prefetch_pages) && length(prefetch_pages) == 1L)) | ||
| stopifnot(is.null(fallback_full_download) || is.logical(fallback_full_download)) | ||
|
|
||
| prev <- list( | ||
| cache_size_mb = as.integer(Sys.getenv("RSQLITE_HTTP_CACHE_MB", unset = "4")), | ||
| prefetch_pages = as.integer(Sys.getenv("RSQLITE_HTTP_PREFETCH_PAGES", unset = "0")), | ||
| fallback_full_download = as.logical(tolower(Sys.getenv("RSQLITE_HTTP_FALLBACK_FULLDL", unset = "1")) %in% c("1","true","yes")) | ||
| ) | ||
|
|
||
| if (!is.null(cache_size_mb)) Sys.setenv(RSQLITE_HTTP_CACHE_MB = as.integer(cache_size_mb)) | ||
| if (!is.null(prefetch_pages)) Sys.setenv(RSQLITE_HTTP_PREFETCH_PAGES = as.integer(prefetch_pages)) | ||
| if (!is.null(fallback_full_download)) Sys.setenv(RSQLITE_HTTP_FALLBACK_FULLDL = if (isTRUE(fallback_full_download)) "1" else "0") | ||
|
|
||
| prev |
There was a problem hiding this comment.
sqlite_http_config() is the central R helper for configuring the HTTP VFS via environment variables, but there are no tests that verify its behavior (round-tripping previous values, correct handling of NULL vs. non-NULL arguments, and encoding of RSQLITE_HTTP_FALLBACK_FULLDL). Adding a small set of tests for this function would help ensure the documented interface and env-var semantics remain stable as the HTTP VFS evolves.
| out.push_back(cpp11::as_sexp(bytes)); // bytes_fetched | ||
| out.push_back(cpp11::as_sexp(ranges)); // range_requests | ||
| out.push_back(cpp11::as_sexp(fulldl)); // full_downloads |
There was a problem hiding this comment.
sqlite_http_stats() currently returns full_download as an integer (0/1) via cpp11::as_sexp(fulldl), but the public sqliteHttpStats() documentation describes full_download as a logical flag. For consistency with the docs and typical R conventions it would be better either to coerce this field to logical before returning (e.g., via an explicit cast) or adjust the documentation to state that it is an integer counter rather than a logical.
| out.push_back(cpp11::as_sexp(bytes)); // bytes_fetched | |
| out.push_back(cpp11::as_sexp(ranges)); // range_requests | |
| out.push_back(cpp11::as_sexp(fulldl)); // full_downloads | |
| out.push_back(cpp11::as_sexp(bytes)); // bytes_fetched | |
| out.push_back(cpp11::as_sexp(ranges)); // range_requests | |
| out.push_back(cpp11::as_sexp(fulldl != 0)); // full_download (logical flag) |
|
@krlmlr regarding the the Windows
The “Windows So I adjusted
That keeps the |
|
Hi @krlmlr, I realized after opening the PR that I also disabled default extension loading in Sorry for the inconvenience/churn from fixing this after the initial PR. To make it easy to reproduce, I put up a minimal working example repo + hosted DB here: https://github.com/davidrsch/rsqlite-httpvfs-example (DB: https://davidrsch.github.io/rsqlite-httpvfs-example/mtcars.sqlite). |
|
merge conflict solved with last commit @krlmlr |
|
@copilot resolve the merge conflicts in this pull request |
krlmlr
left a comment
There was a problem hiding this comment.
Thank you for your patience. Would you like to work on this together, or should I wrap this up? Both options are fine with me.
| export(sqlite_http_config) | ||
| export(sqlite_remote) |
There was a problem hiding this comment.
Why the change in naming convention here (snake case vs. camel case)?
There was a problem hiding this comment.
No strong reason, just an oversight. Will rename to sqliteHttpConfig and sqliteRemote for consistency with sqliteHasHttpVFS, sqliteHttpStats, etc.
| ## Features | ||
|
|
||
| - Experimental: optional HTTP/HTTPS virtual file system (VFS) support. When built | ||
| with `RSQLITE_ENABLE_HTTPVFS` (libcurl detected) you can open remote databases using | ||
| URI filenames like `"https://host/path/db.sqlite?vfs=http&immutable=1"`. | ||
| The initial implementation downloaded the entire database eagerly; it now performs | ||
| on-demand page access via HTTP Range requests backed by an in-memory LRU page cache, | ||
| with a configurable prefetch and an optional full-download fallback when Range is | ||
| unsupported. A conservative configuration interface `sqlite_http_config()` exposes | ||
| cache size, prefetch pages, and fallback toggle; `sqlite_remote()` is a convenience | ||
| helper that ensures lazy registration and read-only connection semantics. | ||
| Capability probing (`sqliteHasHttpVFS()`) now auto-registers the extension so that | ||
| it reports availability immediately on supported builds. A lightweight statistics | ||
| API (`sqliteHttpStats()`) surfaces bytes fetched, range request count, and whether | ||
| a full download occurred. The feature is intended for small immutable databases; | ||
| API and behavior are subject to change while experimental. | ||
|
|
||
| ## Internal | ||
|
|
||
| - Eager HTTP VFS registration is now gated at compile time: it only runs when | ||
| libcurl was detected during build (defining `RSQLITE_ENABLE_HTTPVFS`). A small | ||
| helper is available for diagnostics: `RSQLite:::sqlite_httpvfs_compiled()` | ||
| returns `TRUE` if the package was compiled with HTTP VFS enabled. The | ||
| user-facing capability probe remains `sqliteHasHttpVFS()`. |
There was a problem hiding this comment.
We can document elsewhere, NEWS are autogenerated. Need to be restored to the previous state.
There was a problem hiding this comment.
Missed that NEWS is fledge-autogenerated. I'll revert the HTTP VFS section entirely.
| devtools::install_github("r-dbi/RSQLite") | ||
| ``` | ||
|
|
||
| ## Experimental: HTTP/HTTPS VFS |
There was a problem hiding this comment.
To me, this belongs in a help page. A brief mention of extensions in general is fine, but not more. Maybe structure the pkgdown docs to add a separate extension section?
There was a problem hiding this comment.
Agreed. I'll trim it to a brief mention with a link to the help pages. Happy to add a pkgdown Extensions section too.
| @@ -1,4 +1,15 @@ | |||
| # BOOST_NO_AUTO_PTR: https://github.com/eddelbuettel/bh/issues/57#issuecomment-406261595 | |||
| ############################################################################### | |||
There was a problem hiding this comment.
I'd prefer a smaller diff in this file.
There was a problem hiding this comment.
Agreed, I'll strip it down to only the HTTP VFS additions (HTTPVFS_CPPFLAGS, CURL_CFLAGS, CURL_LIBS, -include Makevars.local, and the $(HTTPVFS_CPPFLAGS) / $(CURL_CFLAGS) / $(CURL_LIBS) references). The SQLITE_ENABLE_PERCENTILE, RSQLITE_CXXFLAGS/RSQLITE_CFLAGS, and PKG_OBJECTS changes are unrelated and I'll revert them.
| path.c_str(), | ||
| &pConn_, | ||
| flags, | ||
| flags | SQLITE_OPEN_URI, |
There was a problem hiding this comment.
What effect can this have to existing code?
There was a problem hiding this comment.
No effect on existing usage. The flag enables optional URI parameter parsing in SQLite, but that parsing only triggers when the connection string is intentionally structured as a URI. For ordinary connection strings the flag adds nothing. It is needed so the remote helper can pass VFS selection and immutability settings through the connection string. Without it, SQLite would ignore those parameters.
| warning_once <<- memoise::memoise(warning_once) | ||
| } | ||
|
|
||
| # Eagerly register HTTP VFS only if compiled in (compile-time gated) |
There was a problem hiding this comment.
Why are we doing this? And why the change above?
There was a problem hiding this comment.
The warning_once guard prevents re-running memoise::memoise() if .onLoad() is called more than once (e.g., during devtools::load_all()). I'll revert that too, it's unrelated to the HTTP VFS feature. I'll also drop the eager registration block since the lazy path in sqliteHasHttpVFS() is sufficient. That restores .onLoad() to its original one-liner.
|
Thanks for the review! I'll push fixes for the items below, naming, NEWS, README, Makevars, and zzz.R. Let me know if you'd also like a vignette and a pkgdown Extensions section. |
Remove HTTP VFS entries from autogenerated NEWS. Trim README to brief mention. Minimize Makevars diff to keep only HTTP VFS additions.
Formatting suggestions availableA patch file with formatting suggestions has been generated. You can apply it using one of these methods: Method 1: Apply via gh CLI# Download and apply the patch directly
gh run download 25611996694 --repo r-dbi/RSQLite --name changes-patch && patch -p1 < changes.patch && rm changes.patchMethod 2: View the patchClick to see the patch contentsdiff --git a/man/sqliteRemote.Rd b/man/sqliteRemote.Rd
index 2585d8f0..e04e3206 100644
--- a/man/sqliteRemote.Rd
+++ b/man/sqliteRemote.Rd
@@ -14,7 +14,7 @@ sqliteRemote(url, immutable = TRUE, ...)
\item{...}{Passed through to \code{\link[DBI:dbConnect]{DBI::dbConnect()}}.}
}
\value{
-A \linkS4class{SQLiteConnection} object.
+A \link[=SQLiteConnection-class]{SQLiteConnection} object.
}
\description{
Constructs a URI filename and sets the appropriate VFS and immutable flags.This comment was automatically generated by the commit-suggester workflow. |
…-5izaso # Conflicts: # .Rbuildignore # src/Makevars # src/Makevars.deps
|
Thanks! This goes into the upcoming RSQLite release. |
This PR adds an experimental HTTP/HTTPS virtual file system (VFS) that enables read-only access to remote, immutable SQLite databases via URI filenames (e.g.
https://host/db.sqlite?vfs=http&immutable=1). The feature is optional and only compiled when libcurl is detected at build time.User-facing API
sqlite_remote(url, immutable = TRUE, ...): convenience helper to open remote DBs read-only and ensure VFS registration.sqliteHasHttpVFS(): runtime capability probe (includes a lazy registration attempt).sqlite_http_config(cache_size_mb, prefetch_pages, fallback_full_download): configure conservative process-wide behavior via env vars.sqliteHttpStats(): best-effort per-process counters (bytes fetched, range request count, full download flag).sqliteHttpVfsCompiled(): compile-time indicator of whether HTTP VFS was built in.Implementation details
http.cwith entrypointsqlite3_http_init(VFS name:"http")..onLoad()is gated to only run when compiled with HTTPVFS enabled.Build / portability
configure/configure.winto detect libcurl (pkg-config / curl-config) and updateMakevarsvariables idempotently (avoids repeated appends).deps.mkto avoid capturing machine-specific Windows absolute include paths in .dd dependency files.Tests
Notes / caveats
skip_on_cran()and requirehttpuv.