Added commands for working with the plugin cache. (#12576)

# Description
This pull request provides three new commands:
`polars store-ls` - moved from `polars ls`. It provides the list of all
object stored in the plugin cache
`polars store-rm` - deletes a cached object
`polars store-get` - gets an object from the cache. 

The addition of `polars store-get` required adding a reference_count to
cached entries. `polars get` is the only command that will increment
this value. `polars rm` will remove the value despite it's count. Calls
to PolarsPlugin::custom_value_dropped will decrement the value.

The prefix store- was chosen due to there already being a `polars cache`
command. These commands were not made sub-commands as there isn't a way
to display help for sub commands in plugins (e.g. `polars store`
displaying help) and I felt the store- seemed fine anyways.

The output of `polars store-ls` now shows the reference count for each
object.

# User-Facing Changes
polars ls has now moved to polars store-ls

---------

Co-authored-by: Jack Wright <jack.wright@disqo.com>
This commit is contained in:
Jack Wright
2024-04-21 17:43:43 -07:00
committed by GitHub
parent aad3ac11da
commit a60381a932
19 changed files with 324 additions and 50 deletions

View File

@ -1,123 +0,0 @@
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
use nu_protocol::{
record, Category, Example, IntoPipelineData, LabeledError, PipelineData, Signature, Value,
};
use crate::{values::PolarsPluginObject, PolarsPlugin};
#[derive(Clone)]
pub struct ListDF;
impl PluginCommand for ListDF {
type Plugin = PolarsPlugin;
fn name(&self) -> &str {
"polars ls"
}
fn usage(&self) -> &str {
"Lists stored dataframes."
}
fn signature(&self) -> Signature {
Signature::build(self.name()).category(Category::Custom("dataframe".into()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Creates a new dataframe and shows it in the dataframe list",
example: r#"let test = ([[a b];[1 2] [3 4]] | dfr into-df);
polars ls"#,
result: None,
}]
}
fn run(
&self,
plugin: &Self::Plugin,
engine: &EngineInterface,
call: &EvaluatedCall,
_input: PipelineData,
) -> Result<PipelineData, LabeledError> {
let vals = plugin.cache.process_entries(|(key, value)| {
let span_contents = engine.get_span_contents(value.span)?;
let span_contents = String::from_utf8_lossy(&span_contents);
match &value.value {
PolarsPluginObject::NuDataFrame(df) => Ok(Some(Value::record(
record! {
"key" => Value::string(key.to_string(), call.head),
"created" => Value::date(value.created, call.head),
"columns" => Value::int(df.as_ref().width() as i64, call.head),
"rows" => Value::int(df.as_ref().height() as i64, call.head),
"type" => Value::string("NuDataFrame", call.head),
"estimated_size" => Value::filesize(df.to_polars().estimated_size() as i64, call.head),
"span_contents" => Value::string(span_contents, value.span),
"span_start" => Value::int(value.span.start as i64, call.head),
"span_end" => Value::int(value.span.end as i64, call.head),
},
call.head,
))),
PolarsPluginObject::NuLazyFrame(lf) => {
let lf = lf.clone().collect(call.head)?;
Ok(Some(Value::record(
record! {
"key" => Value::string(key.to_string(), call.head),
"created" => Value::date(value.created, call.head),
"columns" => Value::int(lf.as_ref().width() as i64, call.head),
"rows" => Value::int(lf.as_ref().height() as i64, call.head),
"type" => Value::string("NuLazyFrame", call.head),
"estimated_size" => Value::filesize(lf.to_polars().estimated_size() as i64, call.head),
"span_contents" => Value::string(span_contents, value.span),
"span_start" => Value::int(value.span.start as i64, call.head),
"span_end" => Value::int(value.span.end as i64, call.head),
},
call.head,
)))
}
PolarsPluginObject::NuExpression(_) => Ok(Some(Value::record(
record! {
"key" => Value::string(key.to_string(), call.head),
"created" => Value::date(value.created, call.head),
"columns" => Value::nothing(call.head),
"rows" => Value::nothing(call.head),
"type" => Value::string("NuExpression", call.head),
"estimated_size" => Value::nothing(call.head),
"span_contents" => Value::string(span_contents, value.span),
"span_start" => Value::int(value.span.start as i64, call.head),
"span_end" => Value::int(value.span.end as i64, call.head),
},
call.head,
))),
PolarsPluginObject::NuLazyGroupBy(_) => Ok(Some(Value::record(
record! {
"key" => Value::string(key.to_string(), call.head),
"columns" => Value::nothing(call.head),
"rows" => Value::nothing(call.head),
"type" => Value::string("NuLazyGroupBy", call.head),
"estimated_size" => Value::nothing(call.head),
"span_contents" => Value::string(span_contents, call.head),
"span_start" => Value::int(call.head.start as i64, call.head),
"span_end" => Value::int(call.head.end as i64, call.head),
},
call.head,
))),
PolarsPluginObject::NuWhen(_) => Ok(Some(Value::record(
record! {
"key" => Value::string(key.to_string(), call.head),
"columns" => Value::nothing(call.head),
"rows" => Value::nothing(call.head),
"type" => Value::string("NuWhen", call.head),
"estimated_size" => Value::nothing(call.head),
"span_contents" => Value::string(span_contents.to_string(), call.head),
"span_start" => Value::int(call.head.start as i64, call.head),
"span_end" => Value::int(call.head.end as i64, call.head),
},
call.head,
))),
}
})?;
let vals = vals.into_iter().flatten().collect();
let list = Value::list(vals, call.head);
Ok(list.into_pipeline_data())
}
}

View File

@ -9,7 +9,6 @@ mod filter_with;
mod first;
mod get;
mod last;
mod list;
mod melt;
mod open;
mod query_df;
@ -45,7 +44,6 @@ pub use filter_with::FilterWith;
pub use first::FirstDF;
pub use get::GetDF;
pub use last::LastDF;
pub use list::ListDF;
pub use melt::MeltDF;
use nu_plugin::PluginCommand;
pub use query_df::QueryDf;
@ -82,7 +80,6 @@ pub(crate) fn eager_commands() -> Vec<Box<dyn PluginCommand<Plugin = PolarsPlugi
Box::new(Summary),
Box::new(FirstDF),
Box::new(LastDF),
Box::new(ListDF),
Box::new(RenameDF),
Box::new(SampleDF),
Box::new(ShapeDF),

View File

@ -186,10 +186,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&AsDateTime)
test_polars_plugin_command_with_decls(&AsDateTime, vec![Box::new(IntoDatetime)])
}
}

