Skip to content

Commit

Permalink
refactor!: RThreadHandle -> RPolarsRThreadHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Dec 1, 2023
1 parent ed4fa4b commit 1fd4785
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 53 deletions.
10 changes: 5 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ S3method("$",RPolarsErr)
S3method("$",RPolarsExpr)
S3method("$",RPolarsLazyFrame)
S3method("$",RPolarsLazyGroupBy)
S3method("$",RPolarsRThreadHandle)
S3method("$",RPolarsSQLContext)
S3method("$",RPolarsSeries)
S3method("$",RPolarsStringCacheHolder)
S3method("$",RPolarsThen)
S3method("$",RPolarsWhen)
S3method("$",RThreadHandle)
S3method("$",VecDataFrame)
S3method("$",pl_polars_env)
S3method("$",private_polars_env)
Expand Down Expand Up @@ -76,12 +76,12 @@ S3method("[[",RPolarsErr)
S3method("[[",RPolarsExpr)
S3method("[[",RPolarsLazyFrame)
S3method("[[",RPolarsLazyGroupBy)
S3method("[[",RPolarsRThreadHandle)
S3method("[[",RPolarsSQLContext)
S3method("[[",RPolarsSeries)
S3method("[[",RPolarsStringCacheHolder)
S3method("[[",RPolarsThen)
S3method("[[",RPolarsWhen)
S3method("[[",RThreadHandle)
S3method("[[",VecDataFrame)
S3method("^",RPolarsExpr)
S3method("|",RPolarsExpr)
Expand All @@ -93,16 +93,16 @@ S3method(.DollarNames,RPolarsDataFrame)
S3method(.DollarNames,RPolarsErr)
S3method(.DollarNames,RPolarsExpr)
S3method(.DollarNames,RPolarsLazyFrame)
S3method(.DollarNames,RPolarsRThreadHandle)
S3method(.DollarNames,RPolarsSQLContext)
S3method(.DollarNames,RPolarsSeries)
S3method(.DollarNames,RPolarsThen)
S3method(.DollarNames,RPolarsWhen)
S3method(.DollarNames,RThreadHandle)
S3method(.DollarNames,VecDataFrame)
S3method(.DollarNames,method_environment)
S3method(as.character,RPolarsErr)
S3method(as.character,RPolarsRThreadHandle)
S3method(as.character,RPolarsSeries)
S3method(as.character,RThreadHandle)
S3method(as.data.frame,RPolarsDataFrame)
S3method(as.data.frame,RPolarsLazyFrame)
S3method(as.list,RPolarsExpr)
Expand Down Expand Up @@ -161,10 +161,10 @@ S3method(print,RPolarsErr)
S3method(print,RPolarsExpr)
S3method(print,RPolarsLazyFrame)
S3method(print,RPolarsLazyGroupBy)
S3method(print,RPolarsRThreadHandle)
S3method(print,RPolarsSeries)
S3method(print,RPolarsThen)
S3method(print,RPolarsWhen)
S3method(print,RThreadHandle)
S3method(print,polars_info)
S3method(row.names,RPolarsDataFrame)
S3method(sum,RPolarsDataFrame)
Expand Down
2 changes: 1 addition & 1 deletion R/after-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extendr_method_to_pure_functions = function(env, class_name = NULL) {
.pr$VecDataFrame = extendr_method_to_pure_functions(VecDataFrame)
.pr$RNullValues = extendr_method_to_pure_functions(RNullValues)
.pr$RPolarsErr = extendr_method_to_pure_functions(RPolarsErr)
.pr$RThreadHandle = extendr_method_to_pure_functions(RThreadHandle)
.pr$RPolarsRThreadHandle = extendr_method_to_pure_functions(RPolarsRThreadHandle)
.pr$RPolarsStringCacheHolder = extendr_method_to_pure_functions(RPolarsStringCacheHolder)
.pr$RPolarsSQLContext = extendr_method_to_pure_functions(RPolarsSQLContext)

Expand Down
12 changes: 6 additions & 6 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,19 @@ RPolarsErr$when <- function(s) .Call(wrap__RPolarsErr__when, self, s)
#' @export
`[[.RPolarsErr` <- `$.RPolarsErr`

RThreadHandle <- new.env(parent = emptyenv())
RPolarsRThreadHandle <- new.env(parent = emptyenv())

RThreadHandle$join <- function() .Call(wrap__RThreadHandle__join, self)
RPolarsRThreadHandle$join <- function() .Call(wrap__RPolarsRThreadHandle__join, self)

RThreadHandle$is_finished <- function() .Call(wrap__RThreadHandle__is_finished, self)
RPolarsRThreadHandle$is_finished <- function() .Call(wrap__RPolarsRThreadHandle__is_finished, self)

RThreadHandle$thread_description <- function() .Call(wrap__RThreadHandle__thread_description, self)
RPolarsRThreadHandle$thread_description <- function() .Call(wrap__RPolarsRThreadHandle__thread_description, self)

#' @export
`$.RThreadHandle` <- function (self, name) { func <- RThreadHandle[[name]]; environment(func) <- environment(); func }
`$.RPolarsRThreadHandle` <- function (self, name) { func <- RPolarsRThreadHandle[[name]]; environment(func) <- environment(); func }

#' @export
`[[.RThreadHandle` <- `$.RThreadHandle`
`[[.RPolarsRThreadHandle` <- `$.RPolarsRThreadHandle`

RPolarsWhen <- new.env(parent = emptyenv())

Expand Down
4 changes: 2 additions & 2 deletions R/lazyframe__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ LazyFrame_collect = function(
#'
#' @details
#' This function immediately returns an [RThreadHandle][RThreadHandle_RThreadHandle_class].
#' Use [`<RThreadHandle>$is_finished()`][RThreadHandle_is_finished] to see if done.
#' Use [`<RThreadHandle>$join()`][RThreadHandle_join] to wait and get the final result.
#' Use [`<RPolarsRThreadHandle>$is_finished()`][RThreadHandle_is_finished] to see if done.
#' Use [`<RPolarsRThreadHandle>$join()`][RThreadHandle_join] to wait and get the final result.
#'
#' It is useful to not block the R session while query executes. If you use
#' [`<Expr>$map_batches()`][Expr_map_batches] or
Expand Down
25 changes: 12 additions & 13 deletions R/rbackground.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#' @title auto complete $-access into a polars object
#' @description called by the interactive R session internally
#' @param x RThreadHandle
#' @param x RPolarsRThreadHandle
#' @param pattern code-stump as string to auto-complete
#' @export
#' @keywords internal
.DollarNames.RThreadHandle = function(x, pattern = "") {
paste0(ls(RThreadHandle, pattern = pattern), "()")
.DollarNames.RPolarsRThreadHandle = function(x, pattern = "") {
paste0(ls(RPolarsRThreadHandle, pattern = pattern), "()")
}


Expand All @@ -15,8 +15,8 @@
#' @param ... not used
#' @export
#' @keywords internal
as.character.RThreadHandle = function(x, ...) {
.pr$RThreadHandle$thread_description(x) |>
as.character.RPolarsRThreadHandle = function(x, ...) {
.pr$RPolarsRThreadHandle$thread_description(x) |>
unwrap_or("An exhausted RThreadHandle")
}

Expand All @@ -34,16 +34,16 @@ as.character.RThreadHandle = function(x, ...) {
#' print(handle)
#' handle$join()
#' print(handle)
print.RThreadHandle = function(x, ...) as.character(x) |> cat("\n")
print.RPolarsRThreadHandle = function(x, ...) as.character(x) |> cat("\n")


#' @title The RThreadHandle class
#' @title The RPolarsRThreadHandle class
#' @name RThreadHandle_RThreadHandle_class
#' @description A handle to some polars query running in a background thread.
#' @details
#' [`<LazyFrame>$collect_in_background()`][LazyFrame_collect_in_background] will execute a polars
#' query detached from the R session and return an `RThreadHandle` immediately. This
#' `RThreadHandle`-class has the methods [`is_finished()`][RThreadHandle_is_finished] and
#' query detached from the R session and return an `RPolarsRThreadHandle` immediately. This
#' `RPolarsRThreadHandle`-class has the methods [`is_finished()`][RThreadHandle_is_finished] and
#' [`join()`][RThreadHandle_join].
#'
#' NOTICE:
Expand Down Expand Up @@ -72,8 +72,7 @@ print.RThreadHandle = function(x, ...) as.character(x) |> cat("\n")
#' if (!handle$is_finished()) print("not done yet")
#' df = handle$join() # get result
#' df
#'
RThreadHandle
NULL

#' Join a RThreadHandle
#' @keywords RThreadHandle
Expand All @@ -84,7 +83,7 @@ RThreadHandle
#' @return return value from background thread
#' @seealso [RThreadHandle_class][RThreadHandle_RThreadHandle_class]
RThreadHandle_join = function() {
.pr$RThreadHandle$join(self) |> unwrap()
.pr$RPolarsRThreadHandle$join(self) |> unwrap()
}


Expand All @@ -93,5 +92,5 @@ RThreadHandle_join = function() {
#' @return trinary value: `TRUE` if finished, `FALSE` if not, and `NULL` if the handle was exhausted
#' with [`<RThreadHandle>$join()`][RThreadHandle_join].
RThreadHandle_is_finished = function() {
.pr$RThreadHandle$is_finished(self) |> unwrap_or(NULL)
.pr$RPolarsRThreadHandle$is_finished(self) |> unwrap_or(NULL)
}
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ replace_private_with_pub_methods(RField, "^RField_")
replace_private_with_pub_methods(RPolarsSeries, "^Series_")

# RThreadHandle
replace_private_with_pub_methods(RThreadHandle, "^RThreadHandle_")
replace_private_with_pub_methods(RPolarsRThreadHandle, "^RThreadHandle_")

# SQLContext
replace_private_with_pub_methods(RPolarsSQLContext, "^SQLContext_")
Expand Down
4 changes: 2 additions & 2 deletions man/LazyFrame_collect_in_background.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/RThreadHandle_RThreadHandle_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/rust/src/lazy/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ impl RPolarsLazyFrame {
collect_with_r_func_support(self.clone().0)
}

pub fn collect_in_background(&self) -> crate::rbackground::RThreadHandle<RResult<RDF>> {
pub fn collect_in_background(&self) -> crate::rbackground::RPolarsRThreadHandle<RResult<RDF>> {
use crate::rbackground::*;
let dup = self.clone();
RThreadHandle::new(move || {
RPolarsRThreadHandle::new(move || {
Ok(RDF::from(
dup.0
.collect()
Expand Down
14 changes: 7 additions & 7 deletions src/rust/src/rbackground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::thread;
#[derive(Debug)]
pub struct RThreadHandle<T> {
pub struct RPolarsRThreadHandle<T> {
handle: Option<thread::JoinHandle<T>>,
}

impl<T: Send + Sync + 'static> RThreadHandle<T> {
impl<T: Send + Sync + 'static> RPolarsRThreadHandle<T> {
pub fn new(compute: impl FnOnce() -> T + Send + 'static) -> Self {
RThreadHandle {
RPolarsRThreadHandle {
handle: Some(thread::spawn(compute)),
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl<T: Send + Sync + 'static> RThreadHandle<T> {
}

#[extendr]
impl RThreadHandle<RResult<RPolarsDataFrame>> {
impl RPolarsRThreadHandle<RResult<RPolarsDataFrame>> {
fn join(&mut self) -> RResult<RPolarsDataFrame> {
// Could use *.flatten() when it's stable
self.join_generic().and_then(std::convert::identity)
Expand Down Expand Up @@ -553,8 +553,8 @@ pub fn test_rbackgroundhandler(lambda: Robj, arg: Robj) -> RResult<Robj> {
}

#[extendr]
pub fn test_rthreadhandle() -> RThreadHandle<RResult<RPolarsDataFrame>> {
RThreadHandle::new(move || {
pub fn test_rthreadhandle() -> RPolarsRThreadHandle<RResult<RPolarsDataFrame>> {
RPolarsRThreadHandle::new(move || {
println!("Intense sleeping in Rust for 10 seconds!");
let duration = std::time::Duration::from_millis(10000);
thread::sleep(duration);
Expand Down Expand Up @@ -588,7 +588,7 @@ pub fn test_serde_df(df: &RPolarsDataFrame) -> RResult<RPolarsDataFrame> {

extendr_module! {
mod rbackground;
impl RThreadHandle<RResult<RPolarsDataFrame>>;
impl RPolarsRThreadHandle<RResult<RPolarsDataFrame>>;
fn setup_renv;
fn set_global_rpool_cap;
fn get_global_rpool_cap;
Expand Down

0 comments on commit 1fd4785

Please sign in to comment.