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

@ -7,7 +7,7 @@ use router_derive::TryGetEnumVariant;
use router_env::logger;
use serde::de;
use crate::{metrics, store::kv::TypedSql, KVRouterStore};
use crate::{metrics, store::kv::TypedSql, KVRouterStore, UniqueConstraints};
pub trait KvStorePartition {
fn partition_number(key: PartitionKey<'_>, num_partitions: u8) -> u32 {
@ -95,7 +95,7 @@ pub async fn kv_wrapper<'a, T, D, S>(
where
T: de::DeserializeOwned,
D: crate::database::store::DatabaseStore,
S: serde::Serialize + Debug + KvStorePartition,
S: serde::Serialize + Debug + KvStorePartition + UniqueConstraints + Sync,
{
let redis_conn = store.get_redis_conn()?;
@ -147,6 +147,8 @@ where
KvOperation::HSetNx(field, value, sql) => {
logger::debug!(kv_operation= %operation, value = ?value);
value.check_for_constraints(&redis_conn).await?;
let result = redis_conn
.serialize_and_set_hash_field_if_not_exist(key, field, value, Some(ttl))
.await?;
@ -168,6 +170,8 @@ where
.serialize_and_set_key_if_not_exist(key, value, Some(ttl.into()))
.await?;
value.check_for_constraints(&redis_conn).await?;
if matches!(result, redis_interface::SetnxReply::KeySet) {
store
.push_to_drainer_stream::<S>(sql, partition_key)