View File

@ -93,10 +93,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetDay)
test_polars_plugin_command_with_decls(&GetDay, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetHour)
test_polars_plugin_command_with_decls(&GetHour, vec![Box::new(IntoDatetime)])
}
}

View File

@ -83,10 +83,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetMinute)
test_polars_plugin_command_with_decls(&GetMinute, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetMonth)
test_polars_plugin_command_with_decls(&GetMonth, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetNanosecond)
test_polars_plugin_command_with_decls(&GetNanosecond, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetOrdinal)
test_polars_plugin_command_with_decls(&GetOrdinal, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetSecond)
test_polars_plugin_command_with_decls(&GetSecond, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetWeek)
test_polars_plugin_command_with_decls(&GetWeek, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetWeekDay)
test_polars_plugin_command_with_decls(&GetWeekDay, vec![Box::new(IntoDatetime)])
}
}

View File

@ -85,10 +85,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&GetYear)
test_polars_plugin_command_with_decls(&GetYear, vec![Box::new(IntoDatetime)])
}
}

View File

@ -104,10 +104,11 @@ fn command(
#[cfg(test)]
mod test {
use super::*;
use crate::test::test_polars_plugin_command;
use crate::test::test_polars_plugin_command_with_decls;
use nu_command::IntoDatetime;
#[test]
fn test_examples() -> Result<(), ShellError> {
test_polars_plugin_command(&StrFTime)
test_polars_plugin_command_with_decls(&StrFTime, vec![Box::new(IntoDatetime)])
}
}

View File

@ -107,6 +107,16 @@ impl PolarsPluginObject {
PolarsPluginObject::NuWhen(w) => w.id,
}
}
pub fn into_value(self, span: Span) -> Value {
match self {
PolarsPluginObject::NuDataFrame(df) => df.into_value(span),
PolarsPluginObject::NuLazyFrame(lf) => lf.into_value(span),
PolarsPluginObject::NuExpression(e) => e.into_value(span),
PolarsPluginObject::NuLazyGroupBy(lg) => lg.into_value(span),
PolarsPluginObject::NuWhen(w) => w.into_value(span),
}
}
}
#[derive(Debug, Clone)]