Migrated MapCluster to Cluster, Began recovery copying.
This commit is contained in:
117
src/mapping.rs
117
src/mapping.rs
@@ -28,9 +28,8 @@ impl Domain {
|
|||||||
|
|
||||||
|
|
||||||
/// A map for data stored in memory for processing and saving to disk.
|
/// A map for data stored in memory for processing and saving to disk.
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
|
||||||
pub struct Cluster {
|
pub struct Cluster {
|
||||||
data: Option<Vec<u8>>,
|
|
||||||
domain: Domain,
|
domain: Domain,
|
||||||
stage: Stage,
|
stage: Stage,
|
||||||
}
|
}
|
||||||
@@ -38,50 +37,24 @@ pub struct Cluster {
|
|||||||
impl Default for Cluster {
|
impl Default for Cluster {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Cluster {
|
Cluster {
|
||||||
data: None,
|
|
||||||
domain: Domain::default(),
|
domain: Domain::default(),
|
||||||
stage: Stage::default()
|
stage: Stage::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Cluster {
|
||||||
/// Map for data stored on disk.
|
|
||||||
/// Rather have a second cluster type than inflating size
|
|
||||||
/// of output map by defining Option::None constantly.
|
|
||||||
#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
|
|
||||||
pub struct MapCluster {
|
|
||||||
pub domain: Domain,
|
|
||||||
pub stage: Stage,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for MapCluster {
|
|
||||||
fn default() -> Self {
|
|
||||||
MapCluster { domain: Domain::default(), stage: Stage::default() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Cluster> for MapCluster {
|
|
||||||
fn from(cluster: Cluster) -> Self {
|
|
||||||
MapCluster {
|
|
||||||
domain: cluster.domain,
|
|
||||||
stage: cluster.stage,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MapCluster {
|
|
||||||
/// Breaks apart into a vec of clusters,
|
/// Breaks apart into a vec of clusters,
|
||||||
/// each of cluster_size, excepting last.
|
/// each of cluster_size, excepting last.
|
||||||
pub fn subdivide(&mut self, cluster_len: usize) -> Vec<MapCluster> {
|
pub fn subdivide(&mut self, cluster_len: usize) -> Vec<Cluster> {
|
||||||
let domain_len = self.domain.len();
|
let domain_len = self.domain.len();
|
||||||
let mut start = self.domain.start;
|
let mut start = self.domain.start;
|
||||||
let mut clusters: Vec<MapCluster> = vec![];
|
let mut clusters: Vec<Cluster> = vec![];
|
||||||
|
|
||||||
for _ in 0..(domain_len as f64 / cluster_len as f64).floor() as usize {
|
for _ in 0..(domain_len as f64 / cluster_len as f64).floor() as usize {
|
||||||
clusters.push(MapCluster {
|
clusters.push(Cluster {
|
||||||
domain: Domain {
|
domain: Domain {
|
||||||
start: start,
|
start,
|
||||||
end: start + cluster_len,
|
end: start + cluster_len,
|
||||||
},
|
},
|
||||||
stage: self.stage,
|
stage: self.stage,
|
||||||
@@ -90,9 +63,9 @@ impl MapCluster {
|
|||||||
start += cluster_len;
|
start += cluster_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
clusters.push(MapCluster {
|
clusters.push(Cluster {
|
||||||
domain: Domain {
|
domain: Domain {
|
||||||
start: start,
|
start,
|
||||||
end: self.domain.end,
|
end: self.domain.end,
|
||||||
},
|
},
|
||||||
stage: self.stage,
|
stage: self.stage,
|
||||||
@@ -126,7 +99,7 @@ impl Default for Stage {
|
|||||||
pub struct MapFile {
|
pub struct MapFile {
|
||||||
pub sector_size: u16,
|
pub sector_size: u16,
|
||||||
pub domain: Domain,
|
pub domain: Domain,
|
||||||
pub map: Vec<MapCluster>,
|
pub map: Vec<Cluster>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<File> for MapFile {
|
impl TryFrom<File> for MapFile {
|
||||||
@@ -142,7 +115,7 @@ impl Default for MapFile {
|
|||||||
MapFile {
|
MapFile {
|
||||||
sector_size: FB_SECTOR_SIZE,
|
sector_size: FB_SECTOR_SIZE,
|
||||||
domain: Domain::default(),
|
domain: Domain::default(),
|
||||||
map: vec![MapCluster {
|
map: vec![Cluster {
|
||||||
domain: Domain::default(),
|
domain: Domain::default(),
|
||||||
stage: Stage::Untested,
|
stage: Stage::Untested,
|
||||||
}],
|
}],
|
||||||
@@ -164,7 +137,7 @@ impl MapFile {
|
|||||||
|
|
||||||
/// Recalculate cluster mappings.
|
/// Recalculate cluster mappings.
|
||||||
fn update(&mut self, new_cluster: Cluster) -> &mut Self {
|
fn update(&mut self, new_cluster: Cluster) -> &mut Self {
|
||||||
let mut new_map: Vec<MapCluster> = vec![MapCluster::from(new_cluster.to_owned())];
|
let mut new_map: Vec<Cluster> = vec![Cluster::from(new_cluster.to_owned())];
|
||||||
|
|
||||||
for map_cluster in self.map.iter() {
|
for map_cluster in self.map.iter() {
|
||||||
let mut map_cluster = *map_cluster;
|
let mut map_cluster = *map_cluster;
|
||||||
@@ -203,7 +176,7 @@ impl MapFile {
|
|||||||
NOTE: Crop completed above.
|
NOTE: Crop completed above.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
new_map.push(MapCluster {
|
new_map.push(Cluster {
|
||||||
domain: Domain {
|
domain: Domain {
|
||||||
start: new_cluster.domain.end,
|
start: new_cluster.domain.end,
|
||||||
end: domain_end,
|
end: domain_end,
|
||||||
@@ -250,7 +223,7 @@ impl MapFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get clusters of common stage.
|
/// Get clusters of common stage.
|
||||||
pub fn get_clusters(&self, stage: Stage) -> Vec<MapCluster> {
|
pub fn get_clusters(&self, stage: Stage) -> Vec<Cluster> {
|
||||||
self.map.iter()
|
self.map.iter()
|
||||||
.filter_map(|mc| {
|
.filter_map(|mc| {
|
||||||
if mc.stage == stage { Some(mc.to_owned()) } else { None }
|
if mc.stage == stage { Some(mc.to_owned()) } else { None }
|
||||||
@@ -262,17 +235,17 @@ impl MapFile {
|
|||||||
/// I.E. check forwards every cluster from current until stage changes,
|
/// I.E. check forwards every cluster from current until stage changes,
|
||||||
/// then group at once.
|
/// then group at once.
|
||||||
fn defrag(&mut self) -> &mut Self {
|
fn defrag(&mut self) -> &mut Self {
|
||||||
let mut new_map: Vec<MapCluster> = vec![];
|
let mut new_map: Vec<Cluster> = vec![];
|
||||||
|
|
||||||
// Fetch first cluster.
|
// Fetch first cluster.
|
||||||
let mut start_cluster = *self.map.iter()
|
let mut start_cluster = self.map.iter()
|
||||||
.find(|c| c.domain.start == 0)
|
.find(|c| c.domain.start == 0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Even though this would be initialized by its first read,
|
// Even though this would be initialized by its first read,
|
||||||
// the compiler won't stop whining, and idk how to assert that to it.
|
// the compiler won't stop whining, and idk how to assert that to it.
|
||||||
let mut end_cluster = MapCluster::default();
|
let mut end_cluster = Cluster::default();
|
||||||
let mut new_cluster: MapCluster;
|
let mut new_cluster: Cluster;
|
||||||
|
|
||||||
let mut stage_common: bool;
|
let mut stage_common: bool;
|
||||||
let mut is_finished = false;
|
let mut is_finished = false;
|
||||||
@@ -282,15 +255,15 @@ impl MapFile {
|
|||||||
|
|
||||||
// Start a new cluster based on the cluster following
|
// Start a new cluster based on the cluster following
|
||||||
// the end of last new_cluster.
|
// the end of last new_cluster.
|
||||||
new_cluster = start_cluster;
|
new_cluster = start_cluster.to_owned();
|
||||||
|
|
||||||
// While stage is common, and not finished,
|
// While stage is common, and not finished,
|
||||||
// find each trailing cluster.
|
// find each trailing cluster.
|
||||||
while stage_common && !is_finished {
|
while stage_common && !is_finished {
|
||||||
end_cluster = start_cluster;
|
end_cluster = start_cluster.to_owned();
|
||||||
|
|
||||||
if end_cluster.domain.end != self.domain.end {
|
if end_cluster.domain.end != self.domain.end {
|
||||||
start_cluster = *self.map.iter()
|
start_cluster = self.map.iter()
|
||||||
.find(|c| end_cluster.domain.end == c.domain.start)
|
.find(|c| end_cluster.domain.end == c.domain.start)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -315,7 +288,7 @@ impl MapFile {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
// Test for MapCluster::subdivide()
|
// Test for Cluster::subdivide()
|
||||||
|
|
||||||
// Test for MapFile::update()
|
// Test for MapFile::update()
|
||||||
|
|
||||||
@@ -345,7 +318,7 @@ mod tests {
|
|||||||
mf.map = vec![];
|
mf.map = vec![];
|
||||||
|
|
||||||
for stage in stages {
|
for stage in stages {
|
||||||
mf.map.push(*MapCluster::default().set_stage(stage));
|
mf.map.push(*Cluster::default().set_stage(stage));
|
||||||
|
|
||||||
mf_stage = mf.get_stage();
|
mf_stage = mf.get_stage();
|
||||||
|
|
||||||
@@ -363,14 +336,14 @@ mod tests {
|
|||||||
let mut mf = MapFile::default();
|
let mut mf = MapFile::default();
|
||||||
|
|
||||||
mf.map = vec![
|
mf.map = vec![
|
||||||
*MapCluster::default().set_stage(Stage::Damaged),
|
*Cluster::default().set_stage(Stage::Damaged),
|
||||||
*MapCluster::default().set_stage(Stage::ForIsolation(0)),
|
*Cluster::default().set_stage(Stage::ForIsolation(0)),
|
||||||
*MapCluster::default().set_stage(Stage::ForIsolation(1)),
|
*Cluster::default().set_stage(Stage::ForIsolation(1)),
|
||||||
MapCluster::default(),
|
Cluster::default(),
|
||||||
MapCluster::default(),
|
Cluster::default(),
|
||||||
*MapCluster::default().set_stage(Stage::ForIsolation(1)),
|
*Cluster::default().set_stage(Stage::ForIsolation(1)),
|
||||||
*MapCluster::default().set_stage(Stage::ForIsolation(0)),
|
*Cluster::default().set_stage(Stage::ForIsolation(0)),
|
||||||
*MapCluster::default().set_stage(Stage::Damaged),
|
*Cluster::default().set_stage(Stage::Damaged),
|
||||||
];
|
];
|
||||||
|
|
||||||
let stages = vec![
|
let stages = vec![
|
||||||
@@ -382,8 +355,8 @@ mod tests {
|
|||||||
|
|
||||||
for stage in stages {
|
for stage in stages {
|
||||||
let expected = vec![
|
let expected = vec![
|
||||||
*MapCluster::default().set_stage(stage),
|
*Cluster::default().set_stage(stage),
|
||||||
*MapCluster::default().set_stage(stage),
|
*Cluster::default().set_stage(stage),
|
||||||
];
|
];
|
||||||
let recieved = mf.get_clusters(stage);
|
let recieved = mf.get_clusters(stage);
|
||||||
|
|
||||||
@@ -402,35 +375,35 @@ mod tests {
|
|||||||
sector_size: 1,
|
sector_size: 1,
|
||||||
domain: Domain { start: 0, end: 8 },
|
domain: Domain { start: 0, end: 8 },
|
||||||
map: vec![
|
map: vec![
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 0, end: 1 },
|
domain: Domain { start: 0, end: 1 },
|
||||||
stage: Stage::Untested,
|
stage: Stage::Untested,
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 1, end: 2 },
|
domain: Domain { start: 1, end: 2 },
|
||||||
stage: Stage::Untested,
|
stage: Stage::Untested,
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 2, end: 3 },
|
domain: Domain { start: 2, end: 3 },
|
||||||
stage: Stage::Untested,
|
stage: Stage::Untested,
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 3, end: 4 },
|
domain: Domain { start: 3, end: 4 },
|
||||||
stage: Stage::ForIsolation(0),
|
stage: Stage::ForIsolation(0),
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 4, end: 5 },
|
domain: Domain { start: 4, end: 5 },
|
||||||
stage: Stage::ForIsolation(0),
|
stage: Stage::ForIsolation(0),
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 5, end: 6 },
|
domain: Domain { start: 5, end: 6 },
|
||||||
stage: Stage::ForIsolation(1),
|
stage: Stage::ForIsolation(1),
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 6, end: 7 },
|
domain: Domain { start: 6, end: 7 },
|
||||||
stage: Stage::ForIsolation(0),
|
stage: Stage::ForIsolation(0),
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 7, end: 8 },
|
domain: Domain { start: 7, end: 8 },
|
||||||
stage: Stage::Damaged,
|
stage: Stage::Damaged,
|
||||||
},
|
},
|
||||||
@@ -438,23 +411,23 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let expected = vec![
|
let expected = vec![
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 0, end: 3 },
|
domain: Domain { start: 0, end: 3 },
|
||||||
stage: Stage::Untested,
|
stage: Stage::Untested,
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 3, end: 5 },
|
domain: Domain { start: 3, end: 5 },
|
||||||
stage: Stage::ForIsolation(0),
|
stage: Stage::ForIsolation(0),
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 5, end: 6 },
|
domain: Domain { start: 5, end: 6 },
|
||||||
stage: Stage::ForIsolation(1),
|
stage: Stage::ForIsolation(1),
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 6, end: 7 },
|
domain: Domain { start: 6, end: 7 },
|
||||||
stage: Stage::ForIsolation(0),
|
stage: Stage::ForIsolation(0),
|
||||||
},
|
},
|
||||||
MapCluster {
|
Cluster {
|
||||||
domain: Domain { start: 7, end: 8 },
|
domain: Domain { start: 7, end: 8 },
|
||||||
stage: Stage::Damaged,
|
stage: Stage::Damaged,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use std::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Args,
|
Args,
|
||||||
mapping::{MapFile, Stage},
|
mapping::{Cluster, MapFile, Stage},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -50,14 +50,44 @@ impl Recover {
|
|||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Recover media from blank slate.
|
/// Recover media.
|
||||||
pub fn run(&mut self) -> &mut Self {
|
pub fn run(&mut self) -> &mut Self {
|
||||||
|
let mut is_finished = false;
|
||||||
|
|
||||||
|
while !is_finished {
|
||||||
|
match self.map.get_stage() {
|
||||||
|
Stage::Untested => { self.copy_untested(); },
|
||||||
|
Stage::ForIsolation(level) => { self.copy_isolate(level); },
|
||||||
|
Stage::Damaged => {
|
||||||
|
println!("Cannot recover further.");
|
||||||
|
|
||||||
|
is_finished = true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to copy all untested blocks.
|
/// Attempt to copy all untested blocks.
|
||||||
fn copy_untested(&mut self) -> &mut Self {
|
fn copy_untested(&mut self) -> &mut Self {
|
||||||
|
|
||||||
|
let mut untested: Vec<Cluster> = vec![];
|
||||||
|
|
||||||
|
for cluster in self.map.get_clusters(Stage::Untested).iter_mut() {
|
||||||
|
untested.append(&mut cluster.subdivide(self.map.sector_size as usize));
|
||||||
|
}
|
||||||
|
|
||||||
|
todo!("Read and save data.");
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempt to copy blocks via isolation at pass level.
|
||||||
|
fn copy_isolate(&mut self, level: u8) -> &mut Self {
|
||||||
|
|
||||||
|
todo!();
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user