iterator: small changes

This commit is contained in:
nym21
2025-02-20 13:11:23 +01:00
parent f0d86f2392
commit 66ecd2fcf8
7 changed files with 23 additions and 24 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::Path;
use crate::path_to_modified_time;
@@ -9,7 +9,7 @@ pub struct BlkMetadata {
}
impl BlkMetadata {
pub fn new(index: usize, path: &PathBuf) -> Self {
pub fn new(index: usize, path: &Path) -> Self {
Self {
index,
modified_time: path_to_modified_time(path),
+2 -2
View File
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::Path;
use bitcoin::{hashes::Hash, BlockHash};
use serde::{Deserialize, Serialize};
@@ -29,7 +29,7 @@ impl BlkRecap {
}
}
pub fn has_different_modified_time(&self, blk_path: &PathBuf) -> bool {
pub fn has_different_modified_time(&self, blk_path: &Path) -> bool {
if self.modified_time != path_to_modified_time(blk_path) {
dbg!(self.modified_time, path_to_modified_time(blk_path));
}
+3
View File
@@ -8,6 +8,7 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub enum Error {
IO(io::Error),
#[cfg(feature = "zerocopy")]
ZeroCopyError,
}
@@ -17,6 +18,7 @@ impl From<io::Error> for Error {
}
}
#[cfg(feature = "zerocopy")]
impl<A, B> From<zerocopy::error::SizeError<A, B>> for Error {
fn from(_: zerocopy::error::SizeError<A, B>) -> Self {
Self::ZeroCopyError
@@ -27,6 +29,7 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::IO(error) => Debug::fmt(&error, f),
#[cfg(feature = "zerocopy")]
Error::ZeroCopyError => write!(f, "Zero copy convert error"),
}
}
+11 -13
View File
@@ -1,18 +1,15 @@
use std::{
fmt::{self, Debug},
fs, io,
ops::{Add, AddAssign, Rem, Sub},
path::Path,
};
use derive_deref::{Deref, DerefMut};
use serde::{Deserialize, Serialize};
#[cfg(feature = "zerocopy")]
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{
rpc::{self, RpcApi},
Error,
};
use crate::rpc::{self, RpcApi};
#[derive(Debug, Clone, Copy, Deref, DerefMut, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes, KnownLayout))]
@@ -21,8 +18,9 @@ pub struct Height(u32);
impl Height {
const ZERO: Self = Height(0);
pub fn write(&self, path: &Path) -> Result<(), io::Error> {
fs::write(path, self.as_bytes())
#[cfg(feature = "zerocopy")]
pub fn write(&self, path: &std::path::Path) -> Result<(), std::io::Error> {
std::fs::write(path, self.as_bytes())
}
pub fn increment(&mut self) {
@@ -166,16 +164,16 @@ impl From<Height> for bitcoin::locktime::absolute::Height {
}
#[cfg(feature = "zerocopy")]
impl TryFrom<&Path> for Height {
type Error = Error;
fn try_from(value: &Path) -> Result<Self, Self::Error> {
Ok(Self::read_from_bytes(fs::read(value)?.as_slice())?.to_owned())
impl TryFrom<&std::path::Path> for Height {
type Error = crate::Error;
fn try_from(value: &std::path::Path) -> Result<Self, Self::Error> {
Ok(Self::read_from_bytes(std::fs::read(value)?.as_slice())?.to_owned())
}
}
#[cfg(feature = "fjall")]
impl TryFrom<fjall::Slice> for Height {
type Error = Error;
type Error = crate::Error;
fn try_from(value: fjall::Slice) -> Result<Self, Self::Error> {
Ok(Self::read_from_bytes(&value)?)
}
+2 -4
View File
@@ -38,7 +38,7 @@ use utils::*;
pub const NUMBER_OF_UNSAFE_BLOCKS: usize = 1000;
const MAGIC_BYTES: [u8; 4] = [249, 190, 180, 217];
const BOUND_CAP: usize = 210;
const BOUND_CAP: usize = 100;
///
/// Returns a crossbeam channel receiver that receives `(usize, Block, BlockHash)` tuples (with `usize` being the height) in sequential order.
@@ -99,7 +99,7 @@ pub fn new(
.iter()
.filter(|(blk_index, _)| **blk_index >= starting_blk_index)
.try_for_each(move |(blk_index, blk_path)| {
let blk_metadata = BlkMetadata::new(*blk_index, blk_path);
let blk_metadata = BlkMetadata::new(*blk_index, blk_path.as_path());
let blk_bytes = fs::read(blk_path).unwrap();
let blk_bytes_len = blk_bytes.len() as u64;
@@ -224,8 +224,6 @@ pub fn new(
tuple: BlkMetadataAndBlock| {
let mut tuple = Some(tuple);
println!("{} {} {}", recent_hashes.len(), recent_chain.len(), future_blocks.len(),);
while let Some(tuple) = tuple.take().or_else(|| future_blocks.remove(prev_hash)) {
let hash = tuple.block.block_hash();
+1 -1
View File
@@ -14,7 +14,7 @@ fn main() {
.unwrap(),
));
let start = Some(460_001_u32.into());
let start = None;
let end = None;
biterator::new(data_dir, start, end, rpc)
+2 -2
View File
@@ -1,6 +1,6 @@
use std::{fs, path::PathBuf, time::UNIX_EPOCH};
use std::{fs, path::Path, time::UNIX_EPOCH};
pub fn path_to_modified_time(path: &PathBuf) -> u64 {
pub fn path_to_modified_time(path: &Path) -> u64 {
fs::metadata(path)
.unwrap()
.modified()