db_put: Methods to Store And Retrieve Database Records

Description Usage Arguments Details Value Note Author(s) References See Also Examples

Description

Store and retrieve R objects as key/value pairs from a Berkeley DB.

Usage

1
2
3
4
5
db_put(dbh, txnid=NULL, key, data, flags=0L)
db_get(dbh, txnid=NULL, key, data, flags=0L)

dbcursor_put(dbc, key, data, flags)
dbcursor_get(dbc, key, data, flags, n)

Arguments

dbh

A DB handle to an open Berkeley database.

dbc

A DBC handle to an open cursor.

txnid

A DB_TXN transaction handle. (currently ignored)

key

An R object, raw or will be coerced to raw. See details.

data

An R object, raw or will be coerced to raw. See details.

flags

A valid flag created with mkFlags.

n

Elements to return.

Details

BerkeleyDB uses key/data pairs for records, with no underlying data schema.

This allows for arbitrary byte-strings to be stored as keys or values in a DB. Internally these are a C struct of type DBT. To the RBerkeley user, these objects can range from standard (serialized) R objects to anything representable as a RAW vector in R, which is everything.

The design of db_put, db_get, dbcursor_put and dbcursor_get differ from package philosophy in that by default all objects are preprocessed via R to make usable byte strings (RAW vectors) for internal Berkeley use.

When passed an R object of any type, a check is made to see if it needs to be converted into a raw vector, if so the serialize function is called. This conversion is applied to both key and value automatically.

To avoid the use of serialize one must provide a vector of class raw.

The db_get and dbcursor_get methods will return a vector or list of vectors of type raw. Further application specific processing may be required.

Value

*put functions return non-zero on failure, 0 on success. The *get calls will return a raw vector that must be processed on success, or non-zero on failure.

These functions are called for their database side effects.

Note

Serialized objects in R have certain limitations with respect to underlying design considerations.

Author(s)

Jeffrey A. Ryan

References

BerkeleyDB DB->put
http://docs.oracle.com/cd/E17076_04/html/gsg/C/PutEntryWCursor.html

BerkeleyDB DB->get
http://docs.oracle.com/cd/E17076_04/html/gsg/C/Positioning.html

See Also

serialize, unserialize

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
## Not run: 
dbh <- db_create()
db_open(dbh, flags=mkFlags(DB_CREATE))

db_put(dbh, key="key", data="value")

db_get(dbh, key="key") # serialized "value"
unserialize(db_get(dbh, key="key")) # "value"

db_put(dbh, key=charToRaw("key2"), data=charToRaw("value"))
db_get(dbh, key=charToRaw("key2"))
rawToChar(db_get(dbh, key=charToRaw("key2"))) # what you expect, "value"

db_get(dbh, key="key2")  # not there, must use symmetric marshalling/conversion

db_close(dbh) # close DB
dbh <- db_create()
db_remove(dbh, "access.db", NULL)

## End(Not run)

RBerkeley documentation built on Jan. 15, 2017, 3:44 p.m.