Renamed cluster_engine to model, replaced cached with lru cache

pull/1/head
Benedikt Terhechte 3 years ago
parent c3bfcc8551
commit 8826b7064d

191
Cargo.lock generated

@ -90,26 +90,6 @@ dependencies = [
"nodrop",
]
[[package]]
name = "async-mutex"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e"
dependencies = [
"event-listener",
]
[[package]]
name = "async-trait"
version = "0.1.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44318e776df68115a881de9a8fd1b9e53368d7a4a5ce4cc48517da3393233a5e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "atomic_refcell"
version = "0.1.8"
@ -155,40 +135,6 @@ version = "3.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9df67f7bf9ef8498769f994239c45613ef0c5899415fb58e9add412d2c1a538"
[[package]]
name = "cached"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b99e696f7b2696ed5eae0d462a9eeafaea111d99e39b2c8ceb418afe1013bcfc"
dependencies = [
"async-mutex",
"async-trait",
"cached_proc_macro",
"cached_proc_macro_types",
"futures",
"hashbrown 0.9.1",
"once_cell",
]
[[package]]
name = "cached_proc_macro"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a685ba39b57a91a53d149dcbef854f50fbe204d1ff6081ea0bec3529a0c456"
dependencies = [
"async-mutex",
"cached_proc_macro_types",
"darling",
"quote",
"syn",
]
[[package]]
name = "cached_proc_macro_types"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a4f925191b4367301851c6d99b09890311d74b0d43f274c0b34c86d308a3663"
[[package]]
name = "calloop"
version = "0.6.5"
@ -620,12 +566,6 @@ dependencies = [
"egui",
]
[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59"
[[package]]
name = "eyre"
version = "0.6.5"
@ -681,100 +621,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "futures"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a12aa0eb539080d55c3f2d45a67c3b58b6b0773c1a3ca2dfec66d58c97fd66ca"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5da6ba8c3bb3c165d3c7319fc1cc8304facf1fb8db99c5de877183c08a273888"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88d1c26957f23603395cd326b0ffe64124b818f4449552f960d815cfba83a53d"
[[package]]
name = "futures-executor"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45025be030969d763025784f7f355043dc6bc74093e4ecc5000ca4dc50d8745c"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "522de2a0fe3e380f1bc577ba0474108faf3f6b18321dbf60b3b9c39a75073377"
[[package]]
name = "futures-macro"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18e4a4b95cea4b4ccbcf1c5675ca7c4ee4e9e75eb79944d07defde18068f79bb"
dependencies = [
"autocfg",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36ea153c13024fe480590b3e3d4cad89a0cfacecc24577b68f86c6ced9c2bc11"
[[package]]
name = "futures-task"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d3d00f4eddb73e498a54394f228cd55853bdf059259e8e7bc6e69d408892e99"
[[package]]
name = "futures-util"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481"
dependencies = [
"autocfg",
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"proc-macro-hack",
"proc-macro-nested",
"slab",
]
[[package]]
name = "getrandom"
version = "0.2.3"
@ -895,7 +741,6 @@ dependencies = [
name = "gmaildb"
version = "0.1.0"
dependencies = [
"cached",
"chrono",
"crossbeam-channel",
"eframe",
@ -903,6 +748,7 @@ dependencies = [
"eyre",
"flate2",
"lazy_static",
"lru",
"num-format",
"rayon",
"regex",
@ -918,12 +764,6 @@ dependencies = [
"treemap",
]
[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
[[package]]
name = "hashbrown"
version = "0.11.2"
@ -939,7 +779,7 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf"
dependencies = [
"hashbrown 0.11.2",
"hashbrown",
]
[[package]]
@ -1074,6 +914,15 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "lru"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c748cfe47cb8da225c37595b3108bea1c198c84aaae8ea0ba76d01dda9fc803"
dependencies = [
"hashbrown",
]
[[package]]
name = "malloc_buf"
version = "0.0.6"
@ -1492,12 +1341,6 @@ version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.20"
@ -1535,12 +1378,6 @@ version = "0.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]]
name = "proc-macro-nested"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086"
[[package]]
name = "proc-macro2"
version = "1.0.29"
@ -1794,12 +1631,6 @@ version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
[[package]]
name = "slab"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c307a32c1c5c437f38c7fd45d753050587732ba8628319fbdf12a7e289ccc590"
[[package]]
name = "smallvec"
version = "1.7.0"

@ -26,13 +26,13 @@ treemap = "0.3.2"
num-format = "0.4.0"
strum = "0.21"
strum_macros = "0.21"
cached = { version = "0.25.0", features = [] }
lru = { version = "0.7.0", optional = true }
[features]
default = ["gui"]
# Trace all SQL Queries
trace-sql = []
gui = ["eframe"]
gui = ["eframe", "lru"]
[profile.dev]
split-debuginfo = "unpacked"

@ -4,7 +4,7 @@ use eyre::{Report, Result};
use eframe::{egui, epi};
use super::widgets::{self, Spinner};
use crate::cluster_engine::{segmentation, Engine};
use crate::model::{segmentations, Engine};
use crate::types::Config;
pub struct GmailDBApp {
@ -77,12 +77,12 @@ impl epi::App for GmailDBApp {
} else {
ui.vertical(|ui| {
ui.horizontal(|ui| {
if let Some((range, total)) = segmentation::segments_range(engine) {
if let Some((range, total)) = segmentations::segments_range(engine) {
ui.label("Limit");
let mut selected = total;
let response = ui.add(egui::Slider::new(&mut selected, range));
if response.changed() {
segmentation::set_segments_range(engine, Some(0..=selected));
segmentations::set_segments_range(engine, Some(0..=selected));
}
}
// This is a hack to get right-alignment.

@ -1,5 +1,5 @@
use crate::cluster_engine::{items, Engine};
use crate::database::query::Field;
use crate::model::{items, Engine};
use eframe::egui::{self, Widget};
use eyre::Report;

@ -1,4 +1,4 @@
use crate::cluster_engine::{segmentation, Engine};
use crate::model::{segmentations, Engine};
use eframe::egui::{self, Widget};
use eyre::Report;
@ -16,10 +16,10 @@ impl<'a> TopBar<'a> {
impl<'a> Widget for TopBar<'a> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
ui.horizontal(|ui| {
let groupings = segmentation::aggregated_by(self.engine);
let groupings = segmentations::aggregated_by(self.engine);
let has_back = groupings.len() > 1;
for (id_index, group) in groupings.iter().enumerate() {
let alternatives = segmentation::aggregation_fields(self.engine, &group);
let alternatives = segmentations::aggregation_fields(self.engine, &group);
if let Some(value) = group.value() {
let label = egui::Label::new(format!("{}: {}", group.name(), value));
ui.add(label);
@ -31,7 +31,7 @@ impl<'a> Widget for TopBar<'a> {
|i| alternatives[i].as_str().to_string(),
);
if p.changed() {
*self.error = segmentation::set_aggregation(
*self.error = segmentations::set_aggregation(
self.engine,
&group,
&alternatives[selected],

@ -1,6 +1,6 @@
use std::collections::hash_map::DefaultHasher;
use crate::cluster_engine::{segmentation, Engine, Segment};
use crate::model::{segmentations, Engine, Segment};
use eframe::egui::{self, epaint::Galley, Pos2, Rgba, Stroke, TextStyle, Widget};
use eyre::Report;
use num_format::{Locale, ToFormattedString};
@ -36,7 +36,7 @@ impl<'a> Widget for Rectangles<'a> {
let size = ui.available_size();
let (rect, mut response) = ui.allocate_exact_size(size, egui::Sense::hover());
let items = match segmentation::layouted_segments(self.engine, rect) {
let items = match segmentations::layouted_segments(self.engine, rect) {
Some(n) => n.to_owned(),
None => return response,
};

@ -1,10 +1,10 @@
use tracing_subscriber::EnvFilter;
mod cluster_engine;
pub mod database;
pub mod filesystem;
#[cfg(feature = "gui")]
pub mod gui;
mod model;
pub mod parse;
pub mod types;

@ -4,15 +4,15 @@
//! See also:
//! - [`segmentations::`]
//! - [`items::`]
use cached::{Cached, SizedCache};
use eyre::{bail, Result};
use lru::LruCache;
use crate::cluster_engine::link::Response;
use crate::database::query::{Field, Query, ValueField};
use crate::model::link::Response;
use crate::types::Config;
use super::link::Link;
use super::segmentation;
use super::segmentations;
use super::types::{LoadingState, Segment, Segmentation};
/// This signifies the action we're currently evaluating
@ -38,7 +38,7 @@ pub struct Engine {
/// This is a very simple cache from ranges to rows.
/// It doesn't account for overlapping ranges.
/// There's a lot of room for improvement here.
pub(super) item_cache: SizedCache<usize, LoadingState>,
pub(super) item_cache: LruCache<usize, LoadingState>,
}
impl Engine {
@ -49,7 +49,7 @@ impl Engine {
search_stack: Vec::new(),
group_by_stack: vec![default_group_by_stack(0)],
segmentations: Vec::new(),
item_cache: SizedCache::with_size(10000),
item_cache: LruCache::new(10000),
};
Ok(engine)
}
@ -60,7 +60,7 @@ impl Engine {
pub fn start(&mut self) -> Result<()> {
Ok(self
.link
.request(&segmentation::make_query(&self)?, Action::PushSegmentation)?)
.request(&segmentations::make_query(&self)?, Action::PushSegmentation)?)
}
/// Return the current stack of `Segmentations`
@ -97,7 +97,7 @@ impl Engine {
// Block UI & Wait for updates
self.link
.request(&segmentation::make_query(&self)?, Action::PushSegmentation)
.request(&segmentations::make_query(&self)?, Action::PushSegmentation)
}
/// Pop the current `Segmentation` from the stack.
@ -125,7 +125,7 @@ impl Engine {
self.segmentations.last_mut().map(|e| e.selected = None);
// Remove any rows that were cached for this segmentation
self.item_cache.cache_clear();
self.item_cache.clear();
}
/// Call this continously to retrieve calculation results and apply them.
@ -144,18 +144,18 @@ impl Engine {
Response::Grouped(_, Action::PushSegmentation, p) => {
self.segmentations.push(p);
// Remove any rows that were cached for this segmentation
self.item_cache.cache_clear();
self.item_cache.clear();
}
Response::Grouped(_, Action::RecalculateSegmentation, p) => {
let len = self.segmentations.len();
self.segmentations[len - 1] = p;
// Remove any rows that were cached for this segmentation
self.item_cache.cache_clear();
self.item_cache.clear();
}
Response::Normal(Query::Normal { range, .. }, Action::LoadItems, r) => {
for (index, row) in range.zip(r) {
let entry = LoadingState::Loaded(row.clone());
self.item_cache.cache_set(index, entry);
self.item_cache.put(index, entry);
}
}
_ => bail!("Invalid Query / Response combination"),

@ -4,7 +4,6 @@
//! These operations allow retreiving the individual items for all
//! segments in the `Segmentation.
use cached::Cached;
use eyre::Result;
use super::types::LoadingState;
@ -38,7 +37,7 @@ pub fn items(engine: &mut Engine, range: Option<Range<usize>>) -> Result<Vec<Opt
let mut missing_data = false;
for index in range.clone() {
let entry = engine.item_cache.cache_get(&index);
let entry = engine.item_cache.get(&index);
let entry = match entry {
Some(LoadingState::Loaded(n)) => Some((*n).clone()),
Some(LoadingState::Loading) => None,
@ -47,7 +46,7 @@ pub fn items(engine: &mut Engine, range: Option<Range<usize>>) -> Result<Vec<Opt
missing_data = true;
// Mark the row as being loaded
engine.item_cache.cache_set(index, LoadingState::Loading);
engine.item_cache.put(index, LoadingState::Loading);
None
}
};

@ -1,7 +1,7 @@
mod engine;
pub mod items;
mod link;
pub mod segmentation;
pub mod segmentations;
mod types;
pub use engine::Engine;

@ -13,7 +13,6 @@
//! to fit into a rectangle.
//! - [`layouted_segments]
use cached::Cached;
use eyre::{eyre, Result};
use super::engine::Action;
@ -27,7 +26,10 @@ use std::ops::RangeInclusive;
/// Filter the `Range` of segments of the current `Segmentation`
///
/// Returns the `Range` and the total number of segments.
/// Returns `Some` if a range has been set with [`set_segments_range`], `None` otherwise.
/// If no custom range has been set with [`set_segments_range`], returns
/// the full range of items, otherwise the custom range.
///
/// Returns `None` if no current `Segmentation` exists.
///
/// # Arguments
///
@ -36,11 +38,10 @@ use std::ops::RangeInclusive;
pub fn segments_range(engine: &Engine) -> Option<(RangeInclusive<usize>, usize)> {
let segmentation = engine.segmentations.last()?;
let len = segmentation.len();
let r = match &segmentation.range {
Some(match &segmentation.range {
Some(n) => (0..=len, *n.end()),
None => (0..=len, len),
};
Some(r)
})
}
/// Set the `Range` of segments of the current `Segmentation`
@ -140,7 +141,7 @@ pub fn set_aggregation(
.get_mut(aggregation.index)
.map(|e| *e = field.clone());
// Remove any rows that were cached for this Segmentation
engine.item_cache.cache_clear();
engine.item_cache.clear();
engine
.link
.request(&make_query(engine)?, Action::RecalculateSegmentation)
Loading…
Cancel
Save