feat: add unique constraint restriction for KV (#3723)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Kartikeya Hegde
2024-02-26 13:37:05 +00:00
committed by GitHub
parent 9aabb14a60
commit c117f8ec63
6 changed files with 126 additions and 10 deletions

View File

@ -15,11 +15,11 @@ use common_utils::{
};
use error_stack::{IntoReport, ResultExt};
use fred::{
interfaces::{HashesInterface, KeysInterface, StreamsInterface},
interfaces::{HashesInterface, KeysInterface, SetsInterface, StreamsInterface},
prelude::RedisErrorKind,
types::{
Expiration, FromRedis, MultipleIDs, MultipleKeys, MultipleOrderedPairs, MultipleStrings,
RedisKey, RedisMap, RedisValue, Scanner, SetOptions, XCap, XReadResponse,
MultipleValues, RedisKey, RedisMap, RedisValue, Scanner, SetOptions, XCap, XReadResponse,
},
};
use futures::StreamExt;
@ -27,7 +27,7 @@ use router_env::{instrument, logger, tracing};
use crate::{
errors,
types::{DelReply, HsetnxReply, MsetnxReply, RedisEntryId, SetnxReply},
types::{DelReply, HsetnxReply, MsetnxReply, RedisEntryId, SaddReply, SetnxReply},
};
impl super::RedisConnectionPool {
@ -466,6 +466,23 @@ impl super::RedisConnectionPool {
.change_context(errors::RedisError::JsonDeserializationFailed)
}
#[instrument(level = "DEBUG", skip(self))]
pub async fn sadd<V>(
&self,
key: &str,
members: V,
) -> CustomResult<SaddReply, errors::RedisError>
where
V: TryInto<MultipleValues> + Debug + Send,
V::Error: Into<fred::error::RedisError> + Send,
{
self.pool
.sadd(key, members)
.await
.into_report()
.change_context(errors::RedisError::SetAddMembersFailed)
}
#[instrument(level = "DEBUG", skip(self))]
pub async fn stream_append_entry<F>(
&self,

View File

@ -50,6 +50,8 @@ pub enum RedisError {
SetHashFailed,
#[error("Failed to set hash field in Redis")]
SetHashFieldFailed,
#[error("Failed to add members to set in Redis")]
SetAddMembersFailed,
#[error("Failed to get hash field in Redis")]
GetHashFieldFailed,
#[error("The requested value was not found in Redis")]

View File

@ -255,3 +255,22 @@ impl fred::types::FromRedis for DelReply {
}
}
}
#[derive(Debug)]
pub enum SaddReply {
KeySet,
KeyNotSet,
}
impl fred::types::FromRedis for SaddReply {
fn from_value(value: fred::types::RedisValue) -> Result<Self, fred::error::RedisError> {
match value {
fred::types::RedisValue::Integer(1) => Ok(Self::KeySet),
fred::types::RedisValue::Integer(0) => Ok(Self::KeyNotSet),
_ => Err(fred::error::RedisError::new(
fred::error::RedisErrorKind::Unknown,
"Unexpected sadd command reply",
)),
}
}
}