1 ######################################################################
2 ## Copyright (C) 2006, Roger D. Peng <rpeng@jhsph.edu>
4 ## This program is free software; you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2 of the License, or
7 ## (at your option) any later version.
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; if not, write to the Free Software
16 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 #####################################################################
20 ################################################################################
21 ## Class 'filehashRDS'
23 setClass("filehashRDS",
24 representation(dir = "character"),
28 setValidity("filehashRDS",
30 if(length(object@dir) != 1)
31 return("only one directory should be set in 'dir'")
32 if(!file.exists(object@dir))
33 return(gettextf("directory '%s' does not exist",
38 createRDS <- function(dbName) {
39 if(!file.exists(dbName)) {
40 status <- dir.create(dbName)
43 stop(gettextf("unable to create database directory '%s'",
47 message(gettextf("database '%s' already exists", dbName))
51 initializeRDS <- function(dbName) {
52 ## Trailing '/' causes a problem in Windows?
53 dbName <- sub("/$", "", dbName, perl = TRUE)
54 new("filehashRDS", dir = normalizePath(dbName),
55 name = basename(dbName))
58 ## For case-insensitive file systems, objects with the same name but
59 ## differ by capitalization might get clobbered. `mangleName()'
60 ## inserts a "@" before each capital letter and `unMangleName()'
61 ## reverses the operation.
63 mangleName <- function(oname) {
64 gsub("([A-Z])", "@\\1", oname, perl = TRUE)
67 unMangleName <- function(mname) {
68 gsub("@", "", mname, fixed = TRUE)
71 ## Function for mapping a key to a path on the filesystem
72 setGeneric("objectFile", function(db, key) standardGeneric("objectFile"))
73 setMethod("objectFile", signature(db = "filehashRDS", key = "character"),
75 file.path(db@dir, mangleName(key))
78 ################################################################################
79 ## Interface functions
82 signature(db = "filehashRDS", key = "character", value = "ANY"),
83 function(db, key, value, safe = TRUE, ...) {
88 con <- gzfile(writefile, "wb")
90 writestatus <- tryCatch({
92 }, condition = function(cond) {
97 if(inherits(writestatus, "condition"))
98 stop(gettextf("unable to write object '%s'", key))
100 return(invisible(!inherits(writestatus, "condition")))
102 cpstatus <- file.copy(writefile, objectFile(db, key),
106 stop(gettextf("unable to insert object '%s'", key))
108 rmstatus <- file.remove(writefile)
111 warning("unable to remove temporary file")
116 setMethod("dbFetch", signature(db = "filehashRDS", key = "character"),
117 function(db, key, ...) {
118 ## Create filename from key
119 ofile <- objectFile(db, key)
124 }, condition = function(cond) {
127 if(inherits(con, "condition"))
128 stop(gettextf("error obtaining value for key '%s'",
133 val <- unserialize(con)
137 setMethod("dbExists", signature(db = "filehashRDS", key = "character"),
138 function(db, key, ...) {
142 setMethod("dbList", "filehashRDS",
144 ## list all keys/files in the database
145 fileList <- dir(db@dir, all.files = TRUE, full.names = TRUE)
146 use <- !file.info(fileList)$isdir
147 fileList <- basename(fileList[use])
149 unMangleName(fileList)
152 setMethod("dbDelete", signature(db = "filehashRDS", key = "character"),
153 function(db, key, ...) {
154 ofile <- objectFile(db, key)
156 ## remove/delete the file
157 status <- file.remove(ofile)
158 invisible(isTRUE(status))
161 setMethod("dbUnlink", "filehashRDS",
163 ## delete the entire database directory
165 status <- unlink(d, recursive = TRUE)