feat(corpus): cache the metadata tables by sha256 between sessions (#64)
R/cache.R was a stub, so a remote session read the ten metadata tables (about 3 MB) over HTTP every time. cog_open() now keeps them in USCOGDATA_CACHE_DIR/corpus/ and points the metadata views there. A cached file is used only while its sha256 matches the session's manifest; a download is checked before it replaces anything. Any failure (an HTTP error such as Hugging Face's 429, a mismatch, an unwritable directory) falls back to remote reads with a message. A local corpus is read in place and never cached. The yearly partitions stay remote; cog_mirror() is still the tool for repeat work on them. Against the public corpus, 2026-11-12, one run each, fresh cache: session open 1.76 s cold -> 0.38 s warm first query 1.16 s cold -> 0.68 s warm cog_spending() for Madison (552025209777), 2012 and 2020, returns the same 46 rows and the same digest as main. Live-corpus tests pass. Closes #64 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
6 files changed
+238
-11
No files matched your search
@@ -138,14 +138,16 @@
|
||||
#' lived in the original `{url}` substitution, unnoticed because nothing ever
|
||||
#' ran on Windows until the mirror's check matrix existed.
|
||||
#' @noRd
|
||||
.render_view_sql <- function(sql, url, manifest = list()) {
|
||||
.render_view_sql <- function(sql, url, manifest = list(), meta_url = url) {
|
||||
sql <- gsub("{long_files}", .long_files_sql(url, manifest), sql, fixed = TRUE)
|
||||
gsub("{url}", url, sql, fixed = TRUE)
|
||||
# {url} appears only in the metadata-table views, so `meta_url` can point
|
||||
# them at the local cache (#64) while {long_files} stays on the corpus.
|
||||
gsub("{url}", meta_url, sql, fixed = TRUE)
|
||||
}
|
||||
|
||||
#' Register DuckDB views from inst/sql/ SQL files
|
||||
#' @noRd
|
||||
.register_views <- function(con, url, manifest) {
|
||||
.register_views <- function(con, url, manifest, meta_url = url) {
|
||||
sql_dir <- system.file("sql", package = "uscogdata")
|
||||
files <- sort(list.files(sql_dir, pattern = "\\.sql$", full.names = TRUE))
|
||||
schema_version <- suppressWarnings(as.integer(manifest$schema_version %||% 0L))
|
||||
@@ -156,7 +158,7 @@
|
||||
!.corpus_has_table(manifest, .representation_view_files[[base]])) next
|
||||
if (base %in% .balance_view_files && !.corpus_has_balance_subtype(con)) next
|
||||
sql <- paste(readLines(f, warn = FALSE), collapse = "\n")
|
||||
sql <- .render_view_sql(sql, url, manifest)
|
||||
sql <- .render_view_sql(sql, url, manifest, meta_url = meta_url)
|
||||
DBI::dbExecute(con, sql)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user