Skip to content

Commit aac77c7

Browse files
committed
needed to save model design files as compressed .rds files due to storage limit in SQLite
1 parent 858f395 commit aac77c7

File tree

1 file changed

+43
-66
lines changed

1 file changed

+43
-66
lines changed

R/sql_functions.R

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ unserialize_table <- function(table, project) {
246246

247247
tab_type <- table_type(table)
248248

249-
serial_tabs <- c("alt choice matrix", "expected catch matrix", "model data", "model designs",
249+
serial_tabs <- c("alt choice matrix", "expected catch matrix", "model data",
250250
"model fit", "predict output", "global check", "model output",
251251
"long format data")
252252

@@ -264,8 +264,7 @@ unserialize_table <- function(table, project) {
264264
"model data" = "ModelInputData", # Note: check for consistency, seen lowercase version
265265
# (depends on whether created in app or console)
266266
"predict output" = "PredictOutput",
267-
"long format data" = "data",
268-
"model designs" = "data")
267+
"long format data" = "data")
269268

270269
sql_qry <- paste0("SELECT ", tab_qry, " FROM ", table, " LIMIT 1")
271270

@@ -499,21 +498,6 @@ model_fit <- function(project, CV = FALSE) {
499498

500499
}
501500

502-
model_names <- function(project) {
503-
#' Return model names
504-
#'
505-
#' Returns model names saved to to the model design file.
506-
#'
507-
#' @param project Name of project.
508-
#' @export
509-
#'
510-
511-
tab_name <- paste0(project, "ModelInputData")
512-
mod_design_list <- unserialize_table(tab_name, project)
513-
514-
vapply(mod_design_list, function(x) x$mod.name, character(1))
515-
}
516-
517501
exp_catch_names <- function(project) {
518502
#' Return names of expected catch matrices
519503
#'
@@ -635,7 +619,7 @@ list_tables <- function(project, type = "main") {
635619
#' (GridTable), "aux" (AuxTable) "ec" (ExpectedCatch), "altc" (AltMatrix),
636620
#' "info" (MainDataTableInfo), "gc" (ldglobalcheck), "fleet" (FleetTable),
637621
#' "filter" (FilterTable), "centroid" (Centroid or FishCentroid), "model"
638-
#' (ModelOut), "model data" or "model design" (ModelInputData),
622+
#' (ModelOut), "model data" (ModelInputData),
639623
#' "outsample" (OutSampleDataTable).
640624
#' @export
641625
#' @examples
@@ -646,7 +630,7 @@ list_tables <- function(project, type = "main") {
646630
#'
647631

648632
tab_types <- c("info", "main", "ec", "altc", "port", "gc", "fleet", "model",
649-
"model data", "model design", "grid", "aux", "spat", "filter",
633+
"model data", "grid", "aux", "spat", "filter",
650634
"centroid", "outsample", "cross valid")
651635

652636
if (!type %in% tab_types) {
@@ -660,7 +644,7 @@ list_tables <- function(project, type = "main") {
660644
"info" = "MainDataTableInfo", "main" = "MainDataTable", "ec" = "ExpectedCatch",
661645
"altc" = "AltMatrix", "port" = "PortTable", "gc" = "ldglobalcheck",
662646
"fleet" = "FleetTable", "model" = "ModelOut", "model data" = "ModelInputData",
663-
"model design" = "ModelInputData", "grid" = "GridTable", "aux" = "AuxTable",
647+
"grid" = "GridTable", "aux" = "AuxTable",
664648
"spat" = "SpatTable", "filter" = "FilterTable", "centroid" = "Centroid",
665649
"outsample" = "OutSampleDataTable", "cross valid" = "CV")
666650

@@ -780,8 +764,7 @@ fishset_tables <- function(project = NULL) {
780764
"FilterTable" = "filter table", "ldglobalcheck" = "global check",
781765
"FleetTable" = "fleet table", "ModelOut" = "model output",
782766
"ModelFit" = "model fit", "ModelInputData" = "model data",
783-
"modelDesignTable" = "model design", "other" = "other",
784-
"GridTable_raw" = "raw grid table", "GridTable" = "grid table",
767+
"other" = "other", "GridTable_raw" = "raw grid table", "GridTable" = "grid table",
785768
"AuxTable_raw" = "raw aux table", "AuxTable" = "aux table",
786769
"SpatTable_raw" = "raw spat table", "SpatTable" = "spat table")
787770
}, character(1))
@@ -855,14 +838,18 @@ model_design_list <- function(project, name = NULL) {
855838
#' @export
856839
#'
857840

858-
if (!is_value_empty(name)) {
859-
860-
unserialize_table(name, project)
861-
862-
} else {
863-
864-
unserialize_table(paste0(project, 'ModelInputData'), project)
865-
}
841+
db_path <- locdatabase(project)
842+
project_dir <- dirname(db_path)
843+
designs_dir <- file.path(project_dir, "ModelDesigns")
844+
845+
if (!dir.exists(designs_dir)) {
846+
stop("No model design files were created for this project. Run fishset_design().")
847+
}
848+
849+
files <- list.files(designs_dir, pattern = "\\.rds$", full.names = FALSE)
850+
files <- tools::file_path_sans_ext(basename(files))
851+
852+
return(files)
866853
}
867854

868855

@@ -873,41 +860,31 @@ remove_model_design <- function(project, names) {
873860
#' @param names Names of model designs to be deleted from the table
874861
#' @export
875862
#'
876-
fishset_db <- DBI::dbConnect(RSQLite::SQLite(), locdatabase(project = project))
877-
on.exit(DBI::dbDisconnect(fishset_db), add = TRUE)
878-
879-
single_sql <- paste0(project, "ModelInputData")
880-
881-
if(!table_exists(single_sql, project)){
882-
if(isRunning()){
883-
showNotification(paste0(single_sql, " does not exist in database"), type = "error", duration = 60)
884-
} else {
885-
stop(paste0(single_sql, " does not exist in database."))
886-
}
887-
863+
db_path <- locdatabase(project)
864+
project_dir <- dirname(db_path)
865+
designs_dir <- file.path(project_dir, "ModelDesigns")
866+
867+
file_name <- paste0(names, ".rds")
868+
file_path <- file.path(designs_dir, file_name)
869+
870+
success_flag <- TRUE
871+
872+
if (!dir.exists(designs_dir)) {
873+
stop("No model design files were created for this project. Run fishset_design().")
874+
}
875+
876+
files <- list.files(designs_dir, pattern = "\\.rds$", full.names = FALSE)
877+
878+
# Remove File from Disk
879+
if (file.exists(file_path)) {
880+
tryCatch({
881+
file.remove(file_path)
882+
message("Deleted design file: ", file_name)
883+
}, error = function(e) {
884+
warning("Could not delete file: ", e$message)
885+
success_flag <<- FALSE
886+
})
888887
} else {
889-
# Load data and find models to delete
890-
ModelInputData <- model_design_list(project)
891-
mod_names <- model_names(project)
892-
del_mods <- which(mod_names %in% names)
893-
894-
if(length(del_mods) == 0){
895-
if(isRunning()){
896-
showNotification("Model(s) do not exist in ModelInputData table", type = "error", duration = 60)
897-
} else {
898-
stop("Model(s) do not exist in ModelInputData table.")
899-
}
900-
}
901-
902-
# Remove models from input data list
903-
ModelInputData[del_mods] <- NULL
904-
905-
# Now remove old table from sql database
906-
table_remove(single_sql, project)
907-
908-
# Add table with updated input data
909-
DBI::dbExecute(fishset_db, paste("CREATE TABLE IF NOT EXISTS", single_sql, "(ModelInputData MODELINPUTDATA)"))
910-
DBI::dbExecute(fishset_db, paste("INSERT INTO", single_sql, "VALUES (:ModelInputData)"),
911-
params = list(ModelInputData = list(serialize(ModelInputData, NULL))))
888+
message("File not found on disk (skipping): ", file_name)
912889
}
913890
}

0 commit comments

Comments
 (0)