|
| 1 | +# Per-State Caching Architecture Proposal |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +Currently, if a nationwide dataset is missing just 1-2 states (e.g., FPL 2022 missing HI and IL), we must re-download all 51 states (~12GB, 30-60 minutes) instead of just the missing states. |
| 6 | + |
| 7 | +**Root Cause**: Individual state ZIP files are not cached - they're downloaded, extracted, then deleted. |
| 8 | + |
| 9 | +## Proposed Architecture |
| 10 | + |
| 11 | +### 1. Cache Structure |
| 12 | + |
| 13 | +``` |
| 14 | +~/.cache/emburden/ |
| 15 | +├── lead_2022_fpl_AL.zip # Individual state ZIPs (kept!) |
| 16 | +├── lead_2022_fpl_AK.zip |
| 17 | +├── lead_2022_fpl_... |
| 18 | +├── lead_2022_fpl_HI.zip # Missing state |
| 19 | +├── lead_2022_fpl_IL.zip # Missing state |
| 20 | +├── lead_2022_fpl_WY.zip |
| 21 | +├── lead_2022_fpl.csv # Merged nationwide CSV |
| 22 | +└── emburden_db.sqlite # Database with nationwide data |
| 23 | +``` |
| 24 | + |
| 25 | +### 2. Smart Download Logic |
| 26 | + |
| 27 | +#### Before (Current): |
| 28 | +```r |
| 29 | +download_and_merge_states() { |
| 30 | + for each state in all 51 states: |
| 31 | + download ZIP |
| 32 | + extract CSV |
| 33 | + delete ZIP # ❌ Lost! |
| 34 | + merge all CSVs |
| 35 | + save merged CSV |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +#### After (Proposed): |
| 40 | +```r |
| 41 | +download_and_merge_states() { |
| 42 | + # 1. Check which states are already cached |
| 43 | + cached_states <- check_cached_state_files(dataset, vintage) |
| 44 | + missing_states <- setdiff(all_states, cached_states) |
| 45 | + |
| 46 | + # 2. Only download missing states |
| 47 | + for each state in missing_states: |
| 48 | + download ZIP to state-specific file (e.g., lead_2022_fpl_HI.zip) |
| 49 | + keep ZIP for future use # ✅ Cached! |
| 50 | + |
| 51 | + # 3. Load all states (cached + newly downloaded) |
| 52 | + for each state in all 51 states: |
| 53 | + if (state ZIP exists): |
| 54 | + extract and load data |
| 55 | + else: |
| 56 | + skip (log warning) |
| 57 | + |
| 58 | + # 4. Merge and validate |
| 59 | + merge all loaded states |
| 60 | + if (missing states): |
| 61 | + report which states are missing |
| 62 | + save merged CSV |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### 3. Validation & Self-Healing |
| 67 | + |
| 68 | +When validation detects corrupt/incomplete nationwide data: |
| 69 | + |
| 70 | +```r |
| 71 | +# Current behavior: |
| 72 | +clear_dataset_cache("fpl", "2022") # Deletes EVERYTHING |
| 73 | +re-download all 51 states # 12GB download |
| 74 | + |
| 75 | +# Proposed behavior: |
| 76 | +detect_missing_states(data) # Returns: ["HI", "IL"] |
| 77 | +clear_state_cache("fpl", "2022", c("HI", "IL")) # Delete only corrupt states |
| 78 | +re-download missing 2 states # 500MB download |
| 79 | +merge with 49 cached states # 1-2 minutes |
| 80 | +``` |
| 81 | + |
| 82 | +### 4. Functions to Implement |
| 83 | + |
| 84 | +#### `check_cached_state_files(dataset, vintage)` |
| 85 | +Returns character vector of states that have valid cached ZIP files. |
| 86 | + |
| 87 | +```r |
| 88 | +check_cached_state_files <- function(dataset, vintage) { |
| 89 | + cache_dir <- get_cache_dir() |
| 90 | + all_states <- get_all_states() |
| 91 | + |
| 92 | + cached <- character() |
| 93 | + for (state in all_states) { |
| 94 | + zip_file <- file.path(cache_dir, |
| 95 | + sprintf("lead_%s_%s_%s.zip", vintage, dataset, state)) |
| 96 | + if (file.exists(zip_file) && file.size(zip_file) > 10000) { # >10KB |
| 97 | + cached <- c(cached, state) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return(cached) |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +#### `clear_state_cache(dataset, vintage, states)` |
| 106 | +Removes specific state ZIP files (for corrupted data). |
| 107 | + |
| 108 | +```r |
| 109 | +clear_state_cache <- function(dataset, vintage, states, verbose = TRUE) { |
| 110 | + cache_dir <- get_cache_dir() |
| 111 | + |
| 112 | + for (state in states) { |
| 113 | + zip_file <- file.path(cache_dir, |
| 114 | + sprintf("lead_%s_%s_%s.zip", vintage, dataset, state)) |
| 115 | + if (file.exists(zip_file)) { |
| 116 | + unlink(zip_file) |
| 117 | + if (verbose) message(" ✓ Deleted: ", basename(zip_file)) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +#### Modified `download_and_merge_states()` |
| 124 | + |
| 125 | +```r |
| 126 | +download_and_merge_states <- function(dataset, vintage, states, verbose = TRUE) { |
| 127 | + |
| 128 | + # Check which states are already cached |
| 129 | + cached_states <- check_cached_state_files(dataset, vintage) |
| 130 | + missing_states <- setdiff(states, cached_states) |
| 131 | + |
| 132 | + if (verbose) { |
| 133 | + message(sprintf("Cached states: %d, Missing states: %d", |
| 134 | + length(cached_states), length(missing_states))) |
| 135 | + if (length(missing_states) > 0) { |
| 136 | + message("Will download: ", paste(missing_states, collapse = ", ")) |
| 137 | + } |
| 138 | + if (length(cached_states) > 0) { |
| 139 | + message("Will load from cache: ", paste(cached_states, collapse = ", ")) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + # Download only missing states |
| 144 | + if (length(missing_states) > 0) { |
| 145 | + for (i in seq_along(missing_states)) { |
| 146 | + state <- missing_states[i] |
| 147 | + if (verbose) { |
| 148 | + message(sprintf("[%d/%d] Downloading %s...", i, length(missing_states), state)) |
| 149 | + } |
| 150 | + download_single_state_cached(dataset, vintage, state, verbose = FALSE) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + # Load all states (cached + newly downloaded) |
| 155 | + all_data <- list() |
| 156 | + failed_states <- character() |
| 157 | + |
| 158 | + for (state in states) { |
| 159 | + tryCatch({ |
| 160 | + state_data <- load_state_from_cache(dataset, vintage, state, verbose = FALSE) |
| 161 | + if (!is.null(state_data) && nrow(state_data) > 0) { |
| 162 | + all_data[[state]] <- state_data |
| 163 | + } else { |
| 164 | + failed_states <- c(failed_states, state) |
| 165 | + } |
| 166 | + }, error = function(e) { |
| 167 | + warning(sprintf("Failed to load %s: %s", state, e$message)) |
| 168 | + failed_states <- c(failed_states, state) |
| 169 | + }) |
| 170 | + } |
| 171 | + |
| 172 | + # Merge and save |
| 173 | + combined_data <- dplyr::bind_rows(all_data) |
| 174 | + |
| 175 | + # Save merged nationwide CSV |
| 176 | + cache_dir <- get_cache_dir() |
| 177 | + cache_file <- file.path(cache_dir, paste0("lead_", vintage, "_", dataset, ".csv")) |
| 178 | + readr::write_csv(combined_data, cache_file) |
| 179 | + |
| 180 | + # Import to database |
| 181 | + try_import_to_database(combined_data, dataset, vintage, verbose = verbose) |
| 182 | + |
| 183 | + return(combined_data) |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +### 5. Benefits |
| 188 | + |
| 189 | +✅ **Efficiency**: Download only missing states (minutes vs hours) |
| 190 | +✅ **Resilience**: Individual state corruption doesn't require full re-download |
| 191 | +✅ **Transparency**: Clear reporting of cached vs downloaded states |
| 192 | +✅ **Storage**: ~13GB per dataset (51 states × ~250MB), but saves bandwidth |
| 193 | +✅ **Debugging**: Can inspect individual state files |
| 194 | + |
| 195 | +### 6. Disk Space Considerations |
| 196 | + |
| 197 | +**Before**: ~50MB merged CSV per dataset |
| 198 | +**After**: ~13GB state ZIPs + ~50MB merged CSV per dataset |
| 199 | + |
| 200 | +**Mitigation**: |
| 201 | +- State ZIPs can be deleted after successful merge (optional) |
| 202 | +- Add `clear_state_cache()` function for manual cleanup |
| 203 | +- Add `--keep-state-cache` flag to regeneration script |
| 204 | + |
| 205 | +### 7. Implementation Priority |
| 206 | + |
| 207 | +1. **Phase 1** (For current regeneration): |
| 208 | + - Modify `download_and_merge_states()` to cache state ZIPs |
| 209 | + - Implement `check_cached_state_files()` |
| 210 | + - Test with current FPL 2022 issue |
| 211 | + |
| 212 | +2. **Phase 2** (Post-CRAN): |
| 213 | + - Add `clear_state_cache()` to `R/cache_utils.R` |
| 214 | + - Update corruption detection to identify missing states |
| 215 | + - Implement selective re-download |
| 216 | + |
| 217 | +3. **Phase 3** (Optional): |
| 218 | + - Add cleanup options to regeneration script |
| 219 | + - Implement automatic state cache expiration (30 days?) |
| 220 | + |
| 221 | +### 8. Migration Strategy |
| 222 | + |
| 223 | +Existing users with no cached state files will simply download as before. Once state caching is implemented, future downloads benefit from the per-state cache. |
| 224 | + |
| 225 | +No breaking changes to existing API. |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +## Implementation Decision |
| 230 | + |
| 231 | +**Should we implement this now?** |
| 232 | + |
| 233 | +### Option A: Implement now (before completing current regeneration) |
| 234 | +- ✅ PRO: Solves FPL 2022 issue efficiently (download just HI, IL) |
| 235 | +- ✅ PRO: Future-proofs against similar issues |
| 236 | +- ❌ CON: Delays Zenodo upload by 1-2 hours |
| 237 | +- ❌ CON: Requires testing with active downloads |
| 238 | + |
| 239 | +### Option B: Implement after Zenodo upload (post-CRAN) |
| 240 | +- ✅ PRO: Current regeneration completes sooner |
| 241 | +- ✅ PRO: Can test thoroughly in development |
| 242 | +- ✅ PRO: CRAN submission not delayed |
| 243 | +- ❌ CON: Must re-download all 51 states for FPL 2022 now |
| 244 | + |
| 245 | +### Recommendation: **Option B** |
| 246 | + |
| 247 | +**Reason**: We're already 71% through AMI 2018 download. Implementing per-state caching now would require: |
| 248 | +1. Stopping current regeneration |
| 249 | +2. Implementing and testing new code |
| 250 | +3. Re-running downloads (losing current progress) |
| 251 | + |
| 252 | +Better to: |
| 253 | +1. Complete current regeneration |
| 254 | +2. Get clean datasets to Zenodo |
| 255 | +3. Implement per-state caching properly in next version |
| 256 | +4. Include in v0.6.0 release notes as improvement |
| 257 | + |
| 258 | +This makes per-state caching a **v0.6.0 feature** rather than rushing it into v0.5.x. |
0 commit comments