Rename level current to head (#61)

This commit is contained in:
Azrenbeth
2021-09-14 12:28:35 +01:00
committed by GitHub
parent 8c72a0de52
commit a409cdbd8e
4 changed files with 29 additions and 23 deletions

View File

@@ -44,7 +44,7 @@ pub struct Level {
/// to recursively following `current`
current_chain_length: usize,
/// The head of this level
current: Option<i64>,
head: Option<i64>,
}
impl Level {
@@ -53,16 +53,16 @@ impl Level {
Level {
max_length,
current_chain_length: 0,
current: None,
head: None,
}
}
/// Creates a new level from stored state
pub fn restore(max_length: usize, current_chain_length: usize, current: Option<i64>) -> Level {
pub fn restore(max_length: usize, current_chain_length: usize, head: Option<i64>) -> Level {
Level {
max_length,
current_chain_length,
current,
head,
}
}
@@ -70,8 +70,8 @@ impl Level {
/// that given state group will (probably) reference the previous head.
///
/// Panics if `delta` is true and the level is already full.
fn update(&mut self, current: i64, delta: bool) {
self.current = Some(current);
fn update(&mut self, new_head: i64, delta: bool) {
self.head = Some(new_head);
if delta {
// If we're referencing the previous head then increment our chain
@@ -87,9 +87,19 @@ impl Level {
}
}
/// Get the max length of the level
pub fn get_max_length(&self) -> usize {
self.max_length
}
/// Get the current length of the level
pub fn get_current_length(&self) -> usize {
self.current_chain_length
}
/// Get the current head of the level
pub fn get_current(&self) -> Option<i64> {
self.current
pub fn get_head(&self) -> Option<i64> {
self.head
}
/// Whether there is space in the current chain at this level. If not then a
@@ -142,12 +152,11 @@ impl<'a> Compressor<'a> {
/// in which case the levels heads are also known
pub fn compress_from_save(
original_state_map: &'a BTreeMap<i64, StateGroupEntry>,
// level_info: &[(usize, usize, Option<i64>)],
level_info: &[Level],
) -> Compressor<'a> {
let levels = level_info
.iter()
.map(|l| Level::restore((*l).max_length, (*l).current_chain_length, (*l).current))
.map(|l| Level::restore((*l).max_length, (*l).current_chain_length, (*l).head))
.collect();
let mut compressor = Compressor {
@@ -200,7 +209,7 @@ impl<'a> Compressor<'a> {
let mut prev_state_group = None;
for level in &mut self.levels {
if level.has_space() {
prev_state_group = level.get_current();
prev_state_group = level.get_head();
level.update(state_group, true);
break;
} else {

View File

@@ -677,11 +677,11 @@ fn get_delta_returns_snapshot_if_no_prev_possible() {
let mut levels_iter = compressor.levels.iter_mut();
let l1 = levels_iter.next().unwrap();
l1.current = Some(3);
l1.head = Some(3);
l1.current_chain_length = 1;
let l2 = levels_iter.next().unwrap();
l2.current = Some(3);
l2.head = Some(3);
l2.current_chain_length = 1;
// Now try and find delta for 4 with 3 as pred

View File

@@ -5,7 +5,7 @@ fn new_produces_empty_level() {
let l = Level::new(15);
assert_eq!(l.max_length, 15);
assert_eq!(l.current_chain_length, 0);
assert_eq!(l.current, None);
assert_eq!(l.head, None);
}
#[test]
@@ -14,7 +14,7 @@ fn update_adds_to_non_full_level() {
l.update(7, true);
assert_eq!(l.max_length, 10);
assert_eq!(l.current_chain_length, 1);
assert_eq!(l.current, Some(7));
assert_eq!(l.head, Some(7));
}
#[test]
@@ -40,15 +40,15 @@ fn update_resets_level_correctly() {
l.update(6, false);
assert_eq!(l.max_length, 5);
assert_eq!(l.current_chain_length, 1);
assert_eq!(l.current, Some(6));
assert_eq!(l.head, Some(6));
}
#[test]
fn get_current_returns_current() {
fn get_head_returns_head() {
let mut l = Level::new(5);
assert_eq!(l.get_current(), None);
assert_eq!(l.get_head(), None);
l.update(23, true);
assert_eq!(l.get_current(), Some(23));
assert_eq!(l.get_head(), Some(23));
}
#[test]

View File

@@ -126,10 +126,7 @@ pub fn reload_data_from_db(
/// * `levels' - The levels who's heads are being requested
fn load_level_heads(client: &mut Client, level_info: &[Level]) -> BTreeMap<i64, StateGroupEntry> {
// obtain all of the heads that aren't None from level_info
let level_heads: Vec<i64> = level_info
.iter()
.filter_map(|l| (*l).get_current())
.collect();
let level_heads: Vec<i64> = level_info.iter().filter_map(|l| (*l).get_head()).collect();
// Query to get id, predecessor and deltas for each state group
let sql = r#"