Title: | A R Convenience Layer for CouchDB 2.0 |
---|---|
Description: | Provides a collection of functions for basic database and document management operations such as add (cdbAddDoc()), get (cdbGetDoc()), list access (cdbGetList()) or delete (cdbDeleteDoc()). Every cdbFunction() gets and returns a list() containing the connection setup. Such a list (named 'cdb' in the documentation) can be generated by cdb <- cdbIni(). Moreover, cdb provides some functions respectively functionality e.g cdb$baseUrl() or cdb$getDocRev(). |
Authors: | Thomas Bock |
Maintainer: | Thomas Bock <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.7.5 |
Built: | 2024-11-03 04:50:58 UTC |
Source: | https://github.com/wactbprot/r4couchdb |
This function adds attachments to a database document that already exists.
cdbAddAttachment(cdb)
cdbAddAttachment(cdb)
cdb |
The list |
The function uses the RCurl
- function
guessMIMEType()
to do exactly this: guessing the mime type of
cdb$fileName
.
If the switch cdb$attachmentsWithPath
is set to TRUE
the attachment is saved with the path. This behavior is default
since version 0.2.5 of R4CouchDB
cdb |
The result is stored in |
wactbprot
## Not run: ccc <- cdbIni(DBName="r4couch_db") ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) # make a 3d plot (stolen from ?persp) x <- seq(-10, 10, length= 30) y <- x f <- function(x,y) {r <- sqrt(x^2+y^2); 10 * sin(r)/r } z <- outer(x, y, f) z[is.na(z)] <- 1 op <- par(bg = "black") ccc$fileName <- "3dplot.pdf" pdf(ccc$fileName) persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue") dev.off() # add the plot as attachment to the database # it workes over ccc$fileName ccc <- cdbAddAttachment(ccc) ## End(Not run)
## Not run: ccc <- cdbIni(DBName="r4couch_db") ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) # make a 3d plot (stolen from ?persp) x <- seq(-10, 10, length= 30) y <- x f <- function(x,y) {r <- sqrt(x^2+y^2); 10 * sin(r)/r } z <- outer(x, y, f) z[is.na(z)] <- 1 op <- par(bg = "black") ccc$fileName <- "3dplot.pdf" pdf(ccc$fileName) persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue") dev.off() # add the plot as attachment to the database # it workes over ccc$fileName ccc <- cdbAddAttachment(ccc) ## End(Not run)
This function adds a new document to an already existing database
cdbAddDoc(cdb)
cdbAddDoc(cdb)
cdb |
The list |
This function is called addDoc (which means add a new document). Therefore
the cdb$id
is requested using cdbGetUuid()
for every document
to add if no cdb$id
is provided. If a cdb$id
is provided the
function fails when a document with the given id already exists. In this
case one should use cdbUpdateDoc()
. Since version v0.6 the function
writes the _rev
and _id
key to the top level of
cdb$dataList
.
cdb |
The couchdb response is stored in |
wactbprot
cdbGetDoc()
## Not run: ccc <- cdbIni() # I assume a database at localhost:5984 already exists ccc$DBName <- "r4couchdb_db" ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) ## End(Not run)
## Not run: ccc <- cdbIni() # I assume a database at localhost:5984 already exists ccc$DBName <- "r4couchdb_db" ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) ## End(Not run)
This is done via the _bulk_docs API provided by an already existing database.
cdbAddDocS(cdb)
cdbAddDocS(cdb)
cdb |
|
The _bulk_docs endpoint requires that cdb$dataList
resolves
to an json array. This is reached with e.g.
cdb$dataList <- list(list(...),list(...),...)
.
Furthermore, _bulk_docs requires the documents to be wrapped in a key
named docs:[...]
; this is done by cdbAddDocS()
if
cdb$dataList
is a list of lists. The user dont need to care.
At the moment the resulting _rev
and _id
will be not
written back to the cdb$dataList
. This means that a second
call of cdbAddDocS()
generates new Documents.
cdb |
The couchdb response is stored in |
parisni, wactbprot
cdbAddDoc()
## Not run: ccc <- cdbIni() # I assume a database at localhost:5984 already exists ccc$DBName <- "r4couchdb_db" docs <- list() for(i in 1:10){ docs[[i]] <- list(normalDistRand = rnorm(20)) } # docs is noe a list of 10 lists ccc$dataList <- docs # generating 10 database documents cccAddDocS(ccc)$res ## End(Not run)
## Not run: ccc <- cdbIni() # I assume a database at localhost:5984 already exists ccc$DBName <- "r4couchdb_db" docs <- list() for(i in 1:10){ docs[[i]] <- list(normalDistRand = rnorm(20)) } # docs is noe a list of 10 lists ccc$dataList <- docs # generating 10 database documents cccAddDocS(ccc)$res ## End(Not run)
With a given cdb$id
this function sends a http "DELETE"
request to the url .../cdb$id?rev=cdb$rev
.
cdbDeleteDoc(cdb)
cdbDeleteDoc(cdb)
cdb |
Beside |
cdb |
The result of the delete request is stored in
|
wactbprot
cdbAddDoc()
Function provides access to the _config
api end point.
cdbGetConfig(cdb)
cdbGetConfig(cdb)
cdb |
Only the connection settings |
cdb |
The result of the request is stored in |
wactbprot
cdbMakeDB
## Not run: cdbGetConfig(cdbIni())$res ## End(Not run)
## Not run: cdbGetConfig(cdbIni())$res ## End(Not run)
With a given cdb$id
this function requests the document.
cdbGetDoc(cdb)
cdbGetDoc(cdb)
cdb |
Beside in cdb$errors |
cdb |
The result of the request is stored in |
wactbprot
cdbAddDoc()
## Not run: ccc <- cdbIni() ccc$newDBName <- "r4couchdb_db" ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) cdbGetDoc(ccc)$res ## End(Not run)
## Not run: ccc <- cdbIni() ccc$newDBName <- "r4couchdb_db" ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) cdbGetDoc(ccc)$res ## End(Not run)
The function provides accesses to CouchDB lists.
cdbGetList(cdb)
cdbGetList(cdb)
cdb |
Beside the connection details ( |
Query params e.g. "reduce=false"
or "group_level=1"
can be
provided in cdb$queryParam
By now multible params must be given in
one string e.g. "a=b&c=d&e=f"
.
cdb |
The result of the request is stored in cdb$res after converting
the json answer into a list using |
wactbprot
The function provides accesses to CouchDB shows.
cdbGetShow(cdb)
cdbGetShow(cdb)
cdb |
Beside the connection details ( |
Query params e.g. "format=json"
can be
provided in cdb$queryParam
. Multible params must be given in
one string e.g. "a=b&c=d&e=f"
.
cdb |
The result of the request is stored in cdb$res after converting
the json answer into a list using |
wactbprot
Function returns a 128bit uuid requested from CouchDB
cdbGetUuid(cdb)
cdbGetUuid(cdb)
cdb |
Only the connection settings |
Simple CouchDB API end point to http://serverName:port/_uuids.
cdb |
The result of the request is stored in |
wactbprot
cdbMakeDB
## Not run: cdbGetUuid(cdbIni())$res ## End(Not run)
## Not run: cdbGetUuid(cdbIni())$res ## End(Not run)
Function returns a 128bit uuid requested from CouchDB
cdbGetUuidS(cdb)
cdbGetUuidS(cdb)
cdb |
Only the connection settings |
CouchDB API provides the url http://serverName:port/_uuids for those clients
who aren't able to create those ids. The number N
of ids received from a CouchDB can be set by cdb$count <- N
since version 0.6. The function writes to cdb$res (in opposite to
cdbGetUuid()
whitch writes to cdb$id
)
cdb |
The result of the request is stored in |
wactbprot
cdbMakeDB
## Not run: ccc <- cdbIni() ccc$count <- 100 cdbGetUuidS(ccc)$res ## End(Not run)
## Not run: ccc <- cdbIni() ccc$count <- 100 cdbGetUuidS(ccc)$res ## End(Not run)
The function provides accesses to CouchDB views.
cdbGetView(cdb)
cdbGetView(cdb)
cdb |
Beside the connection details ( |
Query params e.g. "reduce=false"
or "group_level=1"
can be
provided in cdb$queryParam
cdb |
The result of the request is stored in cdb$res after converting the json answer into a list using fromJSON(). If a needed cdb list entry was not provided cdb$error says something about the R side |
For the moment only one cdb$queryParam
is possible. In the
future maybe Duncans RJavaScript
package can be used to generate
views without leaving R.
wactbprot
Function returns a list with some default settings and often used functions
such as cdb$baseUrl
.
cdbIni(serverName="localhost", port="5984", prot = "http", DBName="", uname = "", pwd = "", newDBName = "", removeDBName = "", id = "", fileName = "", design = "", view = "", list = "", show = "", queryParam = "", encSub = "?", count = 10, dataList = list(), attachmentsWithPath=TRUE, digits = 7)
cdbIni(serverName="localhost", port="5984", prot = "http", DBName="", uname = "", pwd = "", newDBName = "", removeDBName = "", id = "", fileName = "", design = "", view = "", list = "", show = "", queryParam = "", encSub = "?", count = 10, dataList = list(), attachmentsWithPath=TRUE, digits = 7)
serverName |
server name |
port |
port |
prot |
name of the protocol default is http |
DBName |
name of database |
uname |
name of the user |
pwd |
password |
newDBName |
name of the database for cdbMakeDB() |
removeDBName |
name of the database to remove with cdbRemoveDB() |
id |
the document id to get, put, post or delete |
fileName |
for use in cdbAddAttachment |
design |
the name of the design used when asking a view or list |
view |
the name of a view to query |
list |
the name of a list to query |
show |
the name of a show to query |
queryParam |
additional query params |
encSub |
a character which is used as a replacement for chars who can not be converted by iconv |
count |
how many uuids should be returned by cdbGetUuidS() |
dataList |
a list containing data to post or update |
attachmentsWithPath |
effects the result of the function cdbAddAttachment in the way the variable is named |
digits |
digits kept at toJSON conversion |
The list: cdb <- list(serverName = "localhost", ... )
is returned if
the packages library(RCurl)
and library(RJSONIO)
are
successfully loaded.
cdb |
The R4CouchDB (method) chain(ing) list |
wactbprot, parisni
## Not run: ccc <- cdbIni(digits=13, DBName="r4couch_db", attachmentsWithPath=FALSE, dataList=list(normalDistRand = rnorm(20))) ## End(Not run)
## Not run: ccc <- cdbIni(digits=13, DBName="r4couch_db", attachmentsWithPath=FALSE, dataList=list(normalDistRand = rnorm(20))) ## End(Not run)
Gives a list of all databases available at cdb$serverName
.
cdbListDB(cdb)
cdbListDB(cdb)
cdb |
Only the connection settings |
The function uses the _all_dbs
API end point .
cdb |
The result of the request is stored in cdb$res after converting
the json answer into a list using |
wactbprot
cdbMakeDB
## Not run: cdbListDB(cdbIni())$res ## End(Not run)
## Not run: cdbListDB(cdbIni())$res ## End(Not run)
The name of the new database is taken from cdb$newDBName
.
cdbMakeDB(cdb)
cdbMakeDB(cdb)
cdb |
The |
The work is done by getURL()
from Duncans RCurl package.
After creating the new database the function makes the shortcut
cdb$DBName <- cdb$newDBName
so that further operations happen on the
new created database. Finaly cdb$newDBName <- ""
.
cdb |
The CouchDB answer is stored in |
The convention for database naming should be implemented.
wactbprot
cdbUpdateDoc
## Not run: ccc <- cdbIni() ccc$newDBName <- "r4couchdb_db" ccc <- cdbMakeDB(ccc) ccc$res ccc$removeDBName <- ccc$DBName cdbRemoveDB(ccc)$res ## End(Not run)
## Not run: ccc <- cdbIni() ccc$newDBName <- "r4couchdb_db" ccc <- cdbMakeDB(ccc) ccc$res ccc$removeDBName <- ccc$DBName cdbRemoveDB(ccc)$res ## End(Not run)
Removing a database means sending a http- "DELETE"- request to
http://cdb$serverName:cdb$port/ ...
cdbRemoveDB(cdb)
cdbRemoveDB(cdb)
cdb |
The |
In cdb
a entry cdb$delDBName
should be provided for more
explicit deleting respectively more secure removing.
cdb |
The CouchDB answer is stored in |
wactbprot
cdbMakeDB
## Not run: ccc <- cdbIni() ccc$newDBName <- "r4couchdb_db" ccc <- cdbMakeDB(ccc) ccc$res ccc$removeDBName <- ccc$DBName cdbRemoveDB(ccc)$res ## End(Not run)
## Not run: ccc <- cdbIni() ccc$newDBName <- "r4couchdb_db" ccc <- cdbMakeDB(ccc) ccc$res ccc$removeDBName <- ccc$DBName cdbRemoveDB(ccc)$res ## End(Not run)
This essentially means that a
revision, corresponding to the '_id' has to be provided. If no '_rev' is
given in the cdb
list the function gets the doc from the db
and takes the rev number for the update
cdbUpdateDoc(cdb)
cdbUpdateDoc(cdb)
cdb |
the cdb connection configuration list must contain the
|
Updating a doc at couchdb means executing a http "PUT" request. The
cdb
list must contain the cdb$serverName
, cdb$port
,
cdb$DBName
, cdb$id
. Since v0.6 the revision of the document
should exist at the intended place: cdb$dataList$'_rev'
.
getURL()
with customrequest = "PUT"
does the work. If a
needed cdb$
list entry is not provided cdb$error
maybe says
something about the R side.
cdb |
The response of the request is stored in |
wactbprot
cdbInit()
## Not run: ccc <- cdbIni() # I assume a database at localhost:5984 already exists ccc$DBName <- "r4couchdb_db" ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) ccc$dataList$Date <- date() ccc <- cdbUpdateDoc(ccc) ## End(Not run)
## Not run: ccc <- cdbIni() # I assume a database at localhost:5984 already exists ccc$DBName <- "r4couchdb_db" ccc$dataList <- list(normalDistRand = rnorm(20)) ccc <- cdbAddDoc(ccc) ccc$dataList$Date <- date() ccc <- cdbUpdateDoc(ccc) ## End(Not run)