Split out storage_entry

This commit is contained in:
Andrew Morris
2023-10-30 10:04:18 +11:00
parent 884bcad017
commit 7ac3e65ed3
4 changed files with 40 additions and 31 deletions

View File

@@ -5,6 +5,7 @@ mod rc_key;
mod serde_rc;
mod sled_backend;
mod storage_backend;
mod storage_entry;
mod storage_ops;
mod storage_ptr;
mod storage_val;

View File

@@ -0,0 +1,37 @@
use std::rc::Rc;
use serde::{Deserialize, Serialize};
use crate::{
storage_ptr::StorageEntryPtr,
storage_val::{StorageCompoundVal, StorageVal},
};
#[derive(Serialize, Deserialize)]
pub struct StorageEntry {
pub(crate) ref_count: u64,
pub(crate) refs: Vec<StorageEntryPtr>,
pub(crate) data: Vec<u8>,
}
impl StorageEntry {
pub fn move_to_val(self) -> StorageVal {
let Self {
ref_count: _,
refs,
data,
} = self;
let mut val = bincode::deserialize::<StorageVal>(&data).unwrap();
if let StorageVal::Compound(compound) = &mut val {
match Rc::get_mut(compound).expect("Should be single ref") {
StorageCompoundVal::Array(arr) => {
arr.refs = refs;
}
}
};
val
}
}

View File

@@ -2,7 +2,7 @@ use std::hash::Hash;
use rand::{rngs::ThreadRng, Rng};
use crate::storage_val::StorageEntry;
use crate::storage_entry::StorageEntry;
#[derive(serde::Serialize, serde::Deserialize, Hash, PartialEq, Eq)]
pub struct StoragePtr<T> {

View File

@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::rc_key::RcKey;
use crate::serde_rc::{deserialize_rc, serialize_rc};
use crate::storage_entry::StorageEntry;
use crate::storage_ops::StorageOps;
use crate::storage_ptr::StorageEntryPtr;
@@ -189,36 +190,6 @@ impl StorageVal {
}
}
#[derive(Serialize, Deserialize)]
pub struct StorageEntry {
pub(crate) ref_count: u64,
pub(crate) refs: Vec<StorageEntryPtr>,
data: Vec<u8>,
}
impl StorageEntry {
pub fn move_to_val(self) -> StorageVal {
let Self {
ref_count: _,
refs,
data,
} = self;
let mut val = bincode::deserialize::<StorageVal>(&data).unwrap();
if let StorageVal::Compound(compound) = &mut val {
match Rc::get_mut(compound).expect("Should be single ref") {
StorageCompoundVal::Array(arr) => {
arr.refs = refs;
}
}
};
val
}
}
fn cache_and_store<E, SO: StorageOps<E>>(
tx: &mut SO,
val: &StorageVal,