From 8289fa7cdf3cfee44ee927b0a27fd4634b2ad7cf Mon Sep 17 00:00:00 2001 From: Param Arora Date: Thu, 17 Apr 2025 15:07:26 +0530 Subject: [PATCH 1/2] refactor: migrate from Arc<[u8]> to bytes::Bytes --- src/base/value.rs | 26 +++++++++++++------------- src/ops/storages/neo4j.rs | 4 +--- src/ops/storages/postgres.rs | 6 ++++-- src/py/convert.rs | 5 +++-- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/base/value.rs b/src/base/value.rs index 122151c0..98339709 100644 --- a/src/base/value.rs +++ b/src/base/value.rs @@ -1,5 +1,5 @@ use crate::{api_bail, api_error}; - +use bytes::Bytes; use super::schema::*; use anyhow::Result; use base64::prelude::*; @@ -73,7 +73,7 @@ impl<'de> Deserialize<'de> for RangeValue { /// Value of key. #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum KeyValue { - Bytes(Arc<[u8]>), + Bytes(Bytes), Str(Arc), Bool(bool), Int64(i64), @@ -83,15 +83,15 @@ pub enum KeyValue { Struct(Vec), } -impl From> for KeyValue { - fn from(value: Arc<[u8]>) -> Self { +impl From for KeyValue { + fn from(value: Bytes) -> Self { KeyValue::Bytes(value) } } impl From> for KeyValue { fn from(value: Vec) -> Self { - KeyValue::Bytes(Arc::from(value)) + KeyValue::Bytes(Bytes::from(value)) } } @@ -197,7 +197,7 @@ impl KeyValue { .ok_or_else(|| api_error!("Key parts less than expected"))?; match basic_type { BasicValueType::Bytes { .. } => { - KeyValue::Bytes(Arc::from(BASE64_STANDARD.decode(v)?)) + KeyValue::Bytes(Bytes::from(BASE64_STANDARD.decode(v)?)) } BasicValueType::Str { .. } => KeyValue::Str(Arc::from(v)), BasicValueType::Bool => KeyValue::Bool(v.parse()?), @@ -275,7 +275,7 @@ impl KeyValue { } } - pub fn bytes_value(&self) -> Result<&Arc<[u8]>> { + pub fn bytes_value(&self) -> Result<&Bytes> { match self { KeyValue::Bytes(v) => Ok(v), _ => anyhow::bail!("expected bytes value, but got {}", self.kind_str()), @@ -342,7 +342,7 @@ impl KeyValue { #[derive(Debug, Clone)] pub enum BasicValue { - Bytes(Arc<[u8]>), + Bytes(Bytes), Str(Arc), Bool(bool), Int64(i64), @@ -358,15 +358,15 @@ pub enum BasicValue { Vector(Arc<[BasicValue]>), } -impl From> for BasicValue { - fn from(value: Arc<[u8]>) -> Self { +impl From for BasicValue { + fn from(value: Bytes) -> Self { BasicValue::Bytes(value) } } impl From> for BasicValue { fn from(value: Vec) -> Self { - BasicValue::Bytes(Arc::from(value)) + BasicValue::Bytes(Bytes::from(value)) } } @@ -674,7 +674,7 @@ impl Value { } } - pub fn as_bytes(&self) -> Result<&Arc<[u8]>> { + pub fn as_bytes(&self) -> Result<&Bytes> { match self { Value::Basic(BasicValue::Bytes(v)) => Ok(v), _ => anyhow::bail!("expected bytes value, but got {}", self.kind()), @@ -870,7 +870,7 @@ impl BasicValue { pub fn from_json(value: serde_json::Value, schema: &BasicValueType) -> Result { let result = match (value, schema) { (serde_json::Value::String(v), BasicValueType::Bytes { .. }) => { - BasicValue::Bytes(Arc::from(BASE64_STANDARD.decode(v)?)) + BasicValue::Bytes(Bytes::from(BASE64_STANDARD.decode(v)?)) } (serde_json::Value::String(v), BasicValueType::Str { .. }) => { BasicValue::Str(Arc::from(v)) diff --git a/src/ops/storages/neo4j.rs b/src/ops/storages/neo4j.rs index bb582d59..33a218f7 100644 --- a/src/ops/storages/neo4j.rs +++ b/src/ops/storages/neo4j.rs @@ -281,9 +281,7 @@ fn mapped_field_values_to_bolt<'a>( fn basic_value_to_bolt(value: &BasicValue, schema: &BasicValueType) -> Result { let bolt_value = match value { - BasicValue::Bytes(v) => { - BoltType::Bytes(neo4rs::BoltBytes::new(bytes::Bytes::from_owner(v.clone()))) - } + BasicValue::Bytes(v) => BoltType::Bytes(neo4rs::BoltBytes::new(v.clone())), BasicValue::Str(v) => BoltType::String(neo4rs::BoltString::new(v)), BasicValue::Bool(v) => BoltType::Boolean(neo4rs::BoltBoolean::new(*v)), BasicValue::Int64(v) => BoltType::Integer(neo4rs::BoltInteger::new(*v)), diff --git a/src/ops/storages/postgres.rs b/src/ops/storages/postgres.rs index e83439f6..ebbea877 100644 --- a/src/ops/storages/postgres.rs +++ b/src/ops/storages/postgres.rs @@ -17,6 +17,7 @@ use sqlx::postgres::PgRow; use sqlx::{PgPool, Row}; use std::ops::Bound; use uuid::Uuid; +use bytes::Bytes; #[derive(Debug, Deserialize)] pub struct Spec { @@ -175,7 +176,7 @@ fn from_pg_value(row: &PgRow, field_idx: usize, typ: &ValueType) -> Result row .try_get::>, _>(field_idx)? - .map(|v| BasicValue::Bytes(Arc::from(v))), + .map(|v| BasicValue::Bytes(Bytes::from(v))), BasicValueType::Str => row .try_get::, _>(field_idx)? .map(|v| BasicValue::Str(Arc::from(v))), @@ -855,7 +856,8 @@ impl setup::ResourceSetupStatusCheck for SetupStatusCheck { TableUpsertionAction::Create { keys, values } => { let mut fields = (keys .iter() - .map(|(k, v)| format!("{} {} NOT NULL", k, to_column_type_sql(v)))) + .map(|(k, v)| format!("{} {} NOT NULL", k, to_column_type_sql(v))) + ) .chain( values .iter() diff --git a/src/py/convert.rs b/src/py/convert.rs index 45f882b7..ad54e73f 100644 --- a/src/py/convert.rs +++ b/src/py/convert.rs @@ -6,7 +6,8 @@ use serde::de::DeserializeOwned; use serde::Serialize; use std::collections::BTreeMap; use std::ops::Deref; -use std::sync::Arc; +use std::sync::Arc; // Added missing Arc import +use bytes::Bytes; use super::IntoPyResult; use crate::base::{schema, value}; @@ -123,7 +124,7 @@ fn basic_value_from_py_object<'py>( ) -> PyResult { let result = match typ { schema::BasicValueType::Bytes => { - value::BasicValue::Bytes(Arc::from(v.extract::>()?)) + value::BasicValue::Bytes(Bytes::from(v.extract::>()?)) } schema::BasicValueType::Str => value::BasicValue::Str(Arc::from(v.extract::()?)), schema::BasicValueType::Bool => value::BasicValue::Bool(v.extract::()?), From c58948a81e7cf698b06afa393aee102d1f4df907 Mon Sep 17 00:00:00 2001 From: Param Arora Date: Fri, 18 Apr 2025 15:37:19 +0530 Subject: [PATCH 2/2] remove comment --- src/py/convert.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/py/convert.rs b/src/py/convert.rs index ad54e73f..afd01245 100644 --- a/src/py/convert.rs +++ b/src/py/convert.rs @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned; use serde::Serialize; use std::collections::BTreeMap; use std::ops::Deref; -use std::sync::Arc; // Added missing Arc import +use std::sync::Arc; use bytes::Bytes; use super::IntoPyResult;