diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index a938aa7f1d..0edbdb04d0 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -120,6 +120,9 @@ pub struct Dialogs { pub dxf: Option, /// Select By Attributes' Values list: its field, and the values read for it. pub values: crate::fieldvalues::Form, + /// Select By Attributes' count of the rows its clause matches: counted once per clause and + /// set of rows, off the frame (#2946). + pub matching: crate::matchcount::Count, /// Reset collapsed then closed dialogs when reopening pub reset_collapse: bool, } @@ -1041,6 +1044,8 @@ pub fn show(app: &mut RustGisApp, ctx: &egui::Context) { app.dialogs.open = None; // Its values read are dropped with the dialog. app.dialogs.values = Default::default(); + // And so is a count still on its way (dropping it stops the worker). + app.dialogs.matching = Default::default(); if which == "addDxf" { app.dialogs.dxf = None; } @@ -2991,25 +2996,33 @@ fn select_by_attributes(app: &mut RustGisApp, ui: &mut egui::Ui, t: &Tokens) -> crate::fieldvalues::said(ui, &app.dialogs.values); // Live validation, once no suggestion is offered: a word still being chosen isn't an error yet. let offering = crate::exprcomplete::offering(ui, id); - let check = app + // Counted once per clause and set of rows, off the frame (#2946); nothing while a word is + // still being chosen, nor for an empty clause. + let dialogs = &mut app.dialogs; + let shown = app .session .map() - .layer_by_name(&app.dialogs.layer) - .filter(|_| !offering) - .map(|l| rustgis_engine::data::query::select(&app.dialogs.text, &l.dataset)); - match (&check, app.dialogs.text.trim().is_empty()) { - (_, true) => {} - (Some(Ok(rows)), _) => { + .layer_by_name(&dialogs.layer) + .filter(|_| !offering && !dialogs.text.trim().is_empty()) + .map(|l| dialogs.matching.of(ui.ctx(), &dialogs.text, &l.name, &l.dataset, !cfg!(target_arch = "wasm32"))); + match shown { + Some(crate::matchcount::Shown::Rows(n)) => { ui.label( - egui::RichText::new(crate::i18n::fmt("Valid expression · {n} matching rows", &[("n", &rows.len().to_string())])) + egui::RichText::new(crate::i18n::fmt("Valid expression · {n} matching rows", &[("n", &n.to_string())])) .small() .color(t.green), ); } - (Some(Err(e)), _) => { - ui.label(egui::RichText::new(crate::i18n::fmt("Invalid: {error}", &[("error", e)])).small().color(t.red)); + Some(crate::matchcount::Shown::Invalid(e)) => { + ui.label(egui::RichText::new(crate::i18n::fmt("Invalid: {error}", &[("error", &e)])).small().color(t.red)); } - _ => {} + Some(crate::matchcount::Shown::Failed(e)) => { + ui.label(egui::RichText::new(e).small().color(t.red)); + } + Some(crate::matchcount::Shown::Counting) => { + ui.label(egui::RichText::new(tl!("Counting matching rows…")).small().weak()); + } + None => {} } buttons(app, ui, "Apply", |app| { let layer = app.dialogs.layer.clone(); @@ -3806,6 +3819,130 @@ mod tests { app.session.map().layer_by_name("Countries").map_or(0, |l| l.selection.len()) } + /// Frames until the count has landed (it runs on a thread of its own). + fn counted(h: &mut egui_kittest::Harness<'static, RustGisApp>) { + for _ in 0..400 { + h.step(); + if !h.state().dialogs.matching.counting() { + return; + } + std::thread::sleep(std::time::Duration::from_millis(5)); // test-wait: the count runs on a thread of its own + } + panic!("the count never landed"); + } + + /// The count is made once for a clause and its rows, not once per frame, and reads as it + /// always did (#2946). + #[test] + fn select_by_attributes_counts_once_per_clause_not_per_frame() { + use egui_kittest::kittest::Queryable; + let mut h = sba_ready(); + counted(&mut h); + assert_eq!(h.state().dialogs.matching.starts, 1); + h.run_steps(10); + for x in [200.0, 300.0, 250.0] { + h.hover_at(egui::pos2(x, 300.0)); + h.step(); + } + assert_eq!(h.state().dialogs.matching.starts, 1, "not counted again by frames"); + assert!(h.query_by_label("Valid expression · 14 matching rows").is_some(), "the number as before"); + } + + /// An edit to the layer, another clause or another layer is counted again; none is answered + /// with the number for what was before (#2946). + #[test] + fn select_by_attributes_count_follows_the_rows_and_the_clause() { + use egui_kittest::kittest::Queryable; + use rustgis_engine::data::Value; + let mut h = sba_ready(); + counted(&mut h); + let (id, fields) = h.state().session.map().layer_by_name("Countries").map(|l| (l.id, l.dataset.fields.len())).unwrap(); + let pop = h.state().session.map().layer_by_name("Countries").and_then(|l| l.dataset.field_index("pop_est")).unwrap(); + let mut attrs = vec![Value::Null; fields]; + attrs[pop] = Value::Int(1_000_000_000); + h.state_mut().session.map_mut().layer_mut(id).unwrap().dataset.push(None, attrs); + counted(&mut h); + assert_eq!(h.state().dialogs.matching.starts, 2, "a row added"); + assert!(h.query_by_label("Valid expression · 15 matching rows").is_some(), "one more than before"); + h.state_mut().dialogs.text = "pop_est > 2000000000".into(); + counted(&mut h); + assert_eq!(h.state().dialogs.matching.starts, 3, "another clause"); + assert!(h.query_by_label("Valid expression · 0 matching rows").is_some()); + h.state_mut().dialogs.text.clear(); + h.run_steps(3); + assert_eq!(h.state().dialogs.matching.starts, 3, "an empty clause starts nothing"); + assert!(h.query_by_label_contains("matching rows").is_none(), "and says nothing"); + } + + /// A value edited in place (the rows' number and the fields as before), another layer and a + /// layer removed are never answered with the number for what was there before (#2946). + #[test] + fn select_by_attributes_count_follows_a_value_edit_a_layer_switch_and_a_removal() { + use egui_kittest::kittest::Queryable; + use rustgis_engine::data::Value; + let mut h = sba_ready(); + counted(&mut h); + let (id, pop, row) = { + let l = h.state().session.map().layer_by_name("Countries").unwrap(); + let rows = rustgis_engine::data::query::select("pop_est > 100000000", &l.dataset).unwrap(); + (l.id, l.dataset.field_index("pop_est").unwrap(), rows[0]) + }; + h.state_mut().session.map_mut().layer_mut(id).unwrap().dataset.features.set_value(row, pop, Value::Int(0)); + counted(&mut h); + assert!(h.query_by_label("Valid expression · 13 matching rows").is_some(), "one fewer, the same length"); + h.state_mut().dialogs.layer = "Cities".into(); + counted(&mut h); + assert!(h.query_by_label_contains("Valid expression · 13").is_none(), "not Countries' number for Cities"); + assert!( + h.query_by_label_contains("matching rows").is_some() || h.query_by_label_contains("Invalid:").is_some(), + "Cities has its own answer" + ); + h.state_mut().dialogs.layer = "Countries".into(); + counted(&mut h); + h.state_mut().session.run("layer.remove", &json!({"layer": "Countries"})).unwrap(); + h.run_steps(3); + assert!(h.query_by_label_contains("matching rows").is_none(), "a layer that is gone has no count"); + } + + /// A clause that doesn't read is said at once and starts no count (#2946). + #[test] + fn select_by_attributes_names_a_bad_clause_without_counting() { + use egui_kittest::kittest::Queryable; + let mut h = sba_ready(); + counted(&mut h); + h.state_mut().dialogs.text = "nope = 1".into(); + h.step(); + assert!(h.query_by_label("Invalid: no field named 'nope'").is_some(), "said in the frame that sees it"); + assert_eq!(h.state().dialogs.matching.starts, 1, "no second count"); + } + + /// A clause typed is not counted in the frame that draws it (#2946): the count runs on a + /// thread of its own and is polled by a later frame, however fast it is. + #[test] + fn select_by_attributes_does_not_count_in_the_frame_that_draws_it() { + use egui_kittest::kittest::Queryable; + let mut h = sba_ready(); + h.state_mut().dialogs.text = "pop_est > 100000000 OR pop_est < 1".into(); + h.step(); + assert!(h.query_by_label_contains("Counting matching rows…").is_some(), "says it is counting"); + assert!(h.query_by_label_contains("Valid expression").is_none(), "no count in the frame that starts it"); + } + + /// Closing the dialog while it counts stops the count (#2946): the worker is not left reading + /// the layer's rows for a dialog that is gone. + #[test] + fn cancelling_select_by_attributes_stops_its_count() { + use egui::accesskit::Role; + use egui_kittest::kittest::Queryable; + let mut h = sba_ready(); + let stop = h.state().dialogs.matching.stop_flag(); + assert!(!stop.load(std::sync::atomic::Ordering::Relaxed), "counting for the open dialog"); + h.get_by_role_and_label(Role::Button, "Cancel").click(); + h.run(); + assert_eq!(h.state().dialogs.open, None, "Cancel closed it"); + assert!(stop.load(std::sync::atomic::Ordering::Relaxed), "and the count was told to stop"); + } + /// Enter on a focused Cancel cancels: the dialog closes and selects nothing (#2599). #[test] fn enter_on_cancel_cancels() { diff --git a/crates/ui-egui/src/exprcomplete.rs b/crates/ui-egui/src/exprcomplete.rs index e7e04d1c67..577e666dea 100644 --- a/crates/ui-egui/src/exprcomplete.rs +++ b/crates/ui-egui/src/exprcomplete.rs @@ -122,7 +122,14 @@ pub(crate) struct Offer { /// offer worked out over them is kept while it's the same (`Worked`), and its rows' descriptions /// while the offer is (`completion::List`). pub(crate) fn fields_key(ds: &Dataset) -> u64 { - egui::util::hash(ds.fields.iter().map(|f| (f.name.as_str(), f.ty.label(), f.alias.as_deref())).collect::>()) + // Fed to the hasher one by one: no list is built for it, as this runs every frame. + use std::hash::{Hash, Hasher}; + let mut h = std::hash::DefaultHasher::new(); + ds.fields.len().hash(&mut h); + for f in &ds.fields { + (f.name.as_str(), f.ty.label(), f.alias.as_deref()).hash(&mut h); + } + h.finish() } impl Offered for Offer { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 8966523c7b..833ab82cee 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -77,6 +77,7 @@ mod mapview; mod markerimages; mod markerpick; pub use markerpick::{MarkerImage, marker_file_too_large}; +mod matchcount; mod measure_units; mod modelbuilder; mod navui; diff --git a/crates/ui-egui/src/matchcount.rs b/crates/ui-egui/src/matchcount.rs new file mode 100644 index 0000000000..9887be0865 --- /dev/null +++ b/crates/ui-egui/src/matchcount.rs @@ -0,0 +1,390 @@ +//! Select By Attributes' count of the rows a clause matches (#2946). Counting is a pass over every +//! row of the layer, so it is not done in each frame: once per clause and set of rows, on a thread +//! of its own on the desktop (the browser has none, so it counts inline, still once per change), +//! with the dialog saying "Counting matching rows…" until it lands. + +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Weak}; + +use rustgis_engine::data::raster::Crs; +use rustgis_engine::data::{Dataset, Feature, query}; + +use crate::Slot; + +/// How many rows are counted between two looks at the stop flag. +const CHUNK: usize = 4096; + +/// What the dialog shows under the clause. +#[derive(Debug, PartialEq)] +pub(crate) enum Shown { + /// The count is on its way. + Counting, + /// How many rows match. + Rows(usize), + /// Why the clause doesn't read. + Invalid(String), + /// Why the count could not be made, though the clause reads. + Failed(String), +} + +/// What a count came to. +#[derive(Debug, PartialEq)] +enum Answer { + Rows(usize), + Invalid(String), + Failed(String), +} + +impl From for Shown { + fn from(a: Answer) -> Shown { + match a { + Answer::Rows(n) => Shown::Rows(n), + Answer::Invalid(e) => Shown::Invalid(e), + Answer::Failed(e) => Shown::Failed(e), + } + } +} + +/// What a count was made for: it holds only while the same layer has the same rows and fields. The +/// rows are held by their allocation, which any edit, reload, Undo or next view of a streamed +/// layer ends ([`rustgis_engine::data::Features::allocation_key`]). +struct Key { + layer: String, + text: String, + rows: Weak>, + fields: u64, + windowed: bool, + held_in: Crs, +} + +impl Key { + fn is(&self, text: &str, layer: &str, ds: &Dataset) -> bool { + self.text == text + && self.layer == layer + && self.windowed == ds.windowed + && self.held_in == ds.held_in + && self.rows.ptr_eq(&ds.features.allocation_key()) + && self.fields == crate::exprcomplete::fields_key(ds) + } +} + +enum State { + /// On a thread of its own: `Err` in the slot is a thread the system refused. + Counting(Slot>), + Done(Answer), +} + +/// The count between frames, held by the dialog (`Dialogs::matching`). Dropping it (the dialog +/// closed or another opened) stops a count still running. A layer removed while it counts keeps +/// its rows until the count ends or looks at its stop flag, and an edit to the layer in that time +/// copies the rows once (the count shares them). +#[derive(Default)] +pub struct Count { + key: Option, + state: Option, + stop: Arc, + /// How many counts were started (a clause that doesn't read starts none): once per change, not + /// once per frame. + #[cfg(test)] + pub(crate) starts: usize, + /// A test's stand-in for a system with no thread to give. + #[cfg(test)] + pub(crate) refuse: bool, +} + +impl Drop for Count { + fn drop(&mut self) { + self.stop.store(true, Ordering::Relaxed); + } +} + +impl Count { + /// What to show for `text` over layer `layer`'s rows `ds`, each frame it is asked for. A new + /// clause, layer or set of rows starts a count (and stops the one before); the answer is kept + /// while they stay as they were. `off_frame`: count on a thread of its own, else here. + pub(crate) fn of(&mut self, ctx: &egui::Context, text: &str, layer: &str, ds: &Dataset, off_frame: bool) -> Shown { + let current = self.key.as_ref().is_some_and(|k| k.is(text, layer, ds)); + if !current { + self.start(ctx, text, layer, ds, off_frame); + } else if let Some(State::Counting(slot)) = &self.state { + let landed = slot.lock().unwrap_or_else(|e| e.into_inner()).take(); + match landed { + Some(Ok(a)) => self.state = Some(State::Done(a)), + Some(Err(e)) => self.state = Some(State::Done(Answer::Failed(e))), + None => {} + } + } + match &self.state { + Some(State::Done(Answer::Rows(n))) => Shown::Rows(*n), + Some(State::Done(Answer::Invalid(e))) => Shown::Invalid(e.clone()), + Some(State::Done(Answer::Failed(e))) => Shown::Failed(e.clone()), + _ => { + // The thread asks for a frame when it lands; this is for one that never does. + ctx.request_repaint_after(std::time::Duration::from_millis(100)); + Shown::Counting + } + } + } + + fn start(&mut self, ctx: &egui::Context, text: &str, layer: &str, ds: &Dataset, off_frame: bool) { + // Whatever was counting is of a clause or rows no longer asked about. + self.stop.store(true, Ordering::Relaxed); + self.stop = Arc::default(); + self.key = Some(Key { + layer: layer.to_string(), + text: text.to_string(), + rows: ds.features.allocation_key(), + fields: crate::exprcomplete::fields_key(ds), + windowed: ds.windowed, + held_in: ds.held_in, + }); + // The words and fields only: no geometry is read (`parse` reads them all for `$area`), so + // a clause that doesn't read is said at once and costs nothing. The full `parse` is the + // count's first step, so what it refuses is said as it always was. + if let Err(e) = query::parse_fields(text, ds) { + self.state = Some(State::Done(Answer::Invalid(e))); + return; + } + #[cfg(test)] + { + self.starts += 1; + } + if !off_frame { + self.state = Some(State::Done(now(text, ds))); + return; + } + let slot: Slot> = Slot::default(); + let land = slot.clone(); + let (owned, rows, stop, repaint) = (text.to_string(), ds.clone(), self.stop.clone(), ctx.clone()); + let work = move || { + if let Some(a) = guarded(|| count(&owned, &rows, &stop)) { + *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(Ok(a)); + repaint.request_repaint(); + } + }; + #[cfg(test)] + if self.refuse { + crate::work_off_frame_with(|_| Err(std::io::Error::other("no threads left")), &slot, work); + } else { + crate::work_off_frame("rustgis-select-count", &slot, work); + } + #[cfg(not(test))] + crate::work_off_frame("rustgis-select-count", &slot, work); + // A thread the system refused is said as it is: counting here would hold the frame for as + // long as the layer is large, with no Cancel to reach. + let refused = match &*slot.lock().unwrap_or_else(|e| e.into_inner()) { + Some(Err(e)) => Some(e.clone()), + _ => None, + }; + self.state = Some(match refused { + Some(e) => State::Done(Answer::Failed(e)), + None => State::Counting(slot), + }); + } + + /// The flag that stops the count on its way (a test watches it). + #[cfg(test)] + pub(crate) fn stop_flag(&self) -> Arc { + self.stop.clone() + } + + /// Whether a count is on its way. + #[cfg(test)] + pub(crate) fn counting(&self) -> bool { + matches!(self.state, Some(State::Counting(_))) + } +} + +/// Runs `f`, giving a bug in the count as an answer, not a thread that ends with nothing landed +/// (no command's catch reaches it). +fn guarded(f: impl FnOnce() -> Option) -> Option { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) + .unwrap_or_else(|_| Some(Answer::Failed(tl!("Counting the matching rows failed unexpectedly").to_string()))) +} + +/// The count at once, here (the browser's, on its one thread). +fn now(text: &str, ds: &Dataset) -> Answer { + let never = AtomicBool::new(false); + guarded(|| count(text, ds, &never)).unwrap_or_else(|| Answer::Failed(tl!("Counting the matching rows failed unexpectedly").to_string())) +} + +/// How many rows `text` matches, as `query::select(text, ds)` has them: looks at `stop` before it +/// starts and between chunks of [`CHUNK`] rows, and gives nothing once it is set. One row's +/// evaluation, and the full `parse`'s first read of the geometries (`$area`), are not interrupted. +fn count(text: &str, ds: &Dataset, stop: &AtomicBool) -> Option { + count_between(text, ds, stop, || {}) +} + +/// [`count`], calling `between` after each chunk (a test sets the stop there). +fn count_between(text: &str, ds: &Dataset, stop: &AtomicBool, mut between: impl FnMut()) -> Option { + // A count already superseded (a keystroke ago) doesn't start `parse`'s read of the geometries. + if stop.load(Ordering::Relaxed) { + return None; + } + let e = match query::parse(text, ds) { + Ok(e) => e, + Err(e) => return Some(Answer::Invalid(e)), + }; + let rows = ds.features.len(); + let mut n = 0; + let mut from = 0; + while from < rows { + if stop.load(Ordering::Relaxed) { + return None; + } + let to = from.saturating_add(CHUNK).min(rows); + n += (from..to).filter(|r| query::eval(&e, ds, *r)).count(); + from = to; + between(); + } + Some(Answer::Rows(n)) +} + +#[cfg(test)] +mod tests { + use super::*; + use rustgis_engine::data::{Field, FieldType, GeometryType, Value}; + + /// `rows` points with an integer field `n` counting up from 0. + fn layer(rows: usize) -> Dataset { + let mut ds = Dataset::new("t", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]); + for i in 0..rows { + ds.push(None, vec![Value::Int(i as i64)]); + } + ds + } + + /// The count is the number of rows `select` gives, and a clause that doesn't read gives the + /// error `select` gives. + #[test] + fn the_count_is_what_select_gives() { + let ds = layer(10_000); + let never = AtomicBool::new(false); + for (clause, expected) in [("n >= 9000", 1000), ("n < 0", 0), ("n >= 0", 10_000), ("n IN (1, 2, 3)", 3), ("n IS NULL", 0)] { + assert_eq!(count(clause, &ds, &never), Some(Answer::Rows(expected)), "{clause}"); + assert_eq!(now(clause, &ds), Answer::Rows(expected), "{clause} at once"); + assert_eq!(query::select(clause, &ds).map(|r| r.len()), Ok(expected), "{clause} by select"); + } + for bad in ["nope = 1", "", "n >"] { + let said = query::select(bad, &ds).map(|r| r.len()).unwrap_err(); + assert_eq!(count(bad, &ds, &never), Some(Answer::Invalid(said.clone())), "{bad:?}"); + assert_eq!(now(bad, &ds), Answer::Invalid(said), "{bad:?} at once"); + } + assert_eq!(now("nope = 1", &ds), Answer::Invalid("no field named 'nope'".to_string())); + } + + /// A stop that is set leaves no answer, even for a clause that doesn't read: nothing of a + /// count superseded is started. + #[test] + fn a_stopped_count_gives_nothing() { + let ds = layer(3 * CHUNK); + assert_eq!(count("n >= 0", &ds, &AtomicBool::new(true)), None); + assert_eq!(count("nope = 1", &ds, &AtomicBool::new(true)), None, "not even parsed"); + assert_eq!(count("n >= 0", &ds, &AtomicBool::new(false)), Some(Answer::Rows(3 * CHUNK))); + } + + /// A stop set while it counts ends it at the next chunk, with no answer. + #[test] + fn a_count_stopped_while_it_runs_ends_at_the_next_chunk() { + let ds = layer(3 * CHUNK); + let stop = AtomicBool::new(false); + let mut chunks = 0; + let answer = count_between("n >= 0", &ds, &stop, || { + chunks += 1; + stop.store(true, Ordering::Relaxed); + }); + assert_eq!((answer, chunks), (None, 1), "one chunk of three"); + } + + /// A bug in the count is an answer, not a thread that ends with none. + #[test] + fn a_panic_in_the_count_is_an_answer() { + let answer = guarded(|| panic!("a bug in the count")); + assert!(matches!(answer, Some(Answer::Failed(_))), "{answer:?}"); + assert_eq!(guarded(|| Some(Answer::Rows(3))), Some(Answer::Rows(3)), "an answer passes through"); + } + + /// A thread the system refuses is said, not counted on the frame's thread (a large layer would + /// freeze the window), and not called invalid or left counting. + #[test] + fn a_refused_thread_is_said_not_counted_here() { + let ds = layer(100); + let mut c = Count::default(); + c.refuse = true; + let ctx = egui::Context::default(); + let shown = c.of(&ctx, "n >= 90", "t", &ds, true); + assert!(matches!(&shown, Shown::Failed(e) if e.contains("no threads left")), "{shown:?}"); + assert!(!c.counting()); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, true), shown, "kept"); + assert_eq!(c.starts, 1); + } + + /// On a thread of its own the answer lands and the next ask shows it. + #[test] + fn a_count_on_a_thread_lands() { + let ds = layer(100); + let mut c = Count::default(); + let ctx = egui::Context::default(); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, true), Shown::Counting, "not in the frame that starts it"); + let mut shown = Shown::Counting; + for _ in 0..400 { + shown = c.of(&ctx, "n >= 90", "t", &ds, true); + if shown != Shown::Counting { + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); // test-wait: the count runs on a thread of its own + } + assert_eq!(shown, Shown::Rows(10)); + assert_eq!(c.starts, 1); + } + + /// The browser's path counts at once, on the first ask, and starts no thread. + #[test] + fn the_browser_counts_inline() { + let ds = layer(100); + let mut c = Count::default(); + let ctx = egui::Context::default(); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, false), Shown::Rows(10)); + assert!(!c.counting()); + assert_eq!(c.starts, 1); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, false), Shown::Rows(10), "kept"); + assert_eq!(c.starts, 1, "not counted again"); + } + + /// Dropping the count stops one on its way. + #[test] + fn dropping_the_count_stops_it() { + let c = Count::default(); + let stop = c.stop.clone(); + assert!(!stop.load(Ordering::Relaxed)); + drop(c); + assert!(stop.load(Ordering::Relaxed)); + } + + /// Edited rows, another clause or another layer's name are counted again, not answered with + /// the number for the rows before. + #[test] + fn rows_clause_and_layer_changed_are_counted_again() { + let mut ds = layer(100); + let mut c = Count::default(); + let ctx = egui::Context::default(); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, false), Shown::Rows(10)); + ds.push(None, vec![Value::Int(500)]); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, false), Shown::Rows(11), "a row added"); + assert_eq!(c.of(&ctx, "n >= 95", "t", &ds, false), Shown::Rows(6), "another clause"); + assert_eq!(c.of(&ctx, "n >= 95", "u", &ds, false), Shown::Rows(6), "another layer"); + assert_eq!(c.starts, 4); + assert_eq!( + c.of(&ctx, "n >", "u", &ds, false), + Shown::Invalid("expected a value, found the end".into()), + "no count for a clause that doesn't read" + ); + assert_eq!(c.starts, 4); + // A value changed in place keeps the length and the fields: still counted again. + let mut c = Count::default(); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, false), Shown::Rows(11)); + ds.features.set_value(0, 0, Value::Int(95)); + assert_eq!(c.of(&ctx, "n >= 90", "t", &ds, false), Shown::Rows(12), "a value edited in place"); + assert_eq!(c.starts, 2); + } +} diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 46418483eb..c0d8e52c7f 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -13,7 +13,8 @@ Mercator stops, is measured on the ground instead, so it may be selected outside stops at 85.05°, or left out inside it. The main Select button remains click/rectangle selection. All selection tools take only features that are drawn, in selectable layers: a definition query or a display filter hiding a feature keeps it out, and a click picks the feature drawn on top among the -selectable layers. **Select By Attributes** takes a SQL clause and checks it as you type (buttons +selectable layers. **Select By Attributes** takes a SQL clause and checks it as you type and shows how many rows +match, saying "Counting matching rows…" until the desktop app has the number (buttons add a field or an operator, and the **Functions** and **Variables** menus add the language's functions and variables); **Select By Location** picks features that touch another layer's features or lie within a search distance of them (or, inverted, those that don't). Selected features draw in