global: from custom unsafe_slice to zerocopy

This commit is contained in:
nym21
2025-02-05 16:42:53 +01:00
parent 138ca80c10
commit d86d614520
43 changed files with 684 additions and 366 deletions
+5 -5
View File
@@ -4,10 +4,10 @@ use fjall::{
PartitionCreateOptions, PersistMode, ReadTransaction, Result, Slice, TransactionalKeyspace,
TransactionalPartitionHandle,
};
use storable_vec::Value;
use unsafe_slice_serde::UnsafeSliceSerde;
use storable_vec::{Value, Version};
use zerocopy::{Immutable, IntoBytes};
use crate::structs::{Height, Version};
use crate::structs::Height;
use super::StoreMeta;
@@ -21,7 +21,7 @@ pub struct Store<Key, Value> {
impl<K, V> Store<K, V>
where
K: Into<Slice> + Ord,
K: Into<Slice> + Ord + Immutable + IntoBytes,
V: Into<Slice> + TryFrom<Slice>,
<V as TryFrom<Slice>>::Error: error::Error + Send + Sync + 'static,
{
@@ -54,7 +54,7 @@ where
pub fn get(&self, key: &K) -> color_eyre::Result<Option<Value<V>>> {
if let Some(v) = self.puts.get(key) {
Ok(Some(Value::Ref(v)))
} else if let Some(slice) = self.rtx.get(&self.part, key.unsafe_as_slice())? {
} else if let Some(slice) = self.rtx.get(&self.part, key.as_bytes())? {
Ok(Some(Value::Owned(V::try_from(slice)?)))
} else {
Ok(None)
+5 -4
View File
@@ -3,9 +3,10 @@ use std::{
path::{Path, PathBuf},
};
use unsafe_slice_serde::UnsafeSliceSerde;
use storable_vec::Version;
use zerocopy::{FromBytes, IntoBytes};
use super::{Height, Version};
use super::Height;
pub struct StoreMeta {
pathbuf: PathBuf,
@@ -87,14 +88,14 @@ impl StoreMeta {
// }
fn read_length_(path: &Path) -> color_eyre::Result<usize> {
Ok(fs::read(Self::path_length(path))
.map(|v| usize::unsafe_try_from_slice(v.as_slice()).cloned().unwrap_or_default())
.map(|v| usize::read_from_bytes(v.as_slice()).unwrap_or_default())
.unwrap_or_default())
}
fn write_length(&self) -> io::Result<()> {
Self::write_length_(&self.pathbuf, self.len)
}
fn write_length_(path: &Path, len: usize) -> Result<(), io::Error> {
fs::write(Self::path_length(path), len.to_le_bytes())
fs::write(Self::path_length(path), len.as_bytes())
}
fn path_length(path: &Path) -> PathBuf {
path.join("length")
+4 -1
View File
@@ -1,9 +1,12 @@
use std::{path::Path, thread};
use crate::{structs::Version, AddressHash, Addressindex, BlockHashPrefix, Height, TxidPrefix, Txindex};
use storable_vec::Version;
use crate::{AddressHash, Addressindex, BlockHashPrefix, Height, TxidPrefix, Txindex};
mod base;
mod meta;
// mod version;
pub use base::*;
pub use meta::*;
+51
View File
@@ -0,0 +1,51 @@
use std::{fs, io, path::Path};
use derive_deref::Deref;
use fjall::Slice;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deref)]
pub struct Version(u32);
impl Version {
pub fn write(&self, path: &Path) -> Result<(), io::Error> {
fs::write(path, self.to_ne_bytes())
}
}
impl From<u32> for Version {
fn from(value: u32) -> Self {
Self(value)
}
}
impl TryFrom<&Path> for Version {
type Error = io::Error;
fn try_from(value: &Path) -> Result<Self, Self::Error> {
Self::try_from(&fs::read(value)?)
}
}
impl TryFrom<Slice> for Version {
type Error = fjall::Error;
fn try_from(value: Slice) -> Result<Self, Self::Error> {
Self::from(&value)
}
}
impl TryFrom<&[u8]> for Version {
type Error = storable_vec::Error;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let mut buf: [u8; 4] = [0; 4];
let buf_len = buf.len();
if value.len() != buf_len {
panic!();
}
value.iter().enumerate().for_each(|(i, r)| {
buf[i] = *r;
});
Ok(Self(u32::from_ne_bytes(buf)))
}
}
impl From<Version> for Slice {
fn from(value: Version) -> Self {
Self::new(&value.to_ne_bytes())
}
}
+3 -3
View File
@@ -34,7 +34,7 @@ where
self.vec.flush()
}
pub fn height(&self) -> color_eyre::Result<Height> {
pub fn height(&self) -> storable_vec::Result<Height> {
Height::try_from(self.path_height().as_path())
}
fn path_height(&self) -> PathBuf {
@@ -66,7 +66,7 @@ impl<I, T, const MODE: u8> DerefMut for StorableVec<I, T, MODE> {
}
pub trait AnyStorableVec {
fn height(&self) -> color_eyre::Result<Height>;
fn height(&self) -> storable_vec::Result<Height>;
fn flush(&mut self, height: Height) -> io::Result<()>;
}
@@ -75,7 +75,7 @@ where
I: StorableVecIndex,
T: StorableVecType,
{
fn height(&self) -> color_eyre::Result<Height> {
fn height(&self) -> storable_vec::Result<Height> {
self.height()
}
+5 -6
View File
@@ -1,14 +1,13 @@
use std::{fs, io, path::Path};
use biter::bitcoin::{self, transaction, BlockHash, Txid, Weight};
use exit::Exit;
use rayon::prelude::*;
use storable_vec::{Version, CACHED_GETS};
use crate::structs::{
Addressbytes, Addressindex, Addresstype, Addresstypeindex, Amount, Height, P2PK33AddressBytes, P2PK65AddressBytes,
P2PKHAddressBytes, P2SHAddressBytes, P2TRAddressBytes, P2WPKHAddressBytes, P2WSHAddressBytes, Timestamp, Txindex,
Txinindex, Txoutindex,
Addressbytes, Addressindex, Addresstype, Addresstypeindex, Amount, BlockHash, Height, LockTime, P2PK33AddressBytes,
P2PK65AddressBytes, P2PKHAddressBytes, P2SHAddressBytes, P2TRAddressBytes, P2WPKHAddressBytes, P2WSHAddressBytes,
Timestamp, TxVersion, Txid, Txindex, Txinindex, Txoutindex, Weight,
};
mod base;
@@ -50,9 +49,9 @@ pub struct StorableVecs<const MODE: u8> {
pub txindex_to_first_txinindex: StorableVec<Txindex, Txinindex, MODE>,
pub txindex_to_first_txoutindex: StorableVec<Txindex, Txoutindex, MODE>,
pub txindex_to_height: StorableVec<Txindex, Height, MODE>,
pub txindex_to_locktime: StorableVec<Txindex, bitcoin::absolute::LockTime, MODE>,
pub txindex_to_locktime: StorableVec<Txindex, LockTime, MODE>,
pub txindex_to_txid: StorableVec<Txindex, Txid, MODE>,
pub txindex_to_txversion: StorableVec<Txindex, transaction::Version, MODE>,
pub txindex_to_txversion: StorableVec<Txindex, TxVersion, MODE>,
pub txinindex_to_txoutindex: StorableVec<Txinindex, Txoutindex, MODE>,
pub txoutindex_to_addressindex: StorableVec<Txoutindex, Addressindex, MODE>,
pub txoutindex_to_amount: StorableVec<Txoutindex, Amount, MODE>,