Skip to content

Commit 56e700f

Browse files
authored
Refactor: migrate from Arc<[u8]> to bytes::Bytes (#330)
* refactor: migrate from Arc<[u8]> to bytes::Bytes * remove comment
1 parent 0654716 commit 56e700f

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

src/base/value.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{api_bail, api_error};
2-
2+
use bytes::Bytes;
33
use super::schema::*;
44
use anyhow::Result;
55
use base64::prelude::*;
@@ -73,7 +73,7 @@ impl<'de> Deserialize<'de> for RangeValue {
7373
/// Value of key.
7474
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
7575
pub enum KeyValue {
76-
Bytes(Arc<[u8]>),
76+
Bytes(Bytes),
7777
Str(Arc<str>),
7878
Bool(bool),
7979
Int64(i64),
@@ -83,15 +83,15 @@ pub enum KeyValue {
8383
Struct(Vec<KeyValue>),
8484
}
8585

86-
impl From<Arc<[u8]>> for KeyValue {
87-
fn from(value: Arc<[u8]>) -> Self {
86+
impl From<Bytes> for KeyValue {
87+
fn from(value: Bytes) -> Self {
8888
KeyValue::Bytes(value)
8989
}
9090
}
9191

9292
impl From<Vec<u8>> for KeyValue {
9393
fn from(value: Vec<u8>) -> Self {
94-
KeyValue::Bytes(Arc::from(value))
94+
KeyValue::Bytes(Bytes::from(value))
9595
}
9696
}
9797

@@ -197,7 +197,7 @@ impl KeyValue {
197197
.ok_or_else(|| api_error!("Key parts less than expected"))?;
198198
match basic_type {
199199
BasicValueType::Bytes { .. } => {
200-
KeyValue::Bytes(Arc::from(BASE64_STANDARD.decode(v)?))
200+
KeyValue::Bytes(Bytes::from(BASE64_STANDARD.decode(v)?))
201201
}
202202
BasicValueType::Str { .. } => KeyValue::Str(Arc::from(v)),
203203
BasicValueType::Bool => KeyValue::Bool(v.parse()?),
@@ -275,7 +275,7 @@ impl KeyValue {
275275
}
276276
}
277277

278-
pub fn bytes_value(&self) -> Result<&Arc<[u8]>> {
278+
pub fn bytes_value(&self) -> Result<&Bytes> {
279279
match self {
280280
KeyValue::Bytes(v) => Ok(v),
281281
_ => anyhow::bail!("expected bytes value, but got {}", self.kind_str()),
@@ -342,7 +342,7 @@ impl KeyValue {
342342

343343
#[derive(Debug, Clone)]
344344
pub enum BasicValue {
345-
Bytes(Arc<[u8]>),
345+
Bytes(Bytes),
346346
Str(Arc<str>),
347347
Bool(bool),
348348
Int64(i64),
@@ -358,15 +358,15 @@ pub enum BasicValue {
358358
Vector(Arc<[BasicValue]>),
359359
}
360360

361-
impl From<Arc<[u8]>> for BasicValue {
362-
fn from(value: Arc<[u8]>) -> Self {
361+
impl From<Bytes> for BasicValue {
362+
fn from(value: Bytes) -> Self {
363363
BasicValue::Bytes(value)
364364
}
365365
}
366366

367367
impl From<Vec<u8>> for BasicValue {
368368
fn from(value: Vec<u8>) -> Self {
369-
BasicValue::Bytes(Arc::from(value))
369+
BasicValue::Bytes(Bytes::from(value))
370370
}
371371
}
372372

@@ -674,7 +674,7 @@ impl<VS> Value<VS> {
674674
}
675675
}
676676

677-
pub fn as_bytes(&self) -> Result<&Arc<[u8]>> {
677+
pub fn as_bytes(&self) -> Result<&Bytes> {
678678
match self {
679679
Value::Basic(BasicValue::Bytes(v)) => Ok(v),
680680
_ => anyhow::bail!("expected bytes value, but got {}", self.kind()),
@@ -870,7 +870,7 @@ impl BasicValue {
870870
pub fn from_json(value: serde_json::Value, schema: &BasicValueType) -> Result<Self> {
871871
let result = match (value, schema) {
872872
(serde_json::Value::String(v), BasicValueType::Bytes { .. }) => {
873-
BasicValue::Bytes(Arc::from(BASE64_STANDARD.decode(v)?))
873+
BasicValue::Bytes(Bytes::from(BASE64_STANDARD.decode(v)?))
874874
}
875875
(serde_json::Value::String(v), BasicValueType::Str { .. }) => {
876876
BasicValue::Str(Arc::from(v))

src/ops/storages/neo4j.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,7 @@ fn mapped_field_values_to_bolt<'a>(
283283

284284
fn basic_value_to_bolt(value: &BasicValue, schema: &BasicValueType) -> Result<BoltType> {
285285
let bolt_value = match value {
286-
BasicValue::Bytes(v) => {
287-
BoltType::Bytes(neo4rs::BoltBytes::new(bytes::Bytes::from_owner(v.clone())))
288-
}
286+
BasicValue::Bytes(v) => BoltType::Bytes(neo4rs::BoltBytes::new(v.clone())),
289287
BasicValue::Str(v) => BoltType::String(neo4rs::BoltString::new(v)),
290288
BasicValue::Bool(v) => BoltType::Boolean(neo4rs::BoltBoolean::new(*v)),
291289
BasicValue::Int64(v) => BoltType::Integer(neo4rs::BoltInteger::new(*v)),

src/ops/storages/postgres.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use sqlx::postgres::PgRow;
1717
use sqlx::{PgPool, Row};
1818
use std::ops::Bound;
1919
use uuid::Uuid;
20+
use bytes::Bytes;
2021

2122
#[derive(Debug, Deserialize)]
2223
pub struct Spec {
@@ -175,7 +176,7 @@ fn from_pg_value(row: &PgRow, field_idx: usize, typ: &ValueType) -> Result<Value
175176
let basic_value = match basic_type {
176177
BasicValueType::Bytes => row
177178
.try_get::<Option<Vec<u8>>, _>(field_idx)?
178-
.map(|v| BasicValue::Bytes(Arc::from(v))),
179+
.map(|v| BasicValue::Bytes(Bytes::from(v))),
179180
BasicValueType::Str => row
180181
.try_get::<Option<String>, _>(field_idx)?
181182
.map(|v| BasicValue::Str(Arc::from(v))),
@@ -855,7 +856,8 @@ impl setup::ResourceSetupStatusCheck for SetupStatusCheck {
855856
TableUpsertionAction::Create { keys, values } => {
856857
let mut fields = (keys
857858
.iter()
858-
.map(|(k, v)| format!("{} {} NOT NULL", k, to_column_type_sql(v))))
859+
.map(|(k, v)| format!("{} {} NOT NULL", k, to_column_type_sql(v)))
860+
)
859861
.chain(
860862
values
861863
.iter()

src/py/convert.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use serde::de::DeserializeOwned;
66
use serde::Serialize;
77
use std::collections::BTreeMap;
88
use std::ops::Deref;
9-
use std::sync::Arc;
9+
use std::sync::Arc;
10+
use bytes::Bytes;
1011

1112
use super::IntoPyResult;
1213
use crate::base::{schema, value};
@@ -123,7 +124,7 @@ fn basic_value_from_py_object<'py>(
123124
) -> PyResult<value::BasicValue> {
124125
let result = match typ {
125126
schema::BasicValueType::Bytes => {
126-
value::BasicValue::Bytes(Arc::from(v.extract::<Vec<u8>>()?))
127+
value::BasicValue::Bytes(Bytes::from(v.extract::<Vec<u8>>()?))
127128
}
128129
schema::BasicValueType::Str => value::BasicValue::Str(Arc::from(v.extract::<String>()?)),
129130
schema::BasicValueType::Bool => value::BasicValue::Bool(v.extract::<bool>()?),

0 commit comments

Comments
 (0)