Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [Explorer] ReJSON-RL Supports #40

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ redis = { workspace = true, features = [
"tokio-comp",
"tokio-rustls-comp",
"cluster-async",
"json"
] }
deadpool-redis = { git = "https://github.com/honhimW/deadpool.git", branch = "logging", package = "deadpool-redis", features = [
"serde",
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeMap;

pub fn cli() -> Result<Command> {
let command = Command::new("ratisui")
.about("Redis TUI build on Ratatui")
.about("Redis TUI build with Ratatui.")
.args([
arg!(-t --target <TARGET> "named redis target, default read from config file if exist"),
arg!(-T --theme <THEME> "theme configuration file name, under ~/.config/ratisui/theme/<THEME>.ron"),
Expand Down
69 changes: 68 additions & 1 deletion src/redis_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::StreamExt;
use log::{info};
use once_cell::sync::Lazy;
use redis::ConnectionAddr::{Tcp, TcpTls};
use redis::{AsyncCommands, AsyncIter, Client, Cmd, ConnectionAddr, ConnectionInfo, ConnectionLike, FromRedisValue, RedisConnectionInfo, ScanOptions, ToRedisArgs, Value, VerbatimFormat};
use redis::{AsyncCommands, AsyncIter, Client, Cmd, ConnectionAddr, ConnectionInfo, ConnectionLike, FromRedisValue, JsonAsyncCommands, RedisConnectionInfo, ScanOptions, ToRedisArgs, Value, VerbatimFormat};
use std::collections::HashMap;
use std::future::Future;
use std::ops::DerefMut;
Expand Down Expand Up @@ -984,6 +984,73 @@ impl RedisOperations {
}
}

pub async fn json_type<K: ToRedisArgs + Send + Sync>(&self, key: K) -> Result<String> {
if self.is_cluster() {
let pool = &self.cluster_pool.clone().context("should be cluster")?;
let mut connection = pool.get().await?;
let v: Vec<String> = connection.json_type(key, ".").await?;
let s = v.get(0).cloned().unwrap_or_default();
Ok(s)
} else {
let mut connection = self.pool.get().await?;
let v: Vec<String> = connection.json_type(key, ".").await?;
let s = v.get(0).cloned().unwrap_or_default();
Ok(s)
}
}

pub async fn json_strlen<K: ToRedisArgs + Send + Sync>(&self, key: K) -> Result<usize> {
if self.is_cluster() {
let pool = &self.cluster_pool.clone().context("should be cluster")?;
let mut connection = pool.get().await?;
let v: usize = connection.json_str_len(key, ".").await?;
Ok(v)
} else {
let mut connection = self.pool.get().await?;
let v: usize = connection.json_str_len(key, ".").await?;
Ok(v)
}
}

pub async fn json_arrlen<K: ToRedisArgs + Send + Sync>(&self, key: K) -> Result<usize> {
if self.is_cluster() {
let pool = &self.cluster_pool.clone().context("should be cluster")?;
let mut connection = pool.get().await?;
let v: usize = connection.json_arr_len(key, ".").await?;
Ok(v)
} else {
let mut connection = self.pool.get().await?;
let v: usize = connection.json_arr_len(key, ".").await?;
Ok(v)
}
}

pub async fn json_objlen<K: ToRedisArgs + Send + Sync>(&self, key: K) -> Result<usize> {
if self.is_cluster() {
let pool = &self.cluster_pool.clone().context("should be cluster")?;
let mut connection = pool.get().await?;
let v: usize = connection.json_obj_len(key, ".").await?;
Ok(v)
} else {
let mut connection = self.pool.get().await?;
let v: usize = connection.json_obj_len(key, ".").await?;
Ok(v)
}
}

pub async fn json_get<K: ToRedisArgs + Send + Sync, V: FromRedisValue>(&self, key: K) -> Result<V> {
if self.is_cluster() {
let pool = &self.cluster_pool.clone().context("should be cluster")?;
let mut connection = pool.get().await?;
let v: V = connection.json_get(key, ".").await?;
Ok(v)
} else {
let mut connection = self.pool.get().await?;
let v: V = connection.json_get(key, ".").await?;
Ok(v)
}
}

// pub async fn sscan<K: ToRedisArgs + Send + Sync>(&self, key: K) -> Result<Vec<String>> {
// if self.is_cluster() {
// let pool = &self.cluster_pool.clone().context("should be cluster")?;
Expand Down
17 changes: 16 additions & 1 deletion src/tabs/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn get_type_color(key_type: &str) -> Color {
"Set" | "set" => { get_color(|t| &t.tab.explorer.key_type.set) }
"ZSet" | "zset" => { get_color(|t| &t.tab.explorer.key_type.zset) }
"String" | "string" => { get_color(|t| &t.tab.explorer.key_type.string) }
"JSON" | "json" => { get_color(|t| &t.tab.explorer.key_type.json) }
"JSON" | "json" | "ReJSON-RL" | "ReJSON" => { get_color(|t| &t.tab.explorer.key_type.json) }
"Stream" | "stream" => { get_color(|t| &t.tab.explorer.key_type.stream) }
"unknown" => { get_color(|t| &t.tab.explorer.key_type.unknown) }
_ => { Color::default() }
Expand Down Expand Up @@ -889,6 +889,16 @@ impl ExplorerTab {
"set" => Ok(op.scard(key_name_clone).await?),
"zset" => Ok(op.zcard(key_name_clone).await?),
"stream" => Ok(op.xlen(key_name_clone).await?),
"rejson-rl" => {
let json_type = op.json_type(&key_name_clone).await?;
let len = match json_type.to_ascii_lowercase().as_str() {
"object" => op.json_objlen(key_name_clone).await?,
"array" => op.json_arrlen(key_name_clone).await?,
"string" => op.json_strlen(key_name_clone).await?,
_ => 0,
};
Ok(len)
},
_ => Ok(0)
}
}).await;
Expand Down Expand Up @@ -969,6 +979,11 @@ impl ExplorerTab {
}).collect();
data.selected_stream_value = (true, Some(hash_value));
}
"rejson-rl" => {
let bytes: Vec<u8> = op.json_get(key_name_clone).await?;
let result = deserialize_bytes(bytes).context("Failed to deserialize string")?;
data.selected_string_value = (true, Some((result.0, result.1)));
}
_ => {}
}
Ok(data)
Expand Down
Loading