Skip to content

Commit 9925172

Browse files
committed
removed io module inside storage/disk
1 parent 0b5c3f0 commit 9925172

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

dash-spv/src/storage/disk/headers.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
//! Header storage operations for DiskStorageManager.
22
3+
use std::collections::HashMap;
4+
use std::fs;
35
use std::ops::Range;
6+
use std::path::Path;
47
use std::time::Instant;
58

69
use dashcore::block::Header as BlockHeader;
710
use dashcore::BlockHash;
811

912
use crate::error::StorageResult;
1013
use crate::storage::disk::segments::SegmentCache;
14+
use crate::StorageError;
1115

1216
use super::manager::DiskStorageManager;
1317
use super::segments::{create_sentinel_header, SegmentState};
@@ -450,6 +454,41 @@ impl DiskStorageManager {
450454
}
451455
}
452456

457+
/// Load index from file.
458+
pub(super) async fn load_index_from_file(path: &Path) -> StorageResult<HashMap<BlockHash, u32>> {
459+
tokio::task::spawn_blocking({
460+
let path = path.to_path_buf();
461+
move || {
462+
let content = fs::read(&path)?;
463+
bincode::deserialize(&content).map_err(|e| {
464+
StorageError::ReadFailed(format!("Failed to deserialize index: {}", e))
465+
})
466+
}
467+
})
468+
.await
469+
.map_err(|e| StorageError::ReadFailed(format!("Task join error: {}", e)))?
470+
}
471+
472+
/// Save index to disk.
473+
pub(super) async fn save_index_to_disk(
474+
path: &Path,
475+
index: &HashMap<BlockHash, u32>,
476+
) -> StorageResult<()> {
477+
tokio::task::spawn_blocking({
478+
let path = path.to_path_buf();
479+
let index = index.clone();
480+
move || {
481+
let data = bincode::serialize(&index).map_err(|e| {
482+
StorageError::WriteFailed(format!("Failed to serialize index: {}", e))
483+
})?;
484+
fs::write(&path, data)?;
485+
Ok(())
486+
}
487+
})
488+
.await
489+
.map_err(|e| StorageError::WriteFailed(format!("Task join error: {}", e)))?
490+
}
491+
453492
/// Ensure a segment is loaded in memory.
454493
pub(super) async fn ensure_segment_loaded(
455494
manager: &DiskStorageManager,

dash-spv/src/storage/disk/io.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

dash-spv/src/storage/disk/manager.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ impl DiskStorageManager {
121121

122122
/// Start the background worker and notification channel.
123123
pub(super) async fn start_worker(&mut self) {
124-
use super::io::save_index_to_disk;
125-
126124
let (worker_tx, mut worker_rx) = mpsc::channel::<WorkerCommand>(100);
127125
let (notification_tx, notification_rx) = mpsc::channel::<WorkerNotification>(100);
128126

@@ -165,7 +163,7 @@ impl DiskStorageManager {
165163
index,
166164
} => {
167165
let path = worker_base_path.join("headers/index.dat");
168-
if let Err(e) = save_index_to_disk(&path, &index).await {
166+
if let Err(e) = super::headers::save_index_to_disk(&path, &index).await {
169167
eprintln!("Failed to save index: {}", e);
170168
} else {
171169
tracing::trace!("Background worker completed saving index");
@@ -261,7 +259,7 @@ impl DiskStorageManager {
261259
let index_path = self.base_path.join("headers/index.dat");
262260
let mut index_loaded = false;
263261
if index_path.exists() {
264-
if let Ok(index) = super::io::load_index_from_file(&index_path).await {
262+
if let Ok(index) = super::headers::load_index_from_file(&index_path).await {
265263
*self.header_hash_index.write().await = index;
266264
index_loaded = true;
267265
}

dash-spv/src/storage/disk/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
2222
mod filters;
2323
mod headers;
24-
mod io;
2524
mod manager;
2625
mod segments;
2726
mod state;

0 commit comments

Comments
 (0)