From 1fd4785619d61071465231b7f8109f21924e82da Mon Sep 17 00:00:00 2001 From: eitsupi Date: Fri, 1 Dec 2023 11:26:08 +0000 Subject: [PATCH] refactor!: RThreadHandle -> RPolarsRThreadHandle --- NAMESPACE | 10 ++++---- R/after-wrappers.R | 2 +- R/extendr-wrappers.R | 12 ++++----- R/lazyframe__lazy.R | 4 +-- R/rbackground.R | 25 +++++++++---------- R/zzz.R | 2 +- man/LazyFrame_collect_in_background.Rd | 4 +-- man/RThreadHandle_RThreadHandle_class.Rd | 7 +++--- ...d => as.character.RPolarsRThreadHandle.Rd} | 6 ++--- ...> dot-DollarNames.RPolarsRThreadHandle.Rd} | 8 +++--- ...andle.Rd => print.RPolarsRThreadHandle.Rd} | 6 ++--- src/rust/src/lazy/dataframe.rs | 4 +-- src/rust/src/rbackground.rs | 14 +++++------ 13 files changed, 51 insertions(+), 53 deletions(-) rename man/{as.character.RThreadHandle.Rd => as.character.RPolarsRThreadHandle.Rd} (66%) rename man/{dot-DollarNames.RThreadHandle.Rd => dot-DollarNames.RPolarsRThreadHandle.Rd} (63%) rename man/{print.RThreadHandle.Rd => print.RPolarsRThreadHandle.Rd} (78%) diff --git a/NAMESPACE b/NAMESPACE index d9b5597b7..b039be7bd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/R/after-wrappers.R b/R/after-wrappers.R index 0811b1cc3..c753e8dbe 100644 --- a/R/after-wrappers.R +++ b/R/after-wrappers.R @@ -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) diff --git a/R/extendr-wrappers.R b/R/extendr-wrappers.R index a661a9659..e24f521e6 100644 --- a/R/extendr-wrappers.R +++ b/R/extendr-wrappers.R @@ -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()) diff --git a/R/lazyframe__lazy.R b/R/lazyframe__lazy.R index 2d044f51a..81bcb1f4e 100644 --- a/R/lazyframe__lazy.R +++ b/R/lazyframe__lazy.R @@ -418,8 +418,8 @@ LazyFrame_collect = function( #' #' @details #' This function immediately returns an [RThreadHandle][RThreadHandle_RThreadHandle_class]. -#' Use [`$is_finished()`][RThreadHandle_is_finished] to see if done. -#' Use [`$join()`][RThreadHandle_join] to wait and get the final result. +#' Use [`$is_finished()`][RThreadHandle_is_finished] to see if done. +#' Use [`$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 #' [`$map_batches()`][Expr_map_batches] or diff --git a/R/rbackground.R b/R/rbackground.R index c77f3e2f3..d10bb507c 100644 --- a/R/rbackground.R +++ b/R/rbackground.R @@ -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), "()") } @@ -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") } @@ -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 #' [`$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: @@ -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 @@ -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() } @@ -93,5 +92,5 @@ RThreadHandle_join = function() { #' @return trinary value: `TRUE` if finished, `FALSE` if not, and `NULL` if the handle was exhausted #' with [`$join()`][RThreadHandle_join]. RThreadHandle_is_finished = function() { - .pr$RThreadHandle$is_finished(self) |> unwrap_or(NULL) + .pr$RPolarsRThreadHandle$is_finished(self) |> unwrap_or(NULL) } diff --git a/R/zzz.R b/R/zzz.R index 5bc81829e..1739214c4 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -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_") diff --git a/man/LazyFrame_collect_in_background.Rd b/man/LazyFrame_collect_in_background.Rd index 9d58f5dc5..38aae32a7 100644 --- a/man/LazyFrame_collect_in_background.Rd +++ b/man/LazyFrame_collect_in_background.Rd @@ -15,8 +15,8 @@ a detached thread. This can also be used via \verb{$collect(collect_in_backgroun } \details{ This function immediately returns an \link[=RThreadHandle_RThreadHandle_class]{RThreadHandle}. -Use \code{\link[=RThreadHandle_is_finished]{$is_finished()}} to see if done. -Use \code{\link[=RThreadHandle_join]{$join()}} to wait and get the final result. +Use \code{\link[=RThreadHandle_is_finished]{$is_finished()}} to see if done. +Use \code{\link[=RThreadHandle_join]{$join()}} to wait and get the final result. It is useful to not block the R session while query executes. If you use \code{\link[=Expr_map_batches]{$map_batches()}} or diff --git a/man/RThreadHandle_RThreadHandle_class.Rd b/man/RThreadHandle_RThreadHandle_class.Rd index 3e9096392..ba09a6aad 100644 --- a/man/RThreadHandle_RThreadHandle_class.Rd +++ b/man/RThreadHandle_RThreadHandle_class.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/rbackground.R \name{RThreadHandle_RThreadHandle_class} \alias{RThreadHandle_RThreadHandle_class} -\title{The RThreadHandle class} +\title{The RPolarsRThreadHandle class} \value{ see methods: \code{\link[=RThreadHandle_is_finished]{is_finished()}} @@ -13,8 +13,8 @@ A handle to some polars query running in a background thread. } \details{ \code{\link[=LazyFrame_collect_in_background]{$collect_in_background()}} will execute a polars -query detached from the R session and return an \code{RThreadHandle} immediately. This -\code{RThreadHandle}-class has the methods \code{\link[=RThreadHandle_is_finished]{is_finished()}} and +query detached from the R session and return an \code{RPolarsRThreadHandle} immediately. This +\code{RPolarsRThreadHandle}-class has the methods \code{\link[=RThreadHandle_is_finished]{is_finished()}} and \code{\link[=RThreadHandle_join]{join()}}. NOTICE: @@ -36,7 +36,6 @@ handle = pl$LazyFrame(mtcars)$with_columns(prexpr)$collect_in_background() if (!handle$is_finished()) print("not done yet") df = handle$join() # get result df - } \seealso{ \code{\link[=LazyFrame_collect_in_background]{$collect_in_background()}} diff --git a/man/as.character.RThreadHandle.Rd b/man/as.character.RPolarsRThreadHandle.Rd similarity index 66% rename from man/as.character.RThreadHandle.Rd rename to man/as.character.RPolarsRThreadHandle.Rd index b300aff89..060e1660f 100644 --- a/man/as.character.RThreadHandle.Rd +++ b/man/as.character.RPolarsRThreadHandle.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/rbackground.R -\name{as.character.RThreadHandle} -\alias{as.character.RThreadHandle} +\name{as.character.RPolarsRThreadHandle} +\alias{as.character.RPolarsRThreadHandle} \title{RThreadHandle to string} \usage{ -\method{as.character}{RThreadHandle}(x, ...) +\method{as.character}{RPolarsRThreadHandle}(x, ...) } \arguments{ \item{x}{RThreadHandle} diff --git a/man/dot-DollarNames.RThreadHandle.Rd b/man/dot-DollarNames.RPolarsRThreadHandle.Rd similarity index 63% rename from man/dot-DollarNames.RThreadHandle.Rd rename to man/dot-DollarNames.RPolarsRThreadHandle.Rd index 7bddb9a40..e1b08bd9d 100644 --- a/man/dot-DollarNames.RThreadHandle.Rd +++ b/man/dot-DollarNames.RPolarsRThreadHandle.Rd @@ -1,13 +1,13 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/rbackground.R -\name{.DollarNames.RThreadHandle} -\alias{.DollarNames.RThreadHandle} +\name{.DollarNames.RPolarsRThreadHandle} +\alias{.DollarNames.RPolarsRThreadHandle} \title{auto complete $-access into a polars object} \usage{ -\method{.DollarNames}{RThreadHandle}(x, pattern = "") +\method{.DollarNames}{RPolarsRThreadHandle}(x, pattern = "") } \arguments{ -\item{x}{RThreadHandle} +\item{x}{RPolarsRThreadHandle} \item{pattern}{code-stump as string to auto-complete} } diff --git a/man/print.RThreadHandle.Rd b/man/print.RPolarsRThreadHandle.Rd similarity index 78% rename from man/print.RThreadHandle.Rd rename to man/print.RPolarsRThreadHandle.Rd index 956c546e1..17950b5d8 100644 --- a/man/print.RThreadHandle.Rd +++ b/man/print.RPolarsRThreadHandle.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/rbackground.R -\name{print.RThreadHandle} -\alias{print.RThreadHandle} +\name{print.RPolarsRThreadHandle} +\alias{print.RPolarsRThreadHandle} \title{s3 method print RThreadHandle} \usage{ -\method{print}{RThreadHandle}(x, ...) +\method{print}{RPolarsRThreadHandle}(x, ...) } \arguments{ \item{x}{RThreadHandle} diff --git a/src/rust/src/lazy/dataframe.rs b/src/rust/src/lazy/dataframe.rs index 43ae79fde..5df2047e8 100644 --- a/src/rust/src/lazy/dataframe.rs +++ b/src/rust/src/lazy/dataframe.rs @@ -71,10 +71,10 @@ impl RPolarsLazyFrame { collect_with_r_func_support(self.clone().0) } - pub fn collect_in_background(&self) -> crate::rbackground::RThreadHandle> { + pub fn collect_in_background(&self) -> crate::rbackground::RPolarsRThreadHandle> { use crate::rbackground::*; let dup = self.clone(); - RThreadHandle::new(move || { + RPolarsRThreadHandle::new(move || { Ok(RDF::from( dup.0 .collect() diff --git a/src/rust/src/rbackground.rs b/src/rust/src/rbackground.rs index cfc9e4621..5166c8baf 100644 --- a/src/rust/src/rbackground.rs +++ b/src/rust/src/rbackground.rs @@ -16,13 +16,13 @@ use std::collections::VecDeque; use std::sync::{Arc, Mutex}; use std::thread; #[derive(Debug)] -pub struct RThreadHandle { +pub struct RPolarsRThreadHandle { handle: Option>, } -impl RThreadHandle { +impl RPolarsRThreadHandle { pub fn new(compute: impl FnOnce() -> T + Send + 'static) -> Self { - RThreadHandle { + RPolarsRThreadHandle { handle: Some(thread::spawn(compute)), } } @@ -59,7 +59,7 @@ impl RThreadHandle { } #[extendr] -impl RThreadHandle> { +impl RPolarsRThreadHandle> { fn join(&mut self) -> RResult { // Could use *.flatten() when it's stable self.join_generic().and_then(std::convert::identity) @@ -553,8 +553,8 @@ pub fn test_rbackgroundhandler(lambda: Robj, arg: Robj) -> RResult { } #[extendr] -pub fn test_rthreadhandle() -> RThreadHandle> { - RThreadHandle::new(move || { +pub fn test_rthreadhandle() -> RPolarsRThreadHandle> { + RPolarsRThreadHandle::new(move || { println!("Intense sleeping in Rust for 10 seconds!"); let duration = std::time::Duration::from_millis(10000); thread::sleep(duration); @@ -588,7 +588,7 @@ pub fn test_serde_df(df: &RPolarsDataFrame) -> RResult { extendr_module! { mod rbackground; - impl RThreadHandle>; + impl RPolarsRThreadHandle>; fn setup_renv; fn set_global_rpool_cap; fn get_global_rpool_cap;