| 60153 |
ripley |
1 |
# File src/library/base/makebasedb.R
|
| 68953 |
ripley |
2 |
# Part of the R package, https://www.R-project.org
|
| 60153 |
ripley |
3 |
#
|
|
|
4 |
# Copyright (C) 1995-2012 The R Core Team
|
|
|
5 |
#
|
|
|
6 |
# This program is free software; you can redistribute it and/or modify
|
|
|
7 |
# it under the terms of the GNU General Public License as published by
|
|
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
|
|
9 |
# (at your option) any later version.
|
|
|
10 |
#
|
|
|
11 |
# This program is distributed in the hope that it will be useful,
|
|
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
14 |
# GNU General Public License for more details.
|
|
|
15 |
#
|
|
|
16 |
# A copy of the GNU General Public License is available at
|
| 68953 |
ripley |
17 |
# https://www.R-project.org/Licenses/
|
| 60153 |
ripley |
18 |
|
| 30349 |
ripley |
19 |
local({
|
|
|
20 |
makeLazyLoadDB <- function(from, filebase, compress = TRUE, ascii = FALSE,
|
|
|
21 |
variables) {
|
|
|
22 |
|
| 60407 |
ripley |
23 |
envlist <- function(e)
|
|
|
24 |
.Internal(getVarsFromFrame(ls(e, all = TRUE), e, FALSE))
|
| 30349 |
ripley |
25 |
|
|
|
26 |
envtable <- function() {
|
|
|
27 |
idx <- 0
|
|
|
28 |
envs <- NULL
|
|
|
29 |
enames <- character(0)
|
| 48780 |
ripley |
30 |
find <- function(v, keys, vals) {
|
| 47570 |
hornik |
31 |
for (i in seq_along(keys))
|
| 30349 |
ripley |
32 |
if (identical(v, keys[[i]]))
|
|
|
33 |
return(vals[i])
|
| 48780 |
ripley |
34 |
NULL
|
|
|
35 |
}
|
| 30349 |
ripley |
36 |
getname <- function(e) find(e, envs, enames)
|
|
|
37 |
getenv <- function(n) find(n, enames, envs)
|
|
|
38 |
insert <- function(e) {
|
|
|
39 |
idx <<- idx + 1
|
|
|
40 |
name <- paste("env", idx, sep="::")
|
|
|
41 |
envs <<- c(e, envs)
|
|
|
42 |
enames <<- c(name, enames)
|
|
|
43 |
name
|
|
|
44 |
}
|
|
|
45 |
list(insert = insert, getenv = getenv, getname = getname)
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
lazyLoadDBinsertValue <- function(value, file, ascii, compress, hook)
|
| 60407 |
ripley |
49 |
.Internal(lazyLoadDBinsertValue(value, file, ascii, compress, hook))
|
| 30349 |
ripley |
50 |
|
|
|
51 |
lazyLoadDBinsertListElement <- function(x, i, file, ascii, compress, hook)
|
| 60407 |
ripley |
52 |
.Internal(lazyLoadDBinsertValue(x[[i]], file, ascii, compress, hook))
|
| 30349 |
ripley |
53 |
|
|
|
54 |
lazyLoadDBinsertVariable <- function(n, e, file, ascii, compress, hook) {
|
| 60407 |
ripley |
55 |
x <- .Internal(getVarsFromFrame(n, e, FALSE))
|
|
|
56 |
.Internal(lazyLoadDBinsertValue(x[[1L]], file, ascii, compress, hook))
|
| 30349 |
ripley |
57 |
}
|
|
|
58 |
|
|
|
59 |
mapfile <- paste(filebase, "rdx", sep = ".")
|
|
|
60 |
datafile <- paste(filebase, "rdb", sep = ".")
|
|
|
61 |
close(file(datafile, "w")) # truncate to zero
|
|
|
62 |
table <- envtable()
|
|
|
63 |
varenv <- new.env(hash = TRUE)
|
|
|
64 |
envenv <- new.env(hash = TRUE)
|
|
|
65 |
|
|
|
66 |
envhook <- function(e) {
|
|
|
67 |
if (is.environment(e)) {
|
|
|
68 |
name <- table$getname(e)
|
|
|
69 |
if (is.null(name)) {
|
|
|
70 |
name <- table$insert(e)
|
|
|
71 |
data <- list(bindings = envlist(e),
|
|
|
72 |
enclos = parent.env(e))
|
|
|
73 |
key <- lazyLoadDBinsertValue(data, datafile, ascii,
|
|
|
74 |
compress, envhook)
|
| 41600 |
ripley |
75 |
assign(name, key, envir = envenv)
|
| 30349 |
ripley |
76 |
}
|
|
|
77 |
name
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
| 36174 |
murdoch |
81 |
if (is.environment(from)) {
|
| 30349 |
ripley |
82 |
if (! missing(variables))
|
|
|
83 |
vars <- variables
|
|
|
84 |
else vars <- ls(from, all = TRUE)
|
|
|
85 |
}
|
|
|
86 |
else if (is.list(from)) {
|
|
|
87 |
vars <- names(from)
|
|
|
88 |
if (length(vars) != length(from) || any(nchar(vars) == 0))
|
|
|
89 |
stop("source list must have names for all elements")
|
|
|
90 |
}
|
|
|
91 |
else stop("source must be an environment or a list");
|
|
|
92 |
|
| 47570 |
hornik |
93 |
for (i in seq_along(vars)) {
|
| 36174 |
murdoch |
94 |
if (is.environment(from))
|
| 30349 |
ripley |
95 |
key <- lazyLoadDBinsertVariable(vars[i], from, datafile,
|
|
|
96 |
ascii, compress, envhook)
|
|
|
97 |
else key <- lazyLoadDBinsertListElement(from, i, datafile, ascii,
|
|
|
98 |
compress, envhook)
|
| 41600 |
ripley |
99 |
assign(vars[i], key, envir = varenv)
|
| 30349 |
ripley |
100 |
}
|
|
|
101 |
|
| 41600 |
ripley |
102 |
vals <- lapply(vars, get, envir = varenv, inherits = FALSE)
|
| 30349 |
ripley |
103 |
names(vals) <- vars
|
|
|
104 |
|
|
|
105 |
rvars <- ls(envenv, all = TRUE)
|
| 41600 |
ripley |
106 |
rvals <- lapply(rvars, get, envir = envenv, inherits = FALSE)
|
| 30349 |
ripley |
107 |
names(rvals) <- rvars
|
|
|
108 |
|
|
|
109 |
val <- list(variables = vals, references = rvals,
|
|
|
110 |
compressed = compress)
|
| 54021 |
ripley |
111 |
saveRDS(val, mapfile)
|
| 30349 |
ripley |
112 |
}
|
|
|
113 |
|
| 41531 |
ripley |
114 |
omit <- c(".Last.value", ".AutoloadEnv", ".BaseNamespaceEnv",
|
|
|
115 |
".Device", ".Devices", ".Machine", ".Options", ".Platform")
|
| 30349 |
ripley |
116 |
|
|
|
117 |
if (length(search()[search()!="Autoloads"]) != 2)
|
|
|
118 |
stop("start R with NO packages loaded to create the data base")
|
|
|
119 |
|
| 30994 |
ripley |
120 |
baseFileBase <- file.path(.Library,"base","R","base")
|
| 30983 |
ripley |
121 |
|
| 30349 |
ripley |
122 |
if (file.info(baseFileBase)["size"] < 20000) # crude heuristic
|
|
|
123 |
stop("may already be using lazy loading on base");
|
|
|
124 |
|
| 41593 |
ripley |
125 |
basevars <- ls(baseenv(), all.names=TRUE)
|
| 41531 |
ripley |
126 |
prims <- basevars[sapply(basevars, function(n) is.primitive(get(n, baseenv())))]
|
|
|
127 |
basevars <- basevars[! basevars %in% c(omit, prims)]
|
| 30349 |
ripley |
128 |
|
| 35450 |
murdoch |
129 |
makeLazyLoadDB(baseenv(), baseFileBase, variables = basevars)
|
| 30349 |
ripley |
130 |
})
|