These functions form the interface for a simple file-based key-value database (i.e. hash table).
Objects can be created by calls of the form new("filehash", ...)
.
name
:Object of class "character"
, name of the
database.
datafile
:full path to the database file.
meta
:list containing an environment for database metadata.
Directory where files are stored.
The dbDelete
function is for deleting
elements, but for the "DB1"
format all it does is remove the
key from the lookup table.
The actual data are still in the database (but inaccessible). If
you reinsert data for the same key, the new data are simply
appended on to the end of the file. Therefore, it's possible to
have multiple copies of data lying around after a while,
potentially making the database file big. The "RDS"
format
does not have this problem.
check to see if a key exists.
retrieve the value associated with a given key.
retrieve values associated with multiple keys (a list of those values is returned).
insert a key-value pair into the database. If
that key already exists, its associated value is overwritten. For
"RDS"
type databases, there is a safe
option
(defaults to TRUE
) which allows the user to insert objects
somewhat more safely (objects should not be lost in the event of
an interrupt).
list all keys in the database.
The dbReorganize
function is there for
the purpose of rewriting the database to remove all of the stale
entries. Basically, this function creates a new copy of the
database and then overwrites the old copy. This function has not
been tested extensively and so should be considered
experimental. dbReorganize
is not needed when using
the "RDS"
format.
delete an entire database from the disk
print method
allows with
to be used with "filehash"
objects much like it can be used with lists or data frames
elements of a database can be accessed using the [[
operator much like a list or environment, but only character
indices are allowed
elements of a database can be accessed using the $
operator much like with a list or environment
works much like lapply
with lists; a list is
returned.
returns all of the keys in the database
returns the number of elements in the database
Roger D. Peng rpeng@jhsph.edu
dbCreate("myDB") ## Create database 'myDB'
db <- dbInit("myDB")
dbInsert(db, "a", 1:10)
dbInsert(db, "b", rnorm(1000))
dbExists(db, "b") ## 'TRUE'
dbList(db) ## c("a", "b")
dbDelete(db, "a")
dbList(db) ## "b"
with(db, mean(b))
dbUnlink(db)
Run the code above in your browser using DataLab