From 455cec2079a2afa9b411932bb82be18510feb816 Mon Sep 17 00:00:00 2001 From: Matthew Iannucci Date: Tue, 10 Dec 2024 10:31:54 -0500 Subject: [PATCH] Serializable store, session, and repo --- icechunk/src/lib.rs | 1 + icechunk/src/session.rs | 2 ++ icechunk/src/store.rs | 6 ++++-- icechunk/src/utils/mod.rs | 1 + icechunk/src/utils/serde.rs | 23 +++++++++++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 icechunk/src/utils/mod.rs create mode 100644 icechunk/src/utils/serde.rs diff --git a/icechunk/src/lib.rs b/icechunk/src/lib.rs index 6206856f..ef734f8e 100644 --- a/icechunk/src/lib.rs +++ b/icechunk/src/lib.rs @@ -30,6 +30,7 @@ pub mod storage; pub mod store; #[cfg(test)] pub mod strategies; +pub mod utils; pub mod zarr; pub use repo::RepositoryConfig; diff --git a/icechunk/src/session.rs b/icechunk/src/session.rs index b5fb359d..6c2cda9a 100644 --- a/icechunk/src/session.rs +++ b/icechunk/src/session.rs @@ -9,6 +9,7 @@ use std::{ use bytes::Bytes; use chrono::Utc; use futures::{FutureExt, Stream, StreamExt, TryStreamExt}; +use serde::{Deserialize, Serialize}; use thiserror::Error; use crate::{ @@ -46,6 +47,7 @@ pub enum SessionError { pub type SessionResult = Result; +#[derive(Serialize, Deserialize)] pub struct Session { config: RepositoryConfig, storage: Arc, diff --git a/icechunk/src/store.rs b/icechunk/src/store.rs index dea901d2..4459f0ab 100644 --- a/icechunk/src/store.rs +++ b/icechunk/src/store.rs @@ -26,7 +26,8 @@ use crate::{ }, metadata::{ ArrayShape, ChunkKeyEncoding, ChunkShape, Codec, DataType, DimensionNames, FillValue, StorageTransformer, UserAttributes, - }, refs::RefError, repository::{get_chunk, ChunkPayload, RepositoryError, ZarrArrayMetadata}, session::{Session, SessionError} + }, refs::RefError, repository::{get_chunk, ChunkPayload, RepositoryError, ZarrArrayMetadata}, session::{Session, SessionError}, + utils::serde::arc_rwlock_serde, }; #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -99,8 +100,9 @@ impl Default for StoreOptions { } } - +#[derive(Serialize, Deserialize)] pub struct Store { + #[serde(with = "arc_rwlock_serde")] session: Arc>, config: StoreOptions, read_only: bool, diff --git a/icechunk/src/utils/mod.rs b/icechunk/src/utils/mod.rs new file mode 100644 index 00000000..82b2d094 --- /dev/null +++ b/icechunk/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod serde; diff --git a/icechunk/src/utils/serde.rs b/icechunk/src/utils/serde.rs new file mode 100644 index 00000000..3d196573 --- /dev/null +++ b/icechunk/src/utils/serde.rs @@ -0,0 +1,23 @@ +pub mod arc_rwlock_serde { + use serde::de::Deserializer; + use serde::ser::Serializer; + use serde::{Deserialize, Serialize}; + use std::sync::Arc; + use tokio::sync::RwLock; + + pub fn serialize(val: &Arc>, s: S) -> Result + where + S: Serializer, + T: Serialize, + { + T::serialize(&*val.blocking_read(), s) + } + + pub fn deserialize<'de, D, T>(d: D) -> Result>, D::Error> + where + D: Deserializer<'de>, + T: Deserialize<'de>, + { + Ok(Arc::new(RwLock::new(T::deserialize(d)?))) + } +}