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: Add hashmap support for redis #300

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
69 changes: 69 additions & 0 deletions src/db/kv/index/hashmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use redis::AsyncCommands;

use crate::{db::connectors::redis::get_redis_conn, types::DynError};

/// puts multiple values into a hashmap in Redis
///
/// # Arguments
///
/// * `prefix` - A string slice that represents the prefix for the Redis key.
/// * `key` - A string slice that represents the key under which the value is stored.
/// * `values` - A vector of tuples containing the field and value to be stored.
///
/// # Errors
///
/// Returns an error if the operation fails.
pub async fn put_index_hashmap(
prefix: &str,
key: &str,
values: &[(&str, &str)],
) -> Result<(), DynError> {
let mut conn = get_redis_conn().await?;
let index_key = format!("{}:{}", prefix, key);
let _: () = conn.hset_multiple(index_key, values).await?;
Ok(())
}

/// gets a value from a hashmap in Redis
///
/// # Arguments
///
/// * `prefix` - A string slice that represents the prefix for the Redis key.
/// * `key` - A string slice that represents the key under which the value is stored.
/// * `field` - A string slice that represents the field under which the value is stored.
///
/// # Errors
///
/// Returns an error if the operation fails.
///
/// # Returns
///
/// Returns an Option containing the value if it exists, or None if it does not.
pub async fn get_index_hashmap(
prefix: &str,
key: &str,
field: &str,
) -> Result<Option<String>, DynError> {
let mut conn = get_redis_conn().await?;
let index_key = format!("{}:{}", prefix, key);
let value: Option<String> = conn.hget(index_key, field).await?;
Ok(value)
}

/// deletes a value from a hashmap in Redis
///
/// # Arguments
///
/// * `prefix` - A string slice that represents the prefix for the Redis key.
/// * `key` - A string slice that represents the key under which the value is stored.
/// * `field` - A string slice that represents the field under which the value is stored.
///
/// # Errors
///
/// Returns an error if the operation fails.
pub async fn del_index_hashmap(prefix: &str, key: &str, field: &str) -> Result<(), DynError> {
let mut conn = get_redis_conn().await?;
let index_key = format!("{}:{}", prefix, key);
let _: () = conn.hdel(index_key, field).await?;
Ok(())
}
1 change: 1 addition & 0 deletions src/db/kv/index/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Module for redis Indexing operations split into modules by Redis types
pub mod hashmap;
pub mod json;
pub mod lists;
pub mod sets;
Expand Down
66 changes: 66 additions & 0 deletions src/db/kv/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,4 +679,70 @@ pub trait RedisOps: Serialize + DeserializeOwned + Send + Sync {
let key = key_parts.join(":");
sorted_sets::get_lex_range("Sorted", &key, min, max, skip, limit).await
}

/// Puts multiple values into a hashmap in Redis.
///
/// # Arguments
///
/// * `key_parts` - A slice of string slices used to form the key under which the value is stored.
/// * `values` - A vector of tuples containing the field and value to be stored.
///
/// # Errors
///
/// Returns an error if the operation fails.
///
/// # Returns
///
/// Returns a `Result` indicating success or failure.
async fn put_index_hashmap(
key_parts: &[&str],
values: &[(&str, &str)],
) -> Result<(), DynError> {
let prefix = Self::prefix().await;
let key = key_parts.join(":");
hashmap::put_index_hashmap(&prefix, &key, values).await
}

/// Gets a value from a hashmap in Redis.
///
/// # Arguments
///
/// * `key_parts` - A slice of string slices used to form the key under which the value is stored.
/// * `field` - A string slice representing the field to retrieve from the hashmap.
///
/// # Returns
///
/// Returns an `Option<String>` containing the value if it exists, or `None` if the field does not exist.
///
/// # Errors
///
/// Returns an error if the operation fails.
async fn try_from_index_hashmap(
key_parts: &[&str],
field: &str,
) -> Result<Option<String>, DynError> {
let prefix = Self::prefix().await;
let key = key_parts.join(":");
hashmap::get_index_hashmap(&prefix, &key, field).await
}

/// Deletes a field from a hashmap in Redis.
///
/// # Arguments
///
/// * `key_parts` - A slice of string slices used to form the key under which the value is stored.
/// * `field` - A string slice representing the field to delete from the hashmap.
///
/// # Returns
///
/// Returns a `Result` indicating success or failure.
///
/// # Errors
///
/// Returns an error if the operation fails.
async fn remove_from_index_hashmap(key_parts: &[&str], field: &str) -> Result<(), DynError> {
let prefix = Self::prefix().await;
let key = key_parts.join(":");
hashmap::del_index_hashmap(&prefix, &key, field).await
}
}
Loading