This commit is contained in:
Hong Ooi 2020-10-27 21:17:30 +11:00
Родитель 10e9c26224
Коммит c668adfb63
7 изменённых файлов: 194 добавлений и 5 удалений

Просмотреть файл

@ -2,7 +2,8 @@ Package: AzureCosmosR
Title: Interface to the 'Azure Cosmos DB' document database service
Version: 0.0.1
Authors@R: c(
person("Hong", "Ooi", , "hongooi73@gmail.com", role = c("aut", "cre")),
person("Hong", "Ooi", , "hongooi73@gmail.com", role=c("aut", "cre")),
person("Andrew", "Liu", role="ctb", comment="Assistance with Cosmos DB"),
person("Microsoft", role="cph")
)
Description: An interface to 'Azure CosmosDB': <https://azure.microsoft.com/en-us/services/cosmos-db/>. On the admin side, 'AzureCosmosR' provides functionality to create and manage 'Cosmos DB' instances in Microsoft's 'Azure' cloud. On the client side, it provides an interface to the 'Cosmos DB' SQL API, letting the user store and query documents and attachments in 'Cosmos DB'. Part of the 'AzureR' family of packages.

Просмотреть файл

@ -1,9 +1,12 @@
# Generated by roxygen2: do not edit by hand
S3method(call_stored_procedure,cosmos_container)
S3method(call_stored_procedure,cosmos_stored_procedure)
S3method(create_attachment,cosmos_document)
S3method(create_container,cosmos_database)
S3method(create_database,cosmos_endpoint)
S3method(create_document,cosmos_container)
S3method(create_stored_procedure,cosmos_container)
S3method(delete_attachment,cosmos_attachment)
S3method(delete_attachment,cosmos_document)
S3method(delete_container,cosmos_container)
@ -12,6 +15,8 @@ S3method(delete_database,cosmos_database)
S3method(delete_database,cosmos_endpoint)
S3method(delete_document,cosmos_container)
S3method(delete_document,cosmos_document)
S3method(delete_stored_procedure,cosmos_container)
S3method(delete_stored_procedure,cosmos_stored_procedure)
S3method(do_cosmos_op,cosmos_container)
S3method(do_cosmos_op,cosmos_database)
S3method(do_cosmos_op,cosmos_document)
@ -23,23 +28,28 @@ S3method(get_document,cosmos_container)
S3method(list_attachments,cosmos_document)
S3method(list_databases,cosmos_endpoint)
S3method(list_documents,cosmos_container)
S3method(list_stored_procedures,cosmos_container)
S3method(print,cosmos_attachment)
S3method(print,cosmos_container)
S3method(print,cosmos_database)
S3method(print,cosmos_document)
S3method(print,cosmos_endpoint)
S3method(print,cosmos_stored_procedure)
S3method(process_cosmos_response,list)
S3method(process_cosmos_response,response)
export(call_cosmos_endpoint)
export(call_stored_procedure)
export(cosmos_endpoint)
export(create_attachment)
export(create_container)
export(create_database)
export(create_document)
export(create_stored_procedure)
export(delete_attachment)
export(delete_container)
export(delete_database)
export(delete_document)
export(delete_stored_procedure)
export(do_cosmos_op)
export(download_attachment)
export(get_container)
@ -48,5 +58,6 @@ export(get_document)
export(list_attachments)
export(list_databases)
export(list_documents)
export(list_stored_procedures)
export(process_cosmos_response)
export(query_documents)

Просмотреть файл

@ -9,8 +9,8 @@ list_attachments.cosmos_document <- function(document, ...)
{
res <- do_cosmos_op(document, "attachments", "attachments", "", ...)
atts <- if(inherits(res, "response"))
process_cosmos_response(res)$Attachments
else lapply(process_cosmos_response(res), `[[`, "Attachments")
process_cosmos_response(res, ...)$Attachments
else unlist(lapply(process_cosmos_response(res, ...), `[[`, "Attachments"), recursive=FALSE)
lapply(atts, as_attachment, document=document)
}

Просмотреть файл

@ -11,7 +11,7 @@ get_document.cosmos_container <- function(container, id, partition_key, metadata
headers$`x-ms-documentdb-partitionkey` <- jsonlite::toJSON(partition_key)
res <- do_cosmos_op(container, file.path("docs", id), "docs", file.path("docs", id), headers=headers, ...)
doc <- process_cosmos_response(res, simplify=FALSE)
doc <- process_cosmos_response(res, simplifyDataFrame=FALSE, ...)
if(!metadata)
doc[c("id", "_rid", "_self", "_etag", "_attachments", "_ts")] <- NULL
as_document(doc, container)
@ -43,7 +43,7 @@ create_document.cosmos_container <- function(container, data, headers=list(), ..
data <- jsonlite::toJSON(data, auto_unbox=TRUE, null="null")
res <- do_cosmos_op(container, "docs", "docs", "", headers=headers, body=data, http_verb="POST", ...)
obj <- process_cosmos_response(res)
obj <- process_cosmos_response(res, ...)
invisible(as_document(obj, container))
}

91
R/sproc.R Normal file
Просмотреть файл

@ -0,0 +1,91 @@
#' @export
list_stored_procedures <- function(container, ...)
{
UseMethod("list_stored_procedures")
}
#' @export
list_stored_procedures.cosmos_container <- function(container, ...)
{
res <- do_cosmos_op(container, "sprocs", "sprocs", "", ...)
atts <- if(inherits(res, "response"))
process_cosmos_response(res, ...)$StoredProcedures
else unlist(lapply(process_cosmos_response(res, ...), `[[`, "StoredProcedures"), recursive=FALSE)
lapply(atts, as_stored_procedure, container=container)
}
#' @export
create_stored_procedure <- function(container, ...)
{
UseMethod("create_stored_procedure")
}
#' @export
create_stored_procedure.cosmos_container <- function(container, name, body, ...)
{
body <- list(id=name, body=body)
res <- do_cosmos_op(container, "sprocs", "sprocs", "", body=body, encode="json", http_verb="POST", ...)
sproc <- process_cosmos_response(res, ...)
invisible(as_stored_procedure(sproc, container))
}
#' @export
call_stored_procedure <- function(container, ...)
{
UseMethod("call_stored_procedure")
}
#' @export
call_stored_procedure.cosmos_container <- function(container, name, parameters=list(), ...)
{
path <- file.path("sprocs", name)
res <- do_cosmos_op(container, path, "sprocs", path, body=parameters, encode="json", http_verb="POST", ...)
process_cosmos_response(res, ...)
}
#' @export
call_stored_procedure.cosmos_stored_procedure <- function(container, ...)
{
call_stored_procedure(container$container, container$id, ...)
}
#' @export
delete_stored_procedure <- function(container, ...)
{
UseMethod("delete_stored_procedure")
}
#' @export
delete_stored_procedure.cosmos_container <- function(container, name, confirm=TRUE, ...)
{
if(!delete_confirmed(confirm, id, "stored procedure"))
return(invisible(NULL))
path <- file.path("sprocs", name)
res <- do_cosmos_op(container, path, "attachments", path, http_verb="DELETE", ...)
invisible(process_cosmos_response(res, ...))
}
#' @export
delete_stored_procedure.cosmos_stored_procedure <- function(container, ...)
{
delete_stored_procedure(container$container, container$id, ...)
}
#' @export
print.cosmos_stored_procedure <- function(x, ...)
{
cat("Cosmos DB SQL stored procedure '", x$id, "'\n", sep="")
invisible(x)
}
as_stored_procedure <- function(sproc, container)
{
sproc$container <- container
structure(sproc, class="cosmos_stored_procedure")
}

53
inst/srcjs/bulkDelete.js Normal file
Просмотреть файл

@ -0,0 +1,53 @@
// source: https://azurecosmosdb.github.io/labs/dotnet/labs/07-transactions-with-continuation.html
function bulkDelete(query) {
var container = getContext().getCollection();
var containerLink = container.getSelfLink();
var response = getContext().getResponse();
var responseBody = {
deleted: 0,
continuation: true
};
if (!query) throw new Error("The query is undefined or null.");
tryQueryAndDelete();
function tryQueryAndDelete(continuation) {
var requestOptions = { continuation: continuation };
var isAccepted = container.queryDocuments(
containerLink,
query,
requestOptions,
function (err, retrievedDocs, responseOptions) {
if (err) throw err;
if (retrievedDocs.length > 0) {
tryDelete(retrievedDocs);
} else if (responseOptions.continuation) {
tryQueryAndDelete(responseOptions.continuation);
} else {
responseBody.continuation = false;
response.setBody(responseBody);
}
}
);
if (!isAccepted) {
response.setBody(responseBody);
}
}
function tryDelete(documents) {
if (documents.length > 0) {
var isAccepted = container.deleteDocument(
documents[0]._self,
{},
function (err, responseOptions) {
if (err) throw err;
responseBody.deleted++;
documents.shift();
tryDelete(documents);
}
);
if (!isAccepted) {
response.setBody(responseBody);
}
} else {
tryQueryAndDelete();
}
}
}

33
inst/srcjs/bulkUpload.js Normal file
Просмотреть файл

@ -0,0 +1,33 @@
// source: https://azurecosmosdb.github.io/labs/dotnet/labs/07-transactions-with-continuation.html
function bulkUpload(docs) {
var container = getContext().getCollection();
var containerLink = container.getSelfLink();
var count = 0;
if (!docs) throw new Error("The array is undefined or null.");
var docsLength = docs.length;
if (docsLength == 0) {
getContext()
.getResponse()
.setBody(0);
return;
}
tryCreate(docs[count], callback);
function tryCreate(doc, callback) {
var isAccepted = container.createDocument(containerLink, doc, callback);
if (!isAccepted)
getContext()
.getResponse()
.setBody(count);
}
function callback(err, doc, options) {
if (err) throw err;
count++;
if (count >= docsLength) {
getContext()
.getResponse()
.setBody(count);
} else {
tryCreate(docs[count], callback);
}
}
}