Skip to content

Rename level current to head #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,25 +53,25 @@ 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,
}
}

/// Update the current head of this level. If delta is true then it means
/// 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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/compressor/compressor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/compressor/level_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
5 changes: 1 addition & 4 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

use pyo3::{exceptions, prelude::*};

#[cfg(feature = "jemalloc")]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

use clap::{crate_authors, crate_description, crate_name, crate_version, value_t, App, Arg};
use indicatif::{ProgressBar, ProgressStyle};
use rayon::prelude::*;
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
//! Synapse instance's database. Specifically, it aims to reduce the number of
//! rows that a given room takes up in the `state_groups_state` table.

#[cfg(feature = "jemalloc")]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

use synapse_compress_state as comp_state;

fn main() {
Expand Down