|
1 | 1 | //! Header storage operations for DiskStorageManager. |
2 | 2 |
|
| 3 | +use std::collections::HashMap; |
| 4 | +use std::fs; |
3 | 5 | use std::ops::Range; |
| 6 | +use std::path::Path; |
4 | 7 | use std::time::Instant; |
5 | 8 |
|
6 | 9 | use dashcore::block::Header as BlockHeader; |
7 | 10 | use dashcore::BlockHash; |
8 | 11 |
|
9 | 12 | use crate::error::StorageResult; |
10 | 13 | use crate::storage::disk::segments::SegmentCache; |
| 14 | +use crate::StorageError; |
11 | 15 |
|
12 | 16 | use super::manager::DiskStorageManager; |
13 | 17 | use super::segments::{create_sentinel_header, SegmentState}; |
@@ -450,6 +454,41 @@ impl DiskStorageManager { |
450 | 454 | } |
451 | 455 | } |
452 | 456 |
|
| 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 | + |
453 | 492 | /// Ensure a segment is loaded in memory. |
454 | 493 | pub(super) async fn ensure_segment_loaded( |
455 | 494 | manager: &DiskStorageManager, |
|
0 commit comments