setClass("Broadcast", slots = list(id = "character"))
Broadcast <- function(id, value, jBroadcastRef, objName) {
.broadcastValues[[id]] <- value
.broadcastNames[[as.character(objName)]] <- jBroadcastRef
.broadcastIdToName[[id]] <- as.character(objName)
new("Broadcast", id = id)
}
# @description
# \code{value} can be used to get the value of a broadcast variable inside
# a distributed function.
setMethod("value",
signature(bcast = "Broadcast"),
function(bcast) {
if (exists(bcast@id, envir = .broadcastValues)) {
get(bcast@id, envir = .broadcastValues)
} else {
NULL
}
})
# Internal function to set values of a broadcast variable.
#
# This function is used internally by Spark to set the value of a broadcast
# variable on workers. Not intended for use outside the package.
setBroadcastValue <- function(bcastId, value) {
bcastIdStr <- as.character(bcastId)
.broadcastValues[[bcastIdStr]] <- value
}
# Helper function to clear the list of broadcast variables we know about
# Should be called when the SparkR JVM backend is shutdown
clearBroadcastVariables <- function() {
bcasts <- ls(.broadcastNames)
rm(list = bcasts, envir = .broadcastNames)
